@openfn/language-common 1.13.2 → 1.13.4

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
@@ -1319,7 +1319,7 @@
1319
1319
  "options"
1320
1320
  ],
1321
1321
  "docs": {
1322
- "description": "Sets a cursor property on state.\nSupports natural language dates like `now`, `today`, `yesterday`, `n hours ago`, `n days ago`, and `start`,\nwhich will be converted relative to the environment (ie, the Lightning or CLI locale). Custom timezones \nare not yet supported.\nSee the usage guide at {@link https://docs.openfn.org/documentation/jobs/job-writing-guide#using-cursors}",
1322
+ "description": "Sets a cursor property on state.\nSupports natural language dates like `now`, `today`, `yesterday`, `n hours ago`, `n days ago`, and `start`,\nwhich will be converted relative to the environment (ie, the Lightning or CLI locale). Custom timezones \nare not yet supported.\nYou can provide a formatter to customise the final cursor value, which is useful for normalising\ndifferent inputs. The custom formatter runs after natural language date conversion.\nSee the usage guide at {@link https://docs.openfn.org/documentation/jobs/job-writing-guide#using-cursors}",
1323
1323
  "tags": [
1324
1324
  {
1325
1325
  "title": "public",
@@ -1377,6 +1377,15 @@
1377
1377
  },
1378
1378
  "name": "options.defaultValue"
1379
1379
  },
1380
+ {
1381
+ "title": "param",
1382
+ "description": "custom formatter for the final cursor value",
1383
+ "type": {
1384
+ "type": "NameExpression",
1385
+ "name": "Function"
1386
+ },
1387
+ "name": "options.format"
1388
+ },
1380
1389
  {
1381
1390
  "title": "returns",
1382
1391
  "description": null,
package/dist/index.cjs CHANGED
@@ -105,7 +105,7 @@ var import_jsonpath_plus = require("jsonpath-plus");
105
105
  var import_csv_parse = require("csv-parse");
106
106
  var import_node_stream3 = require("stream");
107
107
  var import_undici2 = require("undici");
108
- var import_date_fns3 = require("date-fns");
108
+ var import_date_fns3 = __toESM(require("date-fns"), 1);
109
109
 
110
110
  // src/util/http.js
111
111
  var import_undici = require("undici");
@@ -645,7 +645,8 @@ var cursorStart = void 0;
645
645
  var cursorKey = "cursor";
646
646
  function cursor(value, options2 = {}) {
647
647
  return (state) => {
648
- const [resolvedValue, resolvedOptions] = expandReferences(state, value, options2);
648
+ const { format: format2, ...optionsWithoutFormat } = options2;
649
+ const [resolvedValue, resolvedOptions] = expandReferences(state, value, optionsWithoutFormat);
649
650
  const {
650
651
  defaultValue,
651
652
  key
@@ -660,14 +661,14 @@ function cursor(value, options2 = {}) {
660
661
  if (typeof cursor2 === "string") {
661
662
  const date = parse_date_default(cursor2, cursorStart);
662
663
  if (date instanceof Date && date.toString !== "Invalid Date") {
663
- state[cursorKey] = date.toISOString();
664
- const formatted = (0, import_date_fns3.format)(date, "HH:MM d MMM yyyy (OOO)");
664
+ state[cursorKey] = (format2 == null ? void 0 : format2(date)) ?? date.toISOString();
665
+ const formatted = format2 ? state[cursorKey] : import_date_fns3.default.format(date, "HH:MM d MMM yyyy (OOO)");
665
666
  console.log(`Setting cursor "${cursor2}" to: ${formatted}`);
666
667
  return state;
667
668
  }
668
669
  }
669
- state[cursorKey] = cursor2;
670
- console.log("Setting cursor to:", cursor2);
670
+ state[cursorKey] = (format2 == null ? void 0 : format2(cursor2)) ?? cursor2;
671
+ console.log("Setting cursor to:", state[cursorKey]);
671
672
  return state;
672
673
  };
673
674
  }
package/dist/index.js CHANGED
@@ -47,7 +47,7 @@ import { JSONPath } from "jsonpath-plus";
47
47
  import { parse as parse2 } from "csv-parse";
48
48
  import { Readable as Readable2 } from "stream";
49
49
  import { request } from "undici";
50
- import { format as format2 } from "date-fns";
50
+ import dateFns from "date-fns";
51
51
 
52
52
  // src/util/http.js
53
53
  import { Client, MockAgent } from "undici";
@@ -586,7 +586,8 @@ var cursorStart = void 0;
586
586
  var cursorKey = "cursor";
587
587
  function cursor(value, options2 = {}) {
588
588
  return (state) => {
589
- const [resolvedValue, resolvedOptions] = expandReferences(state, value, options2);
589
+ const { format: format2, ...optionsWithoutFormat } = options2;
590
+ const [resolvedValue, resolvedOptions] = expandReferences(state, value, optionsWithoutFormat);
590
591
  const {
591
592
  defaultValue,
592
593
  key
@@ -601,14 +602,14 @@ function cursor(value, options2 = {}) {
601
602
  if (typeof cursor2 === "string") {
602
603
  const date = parse_date_default(cursor2, cursorStart);
603
604
  if (date instanceof Date && date.toString !== "Invalid Date") {
604
- state[cursorKey] = date.toISOString();
605
- const formatted = format2(date, "HH:MM d MMM yyyy (OOO)");
605
+ state[cursorKey] = (format2 == null ? void 0 : format2(date)) ?? date.toISOString();
606
+ const formatted = format2 ? state[cursorKey] : dateFns.format(date, "HH:MM d MMM yyyy (OOO)");
606
607
  console.log(`Setting cursor "${cursor2}" to: ${formatted}`);
607
608
  return state;
608
609
  }
609
610
  }
610
- state[cursorKey] = cursor2;
611
- console.log("Setting cursor to:", cursor2);
611
+ state[cursorKey] = (format2 == null ? void 0 : format2(cursor2)) ?? cursor2;
612
+ console.log("Setting cursor to:", state[cursorKey]);
612
613
  return state;
613
614
  };
614
615
  }
package/dist/util.cjs CHANGED
@@ -67,6 +67,7 @@ var getClient = (baseUrl, options) => {
67
67
  };
68
68
  var enableMockClient = (baseUrl) => {
69
69
  const mockAgent = new import_undici.MockAgent({ connections: 1 });
70
+ mockAgent.disableNetConnect();
70
71
  const client = mockAgent.get(baseUrl);
71
72
  if (!clients.has(baseUrl)) {
72
73
  clients.set(baseUrl, client);
@@ -134,7 +135,8 @@ async function request(method, fullUrlOrPath, options = {}) {
134
135
  throwOnError: false,
135
136
  maxRedirections,
136
137
  bodyTimeout: timeout,
137
- headersTimeout: timeout
138
+ headersTimeout: timeout,
139
+ origin: baseUrl
138
140
  });
139
141
  const statusText = (0, import_http_status_codes.getReasonPhrase)(response.statusCode);
140
142
  await assertOK(response, errors, url, method, startTime);
package/dist/util.js CHANGED
@@ -31,6 +31,7 @@ var getClient = (baseUrl, options) => {
31
31
  };
32
32
  var enableMockClient = (baseUrl) => {
33
33
  const mockAgent = new MockAgent({ connections: 1 });
34
+ mockAgent.disableNetConnect();
34
35
  const client = mockAgent.get(baseUrl);
35
36
  if (!clients.has(baseUrl)) {
36
37
  clients.set(baseUrl, client);
@@ -98,7 +99,8 @@ async function request(method, fullUrlOrPath, options = {}) {
98
99
  throwOnError: false,
99
100
  maxRedirections,
100
101
  bodyTimeout: timeout,
101
- headersTimeout: timeout
102
+ headersTimeout: timeout,
103
+ origin: baseUrl
102
104
  });
103
105
  const statusText = getReasonPhrase(response.statusCode);
104
106
  await assertOK(response, errors, url, method, startTime);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openfn/language-common",
3
- "version": "1.13.2",
3
+ "version": "1.13.4",
4
4
  "description": "Common Expressions for OpenFn",
5
5
  "homepage": "https://docs.openfn.org",
6
6
  "repository": {
@@ -352,6 +352,8 @@ export function validate(schema?: string | object, data?: string | object): Oper
352
352
  * Supports natural language dates like `now`, `today`, `yesterday`, `n hours ago`, `n days ago`, and `start`,
353
353
  * which will be converted relative to the environment (ie, the Lightning or CLI locale). Custom timezones
354
354
  * are not yet supported.
355
+ * You can provide a formatter to customise the final cursor value, which is useful for normalising
356
+ * different inputs. The custom formatter runs after natural language date conversion.
355
357
  * See the usage guide at {@link https://docs.openfn.org/documentation/jobs/job-writing-guide#using-cursors}
356
358
  * @public
357
359
  * @example <caption>Use a cursor from state if present, or else use the default value</caption>
@@ -363,11 +365,13 @@ export function validate(schema?: string | object, data?: string | object): Oper
363
365
  * @param {object} options - options to control the cursor.
364
366
  * @param {string} options.key - set the cursor key. Will persist through the whole run.
365
367
  * @param {any} options.defaultValue - the value to use if value is falsy
368
+ * @param {Function} options.format - custom formatter for the final cursor value
366
369
  * @returns {Operation}
367
370
  */
368
371
  export function cursor(value: any, options?: {
369
372
  key: string;
370
373
  defaultValue: any;
374
+ format: Function;
371
375
  }): Operation;
372
376
  export * as beta from "./beta";
373
377
  export * as http from "./http.deprecated";