@openfn/language-common 2.2.1 → 2.3.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 CHANGED
@@ -1544,6 +1544,59 @@
1544
1544
  },
1545
1545
  "valid": false
1546
1546
  },
1547
+ {
1548
+ "name": "assert",
1549
+ "params": [
1550
+ "expression",
1551
+ "errorMessage"
1552
+ ],
1553
+ "docs": {
1554
+ "description": "Asserts the given expression or function resolves to `true`, or else throws an exception. Optionally accepts and error message.",
1555
+ "tags": [
1556
+ {
1557
+ "title": "public",
1558
+ "description": null,
1559
+ "type": null
1560
+ },
1561
+ {
1562
+ "title": "function",
1563
+ "description": null,
1564
+ "name": null
1565
+ },
1566
+ {
1567
+ "title": "example",
1568
+ "description": "assert('a' === 'b', '\"a\" is not equal to \"b\"')"
1569
+ },
1570
+ {
1571
+ "title": "param",
1572
+ "description": "The expression or function to be evaluated.",
1573
+ "type": {
1574
+ "type": "NameExpression",
1575
+ "name": "any"
1576
+ },
1577
+ "name": "expression"
1578
+ },
1579
+ {
1580
+ "title": "param",
1581
+ "description": "The error message thrown in case of a failed state.",
1582
+ "type": {
1583
+ "type": "NameExpression",
1584
+ "name": "string"
1585
+ },
1586
+ "name": "errorMessage"
1587
+ },
1588
+ {
1589
+ "title": "returns",
1590
+ "description": null,
1591
+ "type": {
1592
+ "type": "NameExpression",
1593
+ "name": "operation"
1594
+ }
1595
+ }
1596
+ ]
1597
+ },
1598
+ "valid": true
1599
+ },
1547
1600
  {
1548
1601
  "name": "map",
1549
1602
  "params": [
package/dist/index.cjs CHANGED
@@ -28,6 +28,7 @@ __export(src_exports, {
28
28
  alterState: () => alterState,
29
29
  arrayToString: () => arrayToString,
30
30
  asData: () => asData,
31
+ assert: () => assert,
31
32
  beta: () => beta_exports,
32
33
  chunk: () => chunk,
33
34
  combine: () => combine,
@@ -71,6 +72,7 @@ __export(Adaptor_exports, {
71
72
  alterState: () => alterState,
72
73
  arrayToString: () => arrayToString,
73
74
  asData: () => asData,
75
+ assert: () => assert,
74
76
  chunk: () => chunk,
75
77
  combine: () => combine,
76
78
  composeNextState: () => composeNextState,
@@ -198,6 +200,9 @@ var enableMockClient = (baseUrl) => {
198
200
  return client;
199
201
  };
200
202
  var assertOK = async (response, errorMap, fullUrl, method, startTime) => {
203
+ if (errorMap === false) {
204
+ return;
205
+ }
201
206
  const errMapMessage = errorMap[response.statusCode];
202
207
  const isError = typeof errMapMessage === "boolean" ? errMapMessage : errMapMessage || response.statusCode >= 400;
203
208
  if (isError) {
@@ -369,7 +374,8 @@ var isStream = (value) => {
369
374
  return false;
370
375
  };
371
376
  function expandReference(state, value) {
372
- if (Buffer.isBuffer(value)) {
377
+ var _a;
378
+ if (Buffer.isBuffer(value) || ((_a = value == null ? void 0 : value.constructor) == null ? void 0 : _a.name) === "RegExp") {
373
379
  return value;
374
380
  }
375
381
  if (Array.isArray(value)) {
@@ -429,8 +435,29 @@ var parse_date_default = (d, startDate) => {
429
435
  };
430
436
 
431
437
  // src/util/base64.js
432
- var encode = (data) => Buffer.from(data, "utf-8").toString("base64");
433
- var decode = (base64Data) => Buffer.from(base64Data, "base64").toString("utf-8");
438
+ var import_lodash = __toESM(require("lodash"), 1);
439
+ var encode = (data, options2 = { parseJson: true }) => {
440
+ let str = data;
441
+ if (typeof data !== "string" && options2.parseJson) {
442
+ try {
443
+ str = JSON.stringify(str);
444
+ } catch (e) {
445
+ console.log(e.message);
446
+ }
447
+ }
448
+ return Buffer.from(str, "utf-8").toString("base64");
449
+ };
450
+ var decode = (base64Data, options2 = { parseJson: true }) => {
451
+ let decodedValue = Buffer.from(base64Data, "base64").toString("utf-8");
452
+ if ((import_lodash.default.startsWith(decodedValue, "[") || import_lodash.default.startsWith(decodedValue, "{")) && options2.parseJson) {
453
+ try {
454
+ decodedValue = JSON.parse(decodedValue);
455
+ } catch (e) {
456
+ console.log(e.message);
457
+ }
458
+ }
459
+ return decodedValue;
460
+ };
434
461
 
435
462
  // src/util/uuid.js
436
463
  var import_node_crypto = require("crypto");
@@ -818,6 +845,21 @@ function cursor(value, options2 = {}) {
818
845
  return state;
819
846
  };
820
847
  }
848
+ function assert(expression, errorMessage) {
849
+ return (state) => {
850
+ const [resolvedValue, resolvedErrorMessage] = expandReferences(
851
+ state,
852
+ expression,
853
+ errorMessage
854
+ );
855
+ if (!resolvedValue) {
856
+ throw new Error(
857
+ resolvedErrorMessage || `assertion statement failed with ${resolvedValue}`
858
+ );
859
+ }
860
+ return state;
861
+ };
862
+ }
821
863
 
822
864
  // src/beta.js
823
865
  var beta_exports = {};
@@ -930,6 +972,7 @@ var src_default = Adaptor_exports;
930
972
  alterState,
931
973
  arrayToString,
932
974
  asData,
975
+ assert,
933
976
  beta,
934
977
  chunk,
935
978
  combine,
package/dist/index.js CHANGED
@@ -10,6 +10,7 @@ __export(Adaptor_exports, {
10
10
  alterState: () => alterState,
11
11
  arrayToString: () => arrayToString,
12
12
  asData: () => asData,
13
+ assert: () => assert,
13
14
  chunk: () => chunk,
14
15
  combine: () => combine,
15
16
  composeNextState: () => composeNextState,
@@ -137,6 +138,9 @@ var enableMockClient = (baseUrl) => {
137
138
  return client;
138
139
  };
139
140
  var assertOK = async (response, errorMap, fullUrl, method, startTime) => {
141
+ if (errorMap === false) {
142
+ return;
143
+ }
140
144
  const errMapMessage = errorMap[response.statusCode];
141
145
  const isError = typeof errMapMessage === "boolean" ? errMapMessage : errMapMessage || response.statusCode >= 400;
142
146
  if (isError) {
@@ -308,7 +312,8 @@ var isStream = (value) => {
308
312
  return false;
309
313
  };
310
314
  function expandReference(state, value) {
311
- if (Buffer.isBuffer(value)) {
315
+ var _a;
316
+ if (Buffer.isBuffer(value) || ((_a = value == null ? void 0 : value.constructor) == null ? void 0 : _a.name) === "RegExp") {
312
317
  return value;
313
318
  }
314
319
  if (Array.isArray(value)) {
@@ -368,8 +373,29 @@ var parse_date_default = (d, startDate) => {
368
373
  };
369
374
 
370
375
  // src/util/base64.js
371
- var encode = (data) => Buffer.from(data, "utf-8").toString("base64");
372
- var decode = (base64Data) => Buffer.from(base64Data, "base64").toString("utf-8");
376
+ import _ from "lodash";
377
+ var encode = (data, options2 = { parseJson: true }) => {
378
+ let str = data;
379
+ if (typeof data !== "string" && options2.parseJson) {
380
+ try {
381
+ str = JSON.stringify(str);
382
+ } catch (e) {
383
+ console.log(e.message);
384
+ }
385
+ }
386
+ return Buffer.from(str, "utf-8").toString("base64");
387
+ };
388
+ var decode = (base64Data, options2 = { parseJson: true }) => {
389
+ let decodedValue = Buffer.from(base64Data, "base64").toString("utf-8");
390
+ if ((_.startsWith(decodedValue, "[") || _.startsWith(decodedValue, "{")) && options2.parseJson) {
391
+ try {
392
+ decodedValue = JSON.parse(decodedValue);
393
+ } catch (e) {
394
+ console.log(e.message);
395
+ }
396
+ }
397
+ return decodedValue;
398
+ };
373
399
 
374
400
  // src/util/uuid.js
375
401
  import { randomUUID } from "crypto";
@@ -757,6 +783,21 @@ function cursor(value, options2 = {}) {
757
783
  return state;
758
784
  };
759
785
  }
786
+ function assert(expression, errorMessage) {
787
+ return (state) => {
788
+ const [resolvedValue, resolvedErrorMessage] = expandReferences(
789
+ state,
790
+ expression,
791
+ errorMessage
792
+ );
793
+ if (!resolvedValue) {
794
+ throw new Error(
795
+ resolvedErrorMessage || `assertion statement failed with ${resolvedValue}`
796
+ );
797
+ }
798
+ return state;
799
+ };
800
+ }
760
801
 
761
802
  // src/beta.js
762
803
  var beta_exports = {};
@@ -871,6 +912,7 @@ export {
871
912
  alterState,
872
913
  arrayToString,
873
914
  asData,
915
+ assert,
874
916
  beta_exports as beta,
875
917
  chunk,
876
918
  combine,
package/dist/util.cjs CHANGED
@@ -109,6 +109,9 @@ var enableMockClient = (baseUrl) => {
109
109
  return client;
110
110
  };
111
111
  var assertOK = async (response, errorMap, fullUrl, method, startTime) => {
112
+ if (errorMap === false) {
113
+ return;
114
+ }
112
115
  const errMapMessage = errorMap[response.statusCode];
113
116
  const isError = typeof errMapMessage === "boolean" ? errMapMessage : errMapMessage || response.statusCode >= 400;
114
117
  if (isError) {
@@ -280,7 +283,8 @@ var isStream = (value) => {
280
283
  return false;
281
284
  };
282
285
  function expandReference(state, value) {
283
- if (Buffer.isBuffer(value)) {
286
+ var _a;
287
+ if (Buffer.isBuffer(value) || ((_a = value == null ? void 0 : value.constructor) == null ? void 0 : _a.name) === "RegExp") {
284
288
  return value;
285
289
  }
286
290
  if (Array.isArray(value)) {
@@ -340,8 +344,29 @@ var parse_date_default = (d, startDate) => {
340
344
  };
341
345
 
342
346
  // src/util/base64.js
343
- var encode = (data) => Buffer.from(data, "utf-8").toString("base64");
344
- var decode = (base64Data) => Buffer.from(base64Data, "base64").toString("utf-8");
347
+ var import_lodash = __toESM(require("lodash"), 1);
348
+ var encode = (data, options = { parseJson: true }) => {
349
+ let str = data;
350
+ if (typeof data !== "string" && options.parseJson) {
351
+ try {
352
+ str = JSON.stringify(str);
353
+ } catch (e) {
354
+ console.log(e.message);
355
+ }
356
+ }
357
+ return Buffer.from(str, "utf-8").toString("base64");
358
+ };
359
+ var decode = (base64Data, options = { parseJson: true }) => {
360
+ let decodedValue = Buffer.from(base64Data, "base64").toString("utf-8");
361
+ if ((import_lodash.default.startsWith(decodedValue, "[") || import_lodash.default.startsWith(decodedValue, "{")) && options.parseJson) {
362
+ try {
363
+ decodedValue = JSON.parse(decodedValue);
364
+ } catch (e) {
365
+ console.log(e.message);
366
+ }
367
+ }
368
+ return decodedValue;
369
+ };
345
370
 
346
371
  // src/util/uuid.js
347
372
  var import_node_crypto = require("crypto");
package/dist/util.js CHANGED
@@ -60,6 +60,9 @@ var enableMockClient = (baseUrl) => {
60
60
  return client;
61
61
  };
62
62
  var assertOK = async (response, errorMap, fullUrl, method, startTime) => {
63
+ if (errorMap === false) {
64
+ return;
65
+ }
63
66
  const errMapMessage = errorMap[response.statusCode];
64
67
  const isError = typeof errMapMessage === "boolean" ? errMapMessage : errMapMessage || response.statusCode >= 400;
65
68
  if (isError) {
@@ -231,7 +234,8 @@ var isStream = (value) => {
231
234
  return false;
232
235
  };
233
236
  function expandReference(state, value) {
234
- if (Buffer.isBuffer(value)) {
237
+ var _a;
238
+ if (Buffer.isBuffer(value) || ((_a = value == null ? void 0 : value.constructor) == null ? void 0 : _a.name) === "RegExp") {
235
239
  return value;
236
240
  }
237
241
  if (Array.isArray(value)) {
@@ -291,8 +295,29 @@ var parse_date_default = (d, startDate) => {
291
295
  };
292
296
 
293
297
  // src/util/base64.js
294
- var encode = (data) => Buffer.from(data, "utf-8").toString("base64");
295
- var decode = (base64Data) => Buffer.from(base64Data, "base64").toString("utf-8");
298
+ import _ from "lodash";
299
+ var encode = (data, options = { parseJson: true }) => {
300
+ let str = data;
301
+ if (typeof data !== "string" && options.parseJson) {
302
+ try {
303
+ str = JSON.stringify(str);
304
+ } catch (e) {
305
+ console.log(e.message);
306
+ }
307
+ }
308
+ return Buffer.from(str, "utf-8").toString("base64");
309
+ };
310
+ var decode = (base64Data, options = { parseJson: true }) => {
311
+ let decodedValue = Buffer.from(base64Data, "base64").toString("utf-8");
312
+ if ((_.startsWith(decodedValue, "[") || _.startsWith(decodedValue, "{")) && options.parseJson) {
313
+ try {
314
+ decodedValue = JSON.parse(decodedValue);
315
+ } catch (e) {
316
+ console.log(e.message);
317
+ }
318
+ }
319
+ return decodedValue;
320
+ };
296
321
 
297
322
  // src/util/uuid.js
298
323
  import { randomUUID } from "crypto";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openfn/language-common",
3
- "version": "2.2.1",
3
+ "version": "2.3.1",
4
4
  "description": "Common Expressions for OpenFn",
5
5
  "homepage": "https://docs.openfn.org",
6
6
  "repository": {
@@ -41,10 +41,9 @@
41
41
  "http-status-codes": "^2.3.0",
42
42
  "jsonpath-plus": "^10.2.0",
43
43
  "lodash": "^4.17.21",
44
- "undici": "^5.28.4"
44
+ "undici": "^5.28.5"
45
45
  },
46
46
  "devDependencies": {
47
- "@openfn/simple-ast": "0.4.1",
48
47
  "chai": "4.3.6",
49
48
  "deep-eql": "4.1.1",
50
49
  "esno": "^0.16.3",
@@ -423,6 +423,17 @@ export function cursor(value: any, options?: {
423
423
  defaultValue: any;
424
424
  format: Function;
425
425
  }): Operation;
426
+ /**
427
+ * Asserts the given expression or function resolves to `true`, or else throws an exception. Optionally accepts and error message.
428
+ * @public
429
+ * @function
430
+ * @example
431
+ * assert('a' === 'b', '"a" is not equal to "b"')
432
+ * @param {any} expression - The expression or function to be evaluated.
433
+ * @param {string} errorMessage - The error message thrown in case of a failed state.
434
+ * @returns {operation}
435
+ */
436
+ export function assert(expression: any, errorMessage: string): operation;
426
437
  /**
427
438
  * Scopes an array of data based on a JSONPath.
428
439
  * Useful when the source data has `n` items you would like to map to
package/types/http.d.ts CHANGED
@@ -61,7 +61,7 @@ export type CommonRequestOptions = {
61
61
  /**
62
62
  * - Map of errorCodes -> error messages, ie, `{ 404: 'Resource not found;' }`. Pass `false` to suppress errors.
63
63
  */
64
- errors: object;
64
+ errors: object | boolean;
65
65
  /**
66
66
  * - Pass a JSON object to be serialised into a multipart HTML form (as FormData) in the body.
67
67
  */
@@ -94,7 +94,7 @@ export type CommonHttpState = any;
94
94
  /**
95
95
  * Options provided to the HTTP request
96
96
  * @typedef {Object} CommonRequestOptions
97
- * @property {object} errors - Map of errorCodes -> error messages, ie, `{ 404: 'Resource not found;' }`. Pass `false` to suppress errors.
97
+ * @property {object|boolean} errors - Map of errorCodes -> error messages, ie, `{ 404: 'Resource not found;' }`. Pass `false` to suppress errors.
98
98
  * @property {object} form - Pass a JSON object to be serialised into a multipart HTML form (as FormData) in the body.
99
99
  * @property {object} query - An object of query parameters to be encoded into the URL.
100
100
  * @property {object} headers - An object of headers to append to the request.
@@ -1,2 +1,6 @@
1
- export function encode(data: string): string;
2
- export function decode(base64Data: string): string;
1
+ export function encode(data: string | object, options?: {
2
+ parseJson?: boolean;
3
+ }): string;
4
+ export function decode(base64Data: string, options?: {
5
+ parseJson?: boolean;
6
+ }): string | object;