@openfn/language-common 1.10.0 → 1.10.2

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
@@ -1065,6 +1065,73 @@
1065
1065
  },
1066
1066
  "valid": true
1067
1067
  },
1068
+ {
1069
+ "name": "splitKeys",
1070
+ "params": [
1071
+ "obj",
1072
+ "keys"
1073
+ ],
1074
+ "docs": {
1075
+ "description": "Splits an object into two objects based on a list of keys.\nThe first object contains the keys that are not in the list,\nand the second contains the keys that are.",
1076
+ "tags": [
1077
+ {
1078
+ "title": "public",
1079
+ "description": null,
1080
+ "type": null
1081
+ },
1082
+ {
1083
+ "title": "function",
1084
+ "description": null,
1085
+ "name": null
1086
+ },
1087
+ {
1088
+ "title": "param",
1089
+ "description": "The object to split.",
1090
+ "type": {
1091
+ "type": "NameExpression",
1092
+ "name": "Object"
1093
+ },
1094
+ "name": "obj"
1095
+ },
1096
+ {
1097
+ "title": "param",
1098
+ "description": "List of keys to split on.",
1099
+ "type": {
1100
+ "type": "TypeApplication",
1101
+ "expression": {
1102
+ "type": "NameExpression",
1103
+ "name": "Array"
1104
+ },
1105
+ "applications": [
1106
+ {
1107
+ "type": "NameExpression",
1108
+ "name": "string"
1109
+ }
1110
+ ]
1111
+ },
1112
+ "name": "keys"
1113
+ },
1114
+ {
1115
+ "title": "returns",
1116
+ "description": "Tuple of objects, first object contains keys not in list, second contains keys that are.",
1117
+ "type": {
1118
+ "type": "TypeApplication",
1119
+ "expression": {
1120
+ "type": "NameExpression",
1121
+ "name": "Array"
1122
+ },
1123
+ "applications": [
1124
+ {
1125
+ "type": "NameExpression",
1126
+ "name": "Object"
1127
+ }
1128
+ ]
1129
+ }
1130
+ }
1131
+ ]
1132
+ },
1133
+ "valid": true
1134
+ },
1068
1135
  {
1069
1136
  "name": "scrubEmojis",
1070
1137
  "params": [
@@ -1171,6 +1238,80 @@
1171
1238
  },
1172
1239
  "valid": true
1173
1240
  },
1241
+ {
1242
+ "name": "parseCsv",
1243
+ "params": [
1244
+ "csvData",
1245
+ "parsingOptions",
1246
+ "callback"
1247
+ ],
1248
+ "docs": {
1249
+ "description": "Takes a CSV file string or stream and parsing options as input, and returns a promise that\nresolves to the parsed CSV data as an array of objects.\nOptions for `parsingOptions` include:\n- `delimiter` {string/Buffer/[string/Buffer]} - Defines the character(s) used to delineate the fields inside a record. Default: `','`\n- `quote` {string/Buffer/[string/Buffer]} - Defines the characters used to surround a field. Default: `'\"'`\n- `escape` {Buffer/string/null/boolean} - Set the escape character as one character/byte only. Default: `\"`\n- `columns` {boolean / array / function} - Generates record in the form of object literals. Default: `true`\n- `bom` {boolean} - Strips the {@link https://en.wikipedia.org/wiki/Byte_order_mark byte order mark (BOM)} from the input string or buffer. Default: `true`\n- `trim` {boolean} - Ignore whitespace characters immediately around the `delimiter`. Default: `true`\n- `ltrim` {boolean} - Ignore whitespace characters from the left side of a CSV field. Default: `true`\n- `rtrim` {boolean} - Ignore whitespace characters from the right side of a CSV field. Default: `true`\n- `chunkSize` {number} - The size of each chunk of CSV data. Default: `Infinity`\n- `skip_empty_lines` {boolean} - Ignore empty lines in the CSV file. Default: `true`",
1250
+ "tags": [
1251
+ {
1252
+ "title": "public",
1253
+ "description": null,
1254
+ "type": null
1255
+ },
1256
+ {
1257
+ "title": "function",
1258
+ "description": null,
1259
+ "name": null
1260
+ },
1261
+ {
1262
+ "title": "param",
1263
+ "description": "A CSV string or a readable stream",
1264
+ "type": {
1265
+ "type": "UnionType",
1266
+ "elements": [
1267
+ {
1268
+ "type": "NameExpression",
1269
+ "name": "String"
1270
+ },
1271
+ {
1272
+ "type": "NameExpression",
1273
+ "name": "Stream"
1274
+ }
1275
+ ]
1276
+ },
1277
+ "name": "csvData"
1278
+ },
1279
+ {
1280
+ "title": "param",
1281
+ "description": "Optional. Parsing options for converting CSV to JSON.",
1282
+ "type": {
1283
+ "type": "OptionalType",
1284
+ "expression": {
1285
+ "type": "NameExpression",
1286
+ "name": "Object"
1287
+ }
1288
+ },
1289
+ "name": "parsingOptions"
1290
+ },
1291
+ {
1292
+ "title": "param",
1293
+ "description": "(Optional) callback function. If used it will be called state and an array of rows.",
1294
+ "type": {
1295
+ "type": "OptionalType",
1296
+ "expression": {
1297
+ "type": "NameExpression",
1298
+ "name": "function"
1299
+ }
1300
+ },
1301
+ "name": "callback"
1302
+ },
1303
+ {
1304
+ "title": "returns",
1305
+ "description": "The function returns a Promise that resolves to the result of parsing a CSV `stringOrStream`.",
1306
+ "type": {
1307
+ "type": "NameExpression",
1308
+ "name": "Operation"
1309
+ }
1310
+ }
1311
+ ]
1312
+ },
1313
+ "valid": true
1314
+ },
1174
1315
  {
1175
1316
  "name": "map",
1176
1317
  "params": [
package/dist/index.cjs CHANGED
@@ -38,7 +38,7 @@ __export(src_exports, {
38
38
  default: () => src_default,
39
39
  each: () => each2,
40
40
  execute: () => execute,
41
- expandReferences: () => expandReferences,
41
+ expandReferences: () => expandReferences2,
42
42
  field: () => field,
43
43
  fields: () => fields,
44
44
  fn: () => fn,
@@ -75,7 +75,7 @@ __export(Adaptor_exports, {
75
75
  dateFns: () => dateFns_exports,
76
76
  each: () => each2,
77
77
  execute: () => execute,
78
- expandReferences: () => expandReferences,
78
+ expandReferences: () => expandReferences2,
79
79
  field: () => field,
80
80
  fields: () => fields,
81
81
  fn: () => fn,
@@ -100,6 +100,25 @@ var import_fromPairs = __toESM(require("lodash/fp/fromPairs.js"), 1);
100
100
  var import_jsonpath_plus = require("jsonpath-plus");
101
101
  var import_csv_parse = require("csv-parse");
102
102
 
103
+ // src/util.js
104
+ function expandReferences(state, ...args) {
105
+ return args.map((value) => expandReference(state, value));
106
+ }
107
+ function expandReference(state, value) {
108
+ if (Array.isArray(value)) {
109
+ return value.map((v) => expandReference(state, v));
110
+ }
111
+ if (typeof value == "object" && !!value) {
112
+ return Object.keys(value).reduce((acc, key) => {
113
+ return { ...acc, [key]: expandReference(state, value[key]) };
114
+ }, {});
115
+ }
116
+ if (typeof value == "function") {
117
+ return expandReference(state, value(state));
118
+ }
119
+ return value;
120
+ }
121
+
103
122
  // src/beta.js
104
123
  var beta_exports = {};
105
124
  __export(beta_exports, {
@@ -165,7 +184,7 @@ function expandRequestReferences(params) {
165
184
  return true;
166
185
  };
167
186
  return (state) => {
168
- const expandedParams = expandReferences(toExpand, skipFormData)(state);
187
+ const expandedParams = expandReferences2(toExpand, skipFormData)(state);
169
188
  const safelyExpandedParams = { ...toKeep, ...expandedParams };
170
189
  return safelyExpandedParams;
171
190
  };
@@ -333,20 +352,20 @@ function join(targetPath, sourcePath, targetKey) {
333
352
  });
334
353
  };
335
354
  }
336
- function expandReferences(value, skipFilter) {
355
+ function expandReferences2(value, skipFilter) {
337
356
  return (state) => {
338
357
  if (skipFilter && skipFilter(value))
339
358
  return value;
340
359
  if (Array.isArray(value)) {
341
- return value.map((v) => expandReferences(v)(state));
360
+ return value.map((v) => expandReferences2(v)(state));
342
361
  }
343
362
  if (typeof value == "object" && !!value) {
344
363
  return Object.keys(value).reduce((acc, key) => {
345
- return { ...acc, [key]: expandReferences(value[key])(state) };
364
+ return { ...acc, [key]: expandReferences2(value[key])(state) };
346
365
  }, {});
347
366
  }
348
367
  if (typeof value == "function") {
349
- return expandReferences(value(state))(state);
368
+ return expandReferences2(value(state))(state);
350
369
  }
351
370
  return value;
352
371
  };
@@ -360,7 +379,7 @@ function fields(...fields2) {
360
379
  function merge(dataSource, fields2) {
361
380
  return (state) => {
362
381
  const initialData = source(dataSource)(state);
363
- const additionalData = expandReferences(fields2)(state);
382
+ const additionalData = expandReferences2(fields2)(state);
364
383
  return initialData.reduce((acc, dataItem) => {
365
384
  return [...acc, { ...dataItem, ...additionalData }];
366
385
  }, []);
@@ -438,41 +457,38 @@ function parseCsv(csvData, parsingOptions = {}, callback) {
438
457
  chunkSize: Infinity,
439
458
  skip_empty_lines: true
440
459
  };
441
- const filteredOptions = Object.fromEntries(
442
- Object.entries(parsingOptions).filter(([key]) => key in defaultOptions)
443
- );
444
- const options2 = { ...defaultOptions, ...filteredOptions };
445
- if (options2.chunkSize < 1) {
446
- throw new Error("chunkSize must be at least 1");
447
- }
448
- return (state) => {
449
- return new Promise((resolve, reject) => {
450
- let buffer = [];
451
- const parser = typeof csvData === "string" ? (0, import_csv_parse.parse)(csvData, options2) : csvData.pipe((0, import_csv_parse.parse)(options2));
452
- const flushBuffer = (currentState) => {
453
- const nextState = callback ? callback(currentState, buffer) : composeNextState(currentState, buffer);
454
- buffer = [];
455
- return [nextState, buffer];
456
- };
457
- parser.on("readable", function() {
458
- let chunk2;
459
- while ((chunk2 = parser.read()) !== null) {
460
- buffer.push(chunk2);
461
- if (buffer.length >= options2.chunkSize) {
462
- const [nextState, nextBuffer] = flushBuffer(state);
463
- state = nextState;
464
- buffer = nextBuffer;
465
- }
466
- }
467
- });
468
- parser.on("error", function(error) {
469
- reject(error);
470
- });
471
- parser.on("end", function() {
472
- const [finalState] = flushBuffer(state);
473
- resolve(finalState);
474
- });
475
- });
460
+ return async (state) => {
461
+ const [resolvedCsvData, resolvedParsingOptions] = expandReferences(
462
+ state,
463
+ csvData,
464
+ parsingOptions
465
+ );
466
+ const filteredOptions = Object.fromEntries(
467
+ Object.entries(resolvedParsingOptions).filter(
468
+ ([key]) => key in defaultOptions
469
+ )
470
+ );
471
+ const options2 = { ...defaultOptions, ...filteredOptions };
472
+ if (options2.chunkSize < 1) {
473
+ throw new Error("chunkSize must be at least 1");
474
+ }
475
+ let buffer = [];
476
+ const parser = typeof resolvedCsvData === "string" ? (0, import_csv_parse.parse)(resolvedCsvData, options2) : resolvedCsvData.pipe((0, import_csv_parse.parse)(options2));
477
+ const flushBuffer = async (currentState) => {
478
+ const nextState = callback ? await callback(currentState, buffer) : composeNextState(currentState, buffer);
479
+ buffer = [];
480
+ return [nextState, buffer];
481
+ };
482
+ for await (const record of parser) {
483
+ buffer.push(record);
484
+ if (buffer.length === options2.chunkSize) {
485
+ const [nextState, nextBuffer] = await flushBuffer(state);
486
+ state = nextState;
487
+ buffer = nextBuffer;
488
+ }
489
+ }
490
+ const [finalState] = await flushBuffer(state);
491
+ return finalState;
476
492
  };
477
493
  }
478
494
 
package/dist/index.js CHANGED
@@ -19,7 +19,7 @@ __export(Adaptor_exports, {
19
19
  dateFns: () => dateFns_exports,
20
20
  each: () => each2,
21
21
  execute: () => execute,
22
- expandReferences: () => expandReferences,
22
+ expandReferences: () => expandReferences2,
23
23
  field: () => field,
24
24
  fields: () => fields,
25
25
  fn: () => fn,
@@ -44,6 +44,25 @@ import fromPairs from "lodash/fp/fromPairs.js";
44
44
  import { JSONPath } from "jsonpath-plus";
45
45
  import { parse as parse2 } from "csv-parse";
46
46
 
47
+ // src/util.js
48
+ function expandReferences(state, ...args) {
49
+ return args.map((value) => expandReference(state, value));
50
+ }
51
+ function expandReference(state, value) {
52
+ if (Array.isArray(value)) {
53
+ return value.map((v) => expandReference(state, v));
54
+ }
55
+ if (typeof value == "object" && !!value) {
56
+ return Object.keys(value).reduce((acc, key) => {
57
+ return { ...acc, [key]: expandReference(state, value[key]) };
58
+ }, {});
59
+ }
60
+ if (typeof value == "function") {
61
+ return expandReference(state, value(state));
62
+ }
63
+ return value;
64
+ }
65
+
47
66
  // src/beta.js
48
67
  var beta_exports = {};
49
68
  __export(beta_exports, {
@@ -109,7 +128,7 @@ function expandRequestReferences(params) {
109
128
  return true;
110
129
  };
111
130
  return (state) => {
112
- const expandedParams = expandReferences(toExpand, skipFormData)(state);
131
+ const expandedParams = expandReferences2(toExpand, skipFormData)(state);
113
132
  const safelyExpandedParams = { ...toKeep, ...expandedParams };
114
133
  return safelyExpandedParams;
115
134
  };
@@ -277,20 +296,20 @@ function join(targetPath, sourcePath, targetKey) {
277
296
  });
278
297
  };
279
298
  }
280
- function expandReferences(value, skipFilter) {
299
+ function expandReferences2(value, skipFilter) {
281
300
  return (state) => {
282
301
  if (skipFilter && skipFilter(value))
283
302
  return value;
284
303
  if (Array.isArray(value)) {
285
- return value.map((v) => expandReferences(v)(state));
304
+ return value.map((v) => expandReferences2(v)(state));
286
305
  }
287
306
  if (typeof value == "object" && !!value) {
288
307
  return Object.keys(value).reduce((acc, key) => {
289
- return { ...acc, [key]: expandReferences(value[key])(state) };
308
+ return { ...acc, [key]: expandReferences2(value[key])(state) };
290
309
  }, {});
291
310
  }
292
311
  if (typeof value == "function") {
293
- return expandReferences(value(state))(state);
312
+ return expandReferences2(value(state))(state);
294
313
  }
295
314
  return value;
296
315
  };
@@ -304,7 +323,7 @@ function fields(...fields2) {
304
323
  function merge(dataSource, fields2) {
305
324
  return (state) => {
306
325
  const initialData = source(dataSource)(state);
307
- const additionalData = expandReferences(fields2)(state);
326
+ const additionalData = expandReferences2(fields2)(state);
308
327
  return initialData.reduce((acc, dataItem) => {
309
328
  return [...acc, { ...dataItem, ...additionalData }];
310
329
  }, []);
@@ -382,41 +401,38 @@ function parseCsv(csvData, parsingOptions = {}, callback) {
382
401
  chunkSize: Infinity,
383
402
  skip_empty_lines: true
384
403
  };
385
- const filteredOptions = Object.fromEntries(
386
- Object.entries(parsingOptions).filter(([key]) => key in defaultOptions)
387
- );
388
- const options2 = { ...defaultOptions, ...filteredOptions };
389
- if (options2.chunkSize < 1) {
390
- throw new Error("chunkSize must be at least 1");
391
- }
392
- return (state) => {
393
- return new Promise((resolve, reject) => {
394
- let buffer = [];
395
- const parser = typeof csvData === "string" ? parse2(csvData, options2) : csvData.pipe(parse2(options2));
396
- const flushBuffer = (currentState) => {
397
- const nextState = callback ? callback(currentState, buffer) : composeNextState(currentState, buffer);
398
- buffer = [];
399
- return [nextState, buffer];
400
- };
401
- parser.on("readable", function() {
402
- let chunk2;
403
- while ((chunk2 = parser.read()) !== null) {
404
- buffer.push(chunk2);
405
- if (buffer.length >= options2.chunkSize) {
406
- const [nextState, nextBuffer] = flushBuffer(state);
407
- state = nextState;
408
- buffer = nextBuffer;
409
- }
410
- }
411
- });
412
- parser.on("error", function(error) {
413
- reject(error);
414
- });
415
- parser.on("end", function() {
416
- const [finalState] = flushBuffer(state);
417
- resolve(finalState);
418
- });
419
- });
404
+ return async (state) => {
405
+ const [resolvedCsvData, resolvedParsingOptions] = expandReferences(
406
+ state,
407
+ csvData,
408
+ parsingOptions
409
+ );
410
+ const filteredOptions = Object.fromEntries(
411
+ Object.entries(resolvedParsingOptions).filter(
412
+ ([key]) => key in defaultOptions
413
+ )
414
+ );
415
+ const options2 = { ...defaultOptions, ...filteredOptions };
416
+ if (options2.chunkSize < 1) {
417
+ throw new Error("chunkSize must be at least 1");
418
+ }
419
+ let buffer = [];
420
+ const parser = typeof resolvedCsvData === "string" ? parse2(resolvedCsvData, options2) : resolvedCsvData.pipe(parse2(options2));
421
+ const flushBuffer = async (currentState) => {
422
+ const nextState = callback ? await callback(currentState, buffer) : composeNextState(currentState, buffer);
423
+ buffer = [];
424
+ return [nextState, buffer];
425
+ };
426
+ for await (const record of parser) {
427
+ buffer.push(record);
428
+ if (buffer.length === options2.chunkSize) {
429
+ const [nextState, nextBuffer] = await flushBuffer(state);
430
+ state = nextState;
431
+ buffer = nextBuffer;
432
+ }
433
+ }
434
+ const [finalState] = await flushBuffer(state);
435
+ return finalState;
420
436
  };
421
437
  }
422
438
 
@@ -436,7 +452,7 @@ export {
436
452
  src_default as default,
437
453
  each2 as each,
438
454
  execute,
439
- expandReferences,
455
+ expandReferences2 as expandReferences,
440
456
  field,
441
457
  fields,
442
458
  fn,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openfn/language-common",
3
- "version": "1.10.0",
3
+ "version": "1.10.2",
4
4
  "description": "Common Expressions for OpenFn",
5
5
  "homepage": "https://docs.openfn.org",
6
6
  "repository": {
@@ -276,7 +276,17 @@ export function composeNextState(state: State, response: any): State;
276
276
  * @returns {string}
277
277
  */
278
278
  export function humanProper(str: string): string;
279
- export function splitKeys(obj: any, keys: any): {}[];
279
+ /**
280
+ * Splits an object into two objects based on a list of keys.
281
+ * The first object contains the keys that are not in the list,
282
+ * and the second contains the keys that are.
283
+ * @public
284
+ * @function
285
+ * @param {Object} obj - The object to split.
286
+ * @param {string[]} keys - List of keys to split on.
287
+ * @returns {Object[]} - Tuple of objects, first object contains keys not in list, second contains keys that are.
288
+ */
289
+ export function splitKeys(obj: any, keys: string[]): any[];
280
290
  /**
281
291
  * Replaces emojis in a string.
282
292
  * @public
@@ -300,25 +310,27 @@ export function scrubEmojis(text: string, replacementChars: string): string;
300
310
  */
301
311
  export function chunk(array: any, chunkSize: Integer): any;
302
312
  /**
303
- * The function `parseCsv` takes a CSV file string or stream and parsing options as input, and returns a promise that
304
- * resolves to the parsed CSV data.
313
+ * Takes a CSV file string or stream and parsing options as input, and returns a promise that
314
+ * resolves to the parsed CSV data as an array of objects.
315
+ * Options for `parsingOptions` include:
316
+ * - `delimiter` {string/Buffer/[string/Buffer]} - Defines the character(s) used to delineate the fields inside a record. Default: `','`
317
+ * - `quote` {string/Buffer/[string/Buffer]} - Defines the characters used to surround a field. Default: `'"'`
318
+ * - `escape` {Buffer/string/null/boolean} - Set the escape character as one character/byte only. Default: `"`
319
+ * - `columns` {boolean / array / function} - Generates record in the form of object literals. Default: `true`
320
+ * - `bom` {boolean} - Strips the {@link https://en.wikipedia.org/wiki/Byte_order_mark byte order mark (BOM)} from the input string or buffer. Default: `true`
321
+ * - `trim` {boolean} - Ignore whitespace characters immediately around the `delimiter`. Default: `true`
322
+ * - `ltrim` {boolean} - Ignore whitespace characters from the left side of a CSV field. Default: `true`
323
+ * - `rtrim` {boolean} - Ignore whitespace characters from the right side of a CSV field. Default: `true`
324
+ * - `chunkSize` {number} - The size of each chunk of CSV data. Default: `Infinity`
325
+ * - `skip_empty_lines` {boolean} - Ignore empty lines in the CSV file. Default: `true`
326
+ * @public
327
+ * @function
305
328
  * @param {String | Stream} csvData - A CSV string or a readable stream
306
329
  * @param {Object} [parsingOptions] - Optional. Parsing options for converting CSV to JSON.
307
- * Possible options:
308
- * - `delimiter` {string|Buffer|[string|Buffer]} - Defines the character(s) used to delimitate the fields inside a record. Default: `','`
309
- * - `quote` {string|Buffer|[string|Buffer]} - Defines the characters used to surround a field. Default: `'"'`
310
- * - `escape` {Buffer|string|null|boolean} - Set the escape character as one character/byte only. Default: `"`
311
- * - `columns` {boolean | array | function} - Generates record in the form of object literals. Default: `true`
312
- * - `bom` {boolean} - Strips the {@link https://en.wikipedia.org/wiki/Byte_order_mark byte order mark (BOM)} from the input string or buffer. Default: `true`
313
- * - `trim` {boolean} - Ignore whitespace characters immediately around the `delimiter`. Default: `true`
314
- * - `ltrim` {boolean} - Ignore whitespace characters from the left side of a CSV field. Default: `true`
315
- * - `rtrim` {boolean} - Ignore whitespace characters from the right side of a CSV field. Default: `true`
316
- * - `chunkSize` {number} - The size of each chunk of CSV data. Default: `Infinity`
317
- * - `skip_empty_lines` {boolean} - The `skip_empty_lines` skips any line which is empty. Default: `true`
318
- * @param {function} callback - (Optional) callback function. If used it will take state and csvRows
319
- * @returns {Promise} The function returns a Promise that resolves to the result of parsing a CSV `stringOrStream`.
330
+ * @param {function} [callback] - (Optional) callback function. If used it will be called state and an array of rows.
331
+ * @returns {Operation} The function returns a Promise that resolves to the result of parsing a CSV `stringOrStream`.
320
332
  */
321
- export function parseCsv(csvData: string | Stream, parsingOptions?: any, callback: Function): Promise<any>;
333
+ export function parseCsv(csvData: string | Stream, parsingOptions?: any, callback?: Function): Operation;
322
334
  export * as beta from "./beta";
323
335
  export * as http from "./http";
324
336
  export * as dateFns from "./dateFns";
@@ -0,0 +1,10 @@
1
+ /**
2
+ * General-purpose utility functions
3
+ *
4
+ * These are designed more for use in adaptor code than job code
5
+ * (but we could choose to export util from common)
6
+ *
7
+ * None of these functions are operation factories
8
+ */
9
+ export function expandReferences(state: any, ...args: any[]): any[];
10
+ export function normalizeOauthConfig(configuration: any): any;