From 588166e9f1b1c855f28c5eb1eb12323e8784030f Mon Sep 17 00:00:00 2001 From: aacerox Date: Thu, 19 Oct 2023 17:35:08 +0200 Subject: [PATCH] refactor: improve code --- lib/nrc-parser-manager.js | 3 +- lib/nrc-serializer-manager.js | 135 ++++++++++++++++------------------ 2 files changed, 65 insertions(+), 73 deletions(-) diff --git a/lib/nrc-parser-manager.js b/lib/nrc-parser-manager.js index 2713584..0819d18 100644 --- a/lib/nrc-parser-manager.js +++ b/lib/nrc-parser-manager.js @@ -171,8 +171,7 @@ const initializeParserManager =function() { match: function(response) { let result = false; const contentType = - response.headers['content-type'] && - response.headers['content-type'].replace(/ /g, ''); + response.headers['content-type']?.replace(/ /g, ''); if (!contentType) return result; diff --git a/lib/nrc-serializer-manager.js b/lib/nrc-serializer-manager.js index 03c2efe..ea666af 100644 --- a/lib/nrc-serializer-manager.js +++ b/lib/nrc-serializer-manager.js @@ -188,8 +188,7 @@ const initializeSerializerManager = function() { match: function(request) { let result = false; const contentType = - request.headers['Content-Type'] && - request.headers['Content-Type'].replace(/ /g, ''); + request.headers['content-type']?.replace(/ /g, ''); if (!contentType) return result; @@ -205,97 +204,91 @@ const initializeSerializerManager = function() { // add XML serializer serializerManager.add( - Object.assign( - { - name: 'XML', - options: {}, - contentTypes: [ - 'application/xml', - 'application/xmlcharset=utf-8', - 'text/xml', - 'text/xmlcharset=utf-8', - ], - xmlSerializer: new XmlSerializer.Builder({}), - serialize: function(data, nrcEventEmitter, serializedCallback) { - if (typeof data === 'object') { - data = XmlSerializer.buildObject(data); - } - - serializedCallback(data); - }, - }, - BaseSerializer, - ), + { + name: 'XML', + options: {}, + contentTypes: [ + 'application/xml', + 'application/xmlcharset=utf-8', + 'text/xml', + 'text/xmlcharset=utf-8', + ], + xmlSerializer: new XmlSerializer.Builder({}), + serialize: function(data, nrcEventEmitter, serializedCallback) { + if (typeof data === 'object') { + data = XmlSerializer.buildObject(data); + } + + serializedCallback(data); + }, + ...BaseSerializer, + }, ); // add JSON serializer serializerManager.add( - Object.assign( - { - name: 'JSON', - contentTypes: ['application/json', 'application/jsoncharset=utf-8'], - serialize: function(data, nrcEventEmitter, serializedCallback) { - if (typeof data === 'object') { - data = JSON.stringify(data); - } - serializedCallback(data); - }, - }, - BaseSerializer, - ), + { + name: 'JSON', + contentTypes: ['application/json', 'application/jsoncharset=utf-8'], + serialize: function(data, nrcEventEmitter, serializedCallback) { + if (typeof data === 'object') { + data = JSON.stringify(data); + } + serializedCallback(data); + }, + ...BaseSerializer, + }, ); // add form-encoded serializer serializerManager.add( - Object.assign( - { - name: 'FORM-ENCODED', - contentTypes: [ - 'application/x-www-form-urlencoded', - 'multipart/form-data', - 'text/plain', - ], - encode: function(obj, parent) { - const tokens = []; - - // iterate over all properties - for (const propertyName in obj) { - // if object has property (it's not an array iteration) - if (obj.hasOwnProperty(propertyName)) { - // if property has parent, add nested reference - const parsedProperty = parent ? + { + name: 'FORM-ENCODED', + contentTypes: [ + 'application/x-www-form-urlencoded', + 'multipart/form-data', + 'text/plain', + ], + encode: function(obj, parent) { + const tokens = []; + + // iterate over all properties + for (const propertyName in obj) { + // if object has property (it's not an array iteration) + if (obj.hasOwnProperty(propertyName)) { + // if property has parent, add nested reference + const parsedProperty = parent ? parent + '[' + propertyName + ']' : propertyName; - const propertyValue = obj[propertyName]; + const propertyValue = obj[propertyName]; - /** + /** * if property has value and is object (we must iterate again * , not final leaf) iterate over object property passing * current parsed property as parent else add encoded parsed * property and value to result array */ - tokens.push( + tokens.push( (propertyValue !== null && typeof propertyValue === 'object') ? serialize(propertyValue, parsedProperty) : encodeURIComponent(parsedProperty) + '=' + encodeURIComponent(propertyValue), - ); - } - } - return tokens.join('&'); - }, - serialize: function(data, nrcEventEmitter, serializedCallback) { - if (typeof data === 'object') { - data = this.encode(data); - } - - serializedCallback(data); - }, - }, - BaseSerializer, - ), + ); + } + } + return tokens.join('&'); + }, + serialize: function(data, nrcEventEmitter, serializedCallback) { + if (typeof data === 'object') { + data = this.encode(data); + } + + serializedCallback(data); + }, + ...BaseSerializer, + }, ); serializerManager.add({