@openfn/language-common 2.4.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 +69 -65
- package/dist/index.cjs +53 -55
- package/dist/index.js +51 -54
- package/dist/util.cjs +12 -7
- package/dist/util.js +12 -7
- package/package.json +3 -3
- package/types/Adaptor.d.ts +9 -25
- package/types/http.d.ts +11 -10
- 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 = {};
|
|
@@ -198,8 +196,8 @@ var makeBasicAuthHeader = (username, password) => {
|
|
|
198
196
|
const credentials = buff.toString("base64");
|
|
199
197
|
return { Authorization: `Basic ${credentials}` };
|
|
200
198
|
};
|
|
201
|
-
var logResponse = (response
|
|
202
|
-
const { method, url, statusCode, duration } = response;
|
|
199
|
+
var logResponse = (response) => {
|
|
200
|
+
const { method, url, statusCode, duration, query } = response;
|
|
203
201
|
if (method && url && duration && statusCode) {
|
|
204
202
|
const urlWithQuery = Object.keys(query || {}).length ? `${url}?${new URLSearchParams(query).toString()}` : url;
|
|
205
203
|
const message = `${method} ${urlWithQuery} - ${statusCode} in ${duration}ms`;
|
|
@@ -311,12 +309,13 @@ async function request(method, fullUrlOrPath, options2 = {}) {
|
|
|
311
309
|
maxRedirections
|
|
312
310
|
} = options2;
|
|
313
311
|
const client = getClient(baseUrl, { tls });
|
|
312
|
+
const queryParams = {
|
|
313
|
+
...optionQuery,
|
|
314
|
+
...urlQuery
|
|
315
|
+
};
|
|
314
316
|
const response = await client.request({
|
|
315
317
|
path: path2,
|
|
316
|
-
query:
|
|
317
|
-
...optionQuery,
|
|
318
|
-
...urlQuery
|
|
319
|
-
},
|
|
318
|
+
query: queryParams,
|
|
320
319
|
method,
|
|
321
320
|
headers,
|
|
322
321
|
body: encodeRequestBody(body),
|
|
@@ -331,7 +330,7 @@ async function request(method, fullUrlOrPath, options2 = {}) {
|
|
|
331
330
|
const responseBody = await readResponseBody(response, parseAs);
|
|
332
331
|
const endTime = Date.now();
|
|
333
332
|
const duration = endTime - startTime;
|
|
334
|
-
|
|
333
|
+
const requestResponse = {
|
|
335
334
|
url,
|
|
336
335
|
method,
|
|
337
336
|
statusCode: response.statusCode,
|
|
@@ -340,6 +339,10 @@ async function request(method, fullUrlOrPath, options2 = {}) {
|
|
|
340
339
|
body: responseBody,
|
|
341
340
|
duration
|
|
342
341
|
};
|
|
342
|
+
if (Object.keys(queryParams).length > 0) {
|
|
343
|
+
requestResponse.query = queryParams;
|
|
344
|
+
}
|
|
345
|
+
return requestResponse;
|
|
343
346
|
}
|
|
344
347
|
function encodeRequestBody(body) {
|
|
345
348
|
if (!body) {
|
|
@@ -527,20 +530,18 @@ function lastReferenceValue(path2) {
|
|
|
527
530
|
const lastReferencePath = referencePath("[0]".concat(".", path2));
|
|
528
531
|
return sourceValue(lastReferencePath);
|
|
529
532
|
}
|
|
530
|
-
var map =
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
}
|
|
543
|
-
});
|
|
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
|
+
};
|
|
544
545
|
function asData(data, state) {
|
|
545
546
|
switch (typeof data) {
|
|
546
547
|
case "string":
|
|
@@ -587,34 +588,16 @@ function join(targetPath, sourcePath, targetKey) {
|
|
|
587
588
|
});
|
|
588
589
|
};
|
|
589
590
|
}
|
|
590
|
-
function expandReferences2(value, skipFilter) {
|
|
591
|
-
return (state) => {
|
|
592
|
-
if (skipFilter && skipFilter(value))
|
|
593
|
-
return value;
|
|
594
|
-
if (Array.isArray(value)) {
|
|
595
|
-
return value.map((v) => expandReferences2(v)(state));
|
|
596
|
-
}
|
|
597
|
-
if (typeof value == "object" && !!value) {
|
|
598
|
-
return Object.keys(value).reduce((acc, key) => {
|
|
599
|
-
return { ...acc, [key]: expandReferences2(value[key])(state) };
|
|
600
|
-
}, {});
|
|
601
|
-
}
|
|
602
|
-
if (typeof value == "function") {
|
|
603
|
-
return expandReferences2(value(state))(state);
|
|
604
|
-
}
|
|
605
|
-
return value;
|
|
606
|
-
};
|
|
607
|
-
}
|
|
608
591
|
function field(key, value) {
|
|
609
592
|
return [key, value];
|
|
610
593
|
}
|
|
611
594
|
function fields(...fields2) {
|
|
612
|
-
return
|
|
595
|
+
return import_lodash2.default.fromPairs(fields2);
|
|
613
596
|
}
|
|
614
597
|
function merge(dataSource, fields2) {
|
|
615
598
|
return (state) => {
|
|
616
599
|
const initialData = source(dataSource)(state);
|
|
617
|
-
const additionalData =
|
|
600
|
+
const [additionalData] = expandReferences(state, fields2);
|
|
618
601
|
return initialData.reduce((acc, dataItem) => {
|
|
619
602
|
return [...acc, { ...dataItem, ...additionalData }];
|
|
620
603
|
}, []);
|
|
@@ -627,8 +610,11 @@ function group(arrayOfObjects, keyPath, callback = (s) => s) {
|
|
|
627
610
|
arrayOfObjects,
|
|
628
611
|
keyPath
|
|
629
612
|
);
|
|
630
|
-
const results =
|
|
631
|
-
|
|
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]) });
|
|
632
618
|
};
|
|
633
619
|
}
|
|
634
620
|
function index() {
|
|
@@ -886,6 +872,17 @@ function debug(...args) {
|
|
|
886
872
|
return state;
|
|
887
873
|
};
|
|
888
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
|
+
}
|
|
889
886
|
|
|
890
887
|
// src/beta.js
|
|
891
888
|
var beta_exports = {};
|
|
@@ -921,7 +918,7 @@ function each2(dataSource, operation) {
|
|
|
921
918
|
// src/http.js
|
|
922
919
|
var http_exports = {};
|
|
923
920
|
__export(http_exports, {
|
|
924
|
-
get: () =>
|
|
921
|
+
get: () => get2,
|
|
925
922
|
options: () => options,
|
|
926
923
|
post: () => post2,
|
|
927
924
|
request: () => req
|
|
@@ -976,11 +973,11 @@ var req = function(method, url, options2) {
|
|
|
976
973
|
};
|
|
977
974
|
};
|
|
978
975
|
};
|
|
979
|
-
function
|
|
976
|
+
function get2(url, options2) {
|
|
980
977
|
return req("GET", url, options2);
|
|
981
978
|
}
|
|
982
|
-
function post2(
|
|
983
|
-
return req("POST",
|
|
979
|
+
function post2(url, data, options2) {
|
|
980
|
+
return req("POST", url, { body: data, ...options2 });
|
|
984
981
|
}
|
|
985
982
|
|
|
986
983
|
// src/dateFns.js
|
|
@@ -995,8 +992,10 @@ var import_date_fns3 = require("date-fns");
|
|
|
995
992
|
var src_default = Adaptor_exports;
|
|
996
993
|
// Annotate the CommonJS export names for ESM import in node:
|
|
997
994
|
0 && (module.exports = {
|
|
995
|
+
_,
|
|
998
996
|
alterState,
|
|
999
997
|
arrayToString,
|
|
998
|
+
as,
|
|
1000
999
|
asData,
|
|
1001
1000
|
assert,
|
|
1002
1001
|
beta,
|
|
@@ -1010,7 +1009,6 @@ var src_default = Adaptor_exports;
|
|
|
1010
1009
|
debug,
|
|
1011
1010
|
each,
|
|
1012
1011
|
execute,
|
|
1013
|
-
expandReferences,
|
|
1014
1012
|
field,
|
|
1015
1013
|
fields,
|
|
1016
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 = {};
|
|
@@ -134,8 +131,8 @@ var makeBasicAuthHeader = (username, password) => {
|
|
|
134
131
|
const credentials = buff.toString("base64");
|
|
135
132
|
return { Authorization: `Basic ${credentials}` };
|
|
136
133
|
};
|
|
137
|
-
var logResponse = (response
|
|
138
|
-
const { method, url, statusCode, duration } = response;
|
|
134
|
+
var logResponse = (response) => {
|
|
135
|
+
const { method, url, statusCode, duration, query } = response;
|
|
139
136
|
if (method && url && duration && statusCode) {
|
|
140
137
|
const urlWithQuery = Object.keys(query || {}).length ? `${url}?${new URLSearchParams(query).toString()}` : url;
|
|
141
138
|
const message = `${method} ${urlWithQuery} - ${statusCode} in ${duration}ms`;
|
|
@@ -247,12 +244,13 @@ async function request(method, fullUrlOrPath, options2 = {}) {
|
|
|
247
244
|
maxRedirections
|
|
248
245
|
} = options2;
|
|
249
246
|
const client = getClient(baseUrl, { tls });
|
|
247
|
+
const queryParams = {
|
|
248
|
+
...optionQuery,
|
|
249
|
+
...urlQuery
|
|
250
|
+
};
|
|
250
251
|
const response = await client.request({
|
|
251
252
|
path: path2,
|
|
252
|
-
query:
|
|
253
|
-
...optionQuery,
|
|
254
|
-
...urlQuery
|
|
255
|
-
},
|
|
253
|
+
query: queryParams,
|
|
256
254
|
method,
|
|
257
255
|
headers,
|
|
258
256
|
body: encodeRequestBody(body),
|
|
@@ -267,7 +265,7 @@ async function request(method, fullUrlOrPath, options2 = {}) {
|
|
|
267
265
|
const responseBody = await readResponseBody(response, parseAs);
|
|
268
266
|
const endTime = Date.now();
|
|
269
267
|
const duration = endTime - startTime;
|
|
270
|
-
|
|
268
|
+
const requestResponse = {
|
|
271
269
|
url,
|
|
272
270
|
method,
|
|
273
271
|
statusCode: response.statusCode,
|
|
@@ -276,6 +274,10 @@ async function request(method, fullUrlOrPath, options2 = {}) {
|
|
|
276
274
|
body: responseBody,
|
|
277
275
|
duration
|
|
278
276
|
};
|
|
277
|
+
if (Object.keys(queryParams).length > 0) {
|
|
278
|
+
requestResponse.query = queryParams;
|
|
279
|
+
}
|
|
280
|
+
return requestResponse;
|
|
279
281
|
}
|
|
280
282
|
function encodeRequestBody(body) {
|
|
281
283
|
if (!body) {
|
|
@@ -463,20 +465,18 @@ function lastReferenceValue(path2) {
|
|
|
463
465
|
const lastReferencePath = referencePath("[0]".concat(".", path2));
|
|
464
466
|
return sourceValue(lastReferencePath);
|
|
465
467
|
}
|
|
466
|
-
var map =
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
}
|
|
479
|
-
});
|
|
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
|
+
};
|
|
480
480
|
function asData(data, state) {
|
|
481
481
|
switch (typeof data) {
|
|
482
482
|
case "string":
|
|
@@ -523,34 +523,16 @@ function join(targetPath, sourcePath, targetKey) {
|
|
|
523
523
|
});
|
|
524
524
|
};
|
|
525
525
|
}
|
|
526
|
-
function expandReferences2(value, skipFilter) {
|
|
527
|
-
return (state) => {
|
|
528
|
-
if (skipFilter && skipFilter(value))
|
|
529
|
-
return value;
|
|
530
|
-
if (Array.isArray(value)) {
|
|
531
|
-
return value.map((v) => expandReferences2(v)(state));
|
|
532
|
-
}
|
|
533
|
-
if (typeof value == "object" && !!value) {
|
|
534
|
-
return Object.keys(value).reduce((acc, key) => {
|
|
535
|
-
return { ...acc, [key]: expandReferences2(value[key])(state) };
|
|
536
|
-
}, {});
|
|
537
|
-
}
|
|
538
|
-
if (typeof value == "function") {
|
|
539
|
-
return expandReferences2(value(state))(state);
|
|
540
|
-
}
|
|
541
|
-
return value;
|
|
542
|
-
};
|
|
543
|
-
}
|
|
544
526
|
function field(key, value) {
|
|
545
527
|
return [key, value];
|
|
546
528
|
}
|
|
547
529
|
function fields(...fields2) {
|
|
548
|
-
return fromPairs(fields2);
|
|
530
|
+
return _2.fromPairs(fields2);
|
|
549
531
|
}
|
|
550
532
|
function merge(dataSource, fields2) {
|
|
551
533
|
return (state) => {
|
|
552
534
|
const initialData = source(dataSource)(state);
|
|
553
|
-
const additionalData =
|
|
535
|
+
const [additionalData] = expandReferences(state, fields2);
|
|
554
536
|
return initialData.reduce((acc, dataItem) => {
|
|
555
537
|
return [...acc, { ...dataItem, ...additionalData }];
|
|
556
538
|
}, []);
|
|
@@ -563,8 +545,11 @@ function group(arrayOfObjects, keyPath, callback = (s) => s) {
|
|
|
563
545
|
arrayOfObjects,
|
|
564
546
|
keyPath
|
|
565
547
|
);
|
|
566
|
-
const results = groupBy(
|
|
567
|
-
|
|
548
|
+
const results = _2.groupBy(
|
|
549
|
+
resolvedArray,
|
|
550
|
+
(item) => _2.get(item, resolvedKeyPath)
|
|
551
|
+
);
|
|
552
|
+
return callback({ ...state, data: _2.omit(results, [void 0]) });
|
|
568
553
|
};
|
|
569
554
|
}
|
|
570
555
|
function index() {
|
|
@@ -822,6 +807,17 @@ function debug(...args) {
|
|
|
822
807
|
return state;
|
|
823
808
|
};
|
|
824
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
|
+
}
|
|
825
821
|
|
|
826
822
|
// src/beta.js
|
|
827
823
|
var beta_exports = {};
|
|
@@ -857,7 +853,7 @@ function each2(dataSource, operation) {
|
|
|
857
853
|
// src/http.js
|
|
858
854
|
var http_exports = {};
|
|
859
855
|
__export(http_exports, {
|
|
860
|
-
get: () =>
|
|
856
|
+
get: () => get2,
|
|
861
857
|
options: () => options,
|
|
862
858
|
post: () => post2,
|
|
863
859
|
request: () => req
|
|
@@ -912,11 +908,11 @@ var req = function(method, url, options2) {
|
|
|
912
908
|
};
|
|
913
909
|
};
|
|
914
910
|
};
|
|
915
|
-
function
|
|
911
|
+
function get2(url, options2) {
|
|
916
912
|
return req("GET", url, options2);
|
|
917
913
|
}
|
|
918
|
-
function post2(
|
|
919
|
-
return req("POST",
|
|
914
|
+
function post2(url, data, options2) {
|
|
915
|
+
return req("POST", url, { body: data, ...options2 });
|
|
920
916
|
}
|
|
921
917
|
|
|
922
918
|
// src/dateFns.js
|
|
@@ -933,8 +929,10 @@ import {
|
|
|
933
929
|
// src/index.js
|
|
934
930
|
var src_default = Adaptor_exports;
|
|
935
931
|
export {
|
|
932
|
+
_2 as _,
|
|
936
933
|
alterState,
|
|
937
934
|
arrayToString,
|
|
935
|
+
as,
|
|
938
936
|
asData,
|
|
939
937
|
assert,
|
|
940
938
|
beta_exports as beta,
|
|
@@ -949,7 +947,6 @@ export {
|
|
|
949
947
|
src_default as default,
|
|
950
948
|
each,
|
|
951
949
|
execute,
|
|
952
|
-
expandReferences2 as expandReferences,
|
|
953
950
|
field,
|
|
954
951
|
fields,
|
|
955
952
|
fn,
|
package/dist/util.cjs
CHANGED
|
@@ -103,8 +103,8 @@ var makeBasicAuthHeader = (username, password) => {
|
|
|
103
103
|
const credentials = buff.toString("base64");
|
|
104
104
|
return { Authorization: `Basic ${credentials}` };
|
|
105
105
|
};
|
|
106
|
-
var logResponse = (response
|
|
107
|
-
const { method, url, statusCode, duration } = response;
|
|
106
|
+
var logResponse = (response) => {
|
|
107
|
+
const { method, url, statusCode, duration, query } = response;
|
|
108
108
|
if (method && url && duration && statusCode) {
|
|
109
109
|
const urlWithQuery = Object.keys(query || {}).length ? `${url}?${new URLSearchParams(query).toString()}` : url;
|
|
110
110
|
const message = `${method} ${urlWithQuery} - ${statusCode} in ${duration}ms`;
|
|
@@ -216,12 +216,13 @@ async function request(method, fullUrlOrPath, options = {}) {
|
|
|
216
216
|
maxRedirections
|
|
217
217
|
} = options;
|
|
218
218
|
const client = getClient(baseUrl, { tls });
|
|
219
|
+
const queryParams = {
|
|
220
|
+
...optionQuery,
|
|
221
|
+
...urlQuery
|
|
222
|
+
};
|
|
219
223
|
const response = await client.request({
|
|
220
224
|
path: path2,
|
|
221
|
-
query:
|
|
222
|
-
...optionQuery,
|
|
223
|
-
...urlQuery
|
|
224
|
-
},
|
|
225
|
+
query: queryParams,
|
|
225
226
|
method,
|
|
226
227
|
headers,
|
|
227
228
|
body: encodeRequestBody(body),
|
|
@@ -236,7 +237,7 @@ async function request(method, fullUrlOrPath, options = {}) {
|
|
|
236
237
|
const responseBody = await readResponseBody(response, parseAs);
|
|
237
238
|
const endTime = Date.now();
|
|
238
239
|
const duration = endTime - startTime;
|
|
239
|
-
|
|
240
|
+
const requestResponse = {
|
|
240
241
|
url,
|
|
241
242
|
method,
|
|
242
243
|
statusCode: response.statusCode,
|
|
@@ -245,6 +246,10 @@ async function request(method, fullUrlOrPath, options = {}) {
|
|
|
245
246
|
body: responseBody,
|
|
246
247
|
duration
|
|
247
248
|
};
|
|
249
|
+
if (Object.keys(queryParams).length > 0) {
|
|
250
|
+
requestResponse.query = queryParams;
|
|
251
|
+
}
|
|
252
|
+
return requestResponse;
|
|
248
253
|
}
|
|
249
254
|
function encodeRequestBody(body) {
|
|
250
255
|
if (!body) {
|
package/dist/util.js
CHANGED
|
@@ -54,8 +54,8 @@ var makeBasicAuthHeader = (username, password) => {
|
|
|
54
54
|
const credentials = buff.toString("base64");
|
|
55
55
|
return { Authorization: `Basic ${credentials}` };
|
|
56
56
|
};
|
|
57
|
-
var logResponse = (response
|
|
58
|
-
const { method, url, statusCode, duration } = response;
|
|
57
|
+
var logResponse = (response) => {
|
|
58
|
+
const { method, url, statusCode, duration, query } = response;
|
|
59
59
|
if (method && url && duration && statusCode) {
|
|
60
60
|
const urlWithQuery = Object.keys(query || {}).length ? `${url}?${new URLSearchParams(query).toString()}` : url;
|
|
61
61
|
const message = `${method} ${urlWithQuery} - ${statusCode} in ${duration}ms`;
|
|
@@ -167,12 +167,13 @@ async function request(method, fullUrlOrPath, options = {}) {
|
|
|
167
167
|
maxRedirections
|
|
168
168
|
} = options;
|
|
169
169
|
const client = getClient(baseUrl, { tls });
|
|
170
|
+
const queryParams = {
|
|
171
|
+
...optionQuery,
|
|
172
|
+
...urlQuery
|
|
173
|
+
};
|
|
170
174
|
const response = await client.request({
|
|
171
175
|
path: path2,
|
|
172
|
-
query:
|
|
173
|
-
...optionQuery,
|
|
174
|
-
...urlQuery
|
|
175
|
-
},
|
|
176
|
+
query: queryParams,
|
|
176
177
|
method,
|
|
177
178
|
headers,
|
|
178
179
|
body: encodeRequestBody(body),
|
|
@@ -187,7 +188,7 @@ async function request(method, fullUrlOrPath, options = {}) {
|
|
|
187
188
|
const responseBody = await readResponseBody(response, parseAs);
|
|
188
189
|
const endTime = Date.now();
|
|
189
190
|
const duration = endTime - startTime;
|
|
190
|
-
|
|
191
|
+
const requestResponse = {
|
|
191
192
|
url,
|
|
192
193
|
method,
|
|
193
194
|
statusCode: response.statusCode,
|
|
@@ -196,6 +197,10 @@ async function request(method, fullUrlOrPath, options = {}) {
|
|
|
196
197
|
body: responseBody,
|
|
197
198
|
duration
|
|
198
199
|
};
|
|
200
|
+
if (Object.keys(queryParams).length > 0) {
|
|
201
|
+
requestResponse.query = queryParams;
|
|
202
|
+
}
|
|
203
|
+
return requestResponse;
|
|
199
204
|
}
|
|
200
205
|
function encodeRequestBody(body) {
|
|
201
206
|
if (!body) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openfn/language-common",
|
|
3
|
-
"
|
|
3
|
+
"label": "Common",
|
|
4
|
+
"version": "3.0.0",
|
|
4
5
|
"description": "Common Expressions for OpenFn",
|
|
5
6
|
"homepage": "https://docs.openfn.org",
|
|
6
7
|
"repository": {
|
|
@@ -41,12 +42,11 @@
|
|
|
41
42
|
"http-status-codes": "^2.3.0",
|
|
42
43
|
"jsonpath-plus": "^10.3.0",
|
|
43
44
|
"lodash": "^4.17.21",
|
|
44
|
-
"undici": "^5.
|
|
45
|
+
"undici": "^5.29.0"
|
|
45
46
|
},
|
|
46
47
|
"devDependencies": {
|
|
47
48
|
"chai": "4.3.6",
|
|
48
49
|
"deep-eql": "4.1.1",
|
|
49
|
-
"esno": "^0.16.3",
|
|
50
50
|
"nock": "13.2.9",
|
|
51
51
|
"rimraf": "^3.0.2"
|
|
52
52
|
},
|
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/http.d.ts
CHANGED
|
@@ -3,14 +3,14 @@
|
|
|
3
3
|
* easily add commonly used options. The return object is chainable so you can set
|
|
4
4
|
* as many options as you want.
|
|
5
5
|
* Pass an object to set your own options.
|
|
6
|
-
* @param {
|
|
6
|
+
* @param {OptionsHelpers} opts - options to pass to the request
|
|
7
7
|
* @returns {OptionsHelpers}
|
|
8
8
|
* @function
|
|
9
9
|
* @public
|
|
10
|
-
* @example <caption>Get with a query an
|
|
11
|
-
* get($.data.url, http.options({ query: $.query }).
|
|
10
|
+
* @example <caption>Get with a query an oauth token</caption>
|
|
11
|
+
* http.get($.data.url, http.options({ query: $.query }).oauth($.configuration.access_token))
|
|
12
12
|
*/
|
|
13
|
-
export function options(opts?:
|
|
13
|
+
export function options(opts?: any): any;
|
|
14
14
|
/**
|
|
15
15
|
* Make a GET request.
|
|
16
16
|
* @public
|
|
@@ -38,17 +38,18 @@ export function get(url: string, options: CommonRequestOptions): Operation;
|
|
|
38
38
|
* @public
|
|
39
39
|
* @function
|
|
40
40
|
* @example <caption>Post a JSON object (setting the content-type header)</caption>
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
41
|
+
* http.post(
|
|
42
|
+
* "https://jsonplaceholder.typicode.com/todos",
|
|
43
|
+
* $.data,
|
|
44
|
+
* http.options().json()
|
|
45
|
+
* );
|
|
46
46
|
* @param {string} url - URL to access
|
|
47
|
+
* @param {object} data - Body data to append to the request.
|
|
47
48
|
* @param {CommonRequestOptions} options - Request options
|
|
48
49
|
* @state {CommonHttpState}
|
|
49
50
|
* @returns {Operation}
|
|
50
51
|
*/
|
|
51
|
-
export function post(
|
|
52
|
+
export function post(url: string, data: object, options: CommonRequestOptions): Operation;
|
|
52
53
|
export { req as request };
|
|
53
54
|
/**
|
|
54
55
|
* Helper functions provided by `http.options`.
|
package/types/util/http.d.ts
CHANGED
|
@@ -27,7 +27,7 @@ export function request(method: any, fullUrlOrPath: any, options?: {}): Promise<
|
|
|
27
27
|
export function makeBasicAuthHeader(username: any, password: any): {
|
|
28
28
|
Authorization: string;
|
|
29
29
|
};
|
|
30
|
-
export function logResponse(response: any
|
|
30
|
+
export function logResponse(response: any): any;
|
|
31
31
|
export function enableMockClient(baseUrl: any): 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;
|