|
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 | +│ ├── cloudevent.js |
| 12 | +│ ├── jsonformatter.js |
| 13 | +│ ├── format |
| 14 | +│ │ └── json_0_1.js |
| 15 | +│ └── specs |
| 16 | +│ ├── spec_0_1.js |
| 17 | +│ └── spec_0_2.js |
| 18 | +├── LICENSE |
| 19 | +├── package.json |
| 20 | +├── README.md |
| 21 | +└── test |
| 22 | + ├── cloudevent_spec_0_1.js |
| 23 | + └── cloudevent_spec_0_2.js |
| 24 | +
|
| 25 | +``` |
| 26 | + |
| 27 | +* `index.js`: library exports |
| 28 | + |
| 29 | +* `lib/cloudevent.js`: implementation of Cloudevent, an interface |
| 30 | + |
| 31 | +* `lib/format/`: every format implementation goes here |
| 32 | + |
| 33 | +* `lib/format/json_0_1.js`: implementation for JSON formatting [version 0.1](https://github.com/cloudevents/spec/blob/v0.1/json-format.md) |
| 34 | + |
| 35 | +* `lib/specs/`: every spec implementation goes here |
| 36 | + |
| 37 | +* `lib/specs/spec_0_1.js`: implementation for spec [version 0.1](https://github.com/cloudevents/spec/blob/v0.1/spec.md) |
| 38 | + |
| 39 | +* `lib/specs/spec_0_2.js`: implementation for spec [version 0.2](https://github.com/cloudevents/spec/blob/master/spec.md) |
| 40 | + |
| 41 | +* `test/cloudevent_spec_0_1.js`: unit testing for spec 0.1 |
| 42 | + |
| 43 | +* `test/cloudevent_spec_0_2.js`: unit testing for spec 0.2 |
| 44 | + |
| 45 | +# Unit Testing |
| 46 | + |
| 47 | +The unit test checks the result of formatted payload and the constraints. |
| 48 | + |
| 49 | +```bash |
| 50 | + |
| 51 | +npm test |
| 52 | + |
| 53 | +``` |
| 54 | + |
| 55 | +# The API |
| 56 | + |
| 57 | +## `Cloudevent` class |
| 58 | + |
| 59 | +```js |
| 60 | + |
| 61 | +/* |
| 62 | + * Format the payload and return an Object. |
| 63 | + */ |
| 64 | +Object Cloudevent.format() |
| 65 | + |
| 66 | +/* |
| 67 | + * Format the payload as String. |
| 68 | + */ |
| 69 | +String Cloudevent.toString() |
| 70 | + |
| 71 | +``` |
| 72 | + |
| 73 | +## `Formatter` classes |
| 74 | + |
| 75 | +Every formatter class must implement these methods to work properly. |
| 76 | + |
| 77 | +```js |
| 78 | + |
| 79 | +/* |
| 80 | + * Format the Cloudevent payload argument and return an Object. |
| 81 | + */ |
| 82 | +Object Formatter.format(payload) |
| 83 | + |
| 84 | +/* |
| 85 | + * Format the Cloudevent payload as String. |
| 86 | + */ |
| 87 | +String Formatter.toString(payload) |
| 88 | + |
| 89 | +``` |
| 90 | + |
| 91 | +## `Spec` classes |
| 92 | + |
| 93 | +Every Spec class must implement these methods to work properly. |
| 94 | + |
| 95 | +```js |
| 96 | + |
| 97 | +/* |
| 98 | + * Check the spec constraints, throwing an error if do not pass. |
| 99 | + */ |
| 100 | +Spec.check() |
| 101 | + |
| 102 | +``` |
| 103 | + |
| 104 | +# How to use |
| 105 | + |
| 106 | +The `Cloudevent` constructor arguments. |
| 107 | + |
| 108 | +```js |
| 109 | + |
| 110 | +/* |
| 111 | + * spec : if is null, set the spec 0.1 impl |
| 112 | + * format: if is null, set the JSON Format 0.1 impl |
| 113 | + */ |
| 114 | +Cloudevent(spec, format); |
| 115 | + |
| 116 | +``` |
| 117 | + |
| 118 | +## How to construct instances? |
| 119 | + |
| 120 | +```js |
| 121 | +/* |
| 122 | + * Constructs a default instance with: |
| 123 | + * - Spec 0.1 |
| 124 | + * - JSON Format 0.1 |
| 125 | + */ |
| 126 | +var cloudevent01 = new Cloudevent(); |
| 127 | + |
| 128 | +/* |
| 129 | + * Implemented using Builder Design Pattern |
| 130 | + */ |
| 131 | +cloudevent01 |
| 132 | + .type("com.github.pull.create") |
| 133 | + .source("urn:event:from:myapi/resourse/123"); |
| 134 | + |
| 135 | +/* |
| 136 | + * Backward compatibility by injecting methods from spec implementation to Cloudevent |
| 137 | + */ |
| 138 | +cloudevent01 |
| 139 | + .eventTypeVersion("1.0"); |
| 140 | + |
| 141 | +/* |
| 142 | + * Constructs an instance with: |
| 143 | + * - Spec 0.2 |
| 144 | + * - JSON Format 0.1 |
| 145 | + */ |
| 146 | +var cloudevent02 = new Cloudevent(Cloudevent.specs['0.2']); |
| 147 | + |
| 148 | +/* |
| 149 | + * Different specs, but the same API. |
| 150 | + */ |
| 151 | +cloudevent02 |
| 152 | + .type("com.github.pull.create") |
| 153 | + .source("urn:event:from:myapi/resourse/123"); |
| 154 | + |
| 155 | +``` |
| 156 | + |
| 157 | +## How to get the formatted payload? |
| 158 | + |
| 159 | +```js |
| 160 | +var cloudevent = new Cloudevent() |
| 161 | + .type("com.github.pull.create") |
| 162 | + .source("urn:event:from:myapi/resourse/123"); |
| 163 | + |
| 164 | +/* |
| 165 | + * Format the payload and return it. |
| 166 | + */ |
| 167 | +var formatted = cloudevent.format(); |
| 168 | + |
| 169 | +``` |
| 170 | + |
| 171 | +> See how to implement the method injection [here](lib/specs/spec_0_1.js#L17) |
| 172 | +> |
| 173 | +> Learn about [Builder Design Pattern](https://en.wikipedia.org/wiki/Builder_pattern) |
0 commit comments