@@ -8,14 +8,21 @@ Official CloudEvents' SDK for JavaScript.
88
99<img src =" https://github.com/cncf/artwork/blob/master/projects/cloudevents/horizontal/color/cloudevents-horizontal-color.png " width =" 300 " height =" 58 " alt =" CloudEvents logo " >
1010
11+ ## Contributing
12+
13+ Before create an awesome PR, please read our [ guidelines] ( ./CONTRIBUTING.md ) .
14+
15+ ## Examples
16+
17+ To see working examples, point to [ examples] ( ./examples ) .
18+
1119## Versioning
1220
1321### Before Spec reaches 1.0
1422
15- - ` 0.x.p ` : where ` x ` relates to spec version and ` p ` relates to fixes, see
16- [ semver] ( https://semver.org/ ) .
23+ - ` 0.x.p ` : where ` x ` relates to spec version and ` p ` relates to fixes and releases.
1724
18- ### After Spec reaches 1.0 __
25+ ### After Spec reaches 1.0
1926
2027- ` x.M.p ` : where ` x ` relates to spec version, ` M ` relates to minor and ` p ` relates
2128to fixes. See [ semver] ( https://semver.org/ )
@@ -40,12 +47,22 @@ These are the supported specifications by this version.
4047| HTTP Transport Binding - Binary | yes | yes |
4148| JSON Event Format | yes | yes |
4249
50+ ### What we can do?
51+
52+ | __ What__ | __ v0.1__ | __ v0.2__ |
53+ | ------------------------------------| ----------| ----------|
54+ | Create events | yes | yes |
55+ | Emit Structured events over HTTP | yes | yes |
56+ | Emit Binary events over HTTP | yes | yes |
57+ | JSON Event Format | yes | yes |
58+ | Receice Structured events over HTTP| no | yes |
59+ | Receice Binary events over HTTP | no | yes |
60+
4361## How to use
4462
4563The ` Cloudevent ` constructor arguments.
4664
4765``` js
48-
4966/*
5067 * spec : if is null, set the spec 0.1 impl
5168 * format: if is null, set the JSON Format 0.1 impl
@@ -74,11 +91,11 @@ cloudevent01
7491 .source (" urn:event:from:myapi/resourse/123" );
7592
7693/*
77- * Backward compatibility to spec 0.1 by injecting methods from spec implementation
94+ * Backward compatibility to spec 0.1 by injecting methods from spec implementation
7895 * to Cloudevent
7996 */
8097cloudevent01
81- .eventTypeVersion (" 1.0" );
98+ .eventTypeVersion (" 1.0" );
8299
83100/*
84101 * Constructs an instance with:
@@ -104,7 +121,7 @@ var Cloudevent = require("cloudevents-sdk");
104121/*
105122 * Creates an instance with default spec and format
106123 */
107- var cloudevent =
124+ var cloudevent =
108125 new Cloudevent ()
109126 .type (" com.github.pull.create" )
110127 .source (" urn:event:from:myapi/resourse/123" );
@@ -113,7 +130,6 @@ var cloudevent =
113130 * Format the payload and return it
114131 */
115132var formatted = cloudevent .format ();
116-
117133```
118134
119135#### Emitting
@@ -122,9 +138,10 @@ var formatted = cloudevent.format();
122138var Cloudevent = require (" cloudevents-sdk" );
123139
124140// The event
125- var cloudevent = new Cloudevent ()
126- .type (" com.github.pull.create" )
127- .source (" urn:event:from:myapi/resourse/123" );
141+ var cloudevent =
142+ new Cloudevent ()
143+ .type (" com.github.pull.create" )
144+ .source (" urn:event:from:myapi/resourse/123" );
128145
129146// The binding configuration using POST
130147var config = {
@@ -147,49 +164,85 @@ binding.emit(cloudevent)
147164 console .log (response .data );
148165
149166 }).catch (err => {
150- // Treat the error
167+ // Deal with errors
168+ console .error (err);
169+ });
170+ ```
171+ #### Receiving Events
172+
173+ You can choose any framework for port binding. But, use the Unmarshaller
174+ to process the HTTP Payload and HTTP Headers, extracting the CloudEvents.
175+
176+ The Unmarshaller will parse the HTTP Request and decides if it is a binary
177+ or a structured version of transport binding.
178+
179+ __ :smiley : Checkout the full working example: [ here] ( ./examples/express-ex ) .__
180+
181+ ``` js
182+ // some parts were removed //
183+
184+ var Unmarshaller02 = require (" cloudevents-sdk/http/unmarshaller/v02" );
185+
186+ // some parts were removed //
187+
188+ app .post (' /' , function (req , res ) {
189+ unmarshaller .unmarshall (req .body , req .headers )
190+ .then (cloudevent => {
191+
192+ // TODO use the cloudevent
193+
194+ res .status (201 )
195+ .send (" Event Accepted" );
196+ })
197+ .catch (err => {
151198 console .error (err);
199+ res .status (400 )
200+ .header (" Content-Type" , " application/json" )
201+ .send (JSON .stringify (err));
152202 });
203+ });
204+
205+
153206```
154207
155208## Repository Structure
156209
157210``` text
158211├── index.js
212+ ├── ext
159213├── lib
160214│ ├── bindings
161215│ │ └── http
162216│ ├── cloudevent.js
163- │ ├── format
217+ │ ├── formats
218+ │ │ └── json
164219│ └── specs
165220├── LICENSE
166221├── package.json
167222├── README.md
168223```
169224
170225- ` index.js ` : library exports
226+ - ` ext ` : external stuff, e.g, Cloud Events JSONSchema
171227- ` lib/bindings ` : every binding implementation goes here
172228- ` lib/bindings/http ` : every http binding implementation goes here
173229- ` lib/cloudevent.js ` : implementation of Cloudevent, an interface
174- - ` lib/format / ` : every format implementation goes here
230+ - ` lib/formats / ` : every format implementation goes here
175231- ` lib/specs/ ` : every spec implementation goes here
176232
177233## Unit Testing
178234
179235The unit test checks the result of formatted payload and the constraints.
180236
181237``` bash
182-
183238npm test
184-
185239```
186240
187241## The API
188242
189243### ` Cloudevent ` class
190244
191245``` js
192-
193246/*
194247 * Format the payload and return an Object.
195248 */
@@ -200,49 +253,77 @@ Object Cloudevent.format()
200253 */
201254String Cloudevent .toString ()
202255
256+ /*
257+ * Create a Cloudevent instance from String.
258+ */
259+ Cloudevent Cloudevent .fromString (String )
260+
203261```
204262
205263### ` Formatter ` classes
206264
207265Every formatter class must implement these methods to work properly.
208266
209267``` js
210-
211268/*
212269 * Format the Cloudevent payload argument and return an Object.
213270 */
214- Object Formatter .format (payload )
271+ Object Formatter .format (Object )
215272
216273/*
217274 * Format the Cloudevent payload as String.
218275 */
219- String Formatter .toString (payload)
276+ String Formatter .toString (Object )
277+ ```
278+
279+ ### ` Parser ` classes
280+
281+ Every Parser class must implement these methods to work properly.
282+
283+ ``` js
284+ /*
285+ * The default constructor with Spec as parameter
286+ */
287+ Parser (Spec)
220288
289+ /*
290+ * Try to parse the payload to some event format
291+ */
292+ Object Parser .parse (payload)
221293```
222294
223- ## ` Spec ` classes
295+ ### ` Spec ` classes
224296
225297Every Spec class must implement these methods to work properly.
226298
227299``` js
228-
229300/*
230301 * The constructor must receives the Cloudevent type.
231302 */
232303Spec (Cloudevent)
233304
234305/*
235306 * Checks the spec constraints, throwing an error if do not pass.
307+ * @throws Error when it is an invalid event
236308 */
237309Spec .check ()
238310
311+ /*
312+ * Checks if the argument pass through the spec constraints
313+ * @throws Error when it is an invalid event
314+ */
315+ Spec .check (Object )
239316```
317+
240318### ` Binding ` classes
241319
242320Every Binding class must implement these methods to work properly.
243321
244- ``` js
322+ #### Emitter Binding
245323
324+ Following we have the signature for the binding to emit Cloudevents.
325+
326+ ``` js
246327/*
247328 * The constructor must receives the map of configurations.
248329 */
@@ -252,7 +333,51 @@ Binding(config)
252333 * Emits the event using an instance of Cloudevent.
253334 */
254335Binding .emit (cloudevent)
336+ ```
337+
338+ #### Receiver Binding
339+
340+ Following we have the signature for the binding to receive Cloudevents.
341+
342+ ``` js
343+ /*
344+ * The constructor must receives the map of configurations.
345+ */
346+ Receiver (config)
347+
348+ /*
349+ * Checks if some Object and a Map of headers
350+ * follows the binding definition, throwing an error if did not follow
351+ */
352+ Receiver .check (Object , Map )
353+
354+ /*
355+ * Checks and parse as Cloudevent
356+ */
357+ Cloudevent Receiver .parse (Object , Map )
358+ ```
359+
360+ ### ` Unmarshaller ` classes
255361
362+ The Unmarshaller classes uses the receiver API, abstracting the formats:
363+
364+ - structured
365+ - binary
366+
367+ Choosing the right implementation based on the ` headers ` map.
368+
369+ ``` js
370+ /*
371+ * Constructor without arguments
372+ */
373+ Unmarshaller ()
374+
375+ /*
376+ * The method to unmarshall the payload.
377+ * @arg payload could be a string or a object
378+ * @arg headers a map of headers
379+ */
380+ Promise Unmarshaller .unmarshall (payload, headers)
256381```
257382
258383> See how to implement the method injection [ here] ( lib/specs/spec_0_1.js#L17 )
0 commit comments