0.7.2 • Published 2 years ago
@yourrentals/cloudevent-receiver-hapi v0.7.2
@yourrentals/cloudevent-receiver-hapi
This library defines a plugin for Hapi to receive messages from the message bus.
Installation
yarn add @yourrentals/cloudevent-receiver-hapiUsage
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:
| Error | Description | Expected HTTP status code | Retryable |
|---|---|---|---|
| InvalidSignatureError | The signature of the message is invalid | 401 | No |
| UnparsableMessageError | The message is not a valid CloudEvent | 400 | No |
| NotFoundError | The queue name is not registered | 404 | Yes |
| ConflictError | The queue name is already registered | 409 | Yes |
| TooManyRequestsError | The queue is currently busy | 429 | Yes |
| ClientError | Any other client error | 400 | No |
| ServerError | Any other server error | 500 | Yes |