From 043a4a80729d0d9ee1360680a8e8d748e5fde52c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabio=20Jos=C3=A9?= Date: Tue, 11 Jun 2019 13:13:44 -0300 Subject: [PATCH] Http binary binding with constants MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fabio José --- lib/bindings/http/emitter_binary_0_2.js | 33 +++++++++++++++++-------- lib/cloudevent.js | 2 +- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/lib/bindings/http/emitter_binary_0_2.js b/lib/bindings/http/emitter_binary_0_2.js index bcf9695..f507628 100644 --- a/lib/bindings/http/emitter_binary_0_2.js +++ b/lib/bindings/http/emitter_binary_0_2.js @@ -3,7 +3,17 @@ var empty = require("is-empty"); const Constants = require("./constants.js"); -function HTTPBinary(configuration){ +const Headers02 = { + TYPE : "ce-type", + SPEC_VERSION : "ce-specversion", + SOURCE : "ce-source", + ID : "ce-id", + TIME : "ce-time", + SCHEMA_URL : "ce-schemaurl", + EXTENSIONS_PREFIX : "ce-" +}; + +function HTTPBinary02(configuration){ this.config = configuration; if(!this.config["headers"]){ @@ -15,7 +25,7 @@ function HTTPBinary(configuration){ Constants.MIME_CE_JSON + "; charset=" + Constants.CHARSET_DEFAULT; } -HTTPBinary.prototype.emit = function(cloudevent){ +HTTPBinary02.prototype.emit = function(cloudevent){ // Create new request object var _config = JSON.parse(JSON.stringify(this.config)); @@ -25,14 +35,14 @@ HTTPBinary.prototype.emit = function(cloudevent){ _headers[Constants.HEADER_CONTENT_TYPE] = cloudevent.getContenttype(); - _headers["ce-type"] = cloudevent.getType(); - _headers["ce-specversion"] = cloudevent.getSpecversion(); - _headers["ce-source"] = cloudevent.getSource(); - _headers["ce-id"] = cloudevent.getId(); + _headers[Headers02.TYPE] = cloudevent.getType(); + _headers[Headers02.SPEC_VERSION] = cloudevent.getSpecversion(); + _headers[Headers02.SOURCE] = cloudevent.getSource(); + _headers[Headers02.ID] = cloudevent.getId(); if(cloudevent.getTime()) { - _headers["ce-time"] = cloudevent.getTime(); + _headers[Headers02.TIME] = cloudevent.getTime(); } - _headers["ce-schemaurl"] = cloudevent.getSchemaurl(); + _headers[Headers02.SCHEMA_URL] = cloudevent.getSchemaurl(); // Set the cloudevent payload _config["data"] = cloudevent.format().data; @@ -41,7 +51,7 @@ HTTPBinary.prototype.emit = function(cloudevent){ var exts = cloudevent.getExtensions(); for(var ext in exts){ if({}.hasOwnProperty.call(exts, ext)){ - _headers["ce-" + ext] = exts[ext]; + _headers[Headers02.EXTENSIONS_PREFIX + ext] = exts[ext]; } } @@ -49,4 +59,7 @@ HTTPBinary.prototype.emit = function(cloudevent){ return axios.request(_config); }; -module.exports = HTTPBinary; +module.exports = { + HTTPBinary02, + Headers02 +}; diff --git a/lib/cloudevent.js b/lib/cloudevent.js index 40fd049..d86ca3a 100644 --- a/lib/cloudevent.js +++ b/lib/cloudevent.js @@ -4,7 +4,7 @@ var JSONFormatter01 = require("./formats/json/formatter.js"); var HTTPStructured01 = require("./bindings/http/emitter_structured_0_1.js"); var HTTPStructured02 = require("./bindings/http/emitter_structured_0_2.js"); var HTTPBinary01 = require("./bindings/http/emitter_binary_0_1.js"); -var HTTPBinary02 = require("./bindings/http/emitter_binary_0_2.js"); +var {HTTPBinary02} = require("./bindings/http/emitter_binary_0_2.js"); /* * Class created using the Builder Design Pattern.