@1mill/cloudevents

Node cloudevents specification and helper functions

Usage no npm install needed!

<script type="module">
  import 1millCloudevents from 'https://cdn.skypack.dev/@1mill/cloudevents';
</script>

README

@1mill/cloudevents

Introduction

This is an implementation and extention of the CloudEvents v1 specification to easily build cloudevents.

How to install

npm install @1mill/cloudevents@^0.11

Example usage

const { Cloudevent } = require('@1mill/cloudevents')

const cloudevent = new Cloudevent({
  data: JSON.stringify({
    someAttribute: 'yes',
    someOtherAttribute: { thing: true },
  }),
  source: 'https://www.my-website.com/my/page/123', // * Required
  subject: '123',
  type: 'cmd.do-this-command.v0', // * Required
})

console.log(cloudevent)

// Cloudevent {
//   id: '-of0T1jfpvD7_lOXtynbb',
//   source: 'https://www.my-website.come/my/page/123',
//   type: 'cmd.do-this-command.v0',
//   specversion: '1.0',
//   data: '{"someAttribute":"yes","someOtherAttribute":{"thing":true}}',
//   datacontenttype: 'application/json',
//   dataschema: undefined,
//   subject: '123',
//   time: '2021-09-06T16:29:26.527Z',
//   originid: '-of0T1jfpvD7_lOXtynbb',
//   originsource: 'https://www.my-website.come/my/page/123',
//   origintype: 'cmd.do-this-command.v0'
// }

Example of Chaining Cloudevents

// 1. Create command
const cmdCloudevent = new Cloudevent({
  source: 'https://my-ui.com/my/feature/page/123',
  subject: '123',
  type: 'cmd.do-this-command.v0',
})

console.log(cmdCloudevent)
// Cloudevent {
//   id: 'pDScxm45M2-BnnIYHw4P3',
//   source: 'https://www.my-website.come/my/page/123',
//   type: 'cmd.do-this-command.v0',
//   specversion: '1.0',
//   data: '{"someAttribute":"yes","someOtherAttribute":{"thing":true}}',
//   datacontenttype: 'application/json',
//   dataschema: undefined,
//   subject: '123',
//   time: '2021-09-06T16:38:49.717Z',
//   originid: 'pDScxm45M2-BnnIYHw4P3',
//   originsource: 'https://www.my-website.come/my/page/123',
//   origintype: 'cmd.do-this-command.v0'
// }

// 2. Publish command to Enterprise Event Bus (e.g. Kafka)

// 3. Subscribe to cloudevent type on Enterprise Event Buss

// 4. React to cloudevent command
const enrichment = dataFromMyBusinessProcess()
const fctCloudevent = new Cloudevent({
  ...cmdCloudevent,
  data: JSON.stringify(enrichment),
  source: 'arn:aws:lambda:us-east-1:123456789012:function:my-function',
  type: 'fct.this-thing-happened.v0',
})

console.log(fctCloudevent)
// Cloudevent {
//   id: 'N02yLAd_bZeZLGRUl78AS',
//   source: 'arn:aws:lambda:us-east-1:123456789012:function:my-function',
//   type: 'fct.this-thing-happened.v0',
//   specversion: '1.0',
//   data: '{...}',
//   datacontenttype: 'application/json',
//   dataschema: undefined,
//   subject: '123',
//   time: '2021-09-06T16:38:49.717Z',
//   originid: 'pDScxm45M2-BnnIYHw4P3',
//   originsource: 'https://www.my-website.come/my/page/123',
//   origintype: 'cmd.do-this-command.v0'
// }

Release new version

npm version <major|minor|patch>
npm publish