1+ [ ![ Codacy Badge] ( https://api.codacy.com/project/badge/Grade/bd66e7c52002481993cd6d610534b0f7 )] ( https://www.codacy.com/app/fabiojose/sdk-javascript?utm_source=github.com& ; utm_medium=referral& ; utm_content=cloudevents/sdk-javascript& ; utm_campaign=Badge_Grade )
2+ [ ![ Build Status] ( https://travis-ci.org/cloudevents/sdk-javascript.svg?branch=master )] ( https://travis-ci.org/cloudevents/sdk-javascript )
3+
14# sdk-javascript
25Javascript SDK for CloudEvents
36
47> This is a WIP
58
6- # Repository Structure
9+ ## Installation
10+
11+ This CloudEvents SDK requires nodejs 6.11+
12+
13+ ### Nodejs
14+
15+ ```
16+ npm install cloudevents-sdk
17+ ```
18+ ## Specification Support
19+
20+ These are the supported specifications by this version.
21+
22+ | ** Specifications** | ** v0.1** | ** v0.2** |
23+ | ----------------------------| ----------| ----------|
24+ | CloudEvents | yes | yes |
25+ | HTTP Transport Binding | yes | yes |
26+ | JSON Event Format | yes | yes |
27+
28+ ## How to use
29+
30+ The ` Cloudevent ` constructor arguments.
31+
32+ ``` js
33+
34+ /*
35+ * spec : if is null, set the spec 0.1 impl
36+ * format: if is null, set the JSON Format 0.1 impl
37+ */
38+ Cloudevent (spec, format);
39+
40+ ```
41+
42+ ### How to construct instances?
43+
44+ ``` js
45+ var Cloudevent = require (" cloudevents-sdk" );
46+
47+ /*
48+ * Constructs a default instance with:
49+ * - Spec 0.1
50+ * - JSON Format 0.1
51+ */
52+ var cloudevent01 = new Cloudevent ();
53+
54+ /*
55+ * Implemented using Builder Design Pattern
56+ */
57+ cloudevent01
58+ .type (" com.github.pull.create" )
59+ .source (" urn:event:from:myapi/resourse/123" );
60+
61+ /*
62+ * Backward compatibility by injecting methods from spec implementation to Cloudevent
63+ */
64+ cloudevent01
65+ .eventTypeVersion (" 1.0" );
66+
67+ /*
68+ * Constructs an instance with:
69+ * - Spec 0.2
70+ * - JSON Format 0.1
71+ */
72+ var cloudevent02 = new Cloudevent (Cloudevent .specs [' 0.2' ]);
73+
74+ /*
75+ * Different specs, but the same API.
76+ */
77+ cloudevent02
78+ .type (" com.github.pull.create" )
79+ .source (" urn:event:from:myapi/resourse/123" );
80+
81+ ```
82+
83+ ### How to get the formatted payload?
84+
85+ ``` js
86+ var Cloudevent = require (" cloudevents-sdk" );
87+
88+ var cloudevent = new Cloudevent ()
89+ .type (" com.github.pull.create" )
90+ .source (" urn:event:from:myapi/resourse/123" );
91+
92+ /*
93+ * Format the payload and return it.
94+ */
95+ var formatted = cloudevent .format ();
96+
97+ ```
98+
99+ ### How to emit an event?
100+
101+ ``` js
102+ var Cloudevent = require (" cloudevents-sdk" );
103+
104+ // The event
105+ var cloudevent = new Cloudevent ()
106+ .type (" com.github.pull.create" )
107+ .source (" urn:event:from:myapi/resourse/123" );
108+
109+ // The binding configuration using POST
110+ var config = {
111+ method: ' POST' ,
112+ url : ' https://mywebhook.com'
113+ };
114+
115+ // The binding instance
116+ var binding = Cloudevent .bindings [' http-structured0.1' ](config);
117+
118+ // Emit the event using Promise
119+ binding .emit (cloudevent)
120+ .then (response => {
121+ // Treat the response
122+ console .log (response .data );
123+
124+ }).catch (err => {
125+ // Treat the error
126+ console .error (err);
127+ });
128+ ```
129+
130+ ## Repository Structure
7131
8132``` text
9133├── index.js
@@ -44,13 +168,13 @@ Javascript SDK for CloudEvents
44168
45169* ` lib/specs/spec_0_1.js ` : implementation for spec [ version 0.1] ( https://github.com/cloudevents/spec/blob/v0.1/spec.md )
46170
47- * ` lib/specs/spec_0_2.js ` : implementation for spec [ version 0.2] ( https://github.com/cloudevents/spec/blob/master /spec.md )
171+ * ` lib/specs/spec_0_2.js ` : implementation for spec [ version 0.2] ( https://github.com/cloudevents/spec/blob/v0.2 /spec.md )
48172
49173* ` test/cloudevent_spec_0_1.js ` : unit testing for spec 0.1
50174
51175* ` test/cloudevent_spec_0_2.js ` : unit testing for spec 0.2
52176
53- # Unit Testing
177+ ## Unit Testing
54178
55179The unit test checks the result of formatted payload and the constraints.
56180
@@ -60,9 +184,9 @@ npm test
60184
61185```
62186
63- # The API
187+ ## The API
64188
65- ## ` Cloudevent ` class
189+ ### ` Cloudevent ` class
66190
67191``` js
68192
@@ -78,7 +202,7 @@ String Cloudevent.toString()
78202
79203```
80204
81- ## ` Formatter ` classes
205+ ### ` Formatter ` classes
82206
83207Every formatter class must implement these methods to work properly.
84208
@@ -113,122 +237,26 @@ Spec(Cloudevent)
113237Spec .check ()
114238
115239```
116- ## ` Binding ` classes
240+ ### ` Binding ` classes
117241
118242Every Binding class must implement these methods to work properly.
119243
120244``` js
121245
122- /*
246+ /*
123247 * The constructor must receives the map of configurations.
124248 */
125249Binding (config)
126250
127- /*
251+ /*
128252 * Emits the event using an instance of Cloudevent.
129253 */
130254Binding .emit (cloudevent)
131255
132256```
133257
134- # How to use
135-
136- The ` Cloudevent ` constructor arguments.
137-
138- ``` js
139-
140- /*
141- * spec : if is null, set the spec 0.1 impl
142- * format: if is null, set the JSON Format 0.1 impl
143- */
144- Cloudevent (spec, format);
145-
146- ```
147-
148- ## How to construct instances?
149-
150- ``` js
151- /*
152- * Constructs a default instance with:
153- * - Spec 0.1
154- * - JSON Format 0.1
155- */
156- var cloudevent01 = new Cloudevent ();
157-
158- /*
159- * Implemented using Builder Design Pattern
160- */
161- cloudevent01
162- .type (" com.github.pull.create" )
163- .source (" urn:event:from:myapi/resourse/123" );
164-
165- /*
166- * Backward compatibility by injecting methods from spec implementation to Cloudevent
167- */
168- cloudevent01
169- .eventTypeVersion (" 1.0" );
170-
171- /*
172- * Constructs an instance with:
173- * - Spec 0.2
174- * - JSON Format 0.1
175- */
176- var cloudevent02 = new Cloudevent (Cloudevent .specs [' 0.2' ]);
177-
178- /*
179- * Different specs, but the same API.
180- */
181- cloudevent02
182- .type (" com.github.pull.create" )
183- .source (" urn:event:from:myapi/resourse/123" );
184-
185- ```
186-
187- ## How to get the formatted payload?
188-
189- ``` js
190- var cloudevent = new Cloudevent ()
191- .type (" com.github.pull.create" )
192- .source (" urn:event:from:myapi/resourse/123" );
193-
194- /*
195- * Format the payload and return it.
196- */
197- var formatted = cloudevent .format ();
198-
199- ```
200-
201- ## How to emit an event?
202-
203- ``` js
204- // The event
205- var cloudevent = new Cloudevent ()
206- .type (" com.github.pull.create" )
207- .source (" urn:event:from:myapi/resourse/123" );
208-
209- // The binding configuration using POST
210- var config = {
211- method: ' POST' ,
212- url : ' https://mywebhook.com'
213- };
214-
215- // The binding instance
216- var binding = Cloudevent .bindings [' http-structured0.1' ](config);
217-
218- // Emit the event using Promise
219- binding .emit (cloudevent)
220- .then (response => {
221- // Treat the response
222- console .log (response .data );
223-
224- }).catch (err => {
225- // Treat the error
226- console .error (err);
227- });
228- ```
229-
230258> See how to implement the method injection [ here] ( lib/specs/spec_0_1.js#L17 )
231259>
232260> Learn about [ Builder Design Pattern] ( https://en.wikipedia.org/wiki/Builder_pattern )
233- >
261+ >
234262> Check out the produced event payload using this [ tool] ( https://webhook.site )
0 commit comments