Skip to content

Commit c7cfa46

Browse files
committed
Update the examples to v1
Signed-off-by: Fabio José <[email protected]>
1 parent 6600663 commit c7cfa46

File tree

1 file changed

+64
-129
lines changed

1 file changed

+64
-129
lines changed

README.md

Lines changed: 64 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -28,40 +28,35 @@ To see working examples, point to [examples](./examples).
2828

2929
## :newspaper: Newsletter :newspaper:
3030

31-
> all the API developed before, for 0.1 and 0.2, works as the same.
31+
> all the API developed before, for 0.1, 0.2 and 0.3, works as the same.
3232
3333
Checkout the new expressive additions.
3434

35-
### New way to import the specifications stuff
35+
### Use typed CloudEvents with Typescript
3636

37-
```js
38-
// Import the v0.3 stuff
39-
const v03 = require("cloudevents-sdk/v03");
40-
41-
// Access the spec
42-
v03.Spec;
37+
> There is full example: [typescript-ex](,/examples/typescript-ex)
4338
44-
// Access the structured http event emitter
45-
v03.StructuredHTTPEmitter;
39+
```ts
40+
import Cloudevent, {
41+
event,
42+
StructuredHTTPEmitter,
43+
BinaryHTTPEmitter,
4644

47-
// Access the binary http event emitter
48-
v03.BinaryHTTPEmitter;
45+
StructuredHTTPReceiver,
46+
BinaryHTTPReceiver
47+
} from 'cloudevents-sdk/v1';
4948

50-
// Access http unmarshaller to process incoming events, Binary or Structured
51-
v03.HTTPUnmarshaller;
52-
```
49+
let myevent: Cloudevent = event()
50+
.source('/source')
51+
.type('type')
52+
.dataContentType('text/plain')
53+
.dataschema('http://d.schema.com/my.json')
54+
.subject('cha.json')
55+
.data('my-data')
56+
.addExtension("my-ext", "0x600");
5357

54-
### An easy way to create events
58+
// . . .
5559

56-
```js
57-
// Import the v0.3 stuff
58-
const v03 = require("cloudevents-sdk/v03");
59-
60-
// Creates an event using the v0.3 spec
61-
let ce =
62-
v03.event()
63-
.type("com.github.pull.create")
64-
.source("urn:event:from:myapi/resourse/123");
6560
```
6661

6762
## Versioning
@@ -89,26 +84,28 @@ npm install cloudevents-sdk
8984

9085
These are the supported specifications by this version.
9186

92-
| **Specifications** | **v0.1** | **v0.2** | **v0.3** |
93-
|---------------------------------------|----------|----------|----------|
94-
| CloudEvents | yes | yes | yes |
95-
| HTTP Transport Binding - Structured | yes | yes | yes |
96-
| HTTP Transport Binding - Binary | yes | yes | yes |
97-
| JSON Event Format | yes | yes | yes |
87+
| **Specifications** | v0.1 | v0.2 | v0.3 | **v1.0** |
88+
|---------------------------------------|------|------|------|----------|
89+
| CloudEvents | yes | yes | yes | yes |
90+
| HTTP Transport Binding - Structured | yes | yes | yes | yes |
91+
| HTTP Transport Binding - Binary | yes | yes | yes | yes |
92+
| JSON Event Format | yes | yes | yes | yes |
9893

9994
### What we can do
10095

101-
| **What** | **v0.1** | **v0.2** | **v0.3** |
102-
|-------------------------------------|----------|----------|----------|
103-
| Create events | yes | yes | yes |
104-
| Emit Structured events over HTTP | yes | yes | yes |
105-
| Emit Binary events over HTTP | yes | yes | yes |
106-
| JSON Event Format | yes | yes | yes |
107-
| Receice Structured events over HTTP | no | yes | yes |
108-
| Receice Binary events over HTTP | no | yes | yes |
96+
| **What** | v0.1 | v0.2 | v0.3 | **v1.0** |
97+
|-------------------------------------|--------|------|------|----------|
98+
| Create events | yes | yes | yes | yes |
99+
| Emit Structured events over HTTP | yes | yes | yes | yes |
100+
| Emit Binary events over HTTP | yes | yes | yes | yes |
101+
| JSON Event Format | yes | yes | yes | yes |
102+
| Receice Structured events over HTTP | **no** | yes | yes | yes |
103+
| Receice Binary events over HTTP | **no** | yes | yes | yes |
109104

110105
## How to use
111106

107+
> If you want old examples, they are [here](./OLDOCS.md)
108+
112109
The `Cloudevent` constructor arguments.
113110

114111
```js
@@ -123,93 +120,57 @@ Cloudevent(spec, format);
123120
### Usage
124121

125122
```js
126-
var Cloudevent = require("cloudevents-sdk");
127-
128-
var Spec02 = require("cloudevents-sdk/v02");
123+
const v1 = require("cloudevents-sdk/v1");
129124

130125
/*
131-
* Constructs a default instance with:
132-
* - Spec 0.1
133-
* - JSON Format 0.1
126+
* Creating an event
134127
*/
135-
var cloudevent01 = new Cloudevent();
136-
137-
/*
138-
* Implemented using Builder Design Pattern
139-
*/
140-
cloudevent01
128+
let myevent = v1.event()
141129
.type("com.github.pull.create")
142130
.source("urn:event:from:myapi/resourse/123");
143-
144-
/*
145-
* Backward compatibility to spec 0.1 by injecting methods from spec
146-
* implementation to Cloudevent
147-
*/
148-
cloudevent01
149-
.eventTypeVersion("1.0");
150-
151-
/*
152-
* Constructs an instance with:
153-
* - Spec 0.2
154-
* - JSON Format 0.1
155-
*/
156-
var cloudevent02 = new Cloudevent(Cloudevent.specs["0.2"]);
157-
158-
/*
159-
* Different specs, but the same API.
160-
*/
161-
cloudevent02
162-
.type("com.github.pull.create")
163-
.source("urn:event:from:myapi/resourse/123");
164-
165131
```
166132

167133
#### Formatting
168134

169135
```js
170-
var Cloudevent = require("cloudevents-sdk");
136+
const v1 = require("cloudevents-sdk/v1");
171137

172138
/*
173-
* Creates an instance with default spec and format
139+
* Creating an event
174140
*/
175-
var cloudevent =
176-
new Cloudevent()
177-
.type("com.github.pull.create")
178-
.source("urn:event:from:myapi/resourse/123");
141+
let myevent = v1.event()
142+
.type("com.github.pull.create")
143+
.source("urn:event:from:myapi/resourse/123");
179144

180145
/*
181146
* Format the payload and return it
182147
*/
183-
var formatted = cloudevent.format();
148+
let formatted = myevent.format();
184149
```
185150

186151
#### Emitting
187152

188153
```js
189-
var Cloudevent = require("cloudevents-sdk");
154+
const v1 = require("cloudevents-sdk/v1");
190155

191-
// The event
192-
var cloudevent =
193-
new Cloudevent()
194-
.type("com.github.pull.create")
195-
.source("urn:event:from:myapi/resourse/123");
156+
/*
157+
* Creating an event
158+
*/
159+
let myevent = v1.event()
160+
.type("com.github.pull.create")
161+
.source("urn:event:from:myapi/resourse/123");
196162

197163
// The binding configuration using POST
198-
var config = {
164+
let config = {
199165
method: "POST",
200166
url : "https://myserver.com"
201167
};
202168

203-
/*
204-
* To use HTTP Binary:
205-
* Cloudevent.bindings["http-binary0.2"](config);
206-
*/
207-
208169
// The binding instance
209-
var binding = new Cloudevent.bindings["http-structured0.1"](config);
170+
let binding = new v1.StructuredHTTPEmitter(config);
210171

211172
// Emit the event using Promise
212-
binding.emit(cloudevent)
173+
binding.emit(myevent)
213174
.then(response => {
214175
// Treat the response
215176
console.log(response.data);
@@ -219,27 +180,26 @@ binding.emit(cloudevent)
219180
console.error(err);
220181
});
221182
```
222-
#### Receiving Events
223183

224-
You can choose any framework for port binding. But, use the Unmarshaller
225-
to process the HTTP Payload and HTTP Headers, extracting the CloudEvents.
184+
#### Receiving Events
226185

227-
The Unmarshaller will parse the HTTP Request and decides if it is a binary
228-
or a structured version of transport binding.
186+
You can choose any framework for port binding. But, use the
187+
StructuredHTTPReceiver or BinaryHTTPReceiver to process the HTTP Payload and
188+
HTTP Headers, extracting the CloudEvents.
229189

230190
:smiley: **Checkout the full working example: [here](./examples/express-ex).**
231191

232192
```js
233193
// some parts were removed //
234194

235-
const v02 = require("cloudevents-sdk/v02");
236-
const unmarshaller = new v02.HTTPUnmarshaller();
195+
const v1 = require("cloudevents-sdk/v1");
196+
const receiver = new v1.StructuredHTTPReceiver();
237197

238198
// some parts were removed //
239199

240-
app.post('/', function (req, res) {
241-
unmarshaller.unmarshall(req.body, req.headers)
242-
.then(cloudevent => {
200+
app.post("/", function (req, res) {
201+
receiver.parse(req.body, req.headers)
202+
.then(myevent => {
243203

244204
// TODO use the cloudevent
245205

@@ -253,8 +213,6 @@ app.post('/', function (req, res) {
253213
.send(JSON.stringify(err));
254214
});
255215
});
256-
257-
258216
```
259217

260218
## Repository Structure
@@ -409,29 +367,6 @@ Receiver.check(Object, Map)
409367
Cloudevent Receiver.parse(Object, Map)
410368
```
411369

412-
### `Unmarshaller` classes
413-
414-
The Unmarshaller classes uses the receiver API, abstracting the formats:
415-
416-
- structured
417-
- binary
418-
419-
Choosing the right implementation based on the `headers` map.
420-
421-
```js
422-
/*
423-
* Constructor without arguments
424-
*/
425-
Unmarshaller()
426-
427-
/*
428-
* The method to unmarshall the payload.
429-
* @arg payload could be a string or a object
430-
* @arg headers a map of headers
431-
*/
432-
Promise Unmarshaller.unmarshall(payload, headers)
433-
```
434-
435370
> See how to implement the method injection [here](lib/specs/spec_0_1.js#L17)
436371
>
437372
> Learn about [Builder Design Pattern](https://en.wikipedia.org/wiki/Builder_pattern)

0 commit comments

Comments
 (0)