@openfn/language-common 2.5.0 → 3.0.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
@@ -704,58 +704,6 @@
704
704
  },
705
705
  "valid": true
706
706
  },
707
- {
708
- "name": "expandReferences",
709
- "params": [
710
- "value",
711
- "skipFilter"
712
- ],
713
- "docs": {
714
- "description": "Recursively resolves objects that have resolvable values (functions).",
715
- "tags": [
716
- {
717
- "title": "public",
718
- "description": null,
719
- "type": null
720
- },
721
- {
722
- "title": "function",
723
- "description": null,
724
- "name": null
725
- },
726
- {
727
- "title": "param",
728
- "description": "data",
729
- "type": {
730
- "type": "NameExpression",
731
- "name": "object"
732
- },
733
- "name": "value"
734
- },
735
- {
736
- "title": "param",
737
- "description": "a function which returns true if a value should be skipped",
738
- "type": {
739
- "type": "OptionalType",
740
- "expression": {
741
- "type": "NameExpression",
742
- "name": "Function"
743
- }
744
- },
745
- "name": "skipFilter"
746
- },
747
- {
748
- "title": "returns",
749
- "description": null,
750
- "type": {
751
- "type": "NameExpression",
752
- "name": "Operation"
753
- }
754
- }
755
- ]
756
- },
757
- "valid": true
758
- },
759
707
  {
760
708
  "name": "field",
761
709
  "params": [
@@ -1696,14 +1644,13 @@
1696
1644
  "valid": true
1697
1645
  },
1698
1646
  {
1699
- "name": "map",
1647
+ "name": "as",
1700
1648
  "params": [
1701
- "path",
1702
- "operation",
1703
- "state"
1649
+ "key",
1650
+ "operation"
1704
1651
  ],
1705
1652
  "docs": {
1706
- "description": "Scopes an array of data based on a JSONPath.\nUseful when the source data has `n` items you would like to map to\nan operation.\nThe operation will receive a slice of the data based of each item\nof the JSONPath provided.",
1653
+ "description": "Run an operation and save the result to a custom key in state instead of overwriting state.data.",
1707
1654
  "tags": [
1708
1655
  {
1709
1656
  "title": "public",
@@ -1717,34 +1664,91 @@
1717
1664
  },
1718
1665
  {
1719
1666
  "title": "example",
1720
- "description": "map(\"$.[*]\",\n create(\"SObject\",\n field(\"FirstName\", sourceValue(\"$.firstName\"))\n )\n)"
1667
+ "description": "as('cceData', collections.get('cce-data-dhis2', { key: `*:*:${$.syncedAt}*` }));",
1668
+ "caption": "Fetch cce-data from collections and store them under state.cceData"
1721
1669
  },
1722
1670
  {
1723
1671
  "title": "param",
1724
- "description": "JSONPath referencing a point in `state.data`.",
1672
+ "description": "The state key to assign the result of the operation to.",
1725
1673
  "type": {
1726
1674
  "type": "NameExpression",
1727
1675
  "name": "string"
1728
1676
  },
1729
- "name": "path"
1677
+ "name": "key"
1730
1678
  },
1731
1679
  {
1732
1680
  "title": "param",
1733
- "description": "The operation needed to be repeated.",
1681
+ "description": " An operation that returns a new state object with a `data` property",
1734
1682
  "type": {
1735
1683
  "type": "NameExpression",
1736
1684
  "name": "function"
1737
1685
  },
1738
1686
  "name": "operation"
1739
1687
  },
1688
+ {
1689
+ "title": "returns",
1690
+ "description": null,
1691
+ "type": {
1692
+ "type": "NameExpression",
1693
+ "name": "Operation"
1694
+ }
1695
+ }
1696
+ ]
1697
+ },
1698
+ "valid": true
1699
+ },
1700
+ {
1701
+ "name": "map",
1702
+ "params": {},
1703
+ "docs": {
1704
+ "description": "Iterates over a collection of items and returns a new array of mapped values,\nlike Javascript's `Array.map()` function.\n\nEach item in the source array will be passed into the callback function. The returned value\nwill be added to the new array. The callback is passed the original item, the current index\nin the source array (ie, the nth item number), and the state object.\n\nWrites a new array to `state.data` with transformed values.c array.",
1705
+ "tags": [
1706
+ {
1707
+ "title": "public",
1708
+ "description": null,
1709
+ "type": null
1710
+ },
1711
+ {
1712
+ "title": "function",
1713
+ "description": null,
1714
+ "name": null
1715
+ },
1716
+ {
1717
+ "title": "example",
1718
+ "description": "map($.items', (data, index, state) => {\n return {\n id: index + 1,\n name: data.name,\n createdAt: state.cursor,\n };\n});",
1719
+ "caption": "Transform an array of items in state"
1720
+ },
1721
+ {
1722
+ "title": "example",
1723
+ "description": "map($.items, async (data, index, state) => {\n const userInfo = await fetchUserInfo(data.userId);\n return {\n id: index + 1,\n name: data.name,\n extra: userInfo,\n };\n});",
1724
+ "caption": "Map items asynchronously (e.g. fetch extra info)"
1725
+ },
1726
+ {
1727
+ "title": "param",
1728
+ "description": "An array of items or a a JSONPath string which points to an array of items.",
1729
+ "type": {
1730
+ "type": "UnionType",
1731
+ "elements": [
1732
+ {
1733
+ "type": "NameExpression",
1734
+ "name": "string"
1735
+ },
1736
+ {
1737
+ "type": "NameExpression",
1738
+ "name": "Array"
1739
+ }
1740
+ ]
1741
+ },
1742
+ "name": "path"
1743
+ },
1740
1744
  {
1741
1745
  "title": "param",
1742
- "description": "Runtime state.",
1746
+ "description": "The mapping function, invoked with `(data, index, state)` for each item in the array.",
1743
1747
  "type": {
1744
1748
  "type": "NameExpression",
1745
- "name": "State"
1749
+ "name": "function"
1746
1750
  },
1747
- "name": "state"
1751
+ "name": "callback"
1748
1752
  },
1749
1753
  {
1750
1754
  "title": "returns",
@@ -1756,7 +1760,7 @@
1756
1760
  }
1757
1761
  ]
1758
1762
  },
1759
- "valid": true
1763
+ "valid": false
1760
1764
  }
1761
1765
  ],
1762
1766
  "exports": [],
package/dist/index.cjs CHANGED
@@ -25,8 +25,10 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
25
25
  // src/index.js
26
26
  var src_exports = {};
27
27
  __export(src_exports, {
28
+ _: () => import_lodash2.default,
28
29
  alterState: () => alterState,
29
30
  arrayToString: () => arrayToString,
31
+ as: () => as,
30
32
  asData: () => asData,
31
33
  assert: () => assert,
32
34
  beta: () => beta_exports,
@@ -41,7 +43,6 @@ __export(src_exports, {
41
43
  default: () => src_default,
42
44
  each: () => each,
43
45
  execute: () => execute,
44
- expandReferences: () => expandReferences2,
45
46
  field: () => field,
46
47
  fields: () => fields,
47
48
  fn: () => fn,
@@ -71,8 +72,10 @@ module.exports = __toCommonJS(src_exports);
71
72
  // src/Adaptor.js
72
73
  var Adaptor_exports = {};
73
74
  __export(Adaptor_exports, {
75
+ _: () => import_lodash2.default,
74
76
  alterState: () => alterState,
75
77
  arrayToString: () => arrayToString,
78
+ as: () => as,
76
79
  asData: () => asData,
77
80
  assert: () => assert,
78
81
  chunk: () => chunk,
@@ -84,7 +87,6 @@ __export(Adaptor_exports, {
84
87
  debug: () => debug,
85
88
  each: () => each,
86
89
  execute: () => execute,
87
- expandReferences: () => expandReferences2,
88
90
  field: () => field,
89
91
  fields: () => fields,
90
92
  fn: () => fn,
@@ -107,16 +109,12 @@ __export(Adaptor_exports, {
107
109
  toArray: () => toArray,
108
110
  validate: () => validate
109
111
  });
110
- var import_get = __toESM(require("lodash/get.js"), 1);
111
- var import_omit = __toESM(require("lodash/omit.js"), 1);
112
- var import_curry = __toESM(require("lodash/fp/curry.js"), 1);
113
- var import_groupBy = __toESM(require("lodash/groupBy.js"), 1);
114
- var import_fromPairs = __toESM(require("lodash/fp/fromPairs.js"), 1);
115
112
  var import_jsonpath_plus = require("jsonpath-plus");
116
113
  var import_csv_parse = require("csv-parse");
117
114
  var import_node_stream3 = require("stream");
118
115
  var import_undici2 = require("undici");
119
116
  var import_date_fns2 = __toESM(require("date-fns"), 1);
117
+ var import_lodash2 = __toESM(require("lodash"), 1);
120
118
 
121
119
  // src/util/index.js
122
120
  var util_exports = {};
@@ -532,20 +530,18 @@ function lastReferenceValue(path2) {
532
530
  const lastReferencePath = referencePath("[0]".concat(".", path2));
533
531
  return sourceValue(lastReferencePath);
534
532
  }
535
- var map = (0, import_curry.default)(function(path2, operation, state) {
536
- switch (typeof path2) {
537
- case "string":
538
- source(path2)(state).map(function(data) {
539
- return operation({ data, references: state.references });
540
- });
541
- return state;
542
- case "object":
543
- path2.map(function(data) {
544
- return operation({ data, references: state.references });
545
- });
546
- return state;
547
- }
548
- });
533
+ var map = function(path2, callback) {
534
+ return async (state) => {
535
+ const results = [];
536
+ const values = typeof path2 === "string" ? source(path2)(state) : path2;
537
+ let index2 = 0;
538
+ for (const item of values) {
539
+ const value = await callback(item, index2++, state);
540
+ results.push(value);
541
+ }
542
+ return { ...state, data: results };
543
+ };
544
+ };
549
545
  function asData(data, state) {
550
546
  switch (typeof data) {
551
547
  case "string":
@@ -592,34 +588,16 @@ function join(targetPath, sourcePath, targetKey) {
592
588
  });
593
589
  };
594
590
  }
595
- function expandReferences2(value, skipFilter) {
596
- return (state) => {
597
- if (skipFilter && skipFilter(value))
598
- return value;
599
- if (Array.isArray(value)) {
600
- return value.map((v) => expandReferences2(v)(state));
601
- }
602
- if (typeof value == "object" && !!value) {
603
- return Object.keys(value).reduce((acc, key) => {
604
- return { ...acc, [key]: expandReferences2(value[key])(state) };
605
- }, {});
606
- }
607
- if (typeof value == "function") {
608
- return expandReferences2(value(state))(state);
609
- }
610
- return value;
611
- };
612
- }
613
591
  function field(key, value) {
614
592
  return [key, value];
615
593
  }
616
594
  function fields(...fields2) {
617
- return (0, import_fromPairs.default)(fields2);
595
+ return import_lodash2.default.fromPairs(fields2);
618
596
  }
619
597
  function merge(dataSource, fields2) {
620
598
  return (state) => {
621
599
  const initialData = source(dataSource)(state);
622
- const additionalData = expandReferences2(fields2)(state);
600
+ const [additionalData] = expandReferences(state, fields2);
623
601
  return initialData.reduce((acc, dataItem) => {
624
602
  return [...acc, { ...dataItem, ...additionalData }];
625
603
  }, []);
@@ -632,8 +610,11 @@ function group(arrayOfObjects, keyPath, callback = (s) => s) {
632
610
  arrayOfObjects,
633
611
  keyPath
634
612
  );
635
- const results = (0, import_groupBy.default)(resolvedArray, (item) => (0, import_get.default)(item, resolvedKeyPath));
636
- return callback({ ...state, data: (0, import_omit.default)(results, [void 0]) });
613
+ const results = import_lodash2.default.groupBy(
614
+ resolvedArray,
615
+ (item) => import_lodash2.default.get(item, resolvedKeyPath)
616
+ );
617
+ return callback({ ...state, data: import_lodash2.default.omit(results, [void 0]) });
637
618
  };
638
619
  }
639
620
  function index() {
@@ -891,6 +872,17 @@ function debug(...args) {
891
872
  return state;
892
873
  };
893
874
  }
875
+ function as(key, operation) {
876
+ return async (state) => {
877
+ const [resolvedKey] = expandReferences(state, key);
878
+ const prevState = state.data;
879
+ const result = await operation(state);
880
+ const { data, ...rest } = result;
881
+ state[resolvedKey] = data;
882
+ state.data = prevState;
883
+ return { ...state, ...rest };
884
+ };
885
+ }
894
886
 
895
887
  // src/beta.js
896
888
  var beta_exports = {};
@@ -926,7 +918,7 @@ function each2(dataSource, operation) {
926
918
  // src/http.js
927
919
  var http_exports = {};
928
920
  __export(http_exports, {
929
- get: () => get3,
921
+ get: () => get2,
930
922
  options: () => options,
931
923
  post: () => post2,
932
924
  request: () => req
@@ -981,7 +973,7 @@ var req = function(method, url, options2) {
981
973
  };
982
974
  };
983
975
  };
984
- function get3(url, options2) {
976
+ function get2(url, options2) {
985
977
  return req("GET", url, options2);
986
978
  }
987
979
  function post2(url, data, options2) {
@@ -1000,8 +992,10 @@ var import_date_fns3 = require("date-fns");
1000
992
  var src_default = Adaptor_exports;
1001
993
  // Annotate the CommonJS export names for ESM import in node:
1002
994
  0 && (module.exports = {
995
+ _,
1003
996
  alterState,
1004
997
  arrayToString,
998
+ as,
1005
999
  asData,
1006
1000
  assert,
1007
1001
  beta,
@@ -1015,7 +1009,6 @@ var src_default = Adaptor_exports;
1015
1009
  debug,
1016
1010
  each,
1017
1011
  execute,
1018
- expandReferences,
1019
1012
  field,
1020
1013
  fields,
1021
1014
  fn,
package/dist/index.js CHANGED
@@ -7,8 +7,10 @@ var __export = (target, all) => {
7
7
  // src/Adaptor.js
8
8
  var Adaptor_exports = {};
9
9
  __export(Adaptor_exports, {
10
+ _: () => _2,
10
11
  alterState: () => alterState,
11
12
  arrayToString: () => arrayToString,
13
+ as: () => as,
12
14
  asData: () => asData,
13
15
  assert: () => assert,
14
16
  chunk: () => chunk,
@@ -20,7 +22,6 @@ __export(Adaptor_exports, {
20
22
  debug: () => debug,
21
23
  each: () => each,
22
24
  execute: () => execute,
23
- expandReferences: () => expandReferences2,
24
25
  field: () => field,
25
26
  fields: () => fields,
26
27
  fn: () => fn,
@@ -43,16 +44,12 @@ __export(Adaptor_exports, {
43
44
  toArray: () => toArray,
44
45
  validate: () => validate
45
46
  });
46
- import get2 from "lodash/get.js";
47
- import omit from "lodash/omit.js";
48
- import curry from "lodash/fp/curry.js";
49
- import groupBy from "lodash/groupBy.js";
50
- import fromPairs from "lodash/fp/fromPairs.js";
51
47
  import { JSONPath } from "jsonpath-plus";
52
48
  import { parse } from "csv-parse";
53
49
  import { Readable as Readable3 } from "stream";
54
50
  import { request as request2 } from "undici";
55
51
  import dateFns from "date-fns";
52
+ import _2 from "lodash";
56
53
 
57
54
  // src/util/index.js
58
55
  var util_exports = {};
@@ -468,20 +465,18 @@ function lastReferenceValue(path2) {
468
465
  const lastReferencePath = referencePath("[0]".concat(".", path2));
469
466
  return sourceValue(lastReferencePath);
470
467
  }
471
- var map = curry(function(path2, operation, state) {
472
- switch (typeof path2) {
473
- case "string":
474
- source(path2)(state).map(function(data) {
475
- return operation({ data, references: state.references });
476
- });
477
- return state;
478
- case "object":
479
- path2.map(function(data) {
480
- return operation({ data, references: state.references });
481
- });
482
- return state;
483
- }
484
- });
468
+ var map = function(path2, callback) {
469
+ return async (state) => {
470
+ const results = [];
471
+ const values = typeof path2 === "string" ? source(path2)(state) : path2;
472
+ let index2 = 0;
473
+ for (const item of values) {
474
+ const value = await callback(item, index2++, state);
475
+ results.push(value);
476
+ }
477
+ return { ...state, data: results };
478
+ };
479
+ };
485
480
  function asData(data, state) {
486
481
  switch (typeof data) {
487
482
  case "string":
@@ -528,34 +523,16 @@ function join(targetPath, sourcePath, targetKey) {
528
523
  });
529
524
  };
530
525
  }
531
- function expandReferences2(value, skipFilter) {
532
- return (state) => {
533
- if (skipFilter && skipFilter(value))
534
- return value;
535
- if (Array.isArray(value)) {
536
- return value.map((v) => expandReferences2(v)(state));
537
- }
538
- if (typeof value == "object" && !!value) {
539
- return Object.keys(value).reduce((acc, key) => {
540
- return { ...acc, [key]: expandReferences2(value[key])(state) };
541
- }, {});
542
- }
543
- if (typeof value == "function") {
544
- return expandReferences2(value(state))(state);
545
- }
546
- return value;
547
- };
548
- }
549
526
  function field(key, value) {
550
527
  return [key, value];
551
528
  }
552
529
  function fields(...fields2) {
553
- return fromPairs(fields2);
530
+ return _2.fromPairs(fields2);
554
531
  }
555
532
  function merge(dataSource, fields2) {
556
533
  return (state) => {
557
534
  const initialData = source(dataSource)(state);
558
- const additionalData = expandReferences2(fields2)(state);
535
+ const [additionalData] = expandReferences(state, fields2);
559
536
  return initialData.reduce((acc, dataItem) => {
560
537
  return [...acc, { ...dataItem, ...additionalData }];
561
538
  }, []);
@@ -568,8 +545,11 @@ function group(arrayOfObjects, keyPath, callback = (s) => s) {
568
545
  arrayOfObjects,
569
546
  keyPath
570
547
  );
571
- const results = groupBy(resolvedArray, (item) => get2(item, resolvedKeyPath));
572
- return callback({ ...state, data: omit(results, [void 0]) });
548
+ const results = _2.groupBy(
549
+ resolvedArray,
550
+ (item) => _2.get(item, resolvedKeyPath)
551
+ );
552
+ return callback({ ...state, data: _2.omit(results, [void 0]) });
573
553
  };
574
554
  }
575
555
  function index() {
@@ -827,6 +807,17 @@ function debug(...args) {
827
807
  return state;
828
808
  };
829
809
  }
810
+ function as(key, operation) {
811
+ return async (state) => {
812
+ const [resolvedKey] = expandReferences(state, key);
813
+ const prevState = state.data;
814
+ const result = await operation(state);
815
+ const { data, ...rest } = result;
816
+ state[resolvedKey] = data;
817
+ state.data = prevState;
818
+ return { ...state, ...rest };
819
+ };
820
+ }
830
821
 
831
822
  // src/beta.js
832
823
  var beta_exports = {};
@@ -862,7 +853,7 @@ function each2(dataSource, operation) {
862
853
  // src/http.js
863
854
  var http_exports = {};
864
855
  __export(http_exports, {
865
- get: () => get3,
856
+ get: () => get2,
866
857
  options: () => options,
867
858
  post: () => post2,
868
859
  request: () => req
@@ -917,7 +908,7 @@ var req = function(method, url, options2) {
917
908
  };
918
909
  };
919
910
  };
920
- function get3(url, options2) {
911
+ function get2(url, options2) {
921
912
  return req("GET", url, options2);
922
913
  }
923
914
  function post2(url, data, options2) {
@@ -938,8 +929,10 @@ import {
938
929
  // src/index.js
939
930
  var src_default = Adaptor_exports;
940
931
  export {
932
+ _2 as _,
941
933
  alterState,
942
934
  arrayToString,
935
+ as,
943
936
  asData,
944
937
  assert,
945
938
  beta_exports as beta,
@@ -954,7 +947,6 @@ export {
954
947
  src_default as default,
955
948
  each,
956
949
  execute,
957
- expandReferences2 as expandReferences,
958
950
  field,
959
951
  fields,
960
952
  fn,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@openfn/language-common",
3
3
  "label": "Common",
4
- "version": "2.5.0",
4
+ "version": "3.0.0",
5
5
  "description": "Common Expressions for OpenFn",
6
6
  "homepage": "https://docs.openfn.org",
7
7
  "repository": {
@@ -42,7 +42,7 @@
42
42
  "http-status-codes": "^2.3.0",
43
43
  "jsonpath-plus": "^10.3.0",
44
44
  "lodash": "^4.17.21",
45
- "undici": "^5.28.5"
45
+ "undici": "^5.29.0"
46
46
  },
47
47
  "devDependencies": {
48
48
  "chai": "4.3.6",
@@ -200,15 +200,6 @@ export function combine(...operations: Operations): Operation;
200
200
  * @returns {Operation}
201
201
  */
202
202
  export function join(targetPath: string, sourcePath: string, targetKey: string): Operation;
203
- /**
204
- * Recursively resolves objects that have resolvable values (functions).
205
- * @public
206
- * @function
207
- * @param {object} value - data
208
- * @param {Function} [skipFilter] - a function which returns true if a value should be skipped
209
- * @returns {Operation}
210
- */
211
- export function expandReferences(value: object, skipFilter?: Function): Operation;
212
203
  /**
213
204
  * Returns a key, value pair in an array.
214
205
  * @public
@@ -466,22 +457,15 @@ export function log(...args: any): Operation;
466
457
  */
467
458
  export function debug(...args: any): Operation;
468
459
  /**
469
- * Scopes an array of data based on a JSONPath.
470
- * Useful when the source data has `n` items you would like to map to
471
- * an operation.
472
- * The operation will receive a slice of the data based of each item
473
- * of the JSONPath provided.
460
+ * Run an operation and save the result to a custom key in state instead of overwriting state.data.
474
461
  * @public
475
462
  * @function
476
- * @example
477
- * map("$.[*]",
478
- * create("SObject",
479
- * field("FirstName", sourceValue("$.firstName"))
480
- * )
481
- * )
482
- * @param {string} path - JSONPath referencing a point in `state.data`.
483
- * @param {function} operation - The operation needed to be repeated.
484
- * @param {State} state - Runtime state.
485
- * @returns {State}
463
+ * @example <caption>Fetch cce-data from collections and store them under state.cceData</caption>
464
+ * as('cceData', collections.get('cce-data-dhis2', { key: `*:*:${$.syncedAt}*` }));
465
+ * @param {string} key - The state key to assign the result of the operation to.
466
+ * @param {function} operation - An operation that returns a new state object with a `data` property
467
+ * @returns {Operation}
486
468
  */
487
- export const map: any;
469
+ export function as(key: string, operation: Function): Operation;
470
+ export { _ };
471
+ export function map(path: string | any[], callback: Function): State;