@openfn/language-common 2.0.0-rc3 → 2.0.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/package.json CHANGED
@@ -1,69 +1,63 @@
1
1
  {
2
2
  "name": "@openfn/language-common",
3
- "version": "2.0.0-rc3",
3
+ "version": "2.0.0",
4
4
  "description": "Common Expressions for OpenFn",
5
- "scripts": {
6
- "build": "npm exec tsm build.ts",
7
- "build:all": "npm run clean && npm run build",
8
- "clean": "rimraf dist/ types/",
9
- "test": "mocha -r tsm test/*.ts",
10
- "test:watch": "mocha -r tsm --watch-files test --watch test/*.ts",
11
- "ast": "simple-ast --adaptor ./dist/index.js --output ast.json",
12
- "postversion": "git push && git push --tags",
13
- "version": "npm run build && git add -A lib ast.json"
5
+ "homepage": "https://docs.openfn.org",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/openfn/adaptors.git"
14
9
  },
15
- "author": "Open Function Group",
16
- "main": "dist/index.cjs",
17
- "types": "dist/language-common.d.ts",
18
- "type": "module",
19
10
  "exports": {
20
11
  ".": {
21
- "import": {
22
- "default": "./dist/index.js",
23
- "types": "./dist/language-common.d.ts"
24
- },
25
- "require": {
26
- "default": "./dist/index.cjs",
27
- "types": "./dist/language-common.d.ts"
28
- },
29
- "default": "./dist/index.js"
12
+ "import": "./dist/index.js",
13
+ "require": "./dist/index.cjs"
30
14
  },
31
- "./package.json": "./package.json"
32
- },
33
- "devDependencies": {
34
- "@microsoft/api-extractor": "^7.29.0",
35
- "@openfn/simple-ast": "^0.3.2",
36
- "@types/mocha": "^8.2.3",
37
- "@types/node": "^18.0.0",
38
- "axios": "^0.21.1",
39
- "chai": "^4.2.0",
40
- "date-fns": "^2.28.0",
41
- "deep-eql": "^4.0.0",
42
- "dts-bundle-generator": "^6.12.0",
43
- "esbuild": "^0.14.47",
44
- "execa": "^6.1.0",
45
- "jsonpath-plus": "^7.0.0",
46
- "mocha": "^7.1.0",
47
- "nock": "^13.0.5",
48
- "rimraf": "^3.0.2",
49
- "sinon": "^9.2.3",
50
- "tsm": "^2.2.1",
51
- "typescript": "^4.7.4"
52
- },
53
- "directories": {
54
- "dist": "dist"
15
+ "./package.json": "./package.json",
16
+ "./metadata": {
17
+ "import": "./dist/metadata.js",
18
+ "require": "./dist/metadata.cjs"
19
+ },
20
+ "./util": {
21
+ "import": "./dist/util.js",
22
+ "require": "./dist/util.cjs"
23
+ }
55
24
  },
25
+ "author": "Open Function Group",
26
+ "license": "LGPL-3.0-or-later",
56
27
  "files": [
57
- "dist"
28
+ "dist/",
29
+ "types/",
30
+ "ast.json",
31
+ "configuration-schema.json"
58
32
  ],
59
- "repository": {
60
- "type": "git",
61
- "url": "git+https://github.com/OpenFn/language-common.git"
33
+ "type": "module",
34
+ "types": "types/index.d.ts",
35
+ "dependencies": {
36
+ "ajv": "^8.12.0",
37
+ "csv-parse": "^5.4.0",
38
+ "csvtojson": "^2.0.10",
39
+ "date-fns": "^2.25.0",
40
+ "http-status-codes": "^2.3.0",
41
+ "jsonpath-plus": "^4.0.0",
42
+ "lodash": "^4.17.21",
43
+ "undici": "^5.28.4"
62
44
  },
63
- "bugs": {
64
- "url": "https://github.com/OpenFn/language-common/issues"
45
+ "devDependencies": {
46
+ "@openfn/simple-ast": "0.4.1",
47
+ "chai": "4.3.6",
48
+ "deep-eql": "4.1.1",
49
+ "esno": "^0.16.3",
50
+ "mocha": "9.2.2",
51
+ "nock": "13.2.9",
52
+ "rimraf": "^3.0.2"
65
53
  },
66
- "homepage": "https://github.com/OpenFn/language-common#readme",
67
- "license": "LGPL-3.0-or-later",
68
- "bundleDependencies": []
69
- }
54
+ "main": "dist/index.cjs",
55
+ "scripts": {
56
+ "build": "pnpm clean && build-adaptor common",
57
+ "test": "mocha --experimental-specifier-resolution=node --no-warnings --recursive",
58
+ "test:watch": "mocha -w --experimental-specifier-resolution=node --no-warnings --recursive",
59
+ "clean": "rimraf dist types docs",
60
+ "pack": "pnpm pack --pack-destination ../../dist",
61
+ "lint": "eslint src"
62
+ }
63
+ }
@@ -0,0 +1,429 @@
1
+ /**
2
+ * Execute a sequence of operations.
3
+ * Main outer API for executing expressions.
4
+ * @public
5
+ * @example
6
+ * execute(
7
+ * create('foo'),
8
+ * delete('bar')
9
+ * )
10
+ * @private
11
+ * @param {Operations} operations - Operations to be performed.
12
+ * @returns {Promise}
13
+ */
14
+ export function execute(...operations: Operations): Promise<any>;
15
+ /**
16
+ * alias for "fn()"
17
+ * @function
18
+ * @param {Function} func is the function
19
+ * @returns {Operation}
20
+ */
21
+ export function alterState(func: Function): Operation;
22
+ /**
23
+ * Creates a custom step (or operation) for more flexible job writing.
24
+ * @public
25
+ * @function
26
+ * @example
27
+ * fn(state => {
28
+ * // do some things to state
29
+ * return state;
30
+ * });
31
+ * @param {Function} func is the function
32
+ * @returns {Operation}
33
+ */
34
+ export function fn(func: Function): Operation;
35
+ /**
36
+ * A custom operation that will only execute the function if the condition returns true
37
+ * @public
38
+ * @function
39
+ * @example
40
+ * fnIf((state) => state?.data?.name, get("https://example.com"));
41
+ * @param {Boolean} condition - The condition that returns true
42
+ * @param {Operation} operation - The operation needed to be executed.
43
+ * @returns {Operation}
44
+ */
45
+ export function fnIf(condition: boolean, operation: Operation): Operation;
46
+ /**
47
+ * Picks out a single value from a JSON object.
48
+ * If a JSONPath returns more than one value for the reference, the first
49
+ * item will be returned.
50
+ * @public
51
+ * @function
52
+ * @example
53
+ * jsonValue({ a:1 }, 'a')
54
+ * @param {object} obj - A valid JSON object.
55
+ * @param {String} path - JSONPath referencing a point in given JSON object.
56
+ * @returns {Operation}
57
+ */
58
+ export function jsonValue(obj: object, path: string): Operation;
59
+ /**
60
+ * Picks out a single value from source data.
61
+ * If a JSONPath returns more than one value for the reference, the first
62
+ * item will be returned.
63
+ * @public
64
+ * @function
65
+ * @example
66
+ * sourceValue('$.key')
67
+ * @param {String} path - JSONPath referencing a point in `state`.
68
+ * @returns {Operation}
69
+ */
70
+ export function sourceValue(path: string): Operation;
71
+ /**
72
+ * Picks out a value from source data.
73
+ * Will return whatever JSONPath returns, which will always be an array.
74
+ * If you need a single value use `sourceValue` instead.
75
+ * @public
76
+ * @function
77
+ * @example
78
+ * source('$.key')
79
+ * @param {String} path - JSONPath referencing a point in `state`.
80
+ * @returns {Array.<String|Object>}
81
+ */
82
+ export function source(path: string): Array<string | any>;
83
+ /**
84
+ * Ensures a path points at the data.
85
+ * @public
86
+ * @function
87
+ * @example
88
+ * dataPath('key')
89
+ * @param {string} path - JSONPath referencing a point in `data`.
90
+ * @returns {string}
91
+ */
92
+ export function dataPath(path: string): string;
93
+ /**
94
+ * Picks out a single value from the source data object—usually `state.data`.
95
+ * If a JSONPath returns more than one value for the reference, the first
96
+ * item will be returned.
97
+ * @public
98
+ * @function
99
+ * @example
100
+ * dataValue('key')
101
+ * @param {String} path - JSONPath referencing a point in `data`.
102
+ * @returns {Operation}
103
+ */
104
+ export function dataValue(path: string): Operation;
105
+ /**
106
+ * Ensures a path points at references.
107
+ * @public
108
+ * @function
109
+ * @example
110
+ * referencePath('key')
111
+ * @param {string} path - JSONPath referencing a point in `references`.
112
+ * @returns {string}
113
+ */
114
+ export function referencePath(path: string): string;
115
+ /**
116
+ * Picks out the last reference value from source data.
117
+ * @public
118
+ * @function
119
+ * @example
120
+ * lastReferenceValue('key')
121
+ * @param {String} path - JSONPath referencing a point in `references`.
122
+ * @returns {Operation}
123
+ */
124
+ export function lastReferenceValue(path: string): Operation;
125
+ /**
126
+ * Simple switcher allowing other expressions to use either a JSONPath or
127
+ * object literals as a data source.
128
+ * - JSONPath referencing a point in `state`
129
+ * - Object Literal of the data itself.
130
+ * - Function to be called with state.
131
+ * @public
132
+ * @function
133
+ * @example
134
+ * asData('$.key'| key | callback)
135
+ * @param {String|object|function} data
136
+ * @param {object} state - The current state.
137
+ * @returns {array}
138
+ */
139
+ export function asData(data: string | object | Function, state: object): any[];
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.
149
+ * @public
150
+ * @function
151
+ * @example
152
+ * each("$.[*]",
153
+ * create("SObject",
154
+ * field("FirstName", sourceValue("$.firstName"))
155
+ * )
156
+ * )
157
+ * @param {DataSource} dataSource - JSONPath referencing a point in `state`.
158
+ * @param {Operation} operation - The operation needed to be repeated.
159
+ * @returns {Operation}
160
+ */
161
+ export function each(dataSource: DataSource, operation: Operation): Operation;
162
+ /**
163
+ * Combines two operations into one
164
+ * @public
165
+ * @function
166
+ * @example
167
+ * combine(
168
+ * create('foo'),
169
+ * delete('bar')
170
+ * )
171
+ * @param {Operations} operations - Operations to be performed.
172
+ * @returns {Operation}
173
+ */
174
+ export function combine(...operations: Operations): Operation;
175
+ /**
176
+ * Adds data from a target object
177
+ * @public
178
+ * @function
179
+ * @example
180
+ * join('$.key','$.data','newKey')
181
+ * @param {String} targetPath - Target path
182
+ * @param {String} sourcePath - Source path
183
+ * @param {String} targetKey - Target Key
184
+ * @returns {Operation}
185
+ */
186
+ export function join(targetPath: string, sourcePath: string, targetKey: string): Operation;
187
+ /**
188
+ * Recursively resolves objects that have resolvable values (functions).
189
+ * @public
190
+ * @function
191
+ * @param {object} value - data
192
+ * @param {Function} [skipFilter] - a function which returns true if a value should be skipped
193
+ * @returns {Operation}
194
+ */
195
+ export function expandReferences(value: object, skipFilter?: Function): Operation;
196
+ /**
197
+ * Returns a key, value pair in an array.
198
+ * @public
199
+ * @function
200
+ * @example
201
+ * field('destination_field_name__c', 'value')
202
+ * @param {string} key - Name of the field
203
+ * @param {Value} value - The value itself or a sourceable operation.
204
+ * @returns {Field}
205
+ */
206
+ export function field(key: string, value: Value): Field;
207
+ /**
208
+ * Zips key value pairs into an object.
209
+ * @public
210
+ * @function
211
+ * @example
212
+ * fields(list_of_fields)
213
+ * @param {Fields} fields - a list of fields
214
+ * @returns {Object}
215
+ */
216
+ export function fields(...fields: Fields): any;
217
+ /**
218
+ * Merges fields into each item in an array.
219
+ * @public
220
+ * @example
221
+ * merge(
222
+ * "$.books[*]",
223
+ * fields(
224
+ * field( "publisher", sourceValue("$.publisher") )
225
+ * )
226
+ * )
227
+ * @function
228
+ * @public
229
+ * @param {DataSource} dataSource
230
+ * @param {Object} fields - Group of fields to merge in.
231
+ * @returns {DataSource}
232
+ */
233
+ export function merge(dataSource: DataSource, fields: any): DataSource;
234
+ /**
235
+ * Groups an array of objects by a specified key path.
236
+ * @public
237
+ * @example
238
+ * const users = [
239
+ * { name: 'Alice', age: 25, city: 'New York' },
240
+ * { name: 'Bob', age: 30, city: 'San Francisco' },
241
+ * { name: 'Charlie', age: 25, city: 'New York' },
242
+ * { name: 'David', age: 30, city: 'San Francisco' }
243
+ * ];
244
+ * group(users, 'city');
245
+ * // state is { data: { 'New York': [/Alice, Charlie/], 'San Francisco': [ /Bob, David / ] }
246
+ * @function
247
+ * @public
248
+ * @param {Object[]} arrayOfObjects - The array of objects to be grouped.
249
+ * @param {string} keyPath - The key path to group by.
250
+ * @param {function} callback - (Optional) Callback function
251
+ * @returns {Operation}
252
+ */
253
+ export function group(arrayOfObjects: any[], keyPath: string, callback?: Function): Operation;
254
+ /**
255
+ * Returns the index of the current array being iterated.
256
+ * To be used with `each` as a data source.
257
+ * @public
258
+ * @function
259
+ * @example
260
+ * index()
261
+ * @returns {DataSource}
262
+ */
263
+ export function index(): DataSource;
264
+ /**
265
+ * Turns an array into a string, separated by X.
266
+ * @public
267
+ * @function
268
+ * @example
269
+ * field("destination_string__c", function(state) {
270
+ * return arrayToString(dataValue("path_of_array")(state), ', ')
271
+ * })
272
+ * @param {array} arr - Array of toString'able primatives.
273
+ * @param {string} separator - Separator string.
274
+ * @returns {string}
275
+ */
276
+ export function arrayToString(arr: any[], separator: string): string;
277
+ /**
278
+ * Ensures primitive data types are wrapped in an array.
279
+ * Does not affect array objects.
280
+ * @public
281
+ * @function
282
+ * @example
283
+ * each(function(state) {
284
+ * return toArray( dataValue("path_of_array")(state) )
285
+ * }, ...)
286
+ * @param {any} arg - Data required to be in an array
287
+ * @returns {array}
288
+ */
289
+ export function toArray(arg: any): any[];
290
+ /**
291
+ * Prepares next state
292
+ * @public
293
+ * @function
294
+ * @example
295
+ * composeNextState(state, response)
296
+ * @param {State} state - state
297
+ * @param {Object} response - Response to be added
298
+ * @returns {State}
299
+ */
300
+ export function composeNextState(state: State, response: any): State;
301
+ /**
302
+ * Substitutes underscores for spaces and proper-cases a string
303
+ * @public
304
+ * @function
305
+ * @example
306
+ * field("destination_string__c", humanProper(state.data.path_to_string))
307
+ * @param {string} str - String that needs converting
308
+ * @returns {string}
309
+ */
310
+ export function humanProper(str: string): string;
311
+ /**
312
+ * Splits an object into two objects based on a list of keys.
313
+ * The first object contains the keys that are not in the list,
314
+ * and the second contains the keys that are.
315
+ * @public
316
+ * @function
317
+ * @param {Object} obj - The object to split.
318
+ * @param {string[]} keys - List of keys to split on.
319
+ * @returns {Object[]} - Tuple of objects, first object contains keys not in list, second contains keys that are.
320
+ */
321
+ export function splitKeys(obj: any, keys: string[]): any[];
322
+ /**
323
+ * Replaces emojis in a string.
324
+ * @public
325
+ * @function
326
+ * @example
327
+ * scrubEmojis('Dove🕊️⭐ 29')
328
+ * @param {string} text - String that needs to be cleaned
329
+ * @param {string} replacementChars - Characters that replace the emojis
330
+ * @returns {string}
331
+ */
332
+ export function scrubEmojis(text: string, replacementChars: string): string;
333
+ /**
334
+ * Chunks an array into an array of arrays, each with no more than a certain size.
335
+ * @public
336
+ * @function
337
+ * @example
338
+ * chunk([1,2,3,4,5], 2)
339
+ * @param {Object} array - Array to be chunked
340
+ * @param {Integer} chunkSize - The maxiumum size of each chunks
341
+ * @returns {Object}
342
+ */
343
+ export function chunk(array: any, chunkSize: Integer): any;
344
+ /**
345
+ * Takes a CSV file string or stream and parsing options as input, and returns a promise that
346
+ * resolves to the parsed CSV data as an array of objects.
347
+ * Options for `parsingOptions` include:
348
+ * - `delimiter` {string/Buffer/[string/Buffer]} - Defines the character(s) used to delineate the fields inside a record. Default: `','`
349
+ * - `quote` {string/Buffer/[string/Buffer]} - Defines the characters used to surround a field. Default: `'"'`
350
+ * - `escape` {Buffer/string/null/boolean} - Set the escape character as one character/byte only. Default: `"`
351
+ * - `columns` {boolean / array / function} - Generates record in the form of object literals. Default: `true`
352
+ * - `bom` {boolean} - Strips the {@link https://en.wikipedia.org/wiki/Byte_order_mark byte order mark (BOM)} from the input string or buffer. Default: `true`
353
+ * - `trim` {boolean} - Ignore whitespace characters immediately around the `delimiter`. Default: `true`
354
+ * - `ltrim` {boolean} - Ignore whitespace characters from the left side of a CSV field. Default: `true`
355
+ * - `rtrim` {boolean} - Ignore whitespace characters from the right side of a CSV field. Default: `true`
356
+ * - `chunkSize` {number} - The size of each chunk of CSV data. Default: `Infinity`
357
+ * - `skip_empty_lines` {boolean} - Ignore empty lines in the CSV file. Default: `true`
358
+ * @public
359
+ * @function
360
+ * @param {String | Stream} csvData - A CSV string or a readable stream
361
+ * @param {Object} [parsingOptions] - Optional. Parsing options for converting CSV to JSON.
362
+ * @param {function} [callback] - (Optional) callback function. If used it will be called state and an array of rows.
363
+ * @returns {Operation} The function returns a Promise that resolves to the result of parsing a CSV `stringOrStream`.
364
+ */
365
+ export function parseCsv(csvData: string | Stream, parsingOptions?: any, callback?: Function): Operation;
366
+ /**
367
+ * Validate against a JSON schema. Any erors are written to an array at `state.validationErrors`.
368
+ * Schema can be passed directly, loaded as a JSON path from state, or loaded from a URL
369
+ * Data can be passed directly or loaded as a JSON path from state.
370
+ * By default, schema is loaded from `state.schema` and data from `state.data`.
371
+ * @pubic
372
+ * @function
373
+ * @param {string|object} schema - The schema, path or URL to validate against
374
+ * @param {string|object} data - The data or path to validate
375
+ * @example <caption>Validate `state.data` with `state.schema`</caption>
376
+ * validate()
377
+ * @example <caption>Validate form data at `state.form` with a schema from a URL</caption>
378
+ * validate("https://www.example.com/schema/record", "form")
379
+ * @example <caption>Validate the each item in `state.records` with a schema from a URL</caption>
380
+ * each("records[*]", validate("https://www.example.com/schema/record"))
381
+ * @returns {Operation}
382
+ */
383
+ export function validate(schema?: string | object, data?: string | object): Operation;
384
+ /**
385
+ * Sets a cursor property on state.
386
+ * Supports natural language dates like `now`, `today`, `yesterday`, `n hours ago`, `n days ago`, and `start`,
387
+ * which will be converted relative to the environment (ie, the Lightning or CLI locale). Custom timezones
388
+ * are not yet supported.
389
+ * You can provide a formatter to customise the final cursor value, which is useful for normalising
390
+ * different inputs. The custom formatter runs after natural language date conversion.
391
+ * See the usage guide at {@link https://docs.openfn.org/documentation/jobs/job-writing-guide#using-cursors}
392
+ * @public
393
+ * @function
394
+ * @example <caption>Use a cursor from state if present, or else use the default value</caption>
395
+ * cursor($.cursor, { defaultValue: 'today' })
396
+ * @example <caption>Use a pagination cursor</caption>
397
+ * cursor(22)
398
+ * @param {any} value - the cursor value. Usually an ISO date, natural language date, or page number
399
+ * @param {object} options - options to control the cursor.
400
+ * @param {string} options.key - set the cursor key. Will persist through the whole run.
401
+ * @param {any} options.defaultValue - the value to use if value is falsy
402
+ * @param {Function} options.format - custom formatter for the final cursor value
403
+ * @returns {Operation}
404
+ */
405
+ export function cursor(value: any, options?: {
406
+ key: string;
407
+ defaultValue: any;
408
+ format: Function;
409
+ }): Operation;
410
+ /**
411
+ * Scopes an array of data based on a JSONPath.
412
+ * Useful when the source data has `n` items you would like to map to
413
+ * an operation.
414
+ * The operation will receive a slice of the data based of each item
415
+ * of the JSONPath provided.
416
+ * @public
417
+ * @function
418
+ * @example
419
+ * map("$.[*]",
420
+ * create("SObject",
421
+ * field("FirstName", sourceValue("$.firstName"))
422
+ * )
423
+ * )
424
+ * @param {string} path - JSONPath referencing a point in `state.data`.
425
+ * @param {function} operation - The operation needed to be repeated.
426
+ * @param {State} state - Runtime state.
427
+ * @returns {State}
428
+ */
429
+ export const map: any;
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Scopes an array of data based on a JSONPath.
3
+ * Useful when the source data has `n` items you would like to map to
4
+ * an operation.
5
+ * The operation will receive a slice of the data based of each item
6
+ * of the JSONPath provided.
7
+ *
8
+ * It also ensures the results of an operation make their way back into
9
+ * the state's references.
10
+ * @public
11
+ * @example
12
+ * each("$.[*]",
13
+ * create("SObject",
14
+ * field("FirstName", sourceValue("$.firstName")))
15
+ * )
16
+ * @function
17
+ * @param {DataSource} dataSource - JSONPath referencing a point in `state`.
18
+ * @param {Operation} operation - The operation needed to be repeated.
19
+ * @returns {Operation}
20
+ */
21
+ export function each(dataSource: DataSource, operation: Operation): Operation;
@@ -0,0 +1 @@
1
+ export { parse, format } from "date-fns";