diff --git a/lib/bindings/http/constants.js b/lib/bindings/http/constants.js index 7d2a10c..6ca1b48 100644 --- a/lib/bindings/http/constants.js +++ b/lib/bindings/http/constants.js @@ -13,6 +13,7 @@ module.exports = { HEADER_CONTENT_TYPE : "content-type", DEFAULT_CONTENT_TYPE : "application/json; charset=utf-8", + DEFAULT_CE_CONTENT_TYPE : "application/cloudevents+json; charset=utf-8", BINARY_HEADERS_02 : { TYPE : "ce-type", diff --git a/lib/bindings/http/emitter_structured.js b/lib/bindings/http/emitter_structured.js new file mode 100644 index 0000000..0937c32 --- /dev/null +++ b/lib/bindings/http/emitter_structured.js @@ -0,0 +1,30 @@ +var axios = require("axios"); + +const Constants = require("./constants.js"); + +function StructuredHTTPEmitter(configuration){ + this.config = JSON.parse(JSON.stringify(configuration)); + + this.config[Constants.HEADERS] = + (!this.config[Constants.HEADERS] + ? {} + : this.config[Constants.HEADERS]); + + if(!this.config[Constants.HEADERS][Constants.HEADER_CONTENT_TYPE]){ + this.config[Constants.HEADERS][Constants.HEADER_CONTENT_TYPE] = + Constants.DEFAULT_CE_CONTENT_TYPE; + } +} + +StructuredHTTPEmitter.prototype.emit = function(cloudevent) { + // Create new request object + var _config = JSON.parse(JSON.stringify(this.config)); + + // Set the cloudevent payload + _config[Constants.DATA_ATTRIBUTE] = cloudevent.format(); + + // Return the Promise + return axios.request(_config); +}; + +module.exports = StructuredHTTPEmitter; diff --git a/lib/bindings/http/emitter_structured_0_2.js b/lib/bindings/http/emitter_structured_0_2.js index 5448955..78ad50d 100644 --- a/lib/bindings/http/emitter_structured_0_2.js +++ b/lib/bindings/http/emitter_structured_0_2.js @@ -1,29 +1,13 @@ -var axios = require("axios"); +const StructuredHTTPEmitter = require("./emitter_structured.js"); const Constants = require("./constants.js"); function HTTPStructured(configuration){ - this.config = JSON.parse(JSON.stringify(configuration)); - - if(!this.config["headers"]){ - this.config["headers"] = {}; - } - - this.config["headers"] - [Constants.HEADER_CONTENT_TYPE] = - Constants.MIME_CE_JSON + "; charset=" + Constants.CHARSET_DEFAULT; + this.emitter = new StructuredHTTPEmitter(configuration); } 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(); - - // Return the Promise - return axios.request(_config); + return this.emitter.emit(cloudevent); }; module.exports = HTTPStructured;