smartiniongithub / fastify-webhook Goto Github PK
View Code? Open in Web Editor NEWFastify Plugin to serve webhooks with some default settings
License: Apache License 2.0
Fastify Plugin to serve webhooks with some default settings
License: Apache License 2.0
In the plugin I expose some generic functions that could be useful to debug webhooks.
The default handler function works, but when I try to use one of other handlers (loggerWebhookHandler, echoWebhookHandler) from consumers of the plugin (for example in tests), it seems that they are not found (so the default handler is always used).
All this because the plugin is exported but wrapped by 'fastify-plugin' (ok).
For example (extract from tests, currently it's latest which is failing):
test('custom options for webhook (using plugin echoWebhookHandler and optional input content type and body) does not return an error, but a good response (200) and some content', (t) => {
t.plan(5)
const fastify = Fastify()
const webhookPlugin = require('../')
fastify.register(webhookPlugin, {
'url': '/custom-webhook',
'handler': webhookPlugin.echoWebhookHandler // get a reference to another handler
// TODO: probably this is the real problem, it's not found, so default handler is used instead ...
})
fastify.listen(0, (err) => {
fastify.server.unref()
t.error(err)
const port = fastify.server.address().port
const sampleData = '{"payload":"test"}'
request({
method: 'POST',
uri: `http://localhost:${port}/custom-webhook`,
headers: {
'content-type': 'application/json' // force the right mime type to send data here
},
body: JSON.stringify(sampleData)
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.strictEqual(response.headers['content-type'], 'application/json')
t.deepEqual(JSON.parse(body), sampleData)
// TODO: fix last failing test ... wip
console.log(`Response body: "${body}"`) // TODO: temp ...
fastify.close()
})
})
})
So the question:
how is it the recommended way to let my plugin export some functions but in a way that I could get a reference to them ?
Should I wrap those functions in another source in the plugin exported too ? Or have multiple export items in the plugin (the plugin itself wrapped by fastify-plugin like currently, and handler functions too), like:
module.exports = { fp(fastifyWebHook, { ... }) , loggerWebhookHandler, echoWebhookHandler }
// or maybe wrap both handlers in another function like 'webhookHandlers' and export it
but don't know when I use the plugin (with a require) if all works in a good way.
Having multiple exports from a Fastify Plugin is it good ?
In alternative, move handlers in another node.js package and use here as a dependency seems a little overkill to me (but if it's the right way I can do it) ...
@mcollina What do you think ? Thanks for the help.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.