Skip to content

Commit

Permalink
refactor: improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
aacerox committed Oct 19, 2023
1 parent 7eeaa92 commit 588166e
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 73 deletions.
3 changes: 1 addition & 2 deletions lib/nrc-parser-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
135 changes: 64 additions & 71 deletions lib/nrc-serializer-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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({
Expand Down

0 comments on commit 588166e

Please sign in to comment.