@relaycorp/awala-endpoint-internet
TypeScript icon, indicating that this package has built-in type declarations

1.1.6 • Public • Published

@relaycorp/awala-endpoint-internet

High-level JS library to process Awala Internet Endpoint-compatible CloudEvents.

Convert incoming CloudEvent to service message

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();
  });
}

Convert outgoing service message to CloudEvent

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);

API documentation

See API documentation.

Package Sidebar

Install

npm i @relaycorp/awala-endpoint-internet

Weekly Downloads

2

Version

1.1.6

License

MIT

Unpacked Size

28 kB

Total Files

18

Last publish

Collaborators

  • gnarea
  • relaybot