@openfn/language-common 2.0.3 → 2.1.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
@@ -541,7 +541,7 @@
541
541
  "operation"
542
542
  ],
543
543
  "docs": {
544
- "description": "Scopes an array of data based on a JSONPath.\nUseful when the source data has `n` items you would like to map to\nan operation.\nThe operation will receive a slice of the data based of each item\nof the JSONPath provided.\n\nIt also ensures the results of an operation make their way back into\nthe state's references.",
544
+ "description": "Iterates over an array of items and invokes an operation upon each one, where the state\nobject is _scoped_ so that state.data is the item under iteration.\nThe rest of the state object is untouched and can be referenced as usual.\nYou can pass an array directly, or use lazy state or a JSONPath string to\nreference a slice of state.",
545
545
  "tags": [
546
546
  {
547
547
  "title": "public",
@@ -555,7 +555,18 @@
555
555
  },
556
556
  {
557
557
  "title": "example",
558
- "description": "each(\"$.[*]\",\n create(\"SObject\",\n field(\"FirstName\", sourceValue(\"$.firstName\"))\n )\n)"
558
+ "description": "each(\n $.data,\n // Inside the callback operation, `$.data` is scoped to the item under iteration\n insert(\"patient\", {\n patient_name: $.data.properties.case_name,\n patient_id: $.data.case_id,\n })\n);",
559
+ "caption": "Using lazy state ($) to iterate over items in state.data and pass each into an \"insert\" operation"
560
+ },
561
+ {
562
+ "title": "example",
563
+ "description": "each(\n $.data,\n insert(\"patient\", (state) => ({\n patient_id: state.data.case_id,\n ...state.data\n }))\n);",
564
+ "caption": "Iterate over items in state.data and pass each one into an \"insert\" operation"
565
+ },
566
+ {
567
+ "title": "example",
568
+ "description": "each(\n \"$.data[*]\",\n insert(\"patient\", (state) => ({\n patient_name: state.data.properties.case_name,\n patient_id: state.data.case_id,\n }))\n);",
569
+ "caption": "Using JSON path to iterate over items in state.data and pass each one into an \"insert\" operation"
559
570
  },
560
571
  {
561
572
  "title": "param",
package/dist/index.cjs CHANGED
@@ -252,6 +252,9 @@ async function readResponseBody(response, parseAs) {
252
252
  }
253
253
  }
254
254
 
255
+ // src/util/helpers.js
256
+ var import_node_crypto = require("crypto");
257
+
255
258
  // src/util/references.js
256
259
  var import_node_stream2 = require("stream");
257
260
  function expandReferences(state, ...args) {
package/dist/util.cjs CHANGED
@@ -28,8 +28,10 @@ __export(util_exports, {
28
28
  ERROR_ABSOLUTE_URL: () => ERROR_ABSOLUTE_URL,
29
29
  ERROR_URL_MISMATCH: () => ERROR_URL_MISMATCH,
30
30
  assertRelativeUrl: () => assertRelativeUrl,
31
+ decode: () => decode,
31
32
  del: () => del,
32
33
  enableMockClient: () => enableMockClient,
34
+ encode: () => encode,
33
35
  expandReferences: () => expandReferences,
34
36
  get: () => get,
35
37
  logResponse: () => logResponse,
@@ -39,7 +41,9 @@ __export(util_exports, {
39
41
  parseUrl: () => parseUrl,
40
42
  post: () => post,
41
43
  put: () => put,
42
- request: () => request
44
+ request: () => request,
45
+ throwError: () => throw_error_default,
46
+ uuid: () => uuid
43
47
  });
44
48
  module.exports = __toCommonJS(util_exports);
45
49
 
@@ -227,6 +231,12 @@ var post = (url, body, options) => request("POST", url, { body, ...options });
227
231
  var put = (url, body, options) => request("PUT", url, { body, ...options });
228
232
  var del = (url, body, options) => request("DELETE", url, { body, ...options });
229
233
 
234
+ // src/util/helpers.js
235
+ var import_node_crypto = require("crypto");
236
+ var encode = (data) => Buffer.from(data, "utf-8").toString("base64");
237
+ var decode = (base64Data) => Buffer.from(base64Data, "base64").toString("utf-8");
238
+ var uuid = () => (0, import_node_crypto.randomUUID)();
239
+
230
240
  // src/util/references.js
231
241
  var import_node_stream2 = require("stream");
232
242
  function expandReferences(state, ...args) {
@@ -302,13 +312,32 @@ var parse_date_default = (d, startDate) => {
302
312
  }
303
313
  return d;
304
314
  };
315
+
316
+ // src/util/throw-error.js
317
+ var throw_error_default = (code, { description, fix, ...extras } = {}) => {
318
+ const message = description ? `${code}: ${description}` : code;
319
+ const e = new Error(message);
320
+ e.code = code;
321
+ if (description) {
322
+ e.description = description;
323
+ }
324
+ if (fix) {
325
+ e.fix = fix;
326
+ }
327
+ for (const key in extras) {
328
+ e[key] = extras[key];
329
+ }
330
+ throw e;
331
+ };
305
332
  // Annotate the CommonJS export names for ESM import in node:
306
333
  0 && (module.exports = {
307
334
  ERROR_ABSOLUTE_URL,
308
335
  ERROR_URL_MISMATCH,
309
336
  assertRelativeUrl,
337
+ decode,
310
338
  del,
311
339
  enableMockClient,
340
+ encode,
312
341
  expandReferences,
313
342
  get,
314
343
  logResponse,
@@ -318,5 +347,7 @@ var parse_date_default = (d, startDate) => {
318
347
  parseUrl,
319
348
  post,
320
349
  put,
321
- request
350
+ request,
351
+ throwError,
352
+ uuid
322
353
  });
package/dist/util.js CHANGED
@@ -182,6 +182,12 @@ var post = (url, body, options) => request("POST", url, { body, ...options });
182
182
  var put = (url, body, options) => request("PUT", url, { body, ...options });
183
183
  var del = (url, body, options) => request("DELETE", url, { body, ...options });
184
184
 
185
+ // src/util/helpers.js
186
+ import { randomUUID } from "crypto";
187
+ var encode = (data) => Buffer.from(data, "utf-8").toString("base64");
188
+ var decode = (base64Data) => Buffer.from(base64Data, "base64").toString("utf-8");
189
+ var uuid = () => randomUUID();
190
+
185
191
  // src/util/references.js
186
192
  import { Readable as Readable2, Writable } from "stream";
187
193
  function expandReferences(state, ...args) {
@@ -257,12 +263,31 @@ var parse_date_default = (d, startDate) => {
257
263
  }
258
264
  return d;
259
265
  };
266
+
267
+ // src/util/throw-error.js
268
+ var throw_error_default = (code, { description, fix, ...extras } = {}) => {
269
+ const message = description ? `${code}: ${description}` : code;
270
+ const e = new Error(message);
271
+ e.code = code;
272
+ if (description) {
273
+ e.description = description;
274
+ }
275
+ if (fix) {
276
+ e.fix = fix;
277
+ }
278
+ for (const key in extras) {
279
+ e[key] = extras[key];
280
+ }
281
+ throw e;
282
+ };
260
283
  export {
261
284
  ERROR_ABSOLUTE_URL,
262
285
  ERROR_URL_MISMATCH,
263
286
  assertRelativeUrl,
287
+ decode,
264
288
  del,
265
289
  enableMockClient,
290
+ encode,
266
291
  expandReferences,
267
292
  get,
268
293
  logResponse,
@@ -272,5 +297,7 @@ export {
272
297
  parseUrl,
273
298
  post,
274
299
  put,
275
- request
300
+ request,
301
+ throw_error_default as throwError,
302
+ uuid
276
303
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openfn/language-common",
3
- "version": "2.0.3",
3
+ "version": "2.1.1",
4
4
  "description": "Common Expressions for OpenFn",
5
5
  "homepage": "https://docs.openfn.org",
6
6
  "repository": {
@@ -138,22 +138,38 @@ export function lastReferenceValue(path: string): Operation;
138
138
  */
139
139
  export function asData(data: string | object | Function, state: object): any[];
140
140
  /**
141
- * Scopes an array of data based on a JSONPath.
142
- * Useful when the source data has `n` items you would like to map to
143
- * an operation.
144
- * The operation will receive a slice of the data based of each item
145
- * of the JSONPath provided.
146
- *
147
- * It also ensures the results of an operation make their way back into
148
- * the state's references.
141
+ * Iterates over an array of items and invokes an operation upon each one, where the state
142
+ * object is _scoped_ so that state.data is the item under iteration.
143
+ * The rest of the state object is untouched and can be referenced as usual.
144
+ * You can pass an array directly, or use lazy state or a JSONPath string to
145
+ * reference a slice of state.
149
146
  * @public
150
147
  * @function
151
- * @example
152
- * each("$.[*]",
153
- * create("SObject",
154
- * field("FirstName", sourceValue("$.firstName"))
155
- * )
156
- * )
148
+ * @example <caption>Using lazy state ($) to iterate over items in state.data and pass each into an "insert" operation</caption>
149
+ * each(
150
+ * $.data,
151
+ * // Inside the callback operation, `$.data` is scoped to the item under iteration
152
+ * insert("patient", {
153
+ * patient_name: $.data.properties.case_name,
154
+ * patient_id: $.data.case_id,
155
+ * })
156
+ * );
157
+ * @example <caption>Iterate over items in state.data and pass each one into an "insert" operation</caption>
158
+ * each(
159
+ * $.data,
160
+ * insert("patient", (state) => ({
161
+ * patient_id: state.data.case_id,
162
+ * ...state.data
163
+ * }))
164
+ * );
165
+ * @example <caption>Using JSON path to iterate over items in state.data and pass each one into an "insert" operation</caption>
166
+ * each(
167
+ * "$.data[*]",
168
+ * insert("patient", (state) => ({
169
+ * patient_name: state.data.properties.case_name,
170
+ * patient_id: state.data.case_id,
171
+ * }))
172
+ * );
157
173
  * @param {DataSource} dataSource - JSONPath referencing a point in `state`.
158
174
  * @param {Operation} operation - The operation needed to be repeated.
159
175
  * @returns {Operation}
@@ -0,0 +1,3 @@
1
+ export function encode(data: string): string;
2
+ export function decode(base64Data: string): string;
3
+ export function uuid(): string;
@@ -1,4 +1,6 @@
1
1
  export * from "./http";
2
+ export * from "./helpers";
2
3
  export * from "./references";
3
- export { parseDate };
4
4
  import parseDate from "./parse-date";
5
+ import throwError from "./throw-error";
6
+ export { parseDate, throwError };
@@ -0,0 +1,5 @@
1
+ declare function _default(code: any, { description, fix, ...extras }?: {
2
+ description: any;
3
+ fix: any;
4
+ }): never;
5
+ export default _default;