diff --git a/examples/typescript-ex/src/index.ts b/examples/typescript-ex/src/index.ts index 360aad1..5244618 100644 --- a/examples/typescript-ex/src/index.ts +++ b/examples/typescript-ex/src/index.ts @@ -1,4 +1,10 @@ -import Cloudevent, { event, StructuredHTTPEmitter, BinaryHTTPEmitter } from 'cloudevents-sdk/v1'; +import Cloudevent, { + event, + StructuredHTTPEmitter, + BinaryHTTPEmitter, + StructuredHTTPReceiver, + BinaryHTTPReceiver +} from 'cloudevents-sdk/v1'; export function doSomeStuff() { @@ -8,15 +14,18 @@ export function doSomeStuff() { .dataContentType('text/plain') .dataschema('http://d.schema.com/my.json') .subject('cha.json') - .data('my-data'); + .data('my-data') + .addExtension("my-ext", "0x600"); - console.log(myevent.format()); + console.log(myevent.toString()); + console.log(myevent.getExtensions()); let config = { method: "POST", url : "https://enu90y24i64jp.x.pipedream.net/" }; + // ------ emitter structured let structured = new StructuredHTTPEmitter(config); structured.emit(myevent).then(res => { // success @@ -27,6 +36,7 @@ export function doSomeStuff() { console.error(err); }); + // ------ emitter binary let binary = new BinaryHTTPEmitter(config); binary.emit(myevent).then(res => { console.log("Binary Mode: Success!"); @@ -35,7 +45,35 @@ export function doSomeStuff() { console.error(err); }); - return true; + // ------ receiver structured + let payload = myevent.toString(); + let headers = { + "Content-Type":"application/cloudevents+json" + }; + + let receiverStructured = new StructuredHTTPReceiver(); + console.log(receiverStructured.parse(payload, headers).toString()); + + // ------ receiver binary + let extension1 = "mycuston-ext1"; + let data = { + "data" : "dataString" + }; + var attributes = { + "ce-type" : "type", + "ce-specversion" : "1.0", + "ce-source" : "source", + "ce-id" : "id", + "ce-time" : "2019-06-16T11:42:00Z", + "ce-dataschema" : "http://schema.registry/v1", + "Content-Type" : "application/json", + "ce-extension1" : extension1 + }; + + let receiverBinary = new BinaryHTTPReceiver(); + console.log(receiverBinary.parse(data, attributes).toString()); + +return true; } doSomeStuff();