Skip to content

Commit ff3ae0a

Browse files
authored
Merge pull request #2 from fabiojose/start
The start of JavaScript SDK
2 parents 28b81eb + f65bcfa commit ff3ae0a

File tree

12 files changed

+720
-0
lines changed

12 files changed

+720
-0
lines changed

.gitignore

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
8+
# Runtime data
9+
pids
10+
*.pid
11+
*.seed
12+
*.pid.lock
13+
14+
# Directory for instrumented libs generated by jscoverage/JSCover
15+
lib-cov
16+
17+
# Coverage directory used by tools like istanbul
18+
coverage
19+
20+
# nyc test coverage
21+
.nyc_output
22+
23+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
24+
.grunt
25+
26+
# Bower dependency directory (https://bower.io/)
27+
bower_components
28+
29+
# node-waf configuration
30+
.lock-wscript
31+
32+
# Compiled binary addons (https://nodejs.org/api/addons.html)
33+
build/Release
34+
35+
# Dependency directories
36+
node_modules/
37+
jspm_packages/
38+
39+
# TypeScript v1 declaration files
40+
typings/
41+
42+
# Optional npm cache directory
43+
.npm
44+
45+
# Optional eslint cache
46+
.eslintcache
47+
48+
# Optional REPL history
49+
.node_repl_history
50+
51+
# Output of 'npm pack'
52+
*.tgz
53+
54+
# Yarn Integrity file
55+
.yarn-integrity
56+
57+
# dotenv environment variables file
58+
.env
59+
60+
# parcel-bundler cache (https://parceljs.org/)
61+
.cache
62+
63+
# next.js build output
64+
.next
65+
66+
# nuxt.js build output
67+
.nuxt
68+
69+
# vuepress build output
70+
.vuepress/dist
71+
72+
# Serverless directories
73+
.serverless
74+
75+
# FuseBox cache
76+
.fusebox/
77+

README.md

Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,234 @@
11
# sdk-javascript
22
Javascript SDK for CloudEvents
3+
4+
> This is a WIP
5+
6+
# Repository Structure
7+
8+
```text
9+
├── index.js
10+
├── lib
11+
│   ├── bindings
12+
│   │   └── http
13+
│   │   └── structured_0_1.js
14+
│   ├── cloudevent.js
15+
│   ├── format
16+
│   │   └── json_0_1.js
17+
│   └── specs
18+
│   ├── spec_0_1.js
19+
│   └── spec_0_2.js
20+
├── LICENSE
21+
├── package.json
22+
├── README.md
23+
└── test
24+
├── cloudevent_spec_0_1.js
25+
├── cloudevent_spec_0_2.js
26+
└── http_binding_0_1.js
27+
```
28+
29+
* `index.js`: library exports
30+
31+
* `lib/bindings`: every binding implementation goes here
32+
33+
* `lib/bindings/http`: every http binding implementation goes here
34+
35+
* `lib/bindings/http/structured_0_1.js`: implementation of structured HTTP Binding
36+
37+
* `lib/cloudevent.js`: implementation of Cloudevent, an interface
38+
39+
* `lib/format/`: every format implementation goes here
40+
41+
* `lib/format/json_0_1.js`: implementation for JSON formatting [version 0.1](https://github.com/cloudevents/spec/blob/v0.1/json-format.md)
42+
43+
* `lib/specs/`: every spec implementation goes here
44+
45+
* `lib/specs/spec_0_1.js`: implementation for spec [version 0.1](https://github.com/cloudevents/spec/blob/v0.1/spec.md)
46+
47+
* `lib/specs/spec_0_2.js`: implementation for spec [version 0.2](https://github.com/cloudevents/spec/blob/master/spec.md)
48+
49+
* `test/cloudevent_spec_0_1.js`: unit testing for spec 0.1
50+
51+
* `test/cloudevent_spec_0_2.js`: unit testing for spec 0.2
52+
53+
# Unit Testing
54+
55+
The unit test checks the result of formatted payload and the constraints.
56+
57+
```bash
58+
59+
npm test
60+
61+
```
62+
63+
# The API
64+
65+
## `Cloudevent` class
66+
67+
```js
68+
69+
/*
70+
* Format the payload and return an Object.
71+
*/
72+
Object Cloudevent.format()
73+
74+
/*
75+
* Format the payload as String.
76+
*/
77+
String Cloudevent.toString()
78+
79+
```
80+
81+
## `Formatter` classes
82+
83+
Every formatter class must implement these methods to work properly.
84+
85+
```js
86+
87+
/*
88+
* Format the Cloudevent payload argument and return an Object.
89+
*/
90+
Object Formatter.format(payload)
91+
92+
/*
93+
* Format the Cloudevent payload as String.
94+
*/
95+
String Formatter.toString(payload)
96+
97+
```
98+
99+
## `Spec` classes
100+
101+
Every Spec class must implement these methods to work properly.
102+
103+
```js
104+
105+
/*
106+
* The constructor must receives the Cloudevent type.
107+
*/
108+
Spec(Cloudevent)
109+
110+
/*
111+
* Checks the spec constraints, throwing an error if do not pass.
112+
*/
113+
Spec.check()
114+
115+
```
116+
## `Binding` classes
117+
118+
Every Binding class must implement these methods to work properly.
119+
120+
```js
121+
122+
/*
123+
* The constructor must receives the map of configurations.
124+
*/
125+
Binding(config)
126+
127+
/*
128+
* Emits the event using an instance of Cloudevent.
129+
*/
130+
Binding.emit(cloudevent)
131+
132+
```
133+
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+
230+
> See how to implement the method injection [here](lib/specs/spec_0_1.js#L17)
231+
>
232+
> Learn about [Builder Design Pattern](https://en.wikipedia.org/wiki/Builder_pattern)
233+
>
234+
> Check out the produced event payload using this [tool](https://webhook.site)

index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
var Cloudevent = require('./lib/cloudevent.js');
2+
3+
module.exports = Cloudevent;
4+

lib/bindings/http/structured_0_1.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var axios = require("axios");
2+
3+
function HTTPStructured(configuration){
4+
this.config = configuration;
5+
6+
this.config['headers'] = {
7+
'Content-Type':'application/cloudevents+json; charset=utf-8'
8+
};
9+
}
10+
11+
HTTPStructured.prototype.emit = function(cloudevent){
12+
13+
// Create new request object
14+
var _config = JSON.parse(JSON.stringify(this.config));
15+
16+
// Set the cloudevent payload
17+
_config['data'] = cloudevent.format();
18+
19+
// Return the Promise
20+
return axios.request(_config);
21+
}
22+
23+
module.exports = HTTPStructured;
24+

0 commit comments

Comments
 (0)