JavaScript/TypeScript SDK for CloudEvents
Go to file
Lance Ball 09b0c76826
feat: add ValidationError type extending TypeError (#151)
This change adds a `ValidationError` type that extends `TypeError`. Any time a `CloudEvent` cannot be received and created with the given input, this error will be thrown. Tests have all
been updated to check for the error type.

Signed-off-by: Lance Ball <lball@redhat.com>
2020-05-13 13:27:18 -04:00
.github/workflows chore: add action to detect and close stale issues 2020-05-09 00:24:03 -04:00
docs docs: add JSDocs for top level API objects (#140) 2020-05-12 17:27:11 -04:00
examples docs: update README and examples with new API (#138) 2020-05-11 09:41:05 -04:00
lib feat: add ValidationError type extending TypeError (#151) 2020-05-13 13:27:18 -04:00
test feat: add ValidationError type extending TypeError (#151) 2020-05-13 13:27:18 -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 docs: add JSDocs for top level API objects (#140) 2020-05-12 17:27:11 -04:00
.gitignore Ignore package-lock 2019-10-29 20:49:43 -03:00
.jsdoc.json docs: add JSDocs for top level API objects (#140) 2020-05-12 17:27:11 -04: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 docs: add instructions and details to contributors guide (#105) 2020-05-11 20:08:45 -04: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
maintainer_guidelines.md docs: add instructions and details to contributors guide (#105) 2020-05-11 20:08:45 -04:00
package-lock.json build: update package-lock.json 2020-05-13 10:38:10 +02:00
package.json docs: add JSDocs for top level API objects (#140) 2020-05-12 17:27:11 -04:00
pr_guidelines.md docs: add instructions and details to contributors guide (#105) 2020-05-11 20:08:45 -04: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.