Skip to content

Commit 424cfb0

Browse files
committed
docs: update README and examples with new API
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. Fixes: #128 Signed-off-by: Lance Ball <[email protected]>
1 parent c1fda94 commit 424cfb0

File tree

4 files changed

+73
-339
lines changed

4 files changed

+73
-339
lines changed

README.md

Lines changed: 48 additions & 227 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,9 @@
1111

1212
The CloudEvents SDK for JavaScript.
1313

14-
## Status
14+
## CloudEvents Specification Support
1515

16-
This SDK is still considered a work in progress.
17-
18-
This SDK current supports the following versions of CloudEvents:
19-
20-
- v1.0
21-
22-
**Checkout the [changelog](CHANGELOG.md) to see what's going on!**
23-
24-
## Installation
25-
26-
This CloudEvents SDK requires nodejs 6.11+
27-
28-
### Nodejs
29-
30-
```sh
31-
npm install cloudevents-sdk
32-
```
33-
## Specification Support
34-
35-
These are the supported specifications by this version.
16+
This SDK supports the 0.3 and 1.0 versions of the CloudEvents specification:
3617

3718
| **Specifications** | v0.3 | **v1.0** |
3819
|---------------------------------------|------|----------|
@@ -41,244 +22,78 @@ These are the supported specifications by this version.
4122
| HTTP Transport Binding - Binary | yes | yes |
4223
| JSON Event Format | yes | yes |
4324

44-
### What we can do
45-
46-
| **What** | v0.3 | **v1.0** |
25+
| **Capabilities** | v0.3 | **v1.0** |
4726
|-------------------------------------|------|----------|
4827
| Create events | yes | yes |
4928
| Emit Structured events over HTTP | yes | yes |
5029
| Emit Binary events over HTTP | yes | yes |
51-
| JSON Event Format | yes | yes |
5230
| Receive Structured events over HTTP | yes | yes |
5331
| Receive Binary events over HTTP | yes | yes |
5432

55-
## How to use
33+
### A Note on Versioning
5634

57-
### Usage
35+
The CloudEvents protocol version is distinct from this module's version number.
36+
For example, this module may be versioned as v2.0.0 but support the v0.3 and v1.0
37+
versions of the CloudEvent specification.
5838

59-
```js
60-
const v1 = require("cloudevents-sdk/v1");
39+
## Usage
6140

62-
/*
63-
* Creating an event
64-
*/
65-
let myevent = v1.event()
66-
.type("com.github.pull.create")
67-
.source("urn:event:from:myapi/resource/123");
68-
```
41+
**See the full working example: [here](./examples/express-ex).**
6942

70-
#### Formatting
71-
72-
```js
73-
const v1 = require("cloudevents-sdk/v1");
43+
### Installation
7444

75-
/*
76-
* Creating an event
77-
*/
78-
let myevent = v1.event()
79-
.type("com.github.pull.create")
80-
.source("urn:event:from:myapi/resource/123");
45+
The CloudEvents SDK requires a current LTS version of Node.js. At the moment
46+
those are Node.js 10.x and Node.js 12.x. To install in your Node.js project:
8147

82-
/*
83-
* Format the payload and return it
84-
*/
85-
let formatted = myevent.format();
48+
```console
49+
npm install --save cloudevents-sdk
8650
```
8751

88-
#### Emitting
89-
90-
```js
91-
const v1 = require("cloudevents-sdk/v1");
92-
93-
/*
94-
* Creating an event
95-
*/
96-
let myevent = v1.event()
97-
.type("com.github.pull.create")
98-
.source("urn:event:from:myapi/resource/123");
99-
100-
// The binding configuration using POST
101-
let config = {
102-
method: "POST",
103-
url : "https://myserver.com"
104-
};
105-
106-
// The binding instance
107-
let binding = new v1.StructuredHTTPEmitter(config);
108-
109-
// Emit the event using Promise
110-
binding.emit(myevent)
111-
.then(response => {
112-
// Treat the response
113-
console.log(response.data);
114-
115-
}).catch(err => {
116-
// Deal with errors
117-
console.error(err);
118-
});
119-
```
52+
### Receiving and Emitting Events
12053

12154
#### Receiving Events
12255

123-
You can choose any framework for port binding. But, use the
124-
StructuredHTTPReceiver or BinaryHTTPReceiver to process the HTTP Payload and
125-
HTTP Headers, extracting the CloudEvents.
126-
127-
:smiley: **Checkout the full working example: [here](./examples/express-ex).**
128-
129-
```js
130-
// some parts were removed //
131-
132-
const v1 = require("cloudevents-sdk/v1");
133-
134-
const receiver = new v1.StructuredHTTPReceiver();
135-
136-
// some parts were removed //
137-
138-
app.post("/", (req, res) => {
139-
try {
140-
let myevent = receiver.parse(req.body, req.headers);
141-
142-
// TODO use the event
143-
144-
res.status(201).send("Event Accepted");
145-
146-
} catch(err) {
147-
// TODO deal with errors
148-
console.error(err);
149-
res.status(415)
150-
.header("Content-Type", "application/json")
151-
.send(JSON.stringify(err));
152-
}
153-
});
154-
```
155-
156-
## Unit Testing
157-
158-
The unit test checks the result of formatted payload and the constraints.
159-
160-
```bash
161-
npm test
162-
```
163-
164-
## The API
165-
166-
### `CloudEvent` class
56+
You can choose almost any popular web framework for port binding. Use an
57+
`HTTPReceiver` to process the incoming HTTP request. The receiver accepts
58+
binary and structured events in either the 1.0 or 0.3 protocol formats.
16759

16860
```js
169-
/*
170-
* Format the payload and return an Object.
171-
*/
172-
Object CloudEvent.format()
173-
174-
/*
175-
* Format the payload as String.
176-
*/
177-
String CloudEvent.toString()
178-
```
61+
const {
62+
CloudEvent,
63+
HTTPReceiever
64+
} = require("cloudevents-sdk");
17965

180-
### `Formatter` classes
66+
// Create a receiver to accept events over HTTP
67+
const receiver = new HTTPReceiver();
18168

182-
Every formatter class must implement these methods to work properly.
183-
184-
```js
185-
/*
186-
* Format the CloudEvent payload argument and return an Object.
187-
*/
188-
Object Formatter.format(Object)
189-
190-
/*
191-
* Format the CloudEvent payload as String.
192-
*/
193-
String Formatter.toString(Object)
194-
```
195-
196-
### `Parser` classes
197-
198-
Every Parser class must implement these methods to work properly.
199-
200-
```js
201-
/*
202-
* The default constructor with Parser as decorator
203-
*/
204-
Parser(Parser)
205-
206-
/*
207-
* Try to parse the payload to some event format
208-
*/
209-
Object Parser.parse(payload)
69+
// body and headers come from an incoming HTTP request, e.g. express.js
70+
const receivedEvent = receiver.accept(req.body, req.headers);
71+
console.log(receivedEvent.format());
21072
```
21173

212-
### `Spec` classes
74+
#### Emitting Events
21375

214-
Every Spec class must implement these methods to work properly.
76+
Currently, to emit events, you'll need to decide whether the event is in
77+
binary or structured format, and determine what version of the CloudEvents
78+
specification you want to send the event as.
21579

21680
```js
217-
/*
218-
* The constructor must receives the CloudEvent type.
219-
*/
220-
Spec(CloudEvent)
221-
222-
/*
223-
* Checks the spec constraints, throwing an error if do not pass.
224-
* @throws Error when it is an invalid event
225-
*/
226-
Spec.check()
227-
228-
/*
229-
* Checks if the argument pass through the spec constraints
230-
* @throws Error when it is an invalid event
231-
*/
232-
Spec.check(Object)
233-
```
81+
const { CloudEvent } = require("cloudevents-sdk");
82+
const { StructuredHTTPEmitter } = require("cloudevents-sdk/v1");
23483

235-
### `Binding` classes
236-
237-
Every Binding class must implement these methods to work properly.
238-
239-
#### Emitter Binding
240-
241-
Following we have the signature for the binding to emit CloudEvents.
242-
243-
```js
244-
/*
245-
* The constructor must receives the map of configurations.
246-
*/
247-
Binding(config)
248-
249-
/*
250-
* Emits the event using an instance of CloudEvent.
251-
*/
252-
Binding.emit(cloudEvent)
253-
```
254-
255-
#### Receiver Binding
256-
257-
Following we have the signature for the binding to receive CloudEvents.
258-
259-
```js
260-
/*
261-
* The constructor must receives the map of configurations.
262-
*/
263-
Receiver(config)
84+
const myevent = new CloudEvent()
85+
.type("com.github.pull.create")
86+
.source("urn:event:from:myapi/resource/123");
26487

265-
/*
266-
* Checks if some Object and a Map of headers
267-
* follows the binding definition, throwing an error if did not follow
268-
*/
269-
Receiver.check(Object, Map)
88+
const emitter = new StructuredHTTPEmitter({
89+
method: "POST",
90+
url : "https://myserver.com"
91+
});
27092

271-
/*
272-
* Checks and parse as CloudEvent
273-
*/
274-
CloudEvent Receiver.parse(Object, Map)
93+
// Emit the event
94+
emitter.emit(myevent)
27595
```
27696

277-
## Versioning
278-
279-
- `x.M.p`: where `x` relates to spec version, `M` relates to minor and `p` relates
280-
to fixes. See [semver](https://semver.org/)
281-
28297
## Community
28398

28499
- There are bi-weekly calls immediately following the [Serverless/CloudEvents
@@ -291,3 +106,9 @@ to fixes. See [semver](https://semver.org/)
291106
[CNCF's Slack workspace](https://slack.cncf.io/).
292107
- Email: https://lists.cncf.io/g/cncf-cloudevents-sdk
293108
- Contact for additional information: Fabio José (`@fabiojose` on slack).
109+
110+
## Contributing
111+
112+
We love contributions from the community! Please check the
113+
[Contributor's Guide](https://github.com/cloudevents/sdk-javascript/blob/master/CONTRIBUTING.md)
114+
for information on how to get involved.

0 commit comments

Comments
 (0)