@openfn/language-common 2.3.3 → 2.4.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
@@ -1597,6 +1597,104 @@
1597
1597
  },
1598
1598
  "valid": true
1599
1599
  },
1600
+ {
1601
+ "name": "log",
1602
+ "params": [
1603
+ "args"
1604
+ ],
1605
+ "docs": {
1606
+ "description": "Outputs a message, like calling `console.log`. Use this at the top level of your job code, but not inside callbacks.",
1607
+ "tags": [
1608
+ {
1609
+ "title": "public",
1610
+ "description": null,
1611
+ "type": null
1612
+ },
1613
+ {
1614
+ "title": "function",
1615
+ "description": null,
1616
+ "name": null
1617
+ },
1618
+ {
1619
+ "title": "example",
1620
+ "description": "log('Patient List::', $.patients);",
1621
+ "caption": "Log values from state"
1622
+ },
1623
+ {
1624
+ "title": "example",
1625
+ "description": "fn((state) => {\n console.log(state.data);\n return state;\n})",
1626
+ "caption": "Use console.log inside a callback or fn block"
1627
+ },
1628
+ {
1629
+ "title": "param",
1630
+ "description": "A value or message to display in the logs",
1631
+ "type": {
1632
+ "type": "NameExpression",
1633
+ "name": "any"
1634
+ },
1635
+ "name": "args"
1636
+ },
1637
+ {
1638
+ "title": "returns",
1639
+ "description": null,
1640
+ "type": {
1641
+ "type": "NameExpression",
1642
+ "name": "Operation"
1643
+ }
1644
+ }
1645
+ ]
1646
+ },
1647
+ "valid": true
1648
+ },
1649
+ {
1650
+ "name": "debug",
1651
+ "params": [
1652
+ "args"
1653
+ ],
1654
+ "docs": {
1655
+ "description": "Outputs a message to the console with the debug log level. This is usually filtered out by default. Use this at the top level of your job code, but not inside callbacks.",
1656
+ "tags": [
1657
+ {
1658
+ "title": "public",
1659
+ "description": null,
1660
+ "type": null
1661
+ },
1662
+ {
1663
+ "title": "function",
1664
+ "description": null,
1665
+ "name": null
1666
+ },
1667
+ {
1668
+ "title": "example",
1669
+ "description": "debug('Patient List::', $.patients);",
1670
+ "caption": "Log values from state"
1671
+ },
1672
+ {
1673
+ "title": "example",
1674
+ "description": "fn((state) => {\n console.debug(state.data);\n return state;\n})",
1675
+ "caption": "Use console.debug inside a callback or fn block"
1676
+ },
1677
+ {
1678
+ "title": "param",
1679
+ "description": "A value or message to display in the logs",
1680
+ "type": {
1681
+ "type": "NameExpression",
1682
+ "name": "any"
1683
+ },
1684
+ "name": "args"
1685
+ },
1686
+ {
1687
+ "title": "returns",
1688
+ "description": null,
1689
+ "type": {
1690
+ "type": "NameExpression",
1691
+ "name": "Operation"
1692
+ }
1693
+ }
1694
+ ]
1695
+ },
1696
+ "valid": true
1697
+ },
1600
1698
  {
1601
1699
  "name": "map",
1602
1700
  "params": [
package/dist/index.cjs CHANGED
@@ -37,6 +37,7 @@ __export(src_exports, {
37
37
  dataPath: () => dataPath,
38
38
  dataValue: () => dataValue,
39
39
  dateFns: () => dateFns_exports,
40
+ debug: () => debug,
40
41
  default: () => src_default,
41
42
  each: () => each,
42
43
  execute: () => execute,
@@ -52,6 +53,7 @@ __export(src_exports, {
52
53
  join: () => join,
53
54
  jsonValue: () => jsonValue,
54
55
  lastReferenceValue: () => lastReferenceValue,
56
+ log: () => log,
55
57
  map: () => map,
56
58
  merge: () => merge,
57
59
  parseCsv: () => parseCsv,
@@ -79,6 +81,7 @@ __export(Adaptor_exports, {
79
81
  cursor: () => cursor,
80
82
  dataPath: () => dataPath,
81
83
  dataValue: () => dataValue,
84
+ debug: () => debug,
82
85
  each: () => each,
83
86
  execute: () => execute,
84
87
  expandReferences: () => expandReferences2,
@@ -92,6 +95,7 @@ __export(Adaptor_exports, {
92
95
  join: () => join,
93
96
  jsonValue: () => jsonValue,
94
97
  lastReferenceValue: () => lastReferenceValue,
98
+ log: () => log,
95
99
  map: () => map,
96
100
  merge: () => merge,
97
101
  parseCsv: () => parseCsv,
@@ -194,10 +198,11 @@ var makeBasicAuthHeader = (username, password) => {
194
198
  const credentials = buff.toString("base64");
195
199
  return { Authorization: `Basic ${credentials}` };
196
200
  };
197
- var logResponse = (response) => {
201
+ var logResponse = (response, query = {}) => {
198
202
  const { method, url, statusCode, duration } = response;
199
203
  if (method && url && duration && statusCode) {
200
- const message = `${method} ${url} - ${statusCode} in ${duration}ms`;
204
+ const urlWithQuery = Object.keys(query || {}).length ? `${url}?${new URLSearchParams(query).toString()}` : url;
205
+ const message = `${method} ${urlWithQuery} - ${statusCode} in ${duration}ms`;
201
206
  if (response instanceof Error) {
202
207
  console.error(message);
203
208
  console.error("response body: ");
@@ -353,7 +358,9 @@ function encodeRequestBody(body) {
353
358
  }
354
359
  async function readResponseBody(response, parseAs) {
355
360
  var _a;
356
- const contentLength = ((_a = response.body) == null ? void 0 : _a.readableLength) ?? 0;
361
+ const contentLength = parseInt(
362
+ response.headers["content-length"] ?? ((_a = response.body) == null ? void 0 : _a.readableLength)
363
+ );
357
364
  const contentType = response.headers["content-type"];
358
365
  try {
359
366
  if (Number.isNaN(contentLength) || contentLength === 0) {
@@ -865,6 +872,20 @@ function assert(expression, errorMessage) {
865
872
  return state;
866
873
  };
867
874
  }
875
+ function log(...args) {
876
+ return (state) => {
877
+ const [resolvedArgs] = expandReferences(state, args);
878
+ console.log(...resolvedArgs);
879
+ return state;
880
+ };
881
+ }
882
+ function debug(...args) {
883
+ return (state) => {
884
+ const [resolvedArgs] = expandReferences(state, args);
885
+ console.debug(...resolvedArgs);
886
+ return state;
887
+ };
888
+ }
868
889
 
869
890
  // src/beta.js
870
891
  var beta_exports = {};
@@ -986,6 +1007,7 @@ var src_default = Adaptor_exports;
986
1007
  dataPath,
987
1008
  dataValue,
988
1009
  dateFns,
1010
+ debug,
989
1011
  each,
990
1012
  execute,
991
1013
  expandReferences,
@@ -1000,6 +1022,7 @@ var src_default = Adaptor_exports;
1000
1022
  join,
1001
1023
  jsonValue,
1002
1024
  lastReferenceValue,
1025
+ log,
1003
1026
  map,
1004
1027
  merge,
1005
1028
  parseCsv,
package/dist/index.js CHANGED
@@ -17,6 +17,7 @@ __export(Adaptor_exports, {
17
17
  cursor: () => cursor,
18
18
  dataPath: () => dataPath,
19
19
  dataValue: () => dataValue,
20
+ debug: () => debug,
20
21
  each: () => each,
21
22
  execute: () => execute,
22
23
  expandReferences: () => expandReferences2,
@@ -30,6 +31,7 @@ __export(Adaptor_exports, {
30
31
  join: () => join,
31
32
  jsonValue: () => jsonValue,
32
33
  lastReferenceValue: () => lastReferenceValue,
34
+ log: () => log,
33
35
  map: () => map,
34
36
  merge: () => merge,
35
37
  parseCsv: () => parseCsv,
@@ -132,10 +134,11 @@ var makeBasicAuthHeader = (username, password) => {
132
134
  const credentials = buff.toString("base64");
133
135
  return { Authorization: `Basic ${credentials}` };
134
136
  };
135
- var logResponse = (response) => {
137
+ var logResponse = (response, query = {}) => {
136
138
  const { method, url, statusCode, duration } = response;
137
139
  if (method && url && duration && statusCode) {
138
- const message = `${method} ${url} - ${statusCode} in ${duration}ms`;
140
+ const urlWithQuery = Object.keys(query || {}).length ? `${url}?${new URLSearchParams(query).toString()}` : url;
141
+ const message = `${method} ${urlWithQuery} - ${statusCode} in ${duration}ms`;
139
142
  if (response instanceof Error) {
140
143
  console.error(message);
141
144
  console.error("response body: ");
@@ -291,7 +294,9 @@ function encodeRequestBody(body) {
291
294
  }
292
295
  async function readResponseBody(response, parseAs) {
293
296
  var _a;
294
- const contentLength = ((_a = response.body) == null ? void 0 : _a.readableLength) ?? 0;
297
+ const contentLength = parseInt(
298
+ response.headers["content-length"] ?? ((_a = response.body) == null ? void 0 : _a.readableLength)
299
+ );
295
300
  const contentType = response.headers["content-type"];
296
301
  try {
297
302
  if (Number.isNaN(contentLength) || contentLength === 0) {
@@ -803,6 +808,20 @@ function assert(expression, errorMessage) {
803
808
  return state;
804
809
  };
805
810
  }
811
+ function log(...args) {
812
+ return (state) => {
813
+ const [resolvedArgs] = expandReferences(state, args);
814
+ console.log(...resolvedArgs);
815
+ return state;
816
+ };
817
+ }
818
+ function debug(...args) {
819
+ return (state) => {
820
+ const [resolvedArgs] = expandReferences(state, args);
821
+ console.debug(...resolvedArgs);
822
+ return state;
823
+ };
824
+ }
806
825
 
807
826
  // src/beta.js
808
827
  var beta_exports = {};
@@ -926,6 +945,7 @@ export {
926
945
  dataPath,
927
946
  dataValue,
928
947
  dateFns_exports as dateFns,
948
+ debug,
929
949
  src_default as default,
930
950
  each,
931
951
  execute,
@@ -941,6 +961,7 @@ export {
941
961
  join,
942
962
  jsonValue,
943
963
  lastReferenceValue,
964
+ log,
944
965
  map,
945
966
  merge,
946
967
  parseCsv,
package/dist/util.cjs CHANGED
@@ -103,10 +103,11 @@ var makeBasicAuthHeader = (username, password) => {
103
103
  const credentials = buff.toString("base64");
104
104
  return { Authorization: `Basic ${credentials}` };
105
105
  };
106
- var logResponse = (response) => {
106
+ var logResponse = (response, query = {}) => {
107
107
  const { method, url, statusCode, duration } = response;
108
108
  if (method && url && duration && statusCode) {
109
- const message = `${method} ${url} - ${statusCode} in ${duration}ms`;
109
+ const urlWithQuery = Object.keys(query || {}).length ? `${url}?${new URLSearchParams(query).toString()}` : url;
110
+ const message = `${method} ${urlWithQuery} - ${statusCode} in ${duration}ms`;
110
111
  if (response instanceof Error) {
111
112
  console.error(message);
112
113
  console.error("response body: ");
@@ -262,7 +263,9 @@ function encodeRequestBody(body) {
262
263
  }
263
264
  async function readResponseBody(response, parseAs) {
264
265
  var _a;
265
- const contentLength = ((_a = response.body) == null ? void 0 : _a.readableLength) ?? 0;
266
+ const contentLength = parseInt(
267
+ response.headers["content-length"] ?? ((_a = response.body) == null ? void 0 : _a.readableLength)
268
+ );
266
269
  const contentType = response.headers["content-type"];
267
270
  try {
268
271
  if (Number.isNaN(contentLength) || contentLength === 0) {
package/dist/util.js CHANGED
@@ -54,10 +54,11 @@ var makeBasicAuthHeader = (username, password) => {
54
54
  const credentials = buff.toString("base64");
55
55
  return { Authorization: `Basic ${credentials}` };
56
56
  };
57
- var logResponse = (response) => {
57
+ var logResponse = (response, query = {}) => {
58
58
  const { method, url, statusCode, duration } = response;
59
59
  if (method && url && duration && statusCode) {
60
- const message = `${method} ${url} - ${statusCode} in ${duration}ms`;
60
+ const urlWithQuery = Object.keys(query || {}).length ? `${url}?${new URLSearchParams(query).toString()}` : url;
61
+ const message = `${method} ${urlWithQuery} - ${statusCode} in ${duration}ms`;
61
62
  if (response instanceof Error) {
62
63
  console.error(message);
63
64
  console.error("response body: ");
@@ -213,7 +214,9 @@ function encodeRequestBody(body) {
213
214
  }
214
215
  async function readResponseBody(response, parseAs) {
215
216
  var _a;
216
- const contentLength = ((_a = response.body) == null ? void 0 : _a.readableLength) ?? 0;
217
+ const contentLength = parseInt(
218
+ response.headers["content-length"] ?? ((_a = response.body) == null ? void 0 : _a.readableLength)
219
+ );
217
220
  const contentType = response.headers["content-type"];
218
221
  try {
219
222
  if (Number.isNaN(contentLength) || contentLength === 0) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openfn/language-common",
3
- "version": "2.3.3",
3
+ "version": "2.4.0",
4
4
  "description": "Common Expressions for OpenFn",
5
5
  "homepage": "https://docs.openfn.org",
6
6
  "repository": {
@@ -434,6 +434,37 @@ export function cursor(value: any, options?: {
434
434
  * @returns {operation}
435
435
  */
436
436
  export function assert(expression: any, errorMessage: string): operation;
437
+ /**
438
+ * Outputs a message, like calling `console.log`. Use this at the top level of your job code, but not inside callbacks.
439
+ * @public
440
+ * @function
441
+ * @example <caption>Log values from state</caption>
442
+ * log('Patient List::', $.patients);
443
+ * @example <caption>Use console.log inside a callback or fn block</caption>
444
+ * fn((state) => {
445
+ * console.log(state.data);
446
+ * return state;
447
+ * })
448
+ * @param {any} args - A value or message to display in the logs
449
+ * @returns {Operation}
450
+ */
451
+ export function log(...args: any): Operation;
452
+ /**
453
+ * Outputs a message to the console with the debug log level. This is usually filtered out by default. Use this at the top level of your job code, but not inside callbacks.
454
+ * @public
455
+ * @function
456
+ * @example <caption>Log values from state</caption>
457
+ * debug('Patient List::', $.patients);
458
+ * @example <caption>Use console.debug inside a callback or fn block</caption>
459
+ * fn((state) => {
460
+ * console.debug(state.data);
461
+ * return state;
462
+ * })
463
+
464
+ * @param {any} args - A value or message to display in the logs
465
+ * @returns {Operation}
466
+ */
467
+ export function debug(...args: any): Operation;
437
468
  /**
438
469
  * Scopes an array of data based on a JSONPath.
439
470
  * Useful when the source data has `n` items you would like to map to
@@ -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): any;
30
+ export function logResponse(response: any, query?: {}): 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;