@openfn/language-common 2.5.0 → 3.0.1
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 +69 -65
- package/dist/index.cjs +82 -54
- package/dist/index.js +80 -53
- package/dist/util.cjs +43 -8
- package/dist/util.js +43 -8
- package/package.json +2 -2
- package/types/Adaptor.d.ts +9 -25
- package/types/util/http.d.ts +1 -1
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": "
|
|
1647
|
+
"name": "as",
|
|
1700
1648
|
"params": [
|
|
1701
|
-
"
|
|
1702
|
-
"operation"
|
|
1703
|
-
"state"
|
|
1649
|
+
"key",
|
|
1650
|
+
"operation"
|
|
1704
1651
|
],
|
|
1705
1652
|
"docs": {
|
|
1706
|
-
"description": "
|
|
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": "
|
|
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": "
|
|
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": "
|
|
1677
|
+
"name": "key"
|
|
1730
1678
|
},
|
|
1731
1679
|
{
|
|
1732
1680
|
"title": "param",
|
|
1733
|
-
"description": "
|
|
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": "
|
|
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": "
|
|
1749
|
+
"name": "function"
|
|
1746
1750
|
},
|
|
1747
|
-
"name": "
|
|
1751
|
+
"name": "callback"
|
|
1748
1752
|
},
|
|
1749
1753
|
{
|
|
1750
1754
|
"title": "returns",
|
|
@@ -1756,7 +1760,7 @@
|
|
|
1756
1760
|
}
|
|
1757
1761
|
]
|
|
1758
1762
|
},
|
|
1759
|
-
"valid":
|
|
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 = {};
|
|
@@ -220,11 +218,46 @@ var getClient = (baseUrl, options2) => {
|
|
|
220
218
|
}
|
|
221
219
|
return clients.get(baseUrl);
|
|
222
220
|
};
|
|
223
|
-
var enableMockClient = (baseUrl) => {
|
|
221
|
+
var enableMockClient = (baseUrl, options2 = {}) => {
|
|
222
|
+
const { defaultContentType = "application/json" } = options2;
|
|
224
223
|
const mockAgent = new import_undici.MockAgent({ connections: 1 });
|
|
225
224
|
mockAgent.disableNetConnect();
|
|
226
225
|
const client = mockAgent.get(baseUrl);
|
|
227
226
|
if (!clients.has(baseUrl)) {
|
|
227
|
+
if (defaultContentType) {
|
|
228
|
+
const _intercept = client.intercept;
|
|
229
|
+
client.intercept = (...args) => {
|
|
230
|
+
const interceptor = _intercept.apply(client, args);
|
|
231
|
+
const _reply = interceptor.reply;
|
|
232
|
+
const ensureJsonHeader = (headers = {}) => {
|
|
233
|
+
const hasJsonHeader = Object.keys(headers).find(
|
|
234
|
+
(k) => /content-type/i.test(k)
|
|
235
|
+
);
|
|
236
|
+
if (!hasJsonHeader) {
|
|
237
|
+
headers["content-type"] = defaultContentType;
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
const reply = (...args2) => {
|
|
241
|
+
if (typeof args2[0] === "function") {
|
|
242
|
+
const response = _reply.apply(interceptor, args2);
|
|
243
|
+
if (response.body) {
|
|
244
|
+
response.headers ?? (response.headers = {});
|
|
245
|
+
ensureJsonHeader(response.headers);
|
|
246
|
+
}
|
|
247
|
+
return response;
|
|
248
|
+
} else {
|
|
249
|
+
const [code, data, options3 = {}] = args2;
|
|
250
|
+
if (data) {
|
|
251
|
+
options3.headers ?? (options3.headers = {});
|
|
252
|
+
ensureJsonHeader(options3.headers);
|
|
253
|
+
}
|
|
254
|
+
return _reply.call(interceptor, code, data, options3);
|
|
255
|
+
}
|
|
256
|
+
};
|
|
257
|
+
interceptor.reply = reply;
|
|
258
|
+
return interceptor;
|
|
259
|
+
};
|
|
260
|
+
}
|
|
228
261
|
clients.set(baseUrl, client);
|
|
229
262
|
}
|
|
230
263
|
return client;
|
|
@@ -362,15 +395,15 @@ function encodeRequestBody(body) {
|
|
|
362
395
|
throw new Error("Unsupported body type");
|
|
363
396
|
}
|
|
364
397
|
async function readResponseBody(response, parseAs) {
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
response.headers["content-length"]
|
|
368
|
-
|
|
398
|
+
let contentLength = -1;
|
|
399
|
+
if ("content-length" in response.headers) {
|
|
400
|
+
contentLength = parseInt(response.headers["content-length"]);
|
|
401
|
+
}
|
|
369
402
|
const contentType = response.headers["content-type"];
|
|
403
|
+
if (contentLength === 0 || !contentType || response.statusCode === 204) {
|
|
404
|
+
return;
|
|
405
|
+
}
|
|
370
406
|
try {
|
|
371
|
-
if (Number.isNaN(contentLength) || contentLength === 0) {
|
|
372
|
-
return void 0;
|
|
373
|
-
}
|
|
374
407
|
switch (parseAs) {
|
|
375
408
|
case "json":
|
|
376
409
|
return await response.body.json();
|
|
@@ -532,20 +565,18 @@ function lastReferenceValue(path2) {
|
|
|
532
565
|
const lastReferencePath = referencePath("[0]".concat(".", path2));
|
|
533
566
|
return sourceValue(lastReferencePath);
|
|
534
567
|
}
|
|
535
|
-
var map =
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
}
|
|
548
|
-
});
|
|
568
|
+
var map = function(path2, callback) {
|
|
569
|
+
return async (state) => {
|
|
570
|
+
const results = [];
|
|
571
|
+
const values = typeof path2 === "string" ? source(path2)(state) : path2;
|
|
572
|
+
let index2 = 0;
|
|
573
|
+
for (const item of values) {
|
|
574
|
+
const value = await callback(item, index2++, state);
|
|
575
|
+
results.push(value);
|
|
576
|
+
}
|
|
577
|
+
return { ...state, data: results };
|
|
578
|
+
};
|
|
579
|
+
};
|
|
549
580
|
function asData(data, state) {
|
|
550
581
|
switch (typeof data) {
|
|
551
582
|
case "string":
|
|
@@ -592,34 +623,16 @@ function join(targetPath, sourcePath, targetKey) {
|
|
|
592
623
|
});
|
|
593
624
|
};
|
|
594
625
|
}
|
|
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
626
|
function field(key, value) {
|
|
614
627
|
return [key, value];
|
|
615
628
|
}
|
|
616
629
|
function fields(...fields2) {
|
|
617
|
-
return
|
|
630
|
+
return import_lodash2.default.fromPairs(fields2);
|
|
618
631
|
}
|
|
619
632
|
function merge(dataSource, fields2) {
|
|
620
633
|
return (state) => {
|
|
621
634
|
const initialData = source(dataSource)(state);
|
|
622
|
-
const additionalData =
|
|
635
|
+
const [additionalData] = expandReferences(state, fields2);
|
|
623
636
|
return initialData.reduce((acc, dataItem) => {
|
|
624
637
|
return [...acc, { ...dataItem, ...additionalData }];
|
|
625
638
|
}, []);
|
|
@@ -632,8 +645,11 @@ function group(arrayOfObjects, keyPath, callback = (s) => s) {
|
|
|
632
645
|
arrayOfObjects,
|
|
633
646
|
keyPath
|
|
634
647
|
);
|
|
635
|
-
const results =
|
|
636
|
-
|
|
648
|
+
const results = import_lodash2.default.groupBy(
|
|
649
|
+
resolvedArray,
|
|
650
|
+
(item) => import_lodash2.default.get(item, resolvedKeyPath)
|
|
651
|
+
);
|
|
652
|
+
return callback({ ...state, data: import_lodash2.default.omit(results, [void 0]) });
|
|
637
653
|
};
|
|
638
654
|
}
|
|
639
655
|
function index() {
|
|
@@ -891,6 +907,17 @@ function debug(...args) {
|
|
|
891
907
|
return state;
|
|
892
908
|
};
|
|
893
909
|
}
|
|
910
|
+
function as(key, operation) {
|
|
911
|
+
return async (state) => {
|
|
912
|
+
const [resolvedKey] = expandReferences(state, key);
|
|
913
|
+
const prevState = state.data;
|
|
914
|
+
const result = await operation(state);
|
|
915
|
+
const { data, ...rest } = result;
|
|
916
|
+
state[resolvedKey] = data;
|
|
917
|
+
state.data = prevState;
|
|
918
|
+
return { ...state, ...rest };
|
|
919
|
+
};
|
|
920
|
+
}
|
|
894
921
|
|
|
895
922
|
// src/beta.js
|
|
896
923
|
var beta_exports = {};
|
|
@@ -926,7 +953,7 @@ function each2(dataSource, operation) {
|
|
|
926
953
|
// src/http.js
|
|
927
954
|
var http_exports = {};
|
|
928
955
|
__export(http_exports, {
|
|
929
|
-
get: () =>
|
|
956
|
+
get: () => get2,
|
|
930
957
|
options: () => options,
|
|
931
958
|
post: () => post2,
|
|
932
959
|
request: () => req
|
|
@@ -981,7 +1008,7 @@ var req = function(method, url, options2) {
|
|
|
981
1008
|
};
|
|
982
1009
|
};
|
|
983
1010
|
};
|
|
984
|
-
function
|
|
1011
|
+
function get2(url, options2) {
|
|
985
1012
|
return req("GET", url, options2);
|
|
986
1013
|
}
|
|
987
1014
|
function post2(url, data, options2) {
|
|
@@ -1000,8 +1027,10 @@ var import_date_fns3 = require("date-fns");
|
|
|
1000
1027
|
var src_default = Adaptor_exports;
|
|
1001
1028
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1002
1029
|
0 && (module.exports = {
|
|
1030
|
+
_,
|
|
1003
1031
|
alterState,
|
|
1004
1032
|
arrayToString,
|
|
1033
|
+
as,
|
|
1005
1034
|
asData,
|
|
1006
1035
|
assert,
|
|
1007
1036
|
beta,
|
|
@@ -1015,7 +1044,6 @@ var src_default = Adaptor_exports;
|
|
|
1015
1044
|
debug,
|
|
1016
1045
|
each,
|
|
1017
1046
|
execute,
|
|
1018
|
-
expandReferences,
|
|
1019
1047
|
field,
|
|
1020
1048
|
fields,
|
|
1021
1049
|
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 = {};
|
|
@@ -156,11 +153,46 @@ var getClient = (baseUrl, options2) => {
|
|
|
156
153
|
}
|
|
157
154
|
return clients.get(baseUrl);
|
|
158
155
|
};
|
|
159
|
-
var enableMockClient = (baseUrl) => {
|
|
156
|
+
var enableMockClient = (baseUrl, options2 = {}) => {
|
|
157
|
+
const { defaultContentType = "application/json" } = options2;
|
|
160
158
|
const mockAgent = new MockAgent({ connections: 1 });
|
|
161
159
|
mockAgent.disableNetConnect();
|
|
162
160
|
const client = mockAgent.get(baseUrl);
|
|
163
161
|
if (!clients.has(baseUrl)) {
|
|
162
|
+
if (defaultContentType) {
|
|
163
|
+
const _intercept = client.intercept;
|
|
164
|
+
client.intercept = (...args) => {
|
|
165
|
+
const interceptor = _intercept.apply(client, args);
|
|
166
|
+
const _reply = interceptor.reply;
|
|
167
|
+
const ensureJsonHeader = (headers = {}) => {
|
|
168
|
+
const hasJsonHeader = Object.keys(headers).find(
|
|
169
|
+
(k) => /content-type/i.test(k)
|
|
170
|
+
);
|
|
171
|
+
if (!hasJsonHeader) {
|
|
172
|
+
headers["content-type"] = defaultContentType;
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
const reply = (...args2) => {
|
|
176
|
+
if (typeof args2[0] === "function") {
|
|
177
|
+
const response = _reply.apply(interceptor, args2);
|
|
178
|
+
if (response.body) {
|
|
179
|
+
response.headers ?? (response.headers = {});
|
|
180
|
+
ensureJsonHeader(response.headers);
|
|
181
|
+
}
|
|
182
|
+
return response;
|
|
183
|
+
} else {
|
|
184
|
+
const [code, data, options3 = {}] = args2;
|
|
185
|
+
if (data) {
|
|
186
|
+
options3.headers ?? (options3.headers = {});
|
|
187
|
+
ensureJsonHeader(options3.headers);
|
|
188
|
+
}
|
|
189
|
+
return _reply.call(interceptor, code, data, options3);
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
interceptor.reply = reply;
|
|
193
|
+
return interceptor;
|
|
194
|
+
};
|
|
195
|
+
}
|
|
164
196
|
clients.set(baseUrl, client);
|
|
165
197
|
}
|
|
166
198
|
return client;
|
|
@@ -298,15 +330,15 @@ function encodeRequestBody(body) {
|
|
|
298
330
|
throw new Error("Unsupported body type");
|
|
299
331
|
}
|
|
300
332
|
async function readResponseBody(response, parseAs) {
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
response.headers["content-length"]
|
|
304
|
-
|
|
333
|
+
let contentLength = -1;
|
|
334
|
+
if ("content-length" in response.headers) {
|
|
335
|
+
contentLength = parseInt(response.headers["content-length"]);
|
|
336
|
+
}
|
|
305
337
|
const contentType = response.headers["content-type"];
|
|
338
|
+
if (contentLength === 0 || !contentType || response.statusCode === 204) {
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
306
341
|
try {
|
|
307
|
-
if (Number.isNaN(contentLength) || contentLength === 0) {
|
|
308
|
-
return void 0;
|
|
309
|
-
}
|
|
310
342
|
switch (parseAs) {
|
|
311
343
|
case "json":
|
|
312
344
|
return await response.body.json();
|
|
@@ -468,20 +500,18 @@ function lastReferenceValue(path2) {
|
|
|
468
500
|
const lastReferencePath = referencePath("[0]".concat(".", path2));
|
|
469
501
|
return sourceValue(lastReferencePath);
|
|
470
502
|
}
|
|
471
|
-
var map =
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
}
|
|
484
|
-
});
|
|
503
|
+
var map = function(path2, callback) {
|
|
504
|
+
return async (state) => {
|
|
505
|
+
const results = [];
|
|
506
|
+
const values = typeof path2 === "string" ? source(path2)(state) : path2;
|
|
507
|
+
let index2 = 0;
|
|
508
|
+
for (const item of values) {
|
|
509
|
+
const value = await callback(item, index2++, state);
|
|
510
|
+
results.push(value);
|
|
511
|
+
}
|
|
512
|
+
return { ...state, data: results };
|
|
513
|
+
};
|
|
514
|
+
};
|
|
485
515
|
function asData(data, state) {
|
|
486
516
|
switch (typeof data) {
|
|
487
517
|
case "string":
|
|
@@ -528,34 +558,16 @@ function join(targetPath, sourcePath, targetKey) {
|
|
|
528
558
|
});
|
|
529
559
|
};
|
|
530
560
|
}
|
|
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
561
|
function field(key, value) {
|
|
550
562
|
return [key, value];
|
|
551
563
|
}
|
|
552
564
|
function fields(...fields2) {
|
|
553
|
-
return fromPairs(fields2);
|
|
565
|
+
return _2.fromPairs(fields2);
|
|
554
566
|
}
|
|
555
567
|
function merge(dataSource, fields2) {
|
|
556
568
|
return (state) => {
|
|
557
569
|
const initialData = source(dataSource)(state);
|
|
558
|
-
const additionalData =
|
|
570
|
+
const [additionalData] = expandReferences(state, fields2);
|
|
559
571
|
return initialData.reduce((acc, dataItem) => {
|
|
560
572
|
return [...acc, { ...dataItem, ...additionalData }];
|
|
561
573
|
}, []);
|
|
@@ -568,8 +580,11 @@ function group(arrayOfObjects, keyPath, callback = (s) => s) {
|
|
|
568
580
|
arrayOfObjects,
|
|
569
581
|
keyPath
|
|
570
582
|
);
|
|
571
|
-
const results = groupBy(
|
|
572
|
-
|
|
583
|
+
const results = _2.groupBy(
|
|
584
|
+
resolvedArray,
|
|
585
|
+
(item) => _2.get(item, resolvedKeyPath)
|
|
586
|
+
);
|
|
587
|
+
return callback({ ...state, data: _2.omit(results, [void 0]) });
|
|
573
588
|
};
|
|
574
589
|
}
|
|
575
590
|
function index() {
|
|
@@ -827,6 +842,17 @@ function debug(...args) {
|
|
|
827
842
|
return state;
|
|
828
843
|
};
|
|
829
844
|
}
|
|
845
|
+
function as(key, operation) {
|
|
846
|
+
return async (state) => {
|
|
847
|
+
const [resolvedKey] = expandReferences(state, key);
|
|
848
|
+
const prevState = state.data;
|
|
849
|
+
const result = await operation(state);
|
|
850
|
+
const { data, ...rest } = result;
|
|
851
|
+
state[resolvedKey] = data;
|
|
852
|
+
state.data = prevState;
|
|
853
|
+
return { ...state, ...rest };
|
|
854
|
+
};
|
|
855
|
+
}
|
|
830
856
|
|
|
831
857
|
// src/beta.js
|
|
832
858
|
var beta_exports = {};
|
|
@@ -862,7 +888,7 @@ function each2(dataSource, operation) {
|
|
|
862
888
|
// src/http.js
|
|
863
889
|
var http_exports = {};
|
|
864
890
|
__export(http_exports, {
|
|
865
|
-
get: () =>
|
|
891
|
+
get: () => get2,
|
|
866
892
|
options: () => options,
|
|
867
893
|
post: () => post2,
|
|
868
894
|
request: () => req
|
|
@@ -917,7 +943,7 @@ var req = function(method, url, options2) {
|
|
|
917
943
|
};
|
|
918
944
|
};
|
|
919
945
|
};
|
|
920
|
-
function
|
|
946
|
+
function get2(url, options2) {
|
|
921
947
|
return req("GET", url, options2);
|
|
922
948
|
}
|
|
923
949
|
function post2(url, data, options2) {
|
|
@@ -938,8 +964,10 @@ import {
|
|
|
938
964
|
// src/index.js
|
|
939
965
|
var src_default = Adaptor_exports;
|
|
940
966
|
export {
|
|
967
|
+
_2 as _,
|
|
941
968
|
alterState,
|
|
942
969
|
arrayToString,
|
|
970
|
+
as,
|
|
943
971
|
asData,
|
|
944
972
|
assert,
|
|
945
973
|
beta_exports as beta,
|
|
@@ -954,7 +982,6 @@ export {
|
|
|
954
982
|
src_default as default,
|
|
955
983
|
each,
|
|
956
984
|
execute,
|
|
957
|
-
expandReferences2 as expandReferences,
|
|
958
985
|
field,
|
|
959
986
|
fields,
|
|
960
987
|
fn,
|
package/dist/util.cjs
CHANGED
|
@@ -125,11 +125,46 @@ var getClient = (baseUrl, options) => {
|
|
|
125
125
|
}
|
|
126
126
|
return clients.get(baseUrl);
|
|
127
127
|
};
|
|
128
|
-
var enableMockClient = (baseUrl) => {
|
|
128
|
+
var enableMockClient = (baseUrl, options = {}) => {
|
|
129
|
+
const { defaultContentType = "application/json" } = options;
|
|
129
130
|
const mockAgent = new import_undici.MockAgent({ connections: 1 });
|
|
130
131
|
mockAgent.disableNetConnect();
|
|
131
132
|
const client = mockAgent.get(baseUrl);
|
|
132
133
|
if (!clients.has(baseUrl)) {
|
|
134
|
+
if (defaultContentType) {
|
|
135
|
+
const _intercept = client.intercept;
|
|
136
|
+
client.intercept = (...args) => {
|
|
137
|
+
const interceptor = _intercept.apply(client, args);
|
|
138
|
+
const _reply = interceptor.reply;
|
|
139
|
+
const ensureJsonHeader = (headers = {}) => {
|
|
140
|
+
const hasJsonHeader = Object.keys(headers).find(
|
|
141
|
+
(k) => /content-type/i.test(k)
|
|
142
|
+
);
|
|
143
|
+
if (!hasJsonHeader) {
|
|
144
|
+
headers["content-type"] = defaultContentType;
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
const reply = (...args2) => {
|
|
148
|
+
if (typeof args2[0] === "function") {
|
|
149
|
+
const response = _reply.apply(interceptor, args2);
|
|
150
|
+
if (response.body) {
|
|
151
|
+
response.headers ?? (response.headers = {});
|
|
152
|
+
ensureJsonHeader(response.headers);
|
|
153
|
+
}
|
|
154
|
+
return response;
|
|
155
|
+
} else {
|
|
156
|
+
const [code, data, options2 = {}] = args2;
|
|
157
|
+
if (data) {
|
|
158
|
+
options2.headers ?? (options2.headers = {});
|
|
159
|
+
ensureJsonHeader(options2.headers);
|
|
160
|
+
}
|
|
161
|
+
return _reply.call(interceptor, code, data, options2);
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
interceptor.reply = reply;
|
|
165
|
+
return interceptor;
|
|
166
|
+
};
|
|
167
|
+
}
|
|
133
168
|
clients.set(baseUrl, client);
|
|
134
169
|
}
|
|
135
170
|
return client;
|
|
@@ -267,15 +302,15 @@ function encodeRequestBody(body) {
|
|
|
267
302
|
throw new Error("Unsupported body type");
|
|
268
303
|
}
|
|
269
304
|
async function readResponseBody(response, parseAs) {
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
response.headers["content-length"]
|
|
273
|
-
|
|
305
|
+
let contentLength = -1;
|
|
306
|
+
if ("content-length" in response.headers) {
|
|
307
|
+
contentLength = parseInt(response.headers["content-length"]);
|
|
308
|
+
}
|
|
274
309
|
const contentType = response.headers["content-type"];
|
|
310
|
+
if (contentLength === 0 || !contentType || response.statusCode === 204) {
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
275
313
|
try {
|
|
276
|
-
if (Number.isNaN(contentLength) || contentLength === 0) {
|
|
277
|
-
return void 0;
|
|
278
|
-
}
|
|
279
314
|
switch (parseAs) {
|
|
280
315
|
case "json":
|
|
281
316
|
return await response.body.json();
|
package/dist/util.js
CHANGED
|
@@ -76,11 +76,46 @@ var getClient = (baseUrl, options) => {
|
|
|
76
76
|
}
|
|
77
77
|
return clients.get(baseUrl);
|
|
78
78
|
};
|
|
79
|
-
var enableMockClient = (baseUrl) => {
|
|
79
|
+
var enableMockClient = (baseUrl, options = {}) => {
|
|
80
|
+
const { defaultContentType = "application/json" } = options;
|
|
80
81
|
const mockAgent = new MockAgent({ connections: 1 });
|
|
81
82
|
mockAgent.disableNetConnect();
|
|
82
83
|
const client = mockAgent.get(baseUrl);
|
|
83
84
|
if (!clients.has(baseUrl)) {
|
|
85
|
+
if (defaultContentType) {
|
|
86
|
+
const _intercept = client.intercept;
|
|
87
|
+
client.intercept = (...args) => {
|
|
88
|
+
const interceptor = _intercept.apply(client, args);
|
|
89
|
+
const _reply = interceptor.reply;
|
|
90
|
+
const ensureJsonHeader = (headers = {}) => {
|
|
91
|
+
const hasJsonHeader = Object.keys(headers).find(
|
|
92
|
+
(k) => /content-type/i.test(k)
|
|
93
|
+
);
|
|
94
|
+
if (!hasJsonHeader) {
|
|
95
|
+
headers["content-type"] = defaultContentType;
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
const reply = (...args2) => {
|
|
99
|
+
if (typeof args2[0] === "function") {
|
|
100
|
+
const response = _reply.apply(interceptor, args2);
|
|
101
|
+
if (response.body) {
|
|
102
|
+
response.headers ?? (response.headers = {});
|
|
103
|
+
ensureJsonHeader(response.headers);
|
|
104
|
+
}
|
|
105
|
+
return response;
|
|
106
|
+
} else {
|
|
107
|
+
const [code, data, options2 = {}] = args2;
|
|
108
|
+
if (data) {
|
|
109
|
+
options2.headers ?? (options2.headers = {});
|
|
110
|
+
ensureJsonHeader(options2.headers);
|
|
111
|
+
}
|
|
112
|
+
return _reply.call(interceptor, code, data, options2);
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
interceptor.reply = reply;
|
|
116
|
+
return interceptor;
|
|
117
|
+
};
|
|
118
|
+
}
|
|
84
119
|
clients.set(baseUrl, client);
|
|
85
120
|
}
|
|
86
121
|
return client;
|
|
@@ -218,15 +253,15 @@ function encodeRequestBody(body) {
|
|
|
218
253
|
throw new Error("Unsupported body type");
|
|
219
254
|
}
|
|
220
255
|
async function readResponseBody(response, parseAs) {
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
response.headers["content-length"]
|
|
224
|
-
|
|
256
|
+
let contentLength = -1;
|
|
257
|
+
if ("content-length" in response.headers) {
|
|
258
|
+
contentLength = parseInt(response.headers["content-length"]);
|
|
259
|
+
}
|
|
225
260
|
const contentType = response.headers["content-type"];
|
|
261
|
+
if (contentLength === 0 || !contentType || response.statusCode === 204) {
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
226
264
|
try {
|
|
227
|
-
if (Number.isNaN(contentLength) || contentLength === 0) {
|
|
228
|
-
return void 0;
|
|
229
|
-
}
|
|
230
265
|
switch (parseAs) {
|
|
231
266
|
case "json":
|
|
232
267
|
return await response.body.json();
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openfn/language-common",
|
|
3
3
|
"label": "Common",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "3.0.1",
|
|
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.
|
|
45
|
+
"undici": "^5.29.0"
|
|
46
46
|
},
|
|
47
47
|
"devDependencies": {
|
|
48
48
|
"chai": "4.3.6",
|
package/types/Adaptor.d.ts
CHANGED
|
@@ -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
|
-
*
|
|
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
|
-
*
|
|
478
|
-
*
|
|
479
|
-
*
|
|
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
|
|
469
|
+
export function as(key: string, operation: Function): Operation;
|
|
470
|
+
export { _ };
|
|
471
|
+
export function map(path: string | any[], callback: Function): State;
|
package/types/util/http.d.ts
CHANGED
|
@@ -28,7 +28,7 @@ export function makeBasicAuthHeader(username: any, password: any): {
|
|
|
28
28
|
Authorization: string;
|
|
29
29
|
};
|
|
30
30
|
export function logResponse(response: any): any;
|
|
31
|
-
export function enableMockClient(baseUrl: any): import("undici/types/mock-interceptor").Interceptable;
|
|
31
|
+
export function enableMockClient(baseUrl: any, options?: {}): import("undici/types/mock-interceptor").Interceptable;
|
|
32
32
|
export const ERROR_ABSOLUTE_URL: "Absolute URLs not suppored";
|
|
33
33
|
export function assertRelativeUrl(path: any): void;
|
|
34
34
|
export const ERROR_URL_MISMATCH: "Target origin does not match baseUrl origin";
|