move xpath query to insomnia src (#5388)

* remove xpath query package

* fix tests

* install types

* fix test
This commit is contained in:
Jack Kavanagh authored and GitHub committed 2022-11-10 13:13:02 +00:00
1 parent 492a48927b
commit cc3a7cbbda
16 files changed
+145 -6085

No files matched your search

+53 -1
View File
@@ -1,6 +1,5 @@
const { JSONPath } = require('jsonpath-plus');
const iconv = require('iconv-lite');
const { query: queryXPath } = require('insomnia-xpath');
function isFilterableField(field) {
return field !== 'raw' && field !== 'url';
@@ -280,3 +279,56 @@ function matchHeader(headers, name) {
return header.value;
}
/**
* Query an XML blob with XPath
*/
const queryXPath = (xml, query) => {
const DOMParser = require('xmldom').DOMParser
const dom = new DOMParser().parseFromString(xml);
let selectedValues = [];
if (query === undefined) {
throw new Error('Must pass an XPath query.');
}
try {
selectedValues = require('xpath').select(query, dom);
} catch (err) {
throw new Error(`Invalid XPath query: ${query}`);
}
const output = [];
// Functions return plain strings
if (typeof selectedValues === 'string') {
output.push({
outer: selectedValues,
inner: selectedValues,
});
} else {
for (const selectedValue of selectedValues || []) {
switch (selectedValue.constructor.name) {
case 'Attr':
output.push({
outer: selectedValue.toString().trim(),
inner: selectedValue.nodeValue,
});
break;
case 'Element':
output.push({
outer: selectedValue.toString().trim(),
inner: selectedValue.childNodes.toString(),
});
break;
case 'Text':
output.push({
outer: selectedValue.toString().trim(),
inner: selectedValue.toString().trim(),
});
break;
default:
break;
}
}
}
return output;
};
+29 -1
View File
@@ -10,7 +10,9 @@
"license": "MIT",
"dependencies": {
"iconv-lite": "^0.6.3",
"jsonpath-plus": "^6.0.1"
"jsonpath-plus": "^6.0.1",
"xmldom": "^0.5.0",
"xpath": "0.0.32"
}
},
"node_modules/iconv-lite": {
@@ -36,6 +38,22 @@
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz",
"integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg=="
},
"node_modules/xmldom": {
"version": "0.5.0",
"resolved": "https://registry.npmjs.org/xmldom/-/xmldom-0.5.0.tgz",
"integrity": "sha512-Foaj5FXVzgn7xFzsKeNIde9g6aFBxTPi37iwsno8QvApmtg7KYrr+OPyRHcJF7dud2a5nGRBXK3n0dL62Gf7PA==",
"engines": {
"node": ">=10.0.0"
}
},
"node_modules/xpath": {
"version": "0.0.32",
"resolved": "https://registry.npmjs.org/xpath/-/xpath-0.0.32.tgz",
"integrity": "sha512-rxMJhSIoiO8vXcWvSifKqhvV96GjiD5wYb8/QHdoRyQvraTpp4IEv944nhGausZZ3u7dhQXteZuZbaqfpB7uYw==",
"engines": {
"node": ">=0.6.0"
}
}
},
"dependencies": {
@@ -56,6 +74,16 @@
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz",
"integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg=="
},
"xmldom": {
"version": "0.5.0",
"resolved": "https://registry.npmjs.org/xmldom/-/xmldom-0.5.0.tgz",
"integrity": "sha512-Foaj5FXVzgn7xFzsKeNIde9g6aFBxTPi37iwsno8QvApmtg7KYrr+OPyRHcJF7dud2a5nGRBXK3n0dL62Gf7PA=="
},
"xpath": {
"version": "0.0.32",
"resolved": "https://registry.npmjs.org/xpath/-/xpath-0.0.32.tgz",
"integrity": "sha512-rxMJhSIoiO8vXcWvSifKqhvV96GjiD5wYb8/QHdoRyQvraTpp4IEv944nhGausZZ3u7dhQXteZuZbaqfpB7uYw=="
}
}
}
@@ -23,7 +23,8 @@
},
"dependencies": {
"iconv-lite": "^0.6.3",
"insomnia-xpath": "^3.6.1-beta.3",
"jsonpath-plus": "^6.0.1"
"jsonpath-plus": "^6.0.1",
"xmldom": "^0.5.0",
"xpath": "0.0.32"
}
}