UNPKG

@graphql-mesh/serve-runtime

Version:
31 lines (30 loc) 1.25 kB
export function useWebhooks({ pubsub, logger }) { if (!pubsub) { throw new Error(`You must provide a pubsub instance to useWebhooks plugin! Example: export const serveConfig: MeshServeCLIConfig = { pubsub: new PubSub(), plugins: ctx => [ useWebhooks(ctx), ], } See documentation: https://the-guild.dev/docs/mesh/pubsub`); } return { onRequest({ request, url, endResponse, fetchAPI }) { for (const eventName of pubsub.getEventNames()) { if (eventName === `webhook:${request.method.toLowerCase()}:${url.pathname}`) { logger?.debug(`Received webhook request for ${url.pathname}`); return request.text().then(body => { logger?.debug(`Emitted webhook request for ${url.pathname}`, body); pubsub.publish(eventName, request.headers.get('content-type') === 'application/json' ? JSON.parse(body) : body); endResponse(new fetchAPI.Response(null, { status: 204, statusText: 'OK', })); }); } } }, }; }