@openfn/language-http 6.0.0 → 6.2.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,
@@ -1289,6 +1304,83 @@
1289
1304
  ]
1290
1305
  },
1291
1306
  "valid": true
1307
+ },
1308
+ {
1309
+ "name": "cursor",
1310
+ "params": [
1311
+ "value",
1312
+ "options"
1313
+ ],
1314
+ "docs": {
1315
+ "description": "Sets a cursor property on state.\nSupports natural language dates like `now`, `today`, `yesterday`, `n hours ago`, `n days ago`, and `start`,\nwhich will be converted relative to the environment (ie, the Lightning or CLI locale). Custom timezones \nare not yet supported.\nSee the usage guide at {@link https://docs.openfn.org/documentation/jobs/job-writing-guide#using-cursors}",
1316
+ "tags": [
1317
+ {
1318
+ "title": "public",
1319
+ "description": null,
1320
+ "type": null
1321
+ },
1322
+ {
1323
+ "title": "example",
1324
+ "description": "cursor($.cursor, { defaultValue: 'today' })",
1325
+ "caption": "Use a cursor from state if present, or else use the default value"
1326
+ },
1327
+ {
1328
+ "title": "example",
1329
+ "description": "cursor(22)",
1330
+ "caption": "Use a pagination cursor"
1331
+ },
1332
+ {
1333
+ "title": "function",
1334
+ "description": null,
1335
+ "name": null
1336
+ },
1337
+ {
1338
+ "title": "param",
1339
+ "description": "the cursor value. Usually an ISO date, natural language date, or page number",
1340
+ "type": {
1341
+ "type": "NameExpression",
1342
+ "name": "any"
1343
+ },
1344
+ "name": "value"
1345
+ },
1346
+ {
1347
+ "title": "param",
1348
+ "description": "options to control the cursor.",
1349
+ "type": {
1350
+ "type": "NameExpression",
1351
+ "name": "object"
1352
+ },
1353
+ "name": "options"
1354
+ },
1355
+ {
1356
+ "title": "param",
1357
+ "description": "set the cursor key. Will persist through the whole run.",
1358
+ "type": {
1359
+ "type": "NameExpression",
1360
+ "name": "string"
1361
+ },
1362
+ "name": "options.key"
1363
+ },
1364
+ {
1365
+ "title": "param",
1366
+ "description": "the value to use if value is falsy",
1367
+ "type": {
1368
+ "type": "NameExpression",
1369
+ "name": "any"
1370
+ },
1371
+ "name": "options.defaultValue"
1372
+ },
1373
+ {
1374
+ "title": "returns",
1375
+ "description": null,
1376
+ "type": {
1377
+ "type": "NameExpression",
1378
+ "name": "Operation"
1379
+ }
1380
+ }
1381
+ ]
1382
+ },
1383
+ "valid": false
1292
1384
  }
1293
1385
  ]
1294
1386
  }
package/dist/index.cjs CHANGED
@@ -29,6 +29,7 @@ __export(src_exports, {
29
29
  arrayToString: () => import_language_common3.arrayToString,
30
30
  chunk: () => import_language_common3.chunk,
31
31
  combine: () => import_language_common3.combine,
32
+ cursor: () => import_language_common3.cursor,
32
33
  dataPath: () => import_language_common3.dataPath,
33
34
  dataValue: () => import_language_common3.dataValue,
34
35
  dateFns: () => import_language_common3.dateFns,
@@ -63,6 +64,7 @@ __export(Adaptor_exports, {
63
64
  arrayToString: () => import_language_common3.arrayToString,
64
65
  chunk: () => import_language_common3.chunk,
65
66
  combine: () => import_language_common3.combine,
67
+ cursor: () => import_language_common3.cursor,
66
68
  dataPath: () => import_language_common3.dataPath,
67
69
  dataValue: () => import_language_common3.dataValue,
68
70
  dateFns: () => import_language_common3.dateFns,
@@ -88,12 +90,12 @@ __export(Adaptor_exports, {
88
90
  toArray: () => import_language_common3.toArray
89
91
  });
90
92
  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
93
 
94
94
  // src/Utils.js
95
95
  var import_language_common = require("@openfn/language-common");
96
96
  var import_util = require("@openfn/language-common/util");
97
+ var cheerio = __toESM(require("cheerio"), 1);
98
+ var import_cheerio_tableparser = __toESM(require("cheerio-tableparser"), 1);
97
99
  function addBasicAuth(configuration = {}, headers) {
98
100
  const { username, password } = configuration;
99
101
  if (username && password) {
@@ -154,6 +156,24 @@ function request(method, path, params, callback = (s) => s) {
154
156
  });
155
157
  };
156
158
  }
159
+ function xmlParser(body, script, callback = (s) => s) {
160
+ return (state) => {
161
+ const [resolvedBody] = (0, import_util.expandReferences)(state, body);
162
+ const $ = cheerio.load(resolvedBody);
163
+ (0, import_cheerio_tableparser.default)($);
164
+ if (script) {
165
+ const result = script($);
166
+ try {
167
+ const r = JSON.parse(result);
168
+ return callback((0, import_language_common.composeNextState)(state, r));
169
+ } catch (e) {
170
+ return callback((0, import_language_common.composeNextState)(state, { body: result }));
171
+ }
172
+ } else {
173
+ return callback((0, import_language_common.composeNextState)(state, { body: resolvedBody }));
174
+ }
175
+ };
176
+ }
157
177
 
158
178
  // src/Adaptor.js
159
179
  var import_language_common3 = require("@openfn/language-common");
@@ -184,23 +204,8 @@ function patch(path, params, callback) {
184
204
  function del(path, params, callback) {
185
205
  return request("DELETE", path, params, callback);
186
206
  }
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
- };
207
+ function parseXML(body, script, callback) {
208
+ return xmlParser(body, script, callback);
204
209
  }
205
210
 
206
211
  // src/index.js
@@ -211,6 +216,7 @@ var src_default = Adaptor_exports;
211
216
  arrayToString,
212
217
  chunk,
213
218
  combine,
219
+ cursor,
214
220
  dataPath,
215
221
  dataValue,
216
222
  dateFns,
package/dist/index.js CHANGED
@@ -11,6 +11,7 @@ __export(Adaptor_exports, {
11
11
  arrayToString: () => arrayToString,
12
12
  chunk: () => chunk,
13
13
  combine: () => combine,
14
+ cursor: () => cursor,
14
15
  dataPath: () => dataPath,
15
16
  dataValue: () => dataValue,
16
17
  dateFns: () => dateFns,
@@ -35,13 +36,7 @@ __export(Adaptor_exports, {
35
36
  splitKeys: () => splitKeys,
36
37
  toArray: () => toArray
37
38
  });
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";
39
+ import { execute as commonExecute } from "@openfn/language-common";
45
40
 
46
41
  // src/Utils.js
47
42
  import { composeNextState } from "@openfn/language-common";
@@ -51,6 +46,8 @@ import {
51
46
  logResponse,
52
47
  makeBasicAuthHeader
53
48
  } from "@openfn/language-common/util";
49
+ import * as cheerio from "cheerio";
50
+ import cheerioTableparser from "cheerio-tableparser";
54
51
  function addBasicAuth(configuration = {}, headers) {
55
52
  const { username, password } = configuration;
56
53
  if (username && password) {
@@ -111,12 +108,31 @@ function request(method, path, params, callback = (s) => s) {
111
108
  });
112
109
  };
113
110
  }
111
+ function xmlParser(body, script, callback = (s) => s) {
112
+ return (state) => {
113
+ const [resolvedBody] = expandReferences(state, body);
114
+ const $ = cheerio.load(resolvedBody);
115
+ cheerioTableparser($);
116
+ if (script) {
117
+ const result = script($);
118
+ try {
119
+ const r = JSON.parse(result);
120
+ return callback(composeNextState(state, r));
121
+ } catch (e) {
122
+ return callback(composeNextState(state, { body: result }));
123
+ }
124
+ } else {
125
+ return callback(composeNextState(state, { body: resolvedBody }));
126
+ }
127
+ };
128
+ }
114
129
 
115
130
  // src/Adaptor.js
116
131
  import {
117
132
  alterState,
118
133
  arrayToString,
119
134
  combine,
135
+ cursor,
120
136
  dataPath,
121
137
  dataValue,
122
138
  dateFns,
@@ -161,23 +177,8 @@ function patch(path, params, callback) {
161
177
  function del(path, params, callback) {
162
178
  return request("DELETE", path, params, callback);
163
179
  }
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
- };
180
+ function parseXML(body, script, callback) {
181
+ return xmlParser(body, script, callback);
181
182
  }
182
183
 
183
184
  // src/index.js
@@ -187,6 +188,7 @@ export {
187
188
  arrayToString,
188
189
  chunk,
189
190
  combine,
191
+ cursor,
190
192
  dataPath,
191
193
  dataValue,
192
194
  dateFns,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openfn/language-http",
3
- "version": "6.0.0",
3
+ "version": "6.2.0",
4
4
  "description": "An HTTP request language package for use with Open Function",
5
5
  "homepage": "https://docs.openfn.org",
6
6
  "repository": {
@@ -36,8 +36,7 @@
36
36
  "esno": "^0.16.3",
37
37
  "mocha": "9.2.2",
38
38
  "mocha-suppress-logs": "^0.4.1",
39
- "rimraf": "^3.0.2",
40
- "@openfn/buildtools": "^1.0.2"
39
+ "rimraf": "^3.0.2"
41
40
  },
42
41
  "main": "dist/index.cjs",
43
42
  "scripts": {
@@ -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
  */
@@ -167,4 +179,4 @@ export type RequestOptions = {
167
179
  */
168
180
  tls: object;
169
181
  };
170
- export { alterState, arrayToString, combine, dataPath, dataValue, dateFns, each, field, fields, fn, chunk, humanProper, lastReferenceValue, merge, scrubEmojis, sourceValue, splitKeys, toArray, parseCsv } from "@openfn/language-common";
182
+ export { alterState, arrayToString, combine, cursor, dataPath, dataValue, dateFns, each, field, fields, fn, chunk, humanProper, lastReferenceValue, merge, scrubEmojis, sourceValue, splitKeys, toArray, parseCsv } from "@openfn/language-common";
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;