@itentialopensource/adapter-utils 5.4.0 → 5.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +10 -0
- package/lib/restHandler.js +61 -0
- package/package.json +3 -3
- package/refs?service=git-upload-pack +0 -0
package/CHANGELOG.md
CHANGED
package/lib/restHandler.js
CHANGED
|
@@ -8,6 +8,7 @@ const querystring = require('querystring');
|
|
|
8
8
|
const jsonQuery = require('json-query');
|
|
9
9
|
const jsonxml = require('jsontoxml');
|
|
10
10
|
const xml2js = require('xml2js');
|
|
11
|
+
const pako = require('pako');
|
|
11
12
|
|
|
12
13
|
const globalSchema = JSON.parse(require('fs').readFileSync(require('path').join(__dirname, '/../schemas/globalSchema.json')));
|
|
13
14
|
|
|
@@ -52,6 +53,19 @@ function matchResponse(uriPath, method, type, mockresponses) {
|
|
|
52
53
|
return null;
|
|
53
54
|
}
|
|
54
55
|
|
|
56
|
+
/*
|
|
57
|
+
* INTERNAL FUNCTION: Parse XML
|
|
58
|
+
*/
|
|
59
|
+
function parseXML(xmlString, callback) {
|
|
60
|
+
const parser = new xml2js.Parser({ explicitArray: false, attrkey: '_attr' });
|
|
61
|
+
parser.parseString(xmlString, (error, result) => {
|
|
62
|
+
if (error) {
|
|
63
|
+
return callback(error);
|
|
64
|
+
}
|
|
65
|
+
return callback(null, result);
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
55
69
|
/*
|
|
56
70
|
* INTERNAL FUNCTION: recursively inspect body data if heirarchical
|
|
57
71
|
*/
|
|
@@ -477,6 +491,53 @@ function handleRestRequest(request, entityId, entitySchema, callProperties, filt
|
|
|
477
491
|
if (entitySchema && entitySchema.responseDatatype && entitySchema.responseDatatype.toUpperCase() === 'URLENCODE') {
|
|
478
492
|
// return the response
|
|
479
493
|
retResponse = querystring.parse(resObj.response.trim());
|
|
494
|
+
} else if (entitySchema && entitySchema.responseDatatype && (entitySchema.responseDatatype.toUpperCase() === 'GZIP2PLAIN'
|
|
495
|
+
|| entitySchema.responseDatatype.toUpperCase() === 'GZIP2JSON' || entitySchema.responseDatatype.toUpperCase() === 'GZIP2XML'
|
|
496
|
+
|| entitySchema.responseDatatype.toUpperCase() === 'GZIP2XML2JSON')) {
|
|
497
|
+
// If response is GZIP
|
|
498
|
+
const responseDatatype = entitySchema.responseDatatype.toUpperCase();
|
|
499
|
+
const responseBody = pako.inflate(Buffer.from(resObj.response, 'binary'), { to: 'string' });
|
|
500
|
+
// Handle conversion based on responseDatatype
|
|
501
|
+
if (responseDatatype === 'GZIP2PLAIN') {
|
|
502
|
+
retObject.response = responseBody;
|
|
503
|
+
} else if (responseDatatype === 'GZIP2JSON') {
|
|
504
|
+
// Parse responseBody as JSON
|
|
505
|
+
retObject.response = JSON.parse(responseBody);
|
|
506
|
+
} else if (responseDatatype === 'GZIP2XML' || responseDatatype === 'GZIP2XML2JSON') {
|
|
507
|
+
parseXML(responseBody, (error, xmlResult) => {
|
|
508
|
+
if (error) {
|
|
509
|
+
log.warn(`${origin}: Error parsing XML response: ${error}`);
|
|
510
|
+
return callback(retObject);
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
retObject.response = xmlResult;
|
|
514
|
+
|
|
515
|
+
if (responseDatatype === 'GZIP2XML2JSON') {
|
|
516
|
+
// Optionally convert XML to JSON
|
|
517
|
+
try {
|
|
518
|
+
const parser = new xml2js.Parser({ explicitArray: false, attrkey: '_attr' });
|
|
519
|
+
return parser.parseString(xmlResult, (err, result) => {
|
|
520
|
+
if (err) {
|
|
521
|
+
log.warn(`${origin}: Unable to parse xml to json ${err}`);
|
|
522
|
+
return callback(retObject);
|
|
523
|
+
}
|
|
524
|
+
// Logic to convert keys specified to array if object
|
|
525
|
+
if (xmlArrayKeys) {
|
|
526
|
+
setArrays(result, xmlArrayKeys);
|
|
527
|
+
}
|
|
528
|
+
retObject.response = result;
|
|
529
|
+
return callback(retObject);
|
|
530
|
+
});
|
|
531
|
+
} catch (ex) {
|
|
532
|
+
log.warn(`${origin}: Unable to get json from xml ${ex}`);
|
|
533
|
+
return callback(retObject);
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
log.debug(`${origin}: RESPONSE: ${retObject.response}`);
|
|
538
|
+
return callback(retObject);
|
|
539
|
+
});
|
|
540
|
+
}
|
|
480
541
|
} else {
|
|
481
542
|
// process the response - parse it
|
|
482
543
|
try {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@itentialopensource/adapter-utils",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.5.0",
|
|
4
4
|
"description": "Itential Adapter Utility Libraries",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"postinstall": "node utils/setup.js",
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"aws4": "^1.9.1",
|
|
32
32
|
"cookie": "^0.5.0",
|
|
33
33
|
"crypto-js": "^4.1.1",
|
|
34
|
-
"ejs": "^3.1.
|
|
34
|
+
"ejs": "^3.1.10",
|
|
35
35
|
"form-data": "^4.0.0",
|
|
36
36
|
"fs-extra": "^11.1.1",
|
|
37
37
|
"https-proxy-agent": "^7.0.0",
|
|
@@ -39,6 +39,7 @@
|
|
|
39
39
|
"jsontoxml": "^1.0.1",
|
|
40
40
|
"jsonwebtoken": "^9.0.1",
|
|
41
41
|
"mongodb": "^3.7.4",
|
|
42
|
+
"pako": "^2.1.0",
|
|
42
43
|
"querystring": "^0.2.0",
|
|
43
44
|
"readline-sync": "^1.4.10",
|
|
44
45
|
"socks-proxy-agent": "^8.0.1",
|
|
@@ -59,6 +60,5 @@
|
|
|
59
60
|
"testdouble": "^3.18.0",
|
|
60
61
|
"winston": "^3.9.0"
|
|
61
62
|
},
|
|
62
|
-
"peerDependencies": {},
|
|
63
63
|
"private": false
|
|
64
64
|
}
|
|
Binary file
|