Skip to content

Commit bf28816

Browse files
committed
format the how to use
Signed-off-by: Fabio José <[email protected]>
1 parent 0489de6 commit bf28816

File tree

1 file changed

+105
-99
lines changed

1 file changed

+105
-99
lines changed

README.md

Lines changed: 105 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,108 @@ These are the supported specifications by this version.
2525
| HTTP Transport Binding | yes | yes |
2626
| JSON Event Format | yes | yes |
2727

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+
28130
## Repository Structure
29131

30132
```text
@@ -141,116 +243,20 @@ Every Binding class must implement these methods to work properly.
141243

142244
```js
143245

144-
/*
246+
/*
145247
* The constructor must receives the map of configurations.
146248
*/
147249
Binding(config)
148250

149-
/*
251+
/*
150252
* Emits the event using an instance of Cloudevent.
151253
*/
152254
Binding.emit(cloudevent)
153255

154256
```
155257

156-
## How to use
157-
158-
The `Cloudevent` constructor arguments.
159-
160-
```js
161-
162-
/*
163-
* spec : if is null, set the spec 0.1 impl
164-
* format: if is null, set the JSON Format 0.1 impl
165-
*/
166-
Cloudevent(spec, format);
167-
168-
```
169-
170-
### How to construct instances?
171-
172-
```js
173-
/*
174-
* Constructs a default instance with:
175-
* - Spec 0.1
176-
* - JSON Format 0.1
177-
*/
178-
var cloudevent01 = new Cloudevent();
179-
180-
/*
181-
* Implemented using Builder Design Pattern
182-
*/
183-
cloudevent01
184-
.type("com.github.pull.create")
185-
.source("urn:event:from:myapi/resourse/123");
186-
187-
/*
188-
* Backward compatibility by injecting methods from spec implementation to Cloudevent
189-
*/
190-
cloudevent01
191-
.eventTypeVersion("1.0");
192-
193-
/*
194-
* Constructs an instance with:
195-
* - Spec 0.2
196-
* - JSON Format 0.1
197-
*/
198-
var cloudevent02 = new Cloudevent(Cloudevent.specs['0.2']);
199-
200-
/*
201-
* Different specs, but the same API.
202-
*/
203-
cloudevent02
204-
.type("com.github.pull.create")
205-
.source("urn:event:from:myapi/resourse/123");
206-
207-
```
208-
209-
### How to get the formatted payload?
210-
211-
```js
212-
var cloudevent = new Cloudevent()
213-
.type("com.github.pull.create")
214-
.source("urn:event:from:myapi/resourse/123");
215-
216-
/*
217-
* Format the payload and return it.
218-
*/
219-
var formatted = cloudevent.format();
220-
221-
```
222-
223-
### How to emit an event?
224-
225-
```js
226-
// The event
227-
var cloudevent = new Cloudevent()
228-
.type("com.github.pull.create")
229-
.source("urn:event:from:myapi/resourse/123");
230-
231-
// The binding configuration using POST
232-
var config = {
233-
method: 'POST',
234-
url : 'https://mywebhook.com'
235-
};
236-
237-
// The binding instance
238-
var binding = Cloudevent.bindings['http-structured0.1'](config);
239-
240-
// Emit the event using Promise
241-
binding.emit(cloudevent)
242-
.then(response => {
243-
// Treat the response
244-
console.log(response.data);
245-
246-
}).catch(err => {
247-
// Treat the error
248-
console.error(err);
249-
});
250-
```
251-
252258
> See how to implement the method injection [here](lib/specs/spec_0_1.js#L17)
253259
>
254260
> Learn about [Builder Design Pattern](https://en.wikipedia.org/wiki/Builder_pattern)
255-
>
261+
>
256262
> Check out the produced event payload using this [tool](https://webhook.site)

0 commit comments

Comments
 (0)