Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add xml2json func, expandReference parseXML(body) #444

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/http/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"form-data": "^3.0.0",
"lodash": "^4.17.19",
"request": "^2.88.2",
"tough-cookie": "^4.0.0"
"tough-cookie": "^4.0.0",
"xml2js": "0.5.0"
},
"devDependencies": {
"@openfn/buildtools": "workspace:^1.0.2",
Expand Down
31 changes: 29 additions & 2 deletions packages/http/src/Adaptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import nodeRequest from 'request';
import cheerio from 'cheerio';
import cheerioTableparser from 'cheerio-tableparser';
import tough from 'tough-cookie';
import { parseStringPromise } from 'xml2js';

const { axios } = http;

Expand Down Expand Up @@ -310,7 +311,8 @@ export function del(path, params, callback) {
*/
export function parseXML(body, script) {
return state => {
const $ = cheerio.load(body);
const resolvedBody = expandReferences(body)(state);
const $ = cheerio.load(resolvedBody);
cheerioTableparser($);

if (script) {
Expand All @@ -322,11 +324,36 @@ export function parseXML(body, script) {
return composeNextState(state, { body: result });
}
} else {
return composeNextState(state, { body: body });
return composeNextState(state, { body: resolvedBody });
}
};
}

/**
* The xml2json function takes an XML string and converts it to a JSON object.
*
* @param {string} xml - Pass in the xml string that we want to parse
* @param {object} options - Specify the options for the parseStringPromise function
* @param {function} callback - (Optional) Callback function
* @returns {Operation}
*/

export function xml2json(xml, options, callback) {
return state => {
const resolvedXml = expandReferences(xml)(state);
const resolvedOptions = expandReferences(options)(state);

return parseStringPromise(resolvedXml, resolvedOptions).then(result => {
console.log('Done converting xml to json ✅');

const nextState = { ...composeNextState(state, result), result };

if (callback) return callback(nextState);
return nextState;
});
};
}

/**
* Make a request using the 'request' node module. This module is deprecated.
* @example
Expand Down
Loading