diff --git a/lib/bindings/http/binary_0_1.js b/lib/bindings/http/binary_0_1.js index 5bac97bd..240401c8 100644 --- a/lib/bindings/http/binary_0_1.js +++ b/lib/bindings/http/binary_0_1.js @@ -1,10 +1,10 @@ var axios = require("axios"); function HTTPBinary(configuration){ - this.config = configuration; + this.config = JSON.parse(JSON.stringify(configuration)); this.config["headers"] = { - "Content-Type":"application/cloudevents+json; charset=utf-8" + "Content-Type":"application/json; charset=utf-8" }; } @@ -16,25 +16,40 @@ HTTPBinary.prototype.emit = function(cloudevent){ // Always set stuff in _config var _headers = _config["headers"]; + // OPTIONAL CONTENT TYPE ATTRIBUTE if(cloudevent.getContenttype()) { _headers["Content-Type"] = cloudevent.getContenttype(); } + // REQUIRED ATTRIBUTES _headers["CE-EventType"] = cloudevent.getType(); - if(cloudevent.getEventTypeVersion()) { - _headers["CE-EventTypeVersion"] = cloudevent.getEventTypeVersion(); - } _headers["CE-CloudEventsVersion"] = cloudevent.getSpecversion(); _headers["CE-Source"] = cloudevent.getSource(); _headers["CE-EventID"] = cloudevent.getId(); + + // OPTIONAL ATTRIBUTES + if(cloudevent.getEventTypeVersion()) { + _headers["CE-EventTypeVersion"] = cloudevent.getEventTypeVersion(); + } if(cloudevent.getTime()) { _headers["CE-EventTime"] = cloudevent.getTime(); } - _headers["CE-SchemaURL"] = cloudevent.getSchemaurl(); + if(cloudevent.getSchemaurl()) { + _headers["CE-SchemaURL"] = cloudevent.getSchemaurl(); + } // Set the cloudevent payload _config["data"] = cloudevent.format().data; + // EXTENSION CONTEXT ATTRIBUTES + var exts = cloudevent.getExtensions(); + for(var ext in exts){ + if({}.hasOwnProperty.call(exts, ext)){ + let capsExt = ext.charAt(0).toUpperCase() + ext.slice(1) + _headers["CE-X-" + capsExt] = exts[ext]; + } + } + // Return the Promise return axios.request(_config); }; diff --git a/lib/bindings/http/binary_0_2.js b/lib/bindings/http/binary_0_2.js index 8d249e53..822bf41c 100644 --- a/lib/bindings/http/binary_0_2.js +++ b/lib/bindings/http/binary_0_2.js @@ -2,10 +2,10 @@ var axios = require("axios"); var empty = require("is-empty"); function HTTPBinary(configuration){ - this.config = configuration; + this.config = JSON.parse(JSON.stringify(configuration)); this.config["headers"] = { - "Content-Type":"application/cloudevents+json; charset=utf-8" + "Content-Type":"application/json; charset=utf-8" }; } @@ -17,21 +17,29 @@ HTTPBinary.prototype.emit = function(cloudevent){ // Always set stuff in _config var _headers = _config["headers"]; - _headers["Content-Type"] = cloudevent.getContenttype(); + // OPTIONAL CONTENT TYPE ATTRIBUTE + if (cloudevent.getContenttype()) { + _headers["Content-Type"] = cloudevent.getContenttype(); + } + // REQUIRED ATTRIBUTES _headers["ce-type"] = cloudevent.getType(); _headers["ce-specversion"] = cloudevent.getSpecversion(); _headers["ce-source"] = cloudevent.getSource(); _headers["ce-id"] = cloudevent.getId(); - if(cloudevent.getTime()) { + + // OPTIONAL ATTRIBUTES + if (cloudevent.getTime()) { _headers["ce-time"] = cloudevent.getTime(); } - _headers["ce-schemaurl"] = cloudevent.getSchemaurl(); + if (cloudevent.getSchemaurl()) { + _headers["ce-schemaurl"] = cloudevent.getSchemaurl(); + } // Set the cloudevent payload _config["data"] = cloudevent.format().data; - // Have extensions? + // EXTENSION CONTEXT ATTRIBUTES var exts = cloudevent.getExtensions(); for(var ext in exts){ if({}.hasOwnProperty.call(exts, ext)){ diff --git a/lib/bindings/http/structured_0_1.js b/lib/bindings/http/structured_0_1.js index 65c65309..cf352820 100644 --- a/lib/bindings/http/structured_0_1.js +++ b/lib/bindings/http/structured_0_1.js @@ -1,7 +1,7 @@ var axios = require("axios"); function HTTPStructured(configuration){ - this.config = configuration; + this.config = JSON.parse(JSON.stringify(configuration)); this.config["headers"] = { "Content-Type":"application/cloudevents+json; charset=utf-8" @@ -13,6 +13,7 @@ HTTPStructured.prototype.emit = function(cloudevent){ // Create new request object var _config = JSON.parse(JSON.stringify(this.config)); + // Set the cloudevent payload _config["data"] = cloudevent.format(); diff --git a/lib/bindings/http/structured_0_2.js b/lib/bindings/http/structured_0_2.js index 65c65309..fd459560 100644 --- a/lib/bindings/http/structured_0_2.js +++ b/lib/bindings/http/structured_0_2.js @@ -1,7 +1,7 @@ var axios = require("axios"); function HTTPStructured(configuration){ - this.config = configuration; + this.config = JSON.parse(JSON.stringify(configuration)); this.config["headers"] = { "Content-Type":"application/cloudevents+json; charset=utf-8"