@openfn/language-common 1.12.0 → 1.13.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
@@ -1312,6 +1312,83 @@
1312
1312
  },
1313
1313
  "valid": true
1314
1314
  },
1315
+ {
1316
+ "name": "cursor",
1317
+ "params": [
1318
+ "value",
1319
+ "options"
1320
+ ],
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}",
1323
+ "tags": [
1324
+ {
1325
+ "title": "public",
1326
+ "description": null,
1327
+ "type": null
1328
+ },
1329
+ {
1330
+ "title": "example",
1331
+ "description": "cursor($.cursor, { defaultValue: 'today' })",
1332
+ "caption": "Use a cursor from state if present, or else use the default value"
1333
+ },
1334
+ {
1335
+ "title": "example",
1336
+ "description": "cursor(22)",
1337
+ "caption": "Use a pagination cursor"
1338
+ },
1339
+ {
1340
+ "title": "function",
1341
+ "description": null,
1342
+ "name": null
1343
+ },
1344
+ {
1345
+ "title": "param",
1346
+ "description": "the cursor value. Usually an ISO date, natural language date, or page number",
1347
+ "type": {
1348
+ "type": "NameExpression",
1349
+ "name": "any"
1350
+ },
1351
+ "name": "value"
1352
+ },
1353
+ {
1354
+ "title": "param",
1355
+ "description": "options to control the cursor.",
1356
+ "type": {
1357
+ "type": "NameExpression",
1358
+ "name": "object"
1359
+ },
1360
+ "name": "options"
1361
+ },
1362
+ {
1363
+ "title": "param",
1364
+ "description": "set the cursor key. Will persist through the whole run.",
1365
+ "type": {
1366
+ "type": "NameExpression",
1367
+ "name": "string"
1368
+ },
1369
+ "name": "options.key"
1370
+ },
1371
+ {
1372
+ "title": "param",
1373
+ "description": "the value to use if value is falsy",
1374
+ "type": {
1375
+ "type": "NameExpression",
1376
+ "name": "any"
1377
+ },
1378
+ "name": "options.defaultValue"
1379
+ },
1380
+ {
1381
+ "title": "returns",
1382
+ "description": null,
1383
+ "type": {
1384
+ "type": "NameExpression",
1385
+ "name": "Operation"
1386
+ }
1387
+ }
1388
+ ]
1389
+ },
1390
+ "valid": false
1391
+ },
1315
1392
  {
1316
1393
  "name": "map",
1317
1394
  "params": [
package/dist/index.cjs CHANGED
@@ -32,6 +32,7 @@ __export(src_exports, {
32
32
  chunk: () => chunk,
33
33
  combine: () => combine,
34
34
  composeNextState: () => composeNextState,
35
+ cursor: () => cursor,
35
36
  dataPath: () => dataPath,
36
37
  dataValue: () => dataValue,
37
38
  dateFns: () => dateFns_exports,
@@ -71,6 +72,7 @@ __export(Adaptor_exports, {
71
72
  chunk: () => chunk,
72
73
  combine: () => combine,
73
74
  composeNextState: () => composeNextState,
75
+ cursor: () => cursor,
74
76
  dataPath: () => dataPath,
75
77
  dataValue: () => dataValue,
76
78
  dateFns: () => dateFns_exports,
@@ -103,6 +105,7 @@ var import_jsonpath_plus = require("jsonpath-plus");
103
105
  var import_csv_parse = require("csv-parse");
104
106
  var import_node_stream3 = require("stream");
105
107
  var import_undici2 = require("undici");
108
+ var import_date_fns3 = require("date-fns");
106
109
 
107
110
  // src/util/http.js
108
111
  var import_undici = require("undici");
@@ -143,6 +146,32 @@ function expandReference(state, value) {
143
146
  return value;
144
147
  }
145
148
 
149
+ // src/util/parse-date.js
150
+ var import_date_fns = require("date-fns");
151
+ var parse_date_default = (d, startDate) => {
152
+ try {
153
+ if (d === "start") {
154
+ return startDate;
155
+ } else if (d === "now" || d === "end") {
156
+ return new Date();
157
+ } else if (d === "today") {
158
+ return (0, import_date_fns.startOfToday)();
159
+ } else if (d === "yesterday") {
160
+ return (0, import_date_fns.startOfYesterday)();
161
+ } else if (/(hours? ago)$/.test(d)) {
162
+ const [diff] = d.match(/\d+/);
163
+ return (0, import_date_fns.subHours)(new Date(), diff);
164
+ } else if (/(days? ago)$/.test(d)) {
165
+ const [diff] = d.match(/\d+/);
166
+ return (0, import_date_fns.startOfDay)((0, import_date_fns.subDays)(new Date(), diff));
167
+ }
168
+ } catch (e) {
169
+ console.log(`Error converting ${d} into a date`);
170
+ console.log(e);
171
+ }
172
+ return d;
173
+ };
174
+
146
175
  // src/beta.js
147
176
  var beta_exports = {};
148
177
  __export(beta_exports, {
@@ -192,7 +221,7 @@ var import_https = __toESM(require("https"), 1);
192
221
  var printDeprecationNotice = (name) => {
193
222
  if (process.env.NODE_ENV !== "production") {
194
223
  console.warn(
195
- `The common.http.\xAC${name} function has been deprecated. This adaptor should migrate to use common.util.http instead.`
224
+ `The common.http.${name} function has been deprecated. This adaptor should migrate to use common.util.http instead.`
196
225
  );
197
226
  }
198
227
  };
@@ -280,10 +309,10 @@ function options(requestParams) {
280
309
  // src/dateFns.js
281
310
  var dateFns_exports = {};
282
311
  __export(dateFns_exports, {
283
- format: () => import_date_fns.format,
284
- parse: () => import_date_fns.parse
312
+ format: () => import_date_fns2.format,
313
+ parse: () => import_date_fns2.parse
285
314
  });
286
- var import_date_fns = require("date-fns");
315
+ var import_date_fns2 = require("date-fns");
287
316
 
288
317
  // src/Adaptor.js
289
318
  var schemaCache = {};
@@ -612,6 +641,36 @@ function validate(schema = "schema", data = "data") {
612
641
  }
613
642
  };
614
643
  }
644
+ var cursorStart = void 0;
645
+ var cursorKey = "cursor";
646
+ function cursor(value, options2 = {}) {
647
+ return (state) => {
648
+ const [resolvedValue, resolvedOptions] = expandReferences(state, value, options2);
649
+ const {
650
+ defaultValue,
651
+ key
652
+ } = resolvedOptions;
653
+ if (key) {
654
+ cursorKey = key;
655
+ }
656
+ if (!cursorStart) {
657
+ cursorStart = new Date();
658
+ }
659
+ const cursor2 = resolvedValue ?? defaultValue;
660
+ if (typeof cursor2 === "string") {
661
+ const date = parse_date_default(cursor2, cursorStart);
662
+ 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)");
665
+ console.log(`Setting cursor "${cursor2}" to: ${formatted}`);
666
+ return state;
667
+ }
668
+ }
669
+ state[cursorKey] = cursor2;
670
+ console.log("Setting cursor to:", cursor2);
671
+ return state;
672
+ };
673
+ }
615
674
 
616
675
  // src/index.js
617
676
  var src_default = Adaptor_exports;
@@ -624,6 +683,7 @@ var src_default = Adaptor_exports;
624
683
  chunk,
625
684
  combine,
626
685
  composeNextState,
686
+ cursor,
627
687
  dataPath,
628
688
  dataValue,
629
689
  dateFns,
package/dist/index.js CHANGED
@@ -14,6 +14,7 @@ __export(Adaptor_exports, {
14
14
  chunk: () => chunk,
15
15
  combine: () => combine,
16
16
  composeNextState: () => composeNextState,
17
+ cursor: () => cursor,
17
18
  dataPath: () => dataPath,
18
19
  dataValue: () => dataValue,
19
20
  dateFns: () => dateFns_exports,
@@ -46,6 +47,7 @@ import { JSONPath } from "jsonpath-plus";
46
47
  import { parse as parse2 } from "csv-parse";
47
48
  import { Readable as Readable2 } from "stream";
48
49
  import { request } from "undici";
50
+ import { format as format2 } from "date-fns";
49
51
 
50
52
  // src/util/http.js
51
53
  import { Client, MockAgent } from "undici";
@@ -85,6 +87,32 @@ function expandReference(state, value) {
85
87
  return value;
86
88
  }
87
89
 
90
+ // src/util/parse-date.js
91
+ import { startOfToday, startOfYesterday, subHours, subDays, startOfDay } from "date-fns";
92
+ var parse_date_default = (d, startDate) => {
93
+ try {
94
+ if (d === "start") {
95
+ return startDate;
96
+ } else if (d === "now" || d === "end") {
97
+ return new Date();
98
+ } else if (d === "today") {
99
+ return startOfToday();
100
+ } else if (d === "yesterday") {
101
+ return startOfYesterday();
102
+ } else if (/(hours? ago)$/.test(d)) {
103
+ const [diff] = d.match(/\d+/);
104
+ return subHours(new Date(), diff);
105
+ } else if (/(days? ago)$/.test(d)) {
106
+ const [diff] = d.match(/\d+/);
107
+ return startOfDay(subDays(new Date(), diff));
108
+ }
109
+ } catch (e) {
110
+ console.log(`Error converting ${d} into a date`);
111
+ console.log(e);
112
+ }
113
+ return d;
114
+ };
115
+
88
116
  // src/beta.js
89
117
  var beta_exports = {};
90
118
  __export(beta_exports, {
@@ -134,7 +162,7 @@ import https from "https";
134
162
  var printDeprecationNotice = (name) => {
135
163
  if (process.env.NODE_ENV !== "production") {
136
164
  console.warn(
137
- `The common.http.\xAC${name} function has been deprecated. This adaptor should migrate to use common.util.http instead.`
165
+ `The common.http.${name} function has been deprecated. This adaptor should migrate to use common.util.http instead.`
138
166
  );
139
167
  }
140
168
  };
@@ -554,6 +582,36 @@ function validate(schema = "schema", data = "data") {
554
582
  }
555
583
  };
556
584
  }
585
+ var cursorStart = void 0;
586
+ var cursorKey = "cursor";
587
+ function cursor(value, options2 = {}) {
588
+ return (state) => {
589
+ const [resolvedValue, resolvedOptions] = expandReferences(state, value, options2);
590
+ const {
591
+ defaultValue,
592
+ key
593
+ } = resolvedOptions;
594
+ if (key) {
595
+ cursorKey = key;
596
+ }
597
+ if (!cursorStart) {
598
+ cursorStart = new Date();
599
+ }
600
+ const cursor2 = resolvedValue ?? defaultValue;
601
+ if (typeof cursor2 === "string") {
602
+ const date = parse_date_default(cursor2, cursorStart);
603
+ if (date instanceof Date && date.toString !== "Invalid Date") {
604
+ state[cursorKey] = date.toISOString();
605
+ const formatted = format2(date, "HH:MM d MMM yyyy (OOO)");
606
+ console.log(`Setting cursor "${cursor2}" to: ${formatted}`);
607
+ return state;
608
+ }
609
+ }
610
+ state[cursorKey] = cursor2;
611
+ console.log("Setting cursor to:", cursor2);
612
+ return state;
613
+ };
614
+ }
557
615
 
558
616
  // src/index.js
559
617
  var src_default = Adaptor_exports;
@@ -565,6 +623,7 @@ export {
565
623
  chunk,
566
624
  combine,
567
625
  composeNextState,
626
+ cursor,
568
627
  dataPath,
569
628
  dataValue,
570
629
  dateFns_exports as dateFns,
package/dist/util.cjs CHANGED
@@ -26,6 +26,7 @@ __export(util_exports, {
26
26
  logResponse: () => logResponse,
27
27
  makeBasicAuthHeader: () => makeBasicAuthHeader,
28
28
  normalizeOauthConfig: () => normalizeOauthConfig,
29
+ parseDate: () => parse_date_default,
29
30
  parseUrl: () => parseUrl,
30
31
  post: () => post,
31
32
  put: () => put,
@@ -55,6 +56,7 @@ var logResponse = (response) => {
55
56
  console.log(message);
56
57
  }
57
58
  }
59
+ return response;
58
60
  };
59
61
  var getClient = (baseUrl, options) => {
60
62
  const { tls } = options;
@@ -231,6 +233,32 @@ function normalizeOauthConfig(configuration) {
231
233
  console.log('Using "accessToken" from state.configuration');
232
234
  return configuration;
233
235
  }
236
+
237
+ // src/util/parse-date.js
238
+ var import_date_fns = require("date-fns");
239
+ var parse_date_default = (d, startDate) => {
240
+ try {
241
+ if (d === "start") {
242
+ return startDate;
243
+ } else if (d === "now" || d === "end") {
244
+ return new Date();
245
+ } else if (d === "today") {
246
+ return (0, import_date_fns.startOfToday)();
247
+ } else if (d === "yesterday") {
248
+ return (0, import_date_fns.startOfYesterday)();
249
+ } else if (/(hours? ago)$/.test(d)) {
250
+ const [diff] = d.match(/\d+/);
251
+ return (0, import_date_fns.subHours)(new Date(), diff);
252
+ } else if (/(days? ago)$/.test(d)) {
253
+ const [diff] = d.match(/\d+/);
254
+ return (0, import_date_fns.startOfDay)((0, import_date_fns.subDays)(new Date(), diff));
255
+ }
256
+ } catch (e) {
257
+ console.log(`Error converting ${d} into a date`);
258
+ console.log(e);
259
+ }
260
+ return d;
261
+ };
234
262
  // Annotate the CommonJS export names for ESM import in node:
235
263
  0 && (module.exports = {
236
264
  del,
@@ -240,6 +268,7 @@ function normalizeOauthConfig(configuration) {
240
268
  logResponse,
241
269
  makeBasicAuthHeader,
242
270
  normalizeOauthConfig,
271
+ parseDate,
243
272
  parseUrl,
244
273
  post,
245
274
  put,
package/dist/util.js CHANGED
@@ -20,6 +20,7 @@ var logResponse = (response) => {
20
20
  console.log(message);
21
21
  }
22
22
  }
23
+ return response;
23
24
  };
24
25
  var getClient = (baseUrl, options) => {
25
26
  const { tls } = options;
@@ -196,6 +197,32 @@ function normalizeOauthConfig(configuration) {
196
197
  console.log('Using "accessToken" from state.configuration');
197
198
  return configuration;
198
199
  }
200
+
201
+ // src/util/parse-date.js
202
+ import { startOfToday, startOfYesterday, subHours, subDays, startOfDay } from "date-fns";
203
+ var parse_date_default = (d, startDate) => {
204
+ try {
205
+ if (d === "start") {
206
+ return startDate;
207
+ } else if (d === "now" || d === "end") {
208
+ return new Date();
209
+ } else if (d === "today") {
210
+ return startOfToday();
211
+ } else if (d === "yesterday") {
212
+ return startOfYesterday();
213
+ } else if (/(hours? ago)$/.test(d)) {
214
+ const [diff] = d.match(/\d+/);
215
+ return subHours(new Date(), diff);
216
+ } else if (/(days? ago)$/.test(d)) {
217
+ const [diff] = d.match(/\d+/);
218
+ return startOfDay(subDays(new Date(), diff));
219
+ }
220
+ } catch (e) {
221
+ console.log(`Error converting ${d} into a date`);
222
+ console.log(e);
223
+ }
224
+ return d;
225
+ };
199
226
  export {
200
227
  del,
201
228
  enableMockClient,
@@ -204,6 +231,7 @@ export {
204
231
  logResponse,
205
232
  makeBasicAuthHeader,
206
233
  normalizeOauthConfig,
234
+ parse_date_default as parseDate,
207
235
  parseUrl,
208
236
  post,
209
237
  put,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openfn/language-common",
3
- "version": "1.12.0",
3
+ "version": "1.13.0",
4
4
  "description": "Common Expressions for OpenFn",
5
5
  "homepage": "https://docs.openfn.org",
6
6
  "repository": {
@@ -44,7 +44,6 @@
44
44
  "undici": "^5.23.0"
45
45
  },
46
46
  "devDependencies": {
47
- "@openfn/buildtools": "^1.0.2",
48
47
  "@openfn/simple-ast": "0.4.1",
49
48
  "chai": "4.3.6",
50
49
  "deep-eql": "4.1.1",
@@ -347,6 +347,28 @@ export function parseCsv(csvData: string | Stream, parsingOptions?: any, callbac
347
347
  * @returns {Operation}
348
348
  */
349
349
  export function validate(schema?: string | object, data?: string | object): Operation;
350
+ /**
351
+ * Sets a cursor property on state.
352
+ * Supports natural language dates like `now`, `today`, `yesterday`, `n hours ago`, `n days ago`, and `start`,
353
+ * which will be converted relative to the environment (ie, the Lightning or CLI locale). Custom timezones
354
+ * are not yet supported.
355
+ * See the usage guide at @{link https://docs.openfn.org/documentation/jobs/job-writing-guide#using-cursors}
356
+ * @public
357
+ * @example <caption>Use a cursor from state if present, or else use the default value</caption>
358
+ * cursor($.cursor, { defaultValue: 'today' })
359
+ * @example <caption>Use a pagination cursor</caption>
360
+ * cursor(22)
361
+ * @function
362
+ * @param {any} value - the cursor value. Usually an ISO date, natural language date, or page number
363
+ * @param {object} options - options to control the cursor.
364
+ * @param {string} options.key - set the cursor key. Will persist through the whole run.
365
+ * @param {any} options.defaultValue - the value to use if value is falsy
366
+ * @returns {Operation}
367
+ */
368
+ export function cursor(value: any, options?: {
369
+ key: string;
370
+ defaultValue: any;
371
+ }): Operation;
350
372
  export * as beta from "./beta";
351
373
  export * as http from "./http.deprecated";
352
374
  export * as dateFns from "./dateFns";
@@ -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): void;
30
+ export function logResponse(response: any): any;
31
31
  export function enableMockClient(baseUrl: any): import("undici/types/mock-interceptor").Interceptable;
32
32
  export function parseUrl(pathOrUrl: string, baseUrl: any): {
33
33
  url: string;
@@ -1,2 +1,4 @@
1
1
  export * from "./http";
2
2
  export * from "./references";
3
+ export { parseDate };
4
+ import parseDate from "./parse-date";
@@ -0,0 +1,2 @@
1
+ declare function _default(d: any, startDate: any): any;
2
+ export default _default;