High-level JS library to process Awala Internet Endpoint-compatible CloudEvents.
To convert an incoming service message wrapped in a CloudEvent
, simply call makeIncomingServiceMessage
.
For example, the following Fastify route accepts CloudEvents in binary mode and the converts them to service messages:
import { CloudEventV1, HTTP } from 'cloudevents';
import type { FastifyInstance, FastifyPluginOptions } from 'fastify';
export async function registerEventReceiver(server: FastifyInstance): Promise<void> {
// Accept any content type
server.removeAllContentTypeParsers();
server.addContentTypeParser('*', { parseAs: 'buffer' }, (_req, payload, next) => {
next(null, payload);
});
server.post('/', async (request, reply) => {
let event: CloudEventV1<Buffer>;
try {
event = HTTP.toEvent({ headers: request.headers, body: request.body });
} catch (err) {
// Malformed CloudEvent
return reply.status(400).send({ reason: err.message });
}
const message = makeIncomingServiceMessage(event);
request.log.info({ parcelId: message.parcelId }, 'Incoming service message');
return reply.status(204).send();
});
}
To convert an outgoing service message to a CloudEvent
, simply call makeOutgoingCloudEvent
.
For example:
import { makeOutgoingCloudEvent, OutgoingServiceMessage } from '@relaycorp/awala-endpoint-internet';
const outgoingServiceMessage: OutgoingServiceMessage = {
recipientId: '<Insert Awala endpoint id of recipient>',
contentType: 'application/vnd.your-company.your-service.Greeting',
data: Buffer.from('Hello, world!'),
};
const outgoingEvent = makeOutgoingCloudEvent(outgoingServiceMessage);
Finally, send the outgoingEvent
to the Awala Internet Endpoint using your cloudevents emitter. For example:
import { httpTransport, emitterFor } from 'cloudevents';
const emit = emitterFor(httpTransport('https://cloudevents-broker.com'));
emit(event);
See API documentation.