Skip to content

Commit 561c339

Browse files
authored
Merge pull request #3 from cloudevents/spec0.2
Specification 0.2 support
2 parents ff3ae0a + bf28816 commit 561c339

File tree

8 files changed

+387
-118
lines changed

8 files changed

+387
-118
lines changed

.travis.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
language: node_js
2+
node_js:
3+
- 8
4+
- 7
5+
- 6
6+
7+
cache: npm
8+
9+
jobs:
10+
include:
11+
- stage: github release
12+
node_js: "6"
13+
deploy:
14+
provider: releases
15+
api_key:
16+
secure: "ls/IjZdRpFZlEQsZrsmXn7h7QTZuM8x4gZq3r75sOyTWcmKgZLN27hYCtk8SPcuNbt+ZA3otQEQJvDuGTCAwtbU4lxQjfXswP9M/om01hf3J7uTcjtol4XOsmBFqMCktW94pVH4U7Q18IHeP5JBnlfLzAY/YifRKKNWNrsI6bbnfcGgsFJYvICwKL5LEj7bxtJk156lBBJm9TscMX48BFsUBvnCNTDEBYNaaGkoCz6DPM0Da7QPF5exHcNiiF9SoR60WeGI1q+MdZyE+1AG0VpUp7sMxJafXSBbgYquPEZmG9ScZbkVyWP7ps3sgkqDkxtH4kNe9zF4PySRYfxYKJ03ECwbSmj9gxsIc8o2zhrLFinmHydLxoOEh5EygB2euekgMhoEKL0kA8bteaaqmD+3sna8MbXG4HhvIrEAspNtOFZyUm/SmgvXXxaKiDtwTw/5CvPCc7VH51NuC5QZi0UehLzatDpDNsj/ffMwDp5F4SBIZgUUmmIWfnAODNGwSwBKxdPVS3etkTeiBxerw2uzC6qjNW1QJyb9s+iU3rC59IEAuq6u4ymv3caDIHLnJbCMHRHQoIbq86oErqb8DhH6KrTlBnwlGBpUVhdpYMt0QX2Uncu9uHMBZ2CQNZ819PJa4QWvc1Ixoj3auKqvoMfPNQFOskMWnrnxkOokyPZg="
17+
skip_cleanup: true
18+
on:
19+
tags: true
20+
- stage: npm release
21+
node_js: "6"
22+
deploy:
23+
provider: npm
24+
25+
api_key:
26+
secure: "o5aF53FTPUSiJBbCZ/anBWQCgEJCctpxuTgGJbO1NpiheOM/xENiSmv+n2a5sGrhhqT0h7k15mE/ZgtL8TnM+45AHOg3EGez5JoR1XMIXnSeCno1GFK4waHDcEn3eLW9P35r1S5/RMTqbEUvqpyK/fzVZ4ecyh9t7dVvJV8MyQGo+r+oO3SLYEIt7YC6vZRh+dV3cK8jr7MHkHmQfZ6tZrALMKsj6QNTUtmk+IU52WHi4oe1iPuypS5dlaVdmanX7ZRtC7gR0Dko2/wja+DPOAKgG/S0aS794cxal7P5k/K34mvqT8iaCl4vN5uIcRsipgsprIahk2G2NgnIVCetBda9LhpMNUdn3j+v4T0lx3jiqP1eq01nk8YFpTV4Xz9VlYHK/E6NYQhUmd7N6WO9vXmOEDGBvWrDfQ1QMx+/TM9r3vzK9ps3sjkDFaAtJ2ZQ0pvFMEQTaLKak24ntWltSWZKvxdjYnS+bcfyIQQGagvqgZsnkKzeudO9N9Atp4OGcst9CAvvykDsfmLlARAiyvpuDVyUivuaOlCB9J7VBt1sbBfsiHpnJcSsMVz1OMRX5EGewbla530guoePhjTUDVit3NyUMz3ZQQTN9VSK3tA+NLyR0Ex8Oel+byHJDYyf+36GCDvoXagaPUVk5M5BINiCw2IWhxDgRGrcEp9JIUI="
27+
skip_cleanup: true
28+
on:
29+
tags: true

README.md

Lines changed: 134 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,133 @@
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
25
Javascript 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

55179
The 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

83207
Every formatter class must implement these methods to work properly.
84208

@@ -113,122 +237,26 @@ Spec(Cloudevent)
113237
Spec.check()
114238

115239
```
116-
## `Binding` classes
240+
### `Binding` classes
117241

118242
Every 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
*/
125249
Binding(config)
126250

127-
/*
251+
/*
128252
* Emits the event using an instance of Cloudevent.
129253
*/
130254
Binding.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)

lib/cloudevent.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,26 @@ Cloudevent.prototype.time = function(_time){
4848
return this;
4949
}
5050

51+
Cloudevent.prototype.schemaurl = function(_schemaurl) {
52+
this.spec.schemaurl(_schemaurl);
53+
return this;
54+
}
55+
56+
Cloudevent.prototype.contenttype = function(_contenttype){
57+
this.spec.contenttype(_contenttype);
58+
return this;
59+
}
60+
61+
Cloudevent.prototype.data = function(_data) {
62+
this.spec.data(_data);
63+
return this;
64+
}
65+
66+
Cloudevent.prototype.addExtension = function(key, value){
67+
this.spec.addExtension(key, value);
68+
return this;
69+
}
70+
5171
/*
5272
* Export the specs
5373
*/

lib/specs/spec_0_1.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,28 @@ Spec_0_1.prototype.time = function(_time){
5757
return this;
5858
}
5959

60-
//TODO another attributes . . .
60+
Spec_0_1.prototype.schemaurl = function(_schemaurl){
61+
this.payload['schemaURL'] = _schemaurl;
62+
return this;
63+
}
64+
65+
Spec_0_1.prototype.contenttype = function(_contenttype){
66+
this.payload['contentType'] = _contenttype;
67+
return this;
68+
}
69+
70+
Spec_0_1.prototype.data = function(_data){
71+
this.payload['data'] = _data;
72+
return this;
73+
}
74+
75+
Spec_0_1.prototype.addExtension = function(key, value){
76+
if(!this.payload['extensions']){
77+
this.payload['extensions'] = {};
78+
}
79+
this.payload['extensions'][key] = value;
80+
return this;
81+
}
6182

6283
module.exports = Spec_0_1;
6384

0 commit comments

Comments
 (0)