@@ -28,40 +28,35 @@ To see working examples, point to [examples](./examples).
28
28
29
29
## :newspaper : Newsletter :newspaper :
30
30
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.
32
32
33
33
Checkout the new expressive additions.
34
34
35
- ### New way to import the specifications stuff
35
+ ### Use typed CloudEvents with Typescript
36
36
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 )
43
38
44
- // Access the structured http event emitter
45
- v03 .StructuredHTTPEmitter ;
39
+ ``` ts
40
+ import Cloudevent , {
41
+ event ,
42
+ StructuredHTTPEmitter ,
43
+ BinaryHTTPEmitter ,
46
44
47
- // Access the binary http event emitter
48
- v03 .BinaryHTTPEmitter ;
45
+ StructuredHTTPReceiver ,
46
+ BinaryHTTPReceiver
47
+ } from ' cloudevents-sdk/v1' ;
49
48
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" );
53
57
54
- ### An easy way to create events
58
+ // . . .
55
59
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" );
65
60
```
66
61
67
62
## Versioning
@@ -89,26 +84,28 @@ npm install cloudevents-sdk
89
84
90
85
These are the supported specifications by this version.
91
86
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 |
98
93
99
94
### What we can do
100
95
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 |
109
104
110
105
## How to use
111
106
107
+ > If you want old examples, they are [ here] ( ./OLDOCS.md )
108
+
112
109
The ` Cloudevent ` constructor arguments.
113
110
114
111
``` js
@@ -123,93 +120,57 @@ Cloudevent(spec, format);
123
120
### Usage
124
121
125
122
``` js
126
- var Cloudevent = require (" cloudevents-sdk" );
127
-
128
- var Spec02 = require (" cloudevents-sdk/v02" );
123
+ const v1 = require (" cloudevents-sdk/v1" );
129
124
130
125
/*
131
- * Constructs a default instance with:
132
- * - Spec 0.1
133
- * - JSON Format 0.1
126
+ * Creating an event
134
127
*/
135
- var cloudevent01 = new Cloudevent ();
136
-
137
- /*
138
- * Implemented using Builder Design Pattern
139
- */
140
- cloudevent01
128
+ let myevent = v1 .event ()
141
129
.type (" com.github.pull.create" )
142
130
.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
-
165
131
```
166
132
167
133
#### Formatting
168
134
169
135
``` js
170
- var Cloudevent = require (" cloudevents-sdk" );
136
+ const v1 = require (" cloudevents-sdk/v1 " );
171
137
172
138
/*
173
- * Creates an instance with default spec and format
139
+ * Creating an event
174
140
*/
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" );
179
144
180
145
/*
181
146
* Format the payload and return it
182
147
*/
183
- var formatted = cloudevent .format ();
148
+ let formatted = myevent .format ();
184
149
```
185
150
186
151
#### Emitting
187
152
188
153
``` js
189
- var Cloudevent = require (" cloudevents-sdk" );
154
+ const v1 = require (" cloudevents-sdk/v1 " );
190
155
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" );
196
162
197
163
// The binding configuration using POST
198
- var config = {
164
+ let config = {
199
165
method: " POST" ,
200
166
url : " https://myserver.com"
201
167
};
202
168
203
- /*
204
- * To use HTTP Binary:
205
- * Cloudevent.bindings["http-binary0.2"](config);
206
- */
207
-
208
169
// The binding instance
209
- var binding = new Cloudevent.bindings [ " http-structured0.1 " ] (config);
170
+ let binding = new v1.StructuredHTTPEmitter (config);
210
171
211
172
// Emit the event using Promise
212
- binding .emit (cloudevent )
173
+ binding .emit (myevent )
213
174
.then (response => {
214
175
// Treat the response
215
176
console .log (response .data );
@@ -219,27 +180,26 @@ binding.emit(cloudevent)
219
180
console .error (err);
220
181
});
221
182
```
222
- #### Receiving Events
223
183
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
226
185
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.
229
189
230
190
:smiley : ** Checkout the full working example: [ here] ( ./examples/express-ex ) .**
231
191
232
192
``` js
233
193
// some parts were removed //
234
194
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 ();
237
197
238
198
// some parts were removed //
239
199
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 => {
243
203
244
204
// TODO use the cloudevent
245
205
@@ -253,8 +213,6 @@ app.post('/', function (req, res) {
253
213
.send (JSON .stringify (err));
254
214
});
255
215
});
256
-
257
-
258
216
```
259
217
260
218
## Repository Structure
@@ -409,29 +367,6 @@ Receiver.check(Object, Map)
409
367
Cloudevent Receiver .parse (Object , Map )
410
368
```
411
369
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
-
435
370
> See how to implement the method injection [ here] ( lib/specs/spec_0_1.js#L17 )
436
371
>
437
372
> Learn about [ Builder Design Pattern] ( https://en.wikipedia.org/wiki/Builder_pattern )
0 commit comments