@graphql-mesh/plugin-webhook
Version:
40 lines (35 loc) • 1.56 kB
JavaScript
;
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
const stringInterpolation = require('@graphql-mesh/string-interpolation');
const lodashGet = _interopDefault(require('lodash.get'));
function useWebhook(opts) {
return {
async onRequest({ request, serverContext, fetchAPI, endResponse }) {
if (opts.method != null) {
if (request.method !== opts.method) {
return;
}
}
if (request.url.endsWith(opts.path)) {
let payload = await request.json();
opts.logger.debug(`Payload received; `, payload);
if (opts.payload != null) {
payload = lodashGet(payload, opts.payload);
opts.logger.debug([`Extracting ${opts.payload};`, payload]);
}
const interpolationData = {
...serverContext,
payload,
};
opts.logger.debug(`Interpolating ${opts.pubsubTopic} with `, interpolationData);
const pubsubTopic = stringInterpolation.stringInterpolator.parse(opts.pubsubTopic, interpolationData);
opts.pubsub.publish(pubsubTopic, payload);
opts.logger.debug(`Payload sent to ${pubsubTopic}`);
endResponse(new fetchAPI.Response(null, {
status: 204,
}));
}
},
};
}
module.exports = useWebhook;