register-server-cloudevent-handlers

2.1.2 • Public • Published

register-server-cloudevent-handlers

Registers a folder of handlers as HTTP POST routes on a server that parses cloudevents from payload before sending to handler

Usage

Startup

On startup, call registerHandlers

import path from 'path'
import fastify from 'fastify'
import { registerHandlers } from 'register-server-cloudevent-handlers'

const server = fastify({
  ignoreTrailingSlash: true,
  logger: true
})

export const start = async (server) => {

  await registerHandlers({
    server,
    path: path.resolve(process.cwd(), 'src', 'handlers')
  })

  try {
    await server.listen(port)
  } catch (err) {
    server.log.error(err)
    process.exit(1)
  }
}

start(server)

Handlers

Each handler in the given directory must export a named function handle

For example, src/handlers/example.handle.js:

export const handle = async (event, reply) => {
  // get data, type, and source from cloud event
  const { data, type, source } = event
  // do stuff
  // then reply
  return reply.code(202).send({ status: 'complete' })
}

This will register /example.handle as an HTTP Post endpoint.

Dependents (0)

Package Sidebar

Install

npm i register-server-cloudevent-handlers

Weekly Downloads

2

Version

2.1.2

License

ISC

Unpacked Size

74.6 kB

Total Files

25

Last publish

Collaborators

  • patrickleet