0.7.2 • Published 5 months ago

@yourrentals/cloudevent-receiver-hapi v0.7.2

Weekly downloads
-
License
-
Repository
-
Last release
5 months ago

@yourrentals/cloudevent-receiver-hapi

This library defines a plugin for Hapi to receive messages from the message bus.

Installation

yarn add @yourrentals/cloudevent-receiver-hapi

Usage

import { 
	cloudEventReceiverPlugin, 
  CloudEventReceiver,
  WebhookSignatureV1HmacPlugin
 } from '@yourrentals/message-bus-receiver-nestjs';
import * as Hapi from '@hapi/hapi';

const server = new Hapi.Server({
  port: 3000,
  host: 'localhost',
});

const receiver = new CloudEventReceiver({
  queues: [
    {
      name: 'my-queue',
      handler: async (event: CloudEvent) => {
        // ...
      },
    },
    {
      name: 'my-sqs-queue',
      handler: async (event: SQSMessage) => {
        // ...
      },
      isCloudEvent: false,
    },
  ],
  verifierPlugins: [new WebhookSignatureV1HmacPlugin('secret')],
});

server.register({
  plugin: cloudEventReceiverPlugin,
  options: {
    pathPrefix: '/message-bus',
    receiver,
  },
});

// ...
import Axios from 'axios';

const main = async () => {
  // Queue name defined in the receiver are automatically registered
  await Axios.post('http://localhost:3000/message-bus/my-queue', {
    headers: {
      'ce-specversion': '1.0',
      'ce-type': 'my-event',
      'ce-source': 'my-source',
      'ce-id': 'my-id',
      'ce-time': '2020-01-01T00:00:00Z',
      'ce-datacontenttype': 'application/json',
      'ce-dataschema': 'http://myschema.com',
      'ce-subject': 'my-subject',
      'ce-signature': 'my-signature',
    },
    body: {
      foo: 'bar',
    },
  });
};

Raw Mode

The library can receive messages from any sources so long that it implements the Webhook Signature. To disable parsing as cloudevent, set the isCloudEvent option to false.

By default, the event type is assumed to be a SQS Message

const receiver = new CloudEventReceiver({
  queues: [
    {
      name: 'my-queue',
      handler: async (event: SqsMessage) => {
        // ...
      },
      // Disable parsing as cloudevent
      isCloudEvent: false
    },
  ],
  verifierPlugins: [new WebhookSignatureV1HmacPlugin('secret')],
});

Errors

The library defines the following errors:

ErrorDescriptionExpected HTTP status codeRetryable
InvalidSignatureErrorThe signature of the message is invalid401No
UnparsableMessageErrorThe message is not a valid CloudEvent400No
NotFoundErrorThe queue name is not registered404Yes
ConflictErrorThe queue name is already registered409Yes
TooManyRequestsErrorThe queue is currently busy429Yes
ClientErrorAny other client error400No
ServerErrorAny other server error500Yes
0.7.2

5 months ago

0.7.1

5 months ago

0.6.1

7 months ago

0.6.0

7 months ago

0.5.2

7 months ago

0.5.1

7 months ago

0.5.0

7 months ago

0.4.3

7 months ago

0.4.2

7 months ago

0.4.1

7 months ago

0.1.4

8 months ago