@openfn/language-collections 0.5.1 → 0.6.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/dist/index.cjs +69 -20
- package/dist/index.js +69 -20
- package/package.json +1 -2
- package/types/collections.d.ts +3 -2
package/dist/index.cjs
CHANGED
|
@@ -36,6 +36,7 @@ __export(collections_exports, {
|
|
|
36
36
|
each: () => each,
|
|
37
37
|
expandQuery: () => expandQuery,
|
|
38
38
|
get: () => get,
|
|
39
|
+
handleError: () => handleError,
|
|
39
40
|
parseQuery: () => parseQuery,
|
|
40
41
|
remove: () => remove,
|
|
41
42
|
request: () => request,
|
|
@@ -46,11 +47,7 @@ __export(collections_exports, {
|
|
|
46
47
|
var import_node_path = __toESM(require("path"), 1);
|
|
47
48
|
var import_undici2 = __toESM(require("undici"), 1);
|
|
48
49
|
var import_util = require("@openfn/language-common/util");
|
|
49
|
-
var import_stream_chain = __toESM(require("stream-chain"), 1);
|
|
50
50
|
var import_stream_json = __toESM(require("stream-json"), 1);
|
|
51
|
-
var import_Pick = __toESM(require("stream-json/filters/Pick"), 1);
|
|
52
|
-
var import_StreamArray = __toESM(require("stream-json/streamers/StreamArray"), 1);
|
|
53
|
-
var import_StreamValues = __toESM(require("stream-json/streamers/StreamValues"), 1);
|
|
54
51
|
|
|
55
52
|
// src/mock.js
|
|
56
53
|
var import_undici = require("undici");
|
|
@@ -102,12 +99,21 @@ function API() {
|
|
|
102
99
|
return result;
|
|
103
100
|
};
|
|
104
101
|
const byKey = (name, key) => {
|
|
102
|
+
if (!(name in collections)) {
|
|
103
|
+
throw new Error(COLLECTION_NOT_FOUND);
|
|
104
|
+
}
|
|
105
105
|
return collections[name][key];
|
|
106
106
|
};
|
|
107
107
|
const asJSON = (name, key) => {
|
|
108
|
+
if (!(name in collections)) {
|
|
109
|
+
throw new Error(COLLECTION_NOT_FOUND);
|
|
110
|
+
}
|
|
108
111
|
return JSON.parse(collections[name][key]);
|
|
109
112
|
};
|
|
110
113
|
const count = (name) => {
|
|
114
|
+
if (!(name in collections)) {
|
|
115
|
+
throw new Error(COLLECTION_NOT_FOUND);
|
|
116
|
+
}
|
|
111
117
|
return Object.keys(collections[name]).length;
|
|
112
118
|
};
|
|
113
119
|
const remove2 = (name, key) => {
|
|
@@ -335,13 +341,17 @@ function get(name, query = {}) {
|
|
|
335
341
|
getClient(state),
|
|
336
342
|
`${resolvedName}/${key}`
|
|
337
343
|
);
|
|
338
|
-
|
|
339
|
-
if (body.value) {
|
|
340
|
-
data = JSON.parse(body.value);
|
|
341
|
-
console.log(`Collections: Fetched "${key}" from "${name}"`);
|
|
342
|
-
} else {
|
|
344
|
+
if (response.statusCode === 204) {
|
|
343
345
|
data = {};
|
|
344
346
|
console.warn(`Collections: Key "${key}" not found in "${name}"`);
|
|
347
|
+
} else {
|
|
348
|
+
try {
|
|
349
|
+
const body = await response.body.json();
|
|
350
|
+
data = JSON.parse(body.value);
|
|
351
|
+
} catch (e) {
|
|
352
|
+
console.error("Unexpected error parsing response data!");
|
|
353
|
+
throw e;
|
|
354
|
+
}
|
|
345
355
|
}
|
|
346
356
|
}
|
|
347
357
|
state.data = data;
|
|
@@ -349,7 +359,19 @@ function get(name, query = {}) {
|
|
|
349
359
|
};
|
|
350
360
|
}
|
|
351
361
|
function set(name, keyGen, values) {
|
|
362
|
+
let argCount = arguments.length;
|
|
352
363
|
return async (state) => {
|
|
364
|
+
if (argCount < 3) {
|
|
365
|
+
const e = new Error("ILLEGAL_ARGUMENTS");
|
|
366
|
+
e.description = "Insufficient arguments passed to set()";
|
|
367
|
+
e.fix = "Make sure to pass three arguments to: set(name, key/keygen, values)";
|
|
368
|
+
e.args = {
|
|
369
|
+
name,
|
|
370
|
+
keyGen,
|
|
371
|
+
values
|
|
372
|
+
};
|
|
373
|
+
throw e;
|
|
374
|
+
}
|
|
353
375
|
const batchSize = 1e3;
|
|
354
376
|
const [resolvedName, resolvedValues] = (0, import_util.expandReferences)(
|
|
355
377
|
state,
|
|
@@ -357,13 +379,18 @@ function set(name, keyGen, values) {
|
|
|
357
379
|
values
|
|
358
380
|
);
|
|
359
381
|
const dataArray = Array.isArray(resolvedValues) ? resolvedValues : [resolvedValues];
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
key:
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
382
|
+
let kvPairs;
|
|
383
|
+
if (typeof keyGen === "string" && Array.isArray(values)) {
|
|
384
|
+
kvPairs = [{ key: keyGen, value: JSON.stringify(values) }];
|
|
385
|
+
} else {
|
|
386
|
+
const keyGenFn = typeof keyGen === "string" ? () => keyGen : keyGen;
|
|
387
|
+
kvPairs = dataArray.map((value, index) => ({
|
|
388
|
+
key: keyGenFn(value, index),
|
|
389
|
+
value: JSON.stringify(value)
|
|
390
|
+
}));
|
|
391
|
+
}
|
|
392
|
+
while (kvPairs.length) {
|
|
393
|
+
const batch = kvPairs.splice(0, batchSize);
|
|
367
394
|
console.log(
|
|
368
395
|
`Collections: uploading batch of ${batch.length} values to "${name}"...`
|
|
369
396
|
);
|
|
@@ -462,7 +489,7 @@ function each(name, query = {}, callback = () => {
|
|
|
462
489
|
};
|
|
463
490
|
}
|
|
464
491
|
var streamResponse = async (response, onValue) => {
|
|
465
|
-
const pipeline =
|
|
492
|
+
const pipeline = response.body.pipe((0, import_stream_json.default)());
|
|
466
493
|
let isInsideItems = false;
|
|
467
494
|
let cursor;
|
|
468
495
|
const it = pipeline.iterator();
|
|
@@ -509,7 +536,7 @@ var streamResponse = async (response, onValue) => {
|
|
|
509
536
|
key: key.value.value,
|
|
510
537
|
value: value.value.value
|
|
511
538
|
});
|
|
512
|
-
waitFor("endObject");
|
|
539
|
+
await waitFor("endObject");
|
|
513
540
|
}
|
|
514
541
|
if (isInsideItems && token.name === "endArray") {
|
|
515
542
|
isInsideItems = false;
|
|
@@ -538,7 +565,7 @@ var parseQuery = (options = {}) => {
|
|
|
538
565
|
return result;
|
|
539
566
|
}, {});
|
|
540
567
|
};
|
|
541
|
-
var request = (state, client2, path, options = {}) => {
|
|
568
|
+
var request = async (state, client2, path, options = {}) => {
|
|
542
569
|
if (!state.configuration.collections_token) {
|
|
543
570
|
(0, import_util.throwError)("INVALID_AUTH", {
|
|
544
571
|
description: "No access key provided for collection request",
|
|
@@ -560,7 +587,29 @@ var request = (state, client2, path, options = {}) => {
|
|
|
560
587
|
query,
|
|
561
588
|
...otherOptions
|
|
562
589
|
};
|
|
563
|
-
|
|
590
|
+
const response = await client2.request(args);
|
|
591
|
+
if (response.statusCode >= 400) {
|
|
592
|
+
await handleError(response, path, state.configuration.collections_endpoint);
|
|
593
|
+
}
|
|
594
|
+
return response;
|
|
595
|
+
};
|
|
596
|
+
var handleError = async (response, path, endpoint) => {
|
|
597
|
+
if (response.statusCode === 404) {
|
|
598
|
+
const [collection] = path.split("/");
|
|
599
|
+
console.error(`Error! Collection ${collection} does not exist`);
|
|
600
|
+
const e2 = new Error("COLLECTION_NOT_FOUND");
|
|
601
|
+
e2.code = "COLLECTION_NOT_FOUND";
|
|
602
|
+
e2.description = `The collection "${collection}" does not exist`;
|
|
603
|
+
e2.collection = collection;
|
|
604
|
+
e2.endpoint = endpoint;
|
|
605
|
+
e2.fix = "Make sure the collection name is correct, and has been created by a system administrator";
|
|
606
|
+
throw e2;
|
|
607
|
+
}
|
|
608
|
+
const e = new Error("COLLECTIONS_ERROR");
|
|
609
|
+
e.code = response.statusCode;
|
|
610
|
+
e.description = "The server returned an error, see attached message";
|
|
611
|
+
e.message = await response.body.text();
|
|
612
|
+
throw e;
|
|
564
613
|
};
|
|
565
614
|
// Annotate the CommonJS export names for ESM import in node:
|
|
566
615
|
0 && (module.exports = {
|
package/dist/index.js
CHANGED
|
@@ -11,6 +11,7 @@ __export(collections_exports, {
|
|
|
11
11
|
each: () => each,
|
|
12
12
|
expandQuery: () => expandQuery,
|
|
13
13
|
get: () => get,
|
|
14
|
+
handleError: () => handleError,
|
|
14
15
|
parseQuery: () => parseQuery,
|
|
15
16
|
remove: () => remove,
|
|
16
17
|
request: () => request,
|
|
@@ -21,11 +22,7 @@ __export(collections_exports, {
|
|
|
21
22
|
import nodepath from "path";
|
|
22
23
|
import undici from "undici";
|
|
23
24
|
import { throwError, expandReferences } from "@openfn/language-common/util";
|
|
24
|
-
import chain from "stream-chain";
|
|
25
25
|
import parser from "stream-json";
|
|
26
|
-
import Pick from "stream-json/filters/Pick";
|
|
27
|
-
import streamArray from "stream-json/streamers/StreamArray";
|
|
28
|
-
import streamValues from "stream-json/streamers/StreamValues";
|
|
29
26
|
|
|
30
27
|
// src/mock.js
|
|
31
28
|
import { MockAgent } from "undici";
|
|
@@ -77,12 +74,21 @@ function API() {
|
|
|
77
74
|
return result;
|
|
78
75
|
};
|
|
79
76
|
const byKey = (name, key) => {
|
|
77
|
+
if (!(name in collections)) {
|
|
78
|
+
throw new Error(COLLECTION_NOT_FOUND);
|
|
79
|
+
}
|
|
80
80
|
return collections[name][key];
|
|
81
81
|
};
|
|
82
82
|
const asJSON = (name, key) => {
|
|
83
|
+
if (!(name in collections)) {
|
|
84
|
+
throw new Error(COLLECTION_NOT_FOUND);
|
|
85
|
+
}
|
|
83
86
|
return JSON.parse(collections[name][key]);
|
|
84
87
|
};
|
|
85
88
|
const count = (name) => {
|
|
89
|
+
if (!(name in collections)) {
|
|
90
|
+
throw new Error(COLLECTION_NOT_FOUND);
|
|
91
|
+
}
|
|
86
92
|
return Object.keys(collections[name]).length;
|
|
87
93
|
};
|
|
88
94
|
const remove2 = (name, key) => {
|
|
@@ -310,13 +316,17 @@ function get(name, query = {}) {
|
|
|
310
316
|
getClient(state),
|
|
311
317
|
`${resolvedName}/${key}`
|
|
312
318
|
);
|
|
313
|
-
|
|
314
|
-
if (body.value) {
|
|
315
|
-
data = JSON.parse(body.value);
|
|
316
|
-
console.log(`Collections: Fetched "${key}" from "${name}"`);
|
|
317
|
-
} else {
|
|
319
|
+
if (response.statusCode === 204) {
|
|
318
320
|
data = {};
|
|
319
321
|
console.warn(`Collections: Key "${key}" not found in "${name}"`);
|
|
322
|
+
} else {
|
|
323
|
+
try {
|
|
324
|
+
const body = await response.body.json();
|
|
325
|
+
data = JSON.parse(body.value);
|
|
326
|
+
} catch (e) {
|
|
327
|
+
console.error("Unexpected error parsing response data!");
|
|
328
|
+
throw e;
|
|
329
|
+
}
|
|
320
330
|
}
|
|
321
331
|
}
|
|
322
332
|
state.data = data;
|
|
@@ -324,7 +334,19 @@ function get(name, query = {}) {
|
|
|
324
334
|
};
|
|
325
335
|
}
|
|
326
336
|
function set(name, keyGen, values) {
|
|
337
|
+
let argCount = arguments.length;
|
|
327
338
|
return async (state) => {
|
|
339
|
+
if (argCount < 3) {
|
|
340
|
+
const e = new Error("ILLEGAL_ARGUMENTS");
|
|
341
|
+
e.description = "Insufficient arguments passed to set()";
|
|
342
|
+
e.fix = "Make sure to pass three arguments to: set(name, key/keygen, values)";
|
|
343
|
+
e.args = {
|
|
344
|
+
name,
|
|
345
|
+
keyGen,
|
|
346
|
+
values
|
|
347
|
+
};
|
|
348
|
+
throw e;
|
|
349
|
+
}
|
|
328
350
|
const batchSize = 1e3;
|
|
329
351
|
const [resolvedName, resolvedValues] = expandReferences(
|
|
330
352
|
state,
|
|
@@ -332,13 +354,18 @@ function set(name, keyGen, values) {
|
|
|
332
354
|
values
|
|
333
355
|
);
|
|
334
356
|
const dataArray = Array.isArray(resolvedValues) ? resolvedValues : [resolvedValues];
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
key:
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
357
|
+
let kvPairs;
|
|
358
|
+
if (typeof keyGen === "string" && Array.isArray(values)) {
|
|
359
|
+
kvPairs = [{ key: keyGen, value: JSON.stringify(values) }];
|
|
360
|
+
} else {
|
|
361
|
+
const keyGenFn = typeof keyGen === "string" ? () => keyGen : keyGen;
|
|
362
|
+
kvPairs = dataArray.map((value, index) => ({
|
|
363
|
+
key: keyGenFn(value, index),
|
|
364
|
+
value: JSON.stringify(value)
|
|
365
|
+
}));
|
|
366
|
+
}
|
|
367
|
+
while (kvPairs.length) {
|
|
368
|
+
const batch = kvPairs.splice(0, batchSize);
|
|
342
369
|
console.log(
|
|
343
370
|
`Collections: uploading batch of ${batch.length} values to "${name}"...`
|
|
344
371
|
);
|
|
@@ -437,7 +464,7 @@ function each(name, query = {}, callback = () => {
|
|
|
437
464
|
};
|
|
438
465
|
}
|
|
439
466
|
var streamResponse = async (response, onValue) => {
|
|
440
|
-
const pipeline =
|
|
467
|
+
const pipeline = response.body.pipe(parser());
|
|
441
468
|
let isInsideItems = false;
|
|
442
469
|
let cursor;
|
|
443
470
|
const it = pipeline.iterator();
|
|
@@ -484,7 +511,7 @@ var streamResponse = async (response, onValue) => {
|
|
|
484
511
|
key: key.value.value,
|
|
485
512
|
value: value.value.value
|
|
486
513
|
});
|
|
487
|
-
waitFor("endObject");
|
|
514
|
+
await waitFor("endObject");
|
|
488
515
|
}
|
|
489
516
|
if (isInsideItems && token.name === "endArray") {
|
|
490
517
|
isInsideItems = false;
|
|
@@ -513,7 +540,7 @@ var parseQuery = (options = {}) => {
|
|
|
513
540
|
return result;
|
|
514
541
|
}, {});
|
|
515
542
|
};
|
|
516
|
-
var request = (state, client2, path, options = {}) => {
|
|
543
|
+
var request = async (state, client2, path, options = {}) => {
|
|
517
544
|
if (!state.configuration.collections_token) {
|
|
518
545
|
throwError("INVALID_AUTH", {
|
|
519
546
|
description: "No access key provided for collection request",
|
|
@@ -535,7 +562,29 @@ var request = (state, client2, path, options = {}) => {
|
|
|
535
562
|
query,
|
|
536
563
|
...otherOptions
|
|
537
564
|
};
|
|
538
|
-
|
|
565
|
+
const response = await client2.request(args);
|
|
566
|
+
if (response.statusCode >= 400) {
|
|
567
|
+
await handleError(response, path, state.configuration.collections_endpoint);
|
|
568
|
+
}
|
|
569
|
+
return response;
|
|
570
|
+
};
|
|
571
|
+
var handleError = async (response, path, endpoint) => {
|
|
572
|
+
if (response.statusCode === 404) {
|
|
573
|
+
const [collection] = path.split("/");
|
|
574
|
+
console.error(`Error! Collection ${collection} does not exist`);
|
|
575
|
+
const e2 = new Error("COLLECTION_NOT_FOUND");
|
|
576
|
+
e2.code = "COLLECTION_NOT_FOUND";
|
|
577
|
+
e2.description = `The collection "${collection}" does not exist`;
|
|
578
|
+
e2.collection = collection;
|
|
579
|
+
e2.endpoint = endpoint;
|
|
580
|
+
e2.fix = "Make sure the collection name is correct, and has been created by a system administrator";
|
|
581
|
+
throw e2;
|
|
582
|
+
}
|
|
583
|
+
const e = new Error("COLLECTIONS_ERROR");
|
|
584
|
+
e.code = response.statusCode;
|
|
585
|
+
e.description = "The server returned an error, see attached message";
|
|
586
|
+
e.message = await response.body.text();
|
|
587
|
+
throw e;
|
|
539
588
|
};
|
|
540
589
|
export {
|
|
541
590
|
collections_exports as collections
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openfn/language-collections",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "OpenFn collections adaptor",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -20,7 +20,6 @@
|
|
|
20
20
|
"configuration-schema.json"
|
|
21
21
|
],
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"stream-chain": "^3.3.2",
|
|
24
23
|
"stream-json": "^1.8.0",
|
|
25
24
|
"undici": "^5.22.1",
|
|
26
25
|
"@openfn/language-common": "2.1.1"
|
package/types/collections.d.ts
CHANGED
|
@@ -45,7 +45,7 @@ export function get(name: string, query?: string | QueryOptions): (state: any) =
|
|
|
45
45
|
* @example <caption>Set a single value with a static key</caption>
|
|
46
46
|
* collections.set('my-collection', 'city-codes', { NY: 'New York', LDN: 'London' }})
|
|
47
47
|
*/
|
|
48
|
-
export function set(name: any, keyGen: any, values: any): (state: any) => Promise<any>;
|
|
48
|
+
export function set(name: any, keyGen: any, values: any, ...args: any[]): (state: any) => Promise<any>;
|
|
49
49
|
/**
|
|
50
50
|
* Remove one or more values from a collection.
|
|
51
51
|
* You can pass a specific key as a string to only fetch one item, or pass a query
|
|
@@ -91,7 +91,8 @@ export function setMockClient(mockClient: any): void;
|
|
|
91
91
|
export function streamResponse(response: any, onValue: any): Promise<any>;
|
|
92
92
|
export function expandQuery(query: any): any;
|
|
93
93
|
export function parseQuery(options?: {}): {};
|
|
94
|
-
export function request(state: any, client: any, path: any, options?: {}): any
|
|
94
|
+
export function request(state: any, client: any, path: any, options?: {}): Promise<any>;
|
|
95
|
+
export function handleError(response: any, path: any, endpoint: any): Promise<never>;
|
|
95
96
|
/**
|
|
96
97
|
* Query options. All dates should be parseable as ISO 8601 strings, see https://simple.wikipedia.org/wiki/ISO_8601
|
|
97
98
|
*/
|