ts Example using binary receiver
Signed-off-by: Fabio José <fabiojose@gmail.com>
This commit is contained in:
parent
596bd3fd64
commit
807d60462b
|
|
@ -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() {
|
export function doSomeStuff() {
|
||||||
|
|
||||||
|
|
@ -8,15 +14,18 @@ export function doSomeStuff() {
|
||||||
.dataContentType('text/plain')
|
.dataContentType('text/plain')
|
||||||
.dataschema('http://d.schema.com/my.json')
|
.dataschema('http://d.schema.com/my.json')
|
||||||
.subject('cha.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 = {
|
let config = {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
url : "https://enu90y24i64jp.x.pipedream.net/"
|
url : "https://enu90y24i64jp.x.pipedream.net/"
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// ------ emitter structured
|
||||||
let structured = new StructuredHTTPEmitter(config);
|
let structured = new StructuredHTTPEmitter(config);
|
||||||
structured.emit(myevent).then(res => {
|
structured.emit(myevent).then(res => {
|
||||||
// success
|
// success
|
||||||
|
|
@ -27,6 +36,7 @@ export function doSomeStuff() {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// ------ emitter binary
|
||||||
let binary = new BinaryHTTPEmitter(config);
|
let binary = new BinaryHTTPEmitter(config);
|
||||||
binary.emit(myevent).then(res => {
|
binary.emit(myevent).then(res => {
|
||||||
console.log("Binary Mode: Success!");
|
console.log("Binary Mode: Success!");
|
||||||
|
|
@ -35,6 +45,34 @@ export function doSomeStuff() {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// ------ 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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue