@1mill/cloudevents 中文文档教程

发布于 3年前 浏览 29 项目主页 更新于 3年前

@1mill/cloudevents

Introduction

这是 CloudEvents v1 规范 的实现和扩展,可轻松构建具有原始历史记录的 cloudevents。

Install

<script src="https://unpkg.com/@1mill/cloudevents@4.0.0/dist/index.umd.js">

或者

npm install @1mill/cloudevents@^4
const { Cloudevent } = require('@1mill/cloudevents') // CommonJs
import { Cloudevent } from '@1mill/cloudevents' // EMS

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',
//   origintime: '2021-09-06T16:29:26.527Z',
//   originid: '-of0T1jfpvD7_lOXtynbb',
//   originsource: 'https://www.my-website.come/my/page/123',
//   origintype: 'cmd.do-this-command.v0'
// }

const enrichment = payloadFromMyBusinessProcess()
const myReactionCloudevent = new Cloudevent({
  ...cloudevent,
  data: JSON.stringify(enrichment),
  source: 'arn:aws:lambda:us-east-1:123456789012:function:my-function',
  type: 'fct.this-thing-happened.v0',
})

console.log(myReactionCloudevent)
// 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',
//   origintime: '2021-09-06T16:29:26.527Z',
//   originid: '-of0T1jfpvD7_lOXtynbb',
//   originsource: 'https://www.my-website.come/my/page/123',
//   origintype: 'cmd.do-this-command.v0'
// }
RequiredTypeDefaultNotes
dataAny
datacontenttypeStringIf "data" is present, defaults to "application/json" unless specified otherwise
dataschemaString
sourceyesStringprocess.env.MILLCLOUDEVENTSSOURCERecommended to use universal identifier (e.g. https://my-domain.com/my/feature/path/123)
specversionyesString1.0Cloudevent specification version
subjectString
typeyesString
origintimeyesString"time" property"time" property is internally generated as part of the package
originidyesString"id" property"id" property is internally generated as part of the package
originsourceyesString"source" property
origintypeyesString"type" property

Release new version

npm version <major|minor|patch>
npm run depoy

@1mill/cloudevents

Introduction

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

Install

<script src="https://unpkg.com/@1mill/cloudevents@4.0.0/dist/index.umd.js">

or

npm install @1mill/cloudevents@^4
const { Cloudevent } = require('@1mill/cloudevents') // CommonJs
import { Cloudevent } from '@1mill/cloudevents' // EMS

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',
//   origintime: '2021-09-06T16:29:26.527Z',
//   originid: '-of0T1jfpvD7_lOXtynbb',
//   originsource: 'https://www.my-website.come/my/page/123',
//   origintype: 'cmd.do-this-command.v0'
// }

const enrichment = payloadFromMyBusinessProcess()
const myReactionCloudevent = new Cloudevent({
  ...cloudevent,
  data: JSON.stringify(enrichment),
  source: 'arn:aws:lambda:us-east-1:123456789012:function:my-function',
  type: 'fct.this-thing-happened.v0',
})

console.log(myReactionCloudevent)
// 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',
//   origintime: '2021-09-06T16:29:26.527Z',
//   originid: '-of0T1jfpvD7_lOXtynbb',
//   originsource: 'https://www.my-website.come/my/page/123',
//   origintype: 'cmd.do-this-command.v0'
// }
RequiredTypeDefaultNotes
dataAny
datacontenttypeStringIf "data" is present, defaults to "application/json" unless specified otherwise
dataschemaString
sourceyesStringprocess.env.MILLCLOUDEVENTSSOURCERecommended to use universal identifier (e.g. https://my-domain.com/my/feature/path/123)
specversionyesString1.0Cloudevent specification version
subjectString
typeyesString
origintimeyesString"time" property"time" property is internally generated as part of the package
originidyesString"id" property"id" property is internally generated as part of the package
originsourceyesString"source" property
origintypeyesString"type" property

Release new version

npm version <major|minor|patch>
npm run depoy
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文