@openfn/language-collections 0.5.2 → 0.6.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/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,
@@ -98,12 +99,21 @@ function API() {
98
99
  return result;
99
100
  };
100
101
  const byKey = (name, key) => {
102
+ if (!(name in collections)) {
103
+ throw new Error(COLLECTION_NOT_FOUND);
104
+ }
101
105
  return collections[name][key];
102
106
  };
103
107
  const asJSON = (name, key) => {
108
+ if (!(name in collections)) {
109
+ throw new Error(COLLECTION_NOT_FOUND);
110
+ }
104
111
  return JSON.parse(collections[name][key]);
105
112
  };
106
113
  const count = (name) => {
114
+ if (!(name in collections)) {
115
+ throw new Error(COLLECTION_NOT_FOUND);
116
+ }
107
117
  return Object.keys(collections[name]).length;
108
118
  };
109
119
  const remove2 = (name, key) => {
@@ -331,13 +341,17 @@ function get(name, query = {}) {
331
341
  getClient(state),
332
342
  `${resolvedName}/${key}`
333
343
  );
334
- const body = await response.body.json();
335
- if (body.value) {
336
- data = JSON.parse(body.value);
337
- console.log(`Collections: Fetched "${key}" from "${name}"`);
338
- } else {
344
+ if (response.statusCode === 204) {
339
345
  data = {};
340
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
+ }
341
355
  }
342
356
  }
343
357
  state.data = data;
@@ -345,21 +359,38 @@ function get(name, query = {}) {
345
359
  };
346
360
  }
347
361
  function set(name, keyGen, values) {
362
+ let argCount = arguments.length;
348
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
+ }
349
375
  const batchSize = 1e3;
350
376
  const [resolvedName, resolvedValues] = (0, import_util.expandReferences)(
351
377
  state,
352
378
  name,
353
379
  values
354
380
  );
355
- const dataArray = Array.isArray(resolvedValues) ? resolvedValues : [resolvedValues];
356
- const keyGenFn = typeof keyGen === "string" ? () => keyGen : keyGen;
357
- const pairs = dataArray.map((value, index) => ({
358
- key: keyGenFn(value, index),
359
- value: JSON.stringify(value)
360
- }));
361
- while (pairs.length) {
362
- const batch = pairs.splice(0, batchSize);
381
+ let kvPairs;
382
+ if (typeof keyGen === "string" && Array.isArray(resolvedValues)) {
383
+ kvPairs = [{ key: keyGen, value: JSON.stringify(resolvedValues) }];
384
+ } else {
385
+ const dataArray = Array.isArray(resolvedValues) ? resolvedValues : [resolvedValues];
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);
363
394
  console.log(
364
395
  `Collections: uploading batch of ${batch.length} values to "${name}"...`
365
396
  );
@@ -534,7 +565,7 @@ var parseQuery = (options = {}) => {
534
565
  return result;
535
566
  }, {});
536
567
  };
537
- var request = (state, client2, path, options = {}) => {
568
+ var request = async (state, client2, path, options = {}) => {
538
569
  if (!state.configuration.collections_token) {
539
570
  (0, import_util.throwError)("INVALID_AUTH", {
540
571
  description: "No access key provided for collection request",
@@ -556,7 +587,29 @@ var request = (state, client2, path, options = {}) => {
556
587
  query,
557
588
  ...otherOptions
558
589
  };
559
- return client2.request(args);
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;
560
613
  };
561
614
  // Annotate the CommonJS export names for ESM import in node:
562
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,
@@ -73,12 +74,21 @@ function API() {
73
74
  return result;
74
75
  };
75
76
  const byKey = (name, key) => {
77
+ if (!(name in collections)) {
78
+ throw new Error(COLLECTION_NOT_FOUND);
79
+ }
76
80
  return collections[name][key];
77
81
  };
78
82
  const asJSON = (name, key) => {
83
+ if (!(name in collections)) {
84
+ throw new Error(COLLECTION_NOT_FOUND);
85
+ }
79
86
  return JSON.parse(collections[name][key]);
80
87
  };
81
88
  const count = (name) => {
89
+ if (!(name in collections)) {
90
+ throw new Error(COLLECTION_NOT_FOUND);
91
+ }
82
92
  return Object.keys(collections[name]).length;
83
93
  };
84
94
  const remove2 = (name, key) => {
@@ -306,13 +316,17 @@ function get(name, query = {}) {
306
316
  getClient(state),
307
317
  `${resolvedName}/${key}`
308
318
  );
309
- const body = await response.body.json();
310
- if (body.value) {
311
- data = JSON.parse(body.value);
312
- console.log(`Collections: Fetched "${key}" from "${name}"`);
313
- } else {
319
+ if (response.statusCode === 204) {
314
320
  data = {};
315
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
+ }
316
330
  }
317
331
  }
318
332
  state.data = data;
@@ -320,21 +334,38 @@ function get(name, query = {}) {
320
334
  };
321
335
  }
322
336
  function set(name, keyGen, values) {
337
+ let argCount = arguments.length;
323
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
+ }
324
350
  const batchSize = 1e3;
325
351
  const [resolvedName, resolvedValues] = expandReferences(
326
352
  state,
327
353
  name,
328
354
  values
329
355
  );
330
- const dataArray = Array.isArray(resolvedValues) ? resolvedValues : [resolvedValues];
331
- const keyGenFn = typeof keyGen === "string" ? () => keyGen : keyGen;
332
- const pairs = dataArray.map((value, index) => ({
333
- key: keyGenFn(value, index),
334
- value: JSON.stringify(value)
335
- }));
336
- while (pairs.length) {
337
- const batch = pairs.splice(0, batchSize);
356
+ let kvPairs;
357
+ if (typeof keyGen === "string" && Array.isArray(resolvedValues)) {
358
+ kvPairs = [{ key: keyGen, value: JSON.stringify(resolvedValues) }];
359
+ } else {
360
+ const dataArray = Array.isArray(resolvedValues) ? resolvedValues : [resolvedValues];
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);
338
369
  console.log(
339
370
  `Collections: uploading batch of ${batch.length} values to "${name}"...`
340
371
  );
@@ -509,7 +540,7 @@ var parseQuery = (options = {}) => {
509
540
  return result;
510
541
  }, {});
511
542
  };
512
- var request = (state, client2, path, options = {}) => {
543
+ var request = async (state, client2, path, options = {}) => {
513
544
  if (!state.configuration.collections_token) {
514
545
  throwError("INVALID_AUTH", {
515
546
  description: "No access key provided for collection request",
@@ -531,7 +562,29 @@ var request = (state, client2, path, options = {}) => {
531
562
  query,
532
563
  ...otherOptions
533
564
  };
534
- return client2.request(args);
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;
535
588
  };
536
589
  export {
537
590
  collections_exports as collections
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openfn/language-collections",
3
- "version": "0.5.2",
3
+ "version": "0.6.1",
4
4
  "description": "OpenFn collections adaptor",
5
5
  "type": "module",
6
6
  "exports": {
@@ -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
  */