|
1 |
| -import { CloudEvent, HTTPREceiver } from '../../'; |
| 1 | +import { CloudEvent, HTTPReceiver } from "cloudevents-sdk"; |
| 2 | +import { CloudEventV1 } from "cloudevents-sdk/lib/v1"; |
2 | 3 |
|
3 | 4 | export function doSomeStuff() {
|
4 |
| - const receiver = new HTTPREceiver(); |
| 5 | + const receiver = new HTTPReceiver(); |
5 | 6 |
|
6 |
| - const myevent: CloudEvent = new CloudEvent() |
7 |
| - .source('/source') |
8 |
| - .type('type') |
9 |
| - .dataContentType('text/plain') |
10 |
| - .dataschema('http://d.schema.com/my.json') |
11 |
| - .subject('cha.json') |
12 |
| - .data('my-data') |
13 |
| - .addExtension("my-ext", "0x600"); |
| 7 | + const myevent: CloudEventV1 = new CloudEvent({ |
| 8 | + source: "/source", |
| 9 | + type: "type", |
| 10 | + dataContentType: "text/plain", |
| 11 | + dataSchema: "https://d.schema.com/my.json", |
| 12 | + subject: "cha.json", |
| 13 | + data: "my-data" |
| 14 | + }); |
| 15 | + myevent.addExtension("extension-1", "some extension data"); |
14 | 16 |
|
15 |
| - console.log(myevent.toString()); |
16 |
| - console.log(myevent.getExtensions()); |
| 17 | + console.log("My structured event:", myevent.toString()); |
| 18 | + console.log("My structured event extensions:", myevent.getExtensions()); |
17 | 19 |
|
18 | 20 | // ------ receiver structured
|
19 |
| - const payload = myevent.toString(); |
| 21 | + // The header names should be standarized to use lowercase |
20 | 22 | const headers = {
|
21 |
| - "Content-Type":"application/cloudevents+json" |
| 23 | + "content-type": "application/cloudevents+json" |
22 | 24 | };
|
23 | 25 |
|
24 |
| - console.log(receiver.accept(headers, payload).toString()); |
| 26 | + // Typically used with an incoming HTTP request where myevent.format() is the actual |
| 27 | + // body of the HTTP |
| 28 | + console.log("Received structured event:", receiver.accept(headers, myevent.format()).toString()); |
25 | 29 |
|
26 | 30 | // ------ receiver binary
|
27 |
| - const extension1 = "mycuston-ext1"; |
28 | 31 | const data = {
|
29 |
| - "data" : "dataString" |
| 32 | + "data": "dataString" |
30 | 33 | };
|
31 | 34 | const attributes = {
|
32 |
| - "ce-type" : "type", |
33 |
| - "ce-specversion" : "1.0", |
34 |
| - "ce-source" : "source", |
35 |
| - "ce-id" : "id", |
36 |
| - "ce-time" : "2019-06-16T11:42:00Z", |
37 |
| - "ce-dataschema" : "http://schema.registry/v1", |
38 |
| - "Content-Type" : "application/json", |
39 |
| - "ce-extension1" : extension1 |
| 35 | + "ce-type": "type", |
| 36 | + "ce-specversion": "1.0", |
| 37 | + "ce-source": "source", |
| 38 | + "ce-id": "id", |
| 39 | + "ce-time": "2019-06-16T11:42:00Z", |
| 40 | + "ce-dataschema": "http://schema.registry/v1", |
| 41 | + "Content-Type": "application/json", |
| 42 | + "ce-extension1": "extension1" |
40 | 43 | };
|
41 | 44 |
|
42 |
| - console.log(receiver.accept(attributes, data).toString()); |
| 45 | + console.log("My binary event:", receiver.accept(attributes, data).toString()); |
| 46 | + console.log("My binary event extensions:", receiver.accept(attributes, data).toString()); |
43 | 47 |
|
44 | 48 | return true;
|
45 | 49 | }
|
|
0 commit comments