JavaScript/TypeScript SDK for CloudEvents
Go to file
Fabio José c1292a866f Unit testing for time attribute format
Signed-off-by: Fabio José <fabiojose@gmail.com>
2018-11-14 22:04:37 -02:00
lib time attribute 2018-11-14 22:03:41 -02:00
test Unit testing for time attribute format 2018-11-14 22:04:37 -02:00
.gitignore Project start 2018-11-09 16:20:29 -02:00
LICENSE Initial commit 2018-09-20 15:54:57 -04:00
README.md Update the readme 2018-11-09 16:23:07 -02:00
index.js Remove commented code 2018-11-14 21:13:21 -02:00
package.json deps for http request and tests 2018-11-14 21:17:32 -02:00

README.md

sdk-javascript

Javascript SDK for CloudEvents

This is a WIP

Repository Structure

├── index.js
├── lib
│   ├── cloudevent.js
│   ├── jsonformatter.js
│   ├── format
│   │   └── json_0_1.js
│   └── specs
│       ├── spec_0_1.js
│       └── spec_0_2.js
├── LICENSE
├── package.json
├── README.md
└── test
    ├── cloudevent_spec_0_1.js
    └── cloudevent_spec_0_2.js

  • index.js: library exports

  • lib/cloudevent.js: implementation of Cloudevent, an interface

  • lib/format/: every format implementation goes here

  • lib/format/json_0_1.js: implementation for JSON formatting version 0.1

  • lib/specs/: every spec implementation goes here

  • lib/specs/spec_0_1.js: implementation for spec version 0.1

  • lib/specs/spec_0_2.js: implementation for spec version 0.2

  • test/cloudevent_spec_0_1.js: unit testing for spec 0.1

  • test/cloudevent_spec_0_2.js: unit testing for spec 0.2

Unit Testing

The unit test checks the result of formatted payload and the constraints.


npm test

The API

Cloudevent class


/*
 * Format the payload and return an Object.
 */
Object Cloudevent.format()

/*
 * Format the payload as String.
 */
String Cloudevent.toString()

Formatter classes

Every formatter class must implement these methods to work properly.


/*
 * Format the Cloudevent payload argument and return an Object.
 */
Object Formatter.format(payload)

/*
 * Format the Cloudevent payload as String.
 */
String Formatter.toString(payload)

Spec classes

Every Spec class must implement these methods to work properly.


/*
 * Check the spec constraints, throwing an error if do not pass.
 */
Spec.check()

How to use

The Cloudevent constructor arguments.


/*
 * spec  : if is null, set the spec 0.1 impl
 * format: if is null, set the JSON Format 0.1 impl
 */
Cloudevent(spec, format);

How to construct instances?

/* 
 * Constructs a default instance with:
 *   - Spec 0.1
 *   - JSON Format 0.1
 */
var cloudevent01 = new Cloudevent();

/*
 * Implemented using Builder Design Pattern
 */
cloudevent01
  .type("com.github.pull.create")
  .source("urn:event:from:myapi/resourse/123");

/*
 * Backward compatibility by injecting methods from spec implementation to Cloudevent
 */
cloudevent01
 .eventTypeVersion("1.0");

/*
 * Constructs an instance with:
 *   - Spec 0.2
 *   - JSON Format 0.1 
 */
var cloudevent02 = new Cloudevent(Cloudevent.specs['0.2']);

/*
 * Different specs, but the same API.
 */
cloudevent02
  .type("com.github.pull.create")
  .source("urn:event:from:myapi/resourse/123");

How to get the formatted payload?

var cloudevent = new Cloudevent()
                       .type("com.github.pull.create")
                       .source("urn:event:from:myapi/resourse/123");

/*
 * Format the payload and return it.
 */
var formatted = cloudevent.format();
 

See how to implement the method injection here

Learn about Builder Design Pattern