UNPKG

webhook-openapi

Version:

Helps you create you own webhook server

32 lines (29 loc) 979 B
import { Webhook } from '../../index.js'; import '@sinclair/typebox'; function store(db, requestTable, responseTable) { return new Webhook().onBeforeRequest(async ({ request, url, custom, data }) => { if (!("requestId" in custom)) { const [{ id }] = await db.insert(requestTable).values({ headers: request.headers.toJSON(), data, url, method: request.method?.toUpperCase() }).returning({ id: requestTable.id }); custom.requestId = id; } custom.responseStart = performance.now(); }).onAfterResponse(async ({ custom, response, data }) => { if ("requestId" in custom) { await db.insert(responseTable).values({ requestId: custom.requestId, headers: response.headers.toJSON(), data, status: response.status, responseTime: typeof custom.responseStart === "number" ? performance.now() - custom.responseStart : 0 }); } }); } export { store };