Docs for the new functions

Signed-off-by: Fabio José <fabiojose@gmail.com>
This commit is contained in:
Fabio José 2019-06-06 22:48:02 -03:00
parent a8a0c96958
commit 680731d8b9
1 changed files with 66 additions and 12 deletions

View File

@ -12,10 +12,9 @@ Official CloudEvents' SDK for JavaScript.
### Before Spec reaches 1.0 ### Before Spec reaches 1.0
- `0.x.p`: where `x` relates to spec version and `p` relates to fixes, see - `0.x.p`: where `x` relates to spec version and `p` relates to fixes.
[semver](https://semver.org/).
### After Spec reaches 1.0__ ### After Spec reaches 1.0
- `x.M.p`: where `x` relates to spec version, `M` relates to minor and `p` relates - `x.M.p`: where `x` relates to spec version, `M` relates to minor and `p` relates
to fixes. See [semver](https://semver.org/) to fixes. See [semver](https://semver.org/)
@ -40,12 +39,15 @@ These are the supported specifications by this version.
| HTTP Transport Binding - Binary | yes | yes | | HTTP Transport Binding - Binary | yes | yes |
| JSON Event Format | yes | yes | | JSON Event Format | yes | yes |
### What we can do?
## How to use ## How to use
The `Cloudevent` constructor arguments. The `Cloudevent` constructor arguments.
```js ```js
/* /*
* spec : if is null, set the spec 0.1 impl * spec : if is null, set the spec 0.1 impl
* format: if is null, set the JSON Format 0.1 impl * format: if is null, set the JSON Format 0.1 impl
@ -114,6 +116,7 @@ var cloudevent =
*/ */
var formatted = cloudevent.format(); var formatted = cloudevent.format();
var ce =
``` ```
#### Emitting #### Emitting
@ -147,7 +150,33 @@ binding.emit(cloudevent)
console.log(response.data); console.log(response.data);
}).catch(err => { }).catch(err => {
// Treat the error // Deal with errors
console.error(err);
});
```
#### Receiving
```js
var Cloudevent = require("cloudevents-sdk");
// The binding configuration
var config = {
path : "/events",
port : 10300,
method : "POST"
};
// The binding instance
var binding = Cloudevent
.bindings["http-structured0.2"](config);
binding.receive()
.then(cloudevent => {
// do something with event
})
.catch(err => {
// deal with errors
console.error(err); console.error(err);
}); });
``` ```
@ -179,7 +208,6 @@ binding.emit(cloudevent)
The unit test checks the result of formatted payload and the constraints. The unit test checks the result of formatted payload and the constraints.
```bash ```bash
npm test npm test
``` ```
@ -189,7 +217,6 @@ npm test
### `Cloudevent` class ### `Cloudevent` class
```js ```js
/* /*
* Format the payload and return an Object. * Format the payload and return an Object.
*/ */
@ -200,6 +227,11 @@ Object Cloudevent.format()
*/ */
String Cloudevent.toString() String Cloudevent.toString()
/*
* Create a Cloudevent instance from String.
*/
Cloudevent Cloudevent.fromString(String)
``` ```
### `Formatter` classes ### `Formatter` classes
@ -207,7 +239,6 @@ String Cloudevent.toString()
Every formatter class must implement these methods to work properly. Every formatter class must implement these methods to work properly.
```js ```js
/* /*
* Format the Cloudevent payload argument and return an Object. * Format the Cloudevent payload argument and return an Object.
*/ */
@ -225,7 +256,6 @@ String Formatter.toString(payload)
Every Spec class must implement these methods to work properly. Every Spec class must implement these methods to work properly.
```js ```js
/* /*
* The constructor must receives the Cloudevent type. * The constructor must receives the Cloudevent type.
*/ */
@ -236,13 +266,21 @@ Spec(Cloudevent)
*/ */
Spec.check() Spec.check()
/*
* Checks if the argument pass through the spec constraints
*/
Spec.check(Object)
``` ```
### `Binding` classes ### `Binding` classes
Every Binding class must implement these methods to work properly. Every Binding class must implement these methods to work properly.
```js #### Emitter Binding
Following we have the signature for the binding to emit Cloudevents.
```js
/* /*
* The constructor must receives the map of configurations. * The constructor must receives the map of configurations.
*/ */
@ -255,6 +293,22 @@ Binding.emit(cloudevent)
``` ```
#### Receiver Binding
Following we have the signature for the binding to receive Cloudevents.
```js
/*
* The constructor must receives the map of configurations.
*/
ReceiverBinding(config)
/*
* Receives the events and returns a Promise.
*/
ReceiverBinding.receive()
```
> See how to implement the method injection [here](lib/specs/spec_0_1.js#L17) > See how to implement the method injection [here](lib/specs/spec_0_1.js#L17)
> >
> Learn about [Builder Design Pattern](https://en.wikipedia.org/wiki/Builder_pattern) > Learn about [Builder Design Pattern](https://en.wikipedia.org/wiki/Builder_pattern)