Code Monkey home page Code Monkey logo

fastify-webhook's People

Contributors

smartiniongithub avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

drunkod

fastify-webhook's Issues

Unable to reference (directly) handler functions exposed by the plugin

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.