JavaScript/TypeScript SDK for CloudEvents
Go to file
Lance Ball b866edddd9
docs: update README and examples with new API (#138)
This commit modifies the README to show new API usage for the
`HTTPReceiver` and `CloudEvent` classes, and updates the examples
to use this as well.

Overall structure and content has been modified to look more like
the sdk-go README.

Fixes: https://github.com/cloudevents/sdk-javascript/issues/128

Signed-off-by: Lance Ball <lball@redhat.com>
2020-05-11 09:41:05 -04:00
.github/workflows chore: add action to detect and close stale issues 2020-05-09 00:24:03 -04:00
examples docs: update README and examples with new API (#138) 2020-05-11 09:41:05 -04:00
lib fix: ensure binary events can handle no content-type header (#134) 2020-05-08 23:37:10 -04:00
test fix: ensure binary events can handle no content-type header (#134) 2020-05-08 23:37:10 -04:00
v1 feat: use CloudEvents not cloudevents everywhere (#101) 2020-04-30 16:14:13 -07:00
v03 feat: use CloudEvents not cloudevents everywhere (#101) 2020-04-30 16:14:13 -07:00
.eslintrc chore: update eslint rules to disallow var usage 2020-04-30 12:36:04 -03:00
.gitignore Ignore package-lock 2019-10-29 20:49:43 -03:00
.npmignore Igore files for npm publish 2019-06-16 20:53:55 -03:00
.travis.yml chore: Modify CI to also build backport branch(es) (#122) 2020-05-07 10:23:37 -04:00
CHANGELOG.md chore: add standard-version and release script 2020-04-29 08:50:52 -03:00
CONTRIBUTING.md chore: add standard-version and release script 2020-04-29 08:50:52 -03:00
LICENSE Initial commit 2018-09-20 15:54:57 -04:00
README.md docs: update README and examples with new API (#138) 2020-05-11 09:41:05 -04:00
index.js feat: expose a mode and version agnostic event receiver (#120) 2020-05-06 13:25:16 -04:00
package-lock.json chore: Update uuid dependency 2020-04-30 12:34:30 -03:00
package.json chore: Update uuid dependency 2020-04-30 12:34:30 -03:00

README.md

JavaScript SDK for CloudEvents

Codacy Badge Codacy Badge Build Status npm version vulnerabilities licence

The CloudEvents SDK for JavaScript.

This module will help you to:

Note: Supported CloudEvents specification: 0.3, 1.0

A Note on Versioning

The CloudEvents protocol version is distinct from this module's version number. For example, this module may be versioned as v2.0.0 but support the v0.3 and v1.0 versions of the CloudEvent specification.

Usage

See the full working example: here.

Installation

The CloudEvents SDK requires a current LTS version of Node.js. At the moment those are Node.js 10.x and Node.js 12.x. To install in your Node.js project:

npm install --save cloudevents-sdk

Receiving and Emitting Events

Receiving Events

You can choose almost any popular web framework for port binding. Use an HTTPReceiver to process the incoming HTTP request. The receiver accepts binary and structured events in either the 1.0 or 0.3 protocol formats.

const {
  CloudEvent,
  HTTPReceiever
} = require("cloudevents-sdk");

// Create a receiver to accept events over HTTP
const receiver = new HTTPReceiver();

// body and headers come from an incoming HTTP request, e.g. express.js
const receivedEvent = receiver.accept(req.body, req.headers);
console.log(receivedEvent.format());

Emitting Events

Currently, to emit events, you'll need to decide whether the event is in binary or structured format, and determine what version of the CloudEvents specification you want to send the event as.

const { CloudEvent } = require("cloudevents-sdk");
const { StructuredHTTPEmitter } = require("cloudevents-sdk/v1");

const myevent = new CloudEvent()
  .type("com.github.pull.create")
  .source("urn:event:from:myapi/resource/123");

const emitter = new StructuredHTTPEmitter({
  method: "POST",
  url   : "https://myserver.com"
});

// Emit the event
emitter.emit(myevent)

Supported specification features

v0.3 v1.0
CloudEvents Core ✔️ ✔️
AMQP Protocol Binding
AVRO Event Format
HTTP Protocol Binding ✔️ ✔️
JSON Event Format ✔️ ✔️
Kafka Protocol Binding
NATS Protocol Binding
STAN Protocol Binding

Community

Contributing

We love contributions from the community! Please check the Contributor's Guide for information on how to get involved.