2.1.2 • Published 3 years ago

register-server-cloudevent-handlers v2.1.2

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

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.

2.1.2

3 years ago

2.1.1

3 years ago

2.1.0

3 years ago

2.0.0

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago