@openfn/language-http 6.0.0 → 6.1.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/ast.json CHANGED
@@ -392,7 +392,8 @@
392
392
  "name": "parseXML",
393
393
  "params": [
394
394
  "body",
395
- "script"
395
+ "script",
396
+ "callback"
396
397
  ],
397
398
  "docs": {
398
399
  "description": "Parse XML with the Cheerio parser",
@@ -404,7 +405,12 @@
404
405
  },
405
406
  {
406
407
  "title": "example",
407
- "description": "parseXML(body, function($){\n return $(\"table[class=your_table]\").parsetable(true, true, true);\n })"
408
+ "description": "parseXML(\n (state) => state.response,\n ($) => {\n return $(\"table[class=your_table]\").parsetable(true, true, true);\n }\n);"
409
+ },
410
+ {
411
+ "title": "example",
412
+ "description": "parseXML(\n (state) => state.response,\n ($) => {\n return $(\"table[class=your_table]\").parsetable(true, true, true);\n },\n (next) => ({ ...next, results: next.data.body })\n);",
413
+ "caption": "Using parseXML with a callback"
408
414
  },
409
415
  {
410
416
  "title": "function",
@@ -429,6 +435,15 @@
429
435
  },
430
436
  "name": "script"
431
437
  },
438
+ {
439
+ "title": "param",
440
+ "description": "(Optional) Callback function",
441
+ "type": {
442
+ "type": "NameExpression",
443
+ "name": "function"
444
+ },
445
+ "name": "callback"
446
+ },
432
447
  {
433
448
  "title": "returns",
434
449
  "description": null,
package/dist/index.cjs CHANGED
@@ -88,12 +88,12 @@ __export(Adaptor_exports, {
88
88
  toArray: () => import_language_common3.toArray
89
89
  });
90
90
  var import_language_common2 = require("@openfn/language-common");
91
- var import_cheerio = __toESM(require("cheerio"), 1);
92
- var import_cheerio_tableparser = __toESM(require("cheerio-tableparser"), 1);
93
91
 
94
92
  // src/Utils.js
95
93
  var import_language_common = require("@openfn/language-common");
96
94
  var import_util = require("@openfn/language-common/util");
95
+ var cheerio = __toESM(require("cheerio"), 1);
96
+ var import_cheerio_tableparser = __toESM(require("cheerio-tableparser"), 1);
97
97
  function addBasicAuth(configuration = {}, headers) {
98
98
  const { username, password } = configuration;
99
99
  if (username && password) {
@@ -154,6 +154,24 @@ function request(method, path, params, callback = (s) => s) {
154
154
  });
155
155
  };
156
156
  }
157
+ function xmlParser(body, script, callback = (s) => s) {
158
+ return (state) => {
159
+ const [resolvedBody] = (0, import_util.expandReferences)(state, body);
160
+ const $ = cheerio.load(resolvedBody);
161
+ (0, import_cheerio_tableparser.default)($);
162
+ if (script) {
163
+ const result = script($);
164
+ try {
165
+ const r = JSON.parse(result);
166
+ return callback((0, import_language_common.composeNextState)(state, r));
167
+ } catch (e) {
168
+ return callback((0, import_language_common.composeNextState)(state, { body: result }));
169
+ }
170
+ } else {
171
+ return callback((0, import_language_common.composeNextState)(state, { body: resolvedBody }));
172
+ }
173
+ };
174
+ }
157
175
 
158
176
  // src/Adaptor.js
159
177
  var import_language_common3 = require("@openfn/language-common");
@@ -184,23 +202,8 @@ function patch(path, params, callback) {
184
202
  function del(path, params, callback) {
185
203
  return request("DELETE", path, params, callback);
186
204
  }
187
- function parseXML(body, script) {
188
- return (state) => {
189
- const resolvedBody = (0, import_language_common2.expandReferences)(body)(state);
190
- const $ = import_cheerio.default.load(resolvedBody);
191
- (0, import_cheerio_tableparser.default)($);
192
- if (script) {
193
- const result = script($);
194
- try {
195
- const r = JSON.parse(result);
196
- return (0, import_language_common2.composeNextState)(state, r);
197
- } catch (e) {
198
- return (0, import_language_common2.composeNextState)(state, { body: result });
199
- }
200
- } else {
201
- return (0, import_language_common2.composeNextState)(state, { body: resolvedBody });
202
- }
203
- };
205
+ function parseXML(body, script, callback) {
206
+ return xmlParser(body, script, callback);
204
207
  }
205
208
 
206
209
  // src/index.js
package/dist/index.js CHANGED
@@ -35,13 +35,7 @@ __export(Adaptor_exports, {
35
35
  splitKeys: () => splitKeys,
36
36
  toArray: () => toArray
37
37
  });
38
- import {
39
- execute as commonExecute,
40
- composeNextState as composeNextState2,
41
- expandReferences as expandReferences2
42
- } from "@openfn/language-common";
43
- import cheerio from "cheerio";
44
- import cheerioTableparser from "cheerio-tableparser";
38
+ import { execute as commonExecute } from "@openfn/language-common";
45
39
 
46
40
  // src/Utils.js
47
41
  import { composeNextState } from "@openfn/language-common";
@@ -51,6 +45,8 @@ import {
51
45
  logResponse,
52
46
  makeBasicAuthHeader
53
47
  } from "@openfn/language-common/util";
48
+ import * as cheerio from "cheerio";
49
+ import cheerioTableparser from "cheerio-tableparser";
54
50
  function addBasicAuth(configuration = {}, headers) {
55
51
  const { username, password } = configuration;
56
52
  if (username && password) {
@@ -111,6 +107,24 @@ function request(method, path, params, callback = (s) => s) {
111
107
  });
112
108
  };
113
109
  }
110
+ function xmlParser(body, script, callback = (s) => s) {
111
+ return (state) => {
112
+ const [resolvedBody] = expandReferences(state, body);
113
+ const $ = cheerio.load(resolvedBody);
114
+ cheerioTableparser($);
115
+ if (script) {
116
+ const result = script($);
117
+ try {
118
+ const r = JSON.parse(result);
119
+ return callback(composeNextState(state, r));
120
+ } catch (e) {
121
+ return callback(composeNextState(state, { body: result }));
122
+ }
123
+ } else {
124
+ return callback(composeNextState(state, { body: resolvedBody }));
125
+ }
126
+ };
127
+ }
114
128
 
115
129
  // src/Adaptor.js
116
130
  import {
@@ -161,23 +175,8 @@ function patch(path, params, callback) {
161
175
  function del(path, params, callback) {
162
176
  return request("DELETE", path, params, callback);
163
177
  }
164
- function parseXML(body, script) {
165
- return (state) => {
166
- const resolvedBody = expandReferences2(body)(state);
167
- const $ = cheerio.load(resolvedBody);
168
- cheerioTableparser($);
169
- if (script) {
170
- const result = script($);
171
- try {
172
- const r = JSON.parse(result);
173
- return composeNextState2(state, r);
174
- } catch (e) {
175
- return composeNextState2(state, { body: result });
176
- }
177
- } else {
178
- return composeNextState2(state, { body: resolvedBody });
179
- }
180
- };
178
+ function parseXML(body, script, callback) {
179
+ return xmlParser(body, script, callback);
181
180
  }
182
181
 
183
182
  // src/index.js
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openfn/language-http",
3
- "version": "6.0.0",
3
+ "version": "6.1.0",
4
4
  "description": "An HTTP request language package for use with Open Function",
5
5
  "homepage": "https://docs.openfn.org",
6
6
  "repository": {
@@ -121,15 +121,27 @@ export function del(path: string, params: RequestOptions, callback: Function): O
121
121
  * Parse XML with the Cheerio parser
122
122
  * @public
123
123
  * @example
124
- * parseXML(body, function($){
125
- * return $("table[class=your_table]").parsetable(true, true, true);
126
- * })
124
+ * parseXML(
125
+ * (state) => state.response,
126
+ * ($) => {
127
+ * return $("table[class=your_table]").parsetable(true, true, true);
128
+ * }
129
+ * );
130
+ * @example <caption>Using parseXML with a callback</caption>
131
+ * parseXML(
132
+ * (state) => state.response,
133
+ * ($) => {
134
+ * return $("table[class=your_table]").parsetable(true, true, true);
135
+ * },
136
+ * (next) => ({ ...next, results: next.data.body })
137
+ * );
127
138
  * @function
128
139
  * @param {String} body - data string to be parsed
129
140
  * @param {function} script - script for extracting data
141
+ * @param {function} callback - (Optional) Callback function
130
142
  * @returns {Operation}
131
143
  */
132
- export function parseXML(body: string, script: Function): Operation;
144
+ export function parseXML(body: string, script: Function, callback: Function): Operation;
133
145
  /**
134
146
  * Options provided to the HTTP request
135
147
  */
package/types/Utils.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export function addBasicAuth(configuration: {}, headers: any): void;
2
2
  export function request(method: any, path: any, params: any, callback?: (s: any) => any): (state: any) => any;
3
+ export function xmlParser(body: any, script: any, callback?: (s: any) => any): (state: any) => any;