|
1 | 1 | # sdk-javascript
|
2 | 2 | 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) |
0 commit comments