@openfn/language-common 2.1.1 → 2.2.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
@@ -60,6 +60,7 @@ __export(src_exports, {
60
60
  sourceValue: () => sourceValue,
61
61
  splitKeys: () => splitKeys,
62
62
  toArray: () => toArray,
63
+ util: () => util_exports,
63
64
  validate: () => validate
64
65
  });
65
66
  module.exports = __toCommonJS(src_exports);
@@ -111,13 +112,75 @@ var import_node_stream3 = require("stream");
111
112
  var import_undici2 = require("undici");
112
113
  var import_date_fns2 = __toESM(require("date-fns"), 1);
113
114
 
115
+ // src/util/index.js
116
+ var util_exports = {};
117
+ __export(util_exports, {
118
+ ERROR_ABSOLUTE_URL: () => ERROR_ABSOLUTE_URL,
119
+ ERROR_URL_MISMATCH: () => ERROR_URL_MISMATCH,
120
+ assertRelativeUrl: () => assertRelativeUrl,
121
+ decode: () => decode,
122
+ del: () => del,
123
+ enableMockClient: () => enableMockClient,
124
+ encode: () => encode,
125
+ expandReferences: () => expandReferences,
126
+ get: () => get,
127
+ logResponse: () => logResponse,
128
+ makeBasicAuthHeader: () => makeBasicAuthHeader,
129
+ normalizeOauthConfig: () => normalizeOauthConfig,
130
+ parseDate: () => parse_date_default,
131
+ parseUrl: () => parseUrl,
132
+ post: () => post,
133
+ put: () => put,
134
+ request: () => request,
135
+ throwError: () => throw_error_default,
136
+ uuid: () => uuid
137
+ });
138
+
114
139
  // src/util/http.js
115
140
  var import_undici = require("undici");
116
141
  var import_http_status_codes = require("http-status-codes");
117
142
  var import_node_stream = require("stream");
118
143
  var import_node_querystring = __toESM(require("querystring"), 1);
119
144
  var import_node_path = __toESM(require("path"), 1);
145
+
146
+ // src/util/throw-error.js
147
+ var throw_error_default = (code, { description, fix, ...extras } = {}) => {
148
+ const message = description ? `${code}: ${description}` : code;
149
+ const e = new Error(message);
150
+ e.code = code;
151
+ if (description) {
152
+ e.description = description;
153
+ }
154
+ if (fix) {
155
+ e.fix = fix;
156
+ }
157
+ for (const key in extras) {
158
+ e[key] = extras[key];
159
+ }
160
+ throw e;
161
+ };
162
+
163
+ // src/util/http.js
120
164
  var clients = /* @__PURE__ */ new Map();
165
+ var makeBasicAuthHeader = (username, password) => {
166
+ const buff = Buffer.from(`${username}:${password}`);
167
+ const credentials = buff.toString("base64");
168
+ return { Authorization: `Basic ${credentials}` };
169
+ };
170
+ var logResponse = (response) => {
171
+ const { method, url, statusCode, duration } = response;
172
+ if (method && url && duration && statusCode) {
173
+ const message = `${method} ${url} - ${statusCode} in ${duration}ms`;
174
+ if (response instanceof Error) {
175
+ console.error(message);
176
+ console.error("response body: ");
177
+ console.error(response.body || "[no body]");
178
+ } else {
179
+ console.log(message);
180
+ }
181
+ }
182
+ return response;
183
+ };
121
184
  var getClient = (baseUrl, options2) => {
122
185
  const { tls } = options2;
123
186
  if (!clients.has(baseUrl)) {
@@ -125,15 +188,24 @@ var getClient = (baseUrl, options2) => {
125
188
  }
126
189
  return clients.get(baseUrl);
127
190
  };
191
+ var enableMockClient = (baseUrl) => {
192
+ const mockAgent = new import_undici.MockAgent({ connections: 1 });
193
+ mockAgent.disableNetConnect();
194
+ const client = mockAgent.get(baseUrl);
195
+ if (!clients.has(baseUrl)) {
196
+ clients.set(baseUrl, client);
197
+ }
198
+ return client;
199
+ };
128
200
  var assertOK = async (response, errorMap, fullUrl, method, startTime) => {
129
201
  const errMapMessage = errorMap[response.statusCode];
130
202
  const isError = typeof errMapMessage === "boolean" ? errMapMessage : errMapMessage || response.statusCode >= 400;
131
203
  if (isError) {
132
204
  const body = await readResponseBody(response);
133
205
  const statusText = (0, import_http_status_codes.getReasonPhrase)(response.statusCode);
134
- const defaultErrorMesssage = `${method} to ${fullUrl} returned ${response.statusCode}: ${statusText}`;
206
+ const defaultErrorMessage = `${method} to ${fullUrl} returned ${response.statusCode}: ${statusText}`;
135
207
  const duration = Date.now() - startTime;
136
- const errMessage = typeof errMapMessage === "function" ? errMapMessage(response) : errMapMessage || defaultErrorMesssage;
208
+ const errMessage = typeof errMapMessage === "function" ? errMapMessage(response) : errMapMessage || defaultErrorMessage;
137
209
  const error = new Error(errMessage);
138
210
  error.statusCode = response.statusCode;
139
211
  error.statusMessage = statusText;
@@ -145,6 +217,17 @@ var assertOK = async (response, errorMap, fullUrl, method, startTime) => {
145
217
  throw error;
146
218
  }
147
219
  };
220
+ var ERROR_ABSOLUTE_URL = "Absolute URLs not suppored";
221
+ var assertRelativeUrl = (path2) => {
222
+ if (/https?:\/\//.test(path2)) {
223
+ const e = new Error("UNEXPECTED_ABSOLUTE_URL");
224
+ e.code = "UNEXPECTED_ABSOLUTE_URL";
225
+ e.description = "An absolute URL was provided (https://...) but only a path (/a/b/c) is supported";
226
+ e.url = path2;
227
+ e.fix = "Remove the protocol, domain and origin from the provided URL. Maybe you need to use the generic HTTP helper functions instead?";
228
+ throw e;
229
+ }
230
+ };
148
231
  var ERROR_URL_MISMATCH = "Target origin does not match baseUrl origin";
149
232
  var parseUrl = (pathOrUrl = "", baseUrl) => {
150
233
  let fullUrl;
@@ -239,21 +322,35 @@ function encodeRequestBody(body) {
239
322
  throw new Error("Unsupported body type");
240
323
  }
241
324
  async function readResponseBody(response, parseAs) {
242
- const contentType = response.headers["content-type"];
243
- switch (parseAs) {
244
- case "json":
245
- return response.body.json();
246
- case "text":
247
- return response.body.text();
248
- case "stream":
249
- return response.body;
250
- default:
251
- return contentType && contentType.includes("application/json") ? response.body.json() : response.body.text();
325
+ try {
326
+ const contentType = response.headers["content-type"];
327
+ if (+response.headers["content-length"] === 0) {
328
+ return void 0;
329
+ }
330
+ switch (parseAs) {
331
+ case "json":
332
+ return await response.body.json();
333
+ case "text":
334
+ return response.body.text();
335
+ case "stream":
336
+ return response.body;
337
+ default:
338
+ return contentType && contentType.includes("application/json") ? await response.body.json() : response.body.text();
339
+ }
340
+ } catch (error) {
341
+ throw_error_default(response.statusCode, {
342
+ description: "Error parsing the response body",
343
+ parseAs,
344
+ contentType: response.headers["content-type"],
345
+ bodyLength: +response.headers["content-length"],
346
+ error: error.message
347
+ });
252
348
  }
253
349
  }
254
-
255
- // src/util/helpers.js
256
- var import_node_crypto = require("crypto");
350
+ var get = (url, options2) => request("GET", url, options2);
351
+ var post = (url, body, options2) => request("POST", url, { body, ...options2 });
352
+ var put = (url, body, options2) => request("PUT", url, { body, ...options2 });
353
+ var del = (url, body, options2) => request("DELETE", url, { body, ...options2 });
257
354
 
258
355
  // src/util/references.js
259
356
  var import_node_stream2 = require("stream");
@@ -288,6 +385,22 @@ function expandReference(state, value) {
288
385
  }
289
386
  return value;
290
387
  }
388
+ function normalizeOauthConfig(configuration) {
389
+ const { access_token, accessToken } = configuration;
390
+ if (access_token && accessToken)
391
+ throw new Error(
392
+ 'Both "accessToken" & "access_token" keys found in configuration; please use only "access_token" for OAuth2 credentials.'
393
+ );
394
+ if (access_token)
395
+ return { ...configuration, accessToken: access_token };
396
+ console.log(
397
+ 'Key "access_token" not found in state.configuration;',
398
+ "is this a standard OAuth 2.0 JSON credential?"
399
+ );
400
+ if (accessToken)
401
+ console.log('Using "accessToken" from state.configuration');
402
+ return configuration;
403
+ }
291
404
 
292
405
  // src/util/parse-date.js
293
406
  var import_date_fns = require("date-fns");
@@ -315,6 +428,14 @@ var parse_date_default = (d, startDate) => {
315
428
  return d;
316
429
  };
317
430
 
431
+ // src/util/base64.js
432
+ var encode = (data) => Buffer.from(data, "utf-8").toString("base64");
433
+ var decode = (base64Data) => Buffer.from(base64Data, "base64").toString("utf-8");
434
+
435
+ // src/util/uuid.js
436
+ var import_node_crypto = require("crypto");
437
+ var uuid = () => (0, import_node_crypto.randomUUID)();
438
+
318
439
  // src/Adaptor.js
319
440
  var schemaCache = {};
320
441
  function execute(...operations) {
@@ -732,9 +853,9 @@ function each2(dataSource, operation) {
732
853
  // src/http.js
733
854
  var http_exports = {};
734
855
  __export(http_exports, {
735
- get: () => get2,
856
+ get: () => get3,
736
857
  options: () => options,
737
- post: () => post,
858
+ post: () => post2,
738
859
  request: () => req
739
860
  });
740
861
  var import_set = __toESM(require("lodash/set"), 1);
@@ -787,10 +908,10 @@ var req = function(method, url, options2) {
787
908
  };
788
909
  };
789
910
  };
790
- function get2(url, options2) {
911
+ function get3(url, options2) {
791
912
  return req("GET", url, options2);
792
913
  }
793
- function post(path2, data, options2) {
914
+ function post2(path2, data, options2) {
794
915
  return req("POST", path2, { body: data, ...options2 });
795
916
  }
796
917
 
@@ -840,5 +961,6 @@ var src_default = Adaptor_exports;
840
961
  sourceValue,
841
962
  splitKeys,
842
963
  toArray,
964
+ util,
843
965
  validate
844
966
  });
package/dist/index.js CHANGED
@@ -40,7 +40,7 @@ __export(Adaptor_exports, {
40
40
  toArray: () => toArray,
41
41
  validate: () => validate
42
42
  });
43
- import get from "lodash/get.js";
43
+ import get2 from "lodash/get.js";
44
44
  import omit from "lodash/omit.js";
45
45
  import curry from "lodash/fp/curry.js";
46
46
  import groupBy from "lodash/groupBy.js";
@@ -51,13 +51,75 @@ import { Readable as Readable3 } from "stream";
51
51
  import { request as request2 } from "undici";
52
52
  import dateFns from "date-fns";
53
53
 
54
+ // src/util/index.js
55
+ var util_exports = {};
56
+ __export(util_exports, {
57
+ ERROR_ABSOLUTE_URL: () => ERROR_ABSOLUTE_URL,
58
+ ERROR_URL_MISMATCH: () => ERROR_URL_MISMATCH,
59
+ assertRelativeUrl: () => assertRelativeUrl,
60
+ decode: () => decode,
61
+ del: () => del,
62
+ enableMockClient: () => enableMockClient,
63
+ encode: () => encode,
64
+ expandReferences: () => expandReferences,
65
+ get: () => get,
66
+ logResponse: () => logResponse,
67
+ makeBasicAuthHeader: () => makeBasicAuthHeader,
68
+ normalizeOauthConfig: () => normalizeOauthConfig,
69
+ parseDate: () => parse_date_default,
70
+ parseUrl: () => parseUrl,
71
+ post: () => post,
72
+ put: () => put,
73
+ request: () => request,
74
+ throwError: () => throw_error_default,
75
+ uuid: () => uuid
76
+ });
77
+
54
78
  // src/util/http.js
55
79
  import { Client, MockAgent } from "undici";
56
80
  import { getReasonPhrase } from "http-status-codes";
57
81
  import { Readable } from "stream";
58
82
  import querystring from "querystring";
59
83
  import path from "path";
84
+
85
+ // src/util/throw-error.js
86
+ var throw_error_default = (code, { description, fix, ...extras } = {}) => {
87
+ const message = description ? `${code}: ${description}` : code;
88
+ const e = new Error(message);
89
+ e.code = code;
90
+ if (description) {
91
+ e.description = description;
92
+ }
93
+ if (fix) {
94
+ e.fix = fix;
95
+ }
96
+ for (const key in extras) {
97
+ e[key] = extras[key];
98
+ }
99
+ throw e;
100
+ };
101
+
102
+ // src/util/http.js
60
103
  var clients = /* @__PURE__ */ new Map();
104
+ var makeBasicAuthHeader = (username, password) => {
105
+ const buff = Buffer.from(`${username}:${password}`);
106
+ const credentials = buff.toString("base64");
107
+ return { Authorization: `Basic ${credentials}` };
108
+ };
109
+ var logResponse = (response) => {
110
+ const { method, url, statusCode, duration } = response;
111
+ if (method && url && duration && statusCode) {
112
+ const message = `${method} ${url} - ${statusCode} in ${duration}ms`;
113
+ if (response instanceof Error) {
114
+ console.error(message);
115
+ console.error("response body: ");
116
+ console.error(response.body || "[no body]");
117
+ } else {
118
+ console.log(message);
119
+ }
120
+ }
121
+ return response;
122
+ };
61
123
  var getClient = (baseUrl, options2) => {
62
124
  const { tls } = options2;
63
125
  if (!clients.has(baseUrl)) {
@@ -65,15 +127,24 @@ var getClient = (baseUrl, options2) => {
65
127
  }
66
128
  return clients.get(baseUrl);
67
129
  };
130
+ var enableMockClient = (baseUrl) => {
131
+ const mockAgent = new MockAgent({ connections: 1 });
132
+ mockAgent.disableNetConnect();
133
+ const client = mockAgent.get(baseUrl);
134
+ if (!clients.has(baseUrl)) {
135
+ clients.set(baseUrl, client);
136
+ }
137
+ return client;
138
+ };
68
139
  var assertOK = async (response, errorMap, fullUrl, method, startTime) => {
69
140
  const errMapMessage = errorMap[response.statusCode];
70
141
  const isError = typeof errMapMessage === "boolean" ? errMapMessage : errMapMessage || response.statusCode >= 400;
71
142
  if (isError) {
72
143
  const body = await readResponseBody(response);
73
144
  const statusText = getReasonPhrase(response.statusCode);
74
- const defaultErrorMesssage = `${method} to ${fullUrl} returned ${response.statusCode}: ${statusText}`;
145
+ const defaultErrorMessage = `${method} to ${fullUrl} returned ${response.statusCode}: ${statusText}`;
75
146
  const duration = Date.now() - startTime;
76
- const errMessage = typeof errMapMessage === "function" ? errMapMessage(response) : errMapMessage || defaultErrorMesssage;
147
+ const errMessage = typeof errMapMessage === "function" ? errMapMessage(response) : errMapMessage || defaultErrorMessage;
77
148
  const error = new Error(errMessage);
78
149
  error.statusCode = response.statusCode;
79
150
  error.statusMessage = statusText;
@@ -85,6 +156,17 @@ var assertOK = async (response, errorMap, fullUrl, method, startTime) => {
85
156
  throw error;
86
157
  }
87
158
  };
159
+ var ERROR_ABSOLUTE_URL = "Absolute URLs not suppored";
160
+ var assertRelativeUrl = (path2) => {
161
+ if (/https?:\/\//.test(path2)) {
162
+ const e = new Error("UNEXPECTED_ABSOLUTE_URL");
163
+ e.code = "UNEXPECTED_ABSOLUTE_URL";
164
+ e.description = "An absolute URL was provided (https://...) but only a path (/a/b/c) is supported";
165
+ e.url = path2;
166
+ e.fix = "Remove the protocol, domain and origin from the provided URL. Maybe you need to use the generic HTTP helper functions instead?";
167
+ throw e;
168
+ }
169
+ };
88
170
  var ERROR_URL_MISMATCH = "Target origin does not match baseUrl origin";
89
171
  var parseUrl = (pathOrUrl = "", baseUrl) => {
90
172
  let fullUrl;
@@ -179,18 +261,35 @@ function encodeRequestBody(body) {
179
261
  throw new Error("Unsupported body type");
180
262
  }
181
263
  async function readResponseBody(response, parseAs) {
182
- const contentType = response.headers["content-type"];
183
- switch (parseAs) {
184
- case "json":
185
- return response.body.json();
186
- case "text":
187
- return response.body.text();
188
- case "stream":
189
- return response.body;
190
- default:
191
- return contentType && contentType.includes("application/json") ? response.body.json() : response.body.text();
264
+ try {
265
+ const contentType = response.headers["content-type"];
266
+ if (+response.headers["content-length"] === 0) {
267
+ return void 0;
268
+ }
269
+ switch (parseAs) {
270
+ case "json":
271
+ return await response.body.json();
272
+ case "text":
273
+ return response.body.text();
274
+ case "stream":
275
+ return response.body;
276
+ default:
277
+ return contentType && contentType.includes("application/json") ? await response.body.json() : response.body.text();
278
+ }
279
+ } catch (error) {
280
+ throw_error_default(response.statusCode, {
281
+ description: "Error parsing the response body",
282
+ parseAs,
283
+ contentType: response.headers["content-type"],
284
+ bodyLength: +response.headers["content-length"],
285
+ error: error.message
286
+ });
192
287
  }
193
288
  }
289
+ var get = (url, options2) => request("GET", url, options2);
290
+ var post = (url, body, options2) => request("POST", url, { body, ...options2 });
291
+ var put = (url, body, options2) => request("PUT", url, { body, ...options2 });
292
+ var del = (url, body, options2) => request("DELETE", url, { body, ...options2 });
194
293
 
195
294
  // src/util/references.js
196
295
  import { Readable as Readable2, Writable } from "stream";
@@ -225,6 +324,22 @@ function expandReference(state, value) {
225
324
  }
226
325
  return value;
227
326
  }
327
+ function normalizeOauthConfig(configuration) {
328
+ const { access_token, accessToken } = configuration;
329
+ if (access_token && accessToken)
330
+ throw new Error(
331
+ 'Both "accessToken" & "access_token" keys found in configuration; please use only "access_token" for OAuth2 credentials.'
332
+ );
333
+ if (access_token)
334
+ return { ...configuration, accessToken: access_token };
335
+ console.log(
336
+ 'Key "access_token" not found in state.configuration;',
337
+ "is this a standard OAuth 2.0 JSON credential?"
338
+ );
339
+ if (accessToken)
340
+ console.log('Using "accessToken" from state.configuration');
341
+ return configuration;
342
+ }
228
343
 
229
344
  // src/util/parse-date.js
230
345
  import { startOfToday, startOfYesterday, subHours, subDays, startOfDay } from "date-fns";
@@ -252,6 +367,14 @@ var parse_date_default = (d, startDate) => {
252
367
  return d;
253
368
  };
254
369
 
370
+ // src/util/base64.js
371
+ var encode = (data) => Buffer.from(data, "utf-8").toString("base64");
372
+ var decode = (base64Data) => Buffer.from(base64Data, "base64").toString("utf-8");
373
+
374
+ // src/util/uuid.js
375
+ import { randomUUID } from "crypto";
376
+ var uuid = () => randomUUID();
377
+
255
378
  // src/Adaptor.js
256
379
  var schemaCache = {};
257
380
  function execute(...operations) {
@@ -404,7 +527,7 @@ function group(arrayOfObjects, keyPath, callback = (s) => s) {
404
527
  arrayOfObjects,
405
528
  keyPath
406
529
  );
407
- const results = groupBy(resolvedArray, (item) => get(item, resolvedKeyPath));
530
+ const results = groupBy(resolvedArray, (item) => get2(item, resolvedKeyPath));
408
531
  return callback({ ...state, data: omit(results, [void 0]) });
409
532
  };
410
533
  }
@@ -669,9 +792,9 @@ function each2(dataSource, operation) {
669
792
  // src/http.js
670
793
  var http_exports = {};
671
794
  __export(http_exports, {
672
- get: () => get2,
795
+ get: () => get3,
673
796
  options: () => options,
674
- post: () => post,
797
+ post: () => post2,
675
798
  request: () => req
676
799
  });
677
800
  import set from "lodash/set";
@@ -724,10 +847,10 @@ var req = function(method, url, options2) {
724
847
  };
725
848
  };
726
849
  };
727
- function get2(url, options2) {
850
+ function get3(url, options2) {
728
851
  return req("GET", url, options2);
729
852
  }
730
- function post(path2, data, options2) {
853
+ function post2(path2, data, options2) {
731
854
  return req("POST", path2, { body: data, ...options2 });
732
855
  }
733
856
 
@@ -780,5 +903,6 @@ export {
780
903
  sourceValue,
781
904
  splitKeys,
782
905
  toArray,
906
+ util_exports as util,
783
907
  validate
784
908
  };
package/dist/util.cjs CHANGED
@@ -53,6 +53,25 @@ var import_http_status_codes = require("http-status-codes");
53
53
  var import_node_stream = require("stream");
54
54
  var import_node_querystring = __toESM(require("querystring"), 1);
55
55
  var import_node_path = __toESM(require("path"), 1);
56
+
57
+ // src/util/throw-error.js
58
+ var throw_error_default = (code, { description, fix, ...extras } = {}) => {
59
+ const message = description ? `${code}: ${description}` : code;
60
+ const e = new Error(message);
61
+ e.code = code;
62
+ if (description) {
63
+ e.description = description;
64
+ }
65
+ if (fix) {
66
+ e.fix = fix;
67
+ }
68
+ for (const key in extras) {
69
+ e[key] = extras[key];
70
+ }
71
+ throw e;
72
+ };
73
+
74
+ // src/util/http.js
56
75
  var clients = /* @__PURE__ */ new Map();
57
76
  var makeBasicAuthHeader = (username, password) => {
58
77
  const buff = Buffer.from(`${username}:${password}`);
@@ -95,9 +114,9 @@ var assertOK = async (response, errorMap, fullUrl, method, startTime) => {
95
114
  if (isError) {
96
115
  const body = await readResponseBody(response);
97
116
  const statusText = (0, import_http_status_codes.getReasonPhrase)(response.statusCode);
98
- const defaultErrorMesssage = `${method} to ${fullUrl} returned ${response.statusCode}: ${statusText}`;
117
+ const defaultErrorMessage = `${method} to ${fullUrl} returned ${response.statusCode}: ${statusText}`;
99
118
  const duration = Date.now() - startTime;
100
- const errMessage = typeof errMapMessage === "function" ? errMapMessage(response) : errMapMessage || defaultErrorMesssage;
119
+ const errMessage = typeof errMapMessage === "function" ? errMapMessage(response) : errMapMessage || defaultErrorMessage;
101
120
  const error = new Error(errMessage);
102
121
  error.statusCode = response.statusCode;
103
122
  error.statusMessage = statusText;
@@ -214,16 +233,29 @@ function encodeRequestBody(body) {
214
233
  throw new Error("Unsupported body type");
215
234
  }
216
235
  async function readResponseBody(response, parseAs) {
217
- const contentType = response.headers["content-type"];
218
- switch (parseAs) {
219
- case "json":
220
- return response.body.json();
221
- case "text":
222
- return response.body.text();
223
- case "stream":
224
- return response.body;
225
- default:
226
- return contentType && contentType.includes("application/json") ? response.body.json() : response.body.text();
236
+ try {
237
+ const contentType = response.headers["content-type"];
238
+ if (+response.headers["content-length"] === 0) {
239
+ return void 0;
240
+ }
241
+ switch (parseAs) {
242
+ case "json":
243
+ return await response.body.json();
244
+ case "text":
245
+ return response.body.text();
246
+ case "stream":
247
+ return response.body;
248
+ default:
249
+ return contentType && contentType.includes("application/json") ? await response.body.json() : response.body.text();
250
+ }
251
+ } catch (error) {
252
+ throw_error_default(response.statusCode, {
253
+ description: "Error parsing the response body",
254
+ parseAs,
255
+ contentType: response.headers["content-type"],
256
+ bodyLength: +response.headers["content-length"],
257
+ error: error.message
258
+ });
227
259
  }
228
260
  }
229
261
  var get = (url, options) => request("GET", url, options);
@@ -231,12 +263,6 @@ var post = (url, body, options) => request("POST", url, { body, ...options });
231
263
  var put = (url, body, options) => request("PUT", url, { body, ...options });
232
264
  var del = (url, body, options) => request("DELETE", url, { body, ...options });
233
265
 
234
- // src/util/helpers.js
235
- var import_node_crypto = require("crypto");
236
- var encode = (data) => Buffer.from(data, "utf-8").toString("base64");
237
- var decode = (base64Data) => Buffer.from(base64Data, "base64").toString("utf-8");
238
- var uuid = () => (0, import_node_crypto.randomUUID)();
239
-
240
266
  // src/util/references.js
241
267
  var import_node_stream2 = require("stream");
242
268
  function expandReferences(state, ...args) {
@@ -313,22 +339,13 @@ var parse_date_default = (d, startDate) => {
313
339
  return d;
314
340
  };
315
341
 
316
- // src/util/throw-error.js
317
- var throw_error_default = (code, { description, fix, ...extras } = {}) => {
318
- const message = description ? `${code}: ${description}` : code;
319
- const e = new Error(message);
320
- e.code = code;
321
- if (description) {
322
- e.description = description;
323
- }
324
- if (fix) {
325
- e.fix = fix;
326
- }
327
- for (const key in extras) {
328
- e[key] = extras[key];
329
- }
330
- throw e;
331
- };
342
+ // src/util/base64.js
343
+ var encode = (data) => Buffer.from(data, "utf-8").toString("base64");
344
+ var decode = (base64Data) => Buffer.from(base64Data, "base64").toString("utf-8");
345
+
346
+ // src/util/uuid.js
347
+ var import_node_crypto = require("crypto");
348
+ var uuid = () => (0, import_node_crypto.randomUUID)();
332
349
  // Annotate the CommonJS export names for ESM import in node:
333
350
  0 && (module.exports = {
334
351
  ERROR_ABSOLUTE_URL,
package/dist/util.js CHANGED
@@ -4,6 +4,25 @@ import { getReasonPhrase } from "http-status-codes";
4
4
  import { Readable } from "stream";
5
5
  import querystring from "querystring";
6
6
  import path from "path";
7
+
8
+ // src/util/throw-error.js
9
+ var throw_error_default = (code, { description, fix, ...extras } = {}) => {
10
+ const message = description ? `${code}: ${description}` : code;
11
+ const e = new Error(message);
12
+ e.code = code;
13
+ if (description) {
14
+ e.description = description;
15
+ }
16
+ if (fix) {
17
+ e.fix = fix;
18
+ }
19
+ for (const key in extras) {
20
+ e[key] = extras[key];
21
+ }
22
+ throw e;
23
+ };
24
+
25
+ // src/util/http.js
7
26
  var clients = /* @__PURE__ */ new Map();
8
27
  var makeBasicAuthHeader = (username, password) => {
9
28
  const buff = Buffer.from(`${username}:${password}`);
@@ -46,9 +65,9 @@ var assertOK = async (response, errorMap, fullUrl, method, startTime) => {
46
65
  if (isError) {
47
66
  const body = await readResponseBody(response);
48
67
  const statusText = getReasonPhrase(response.statusCode);
49
- const defaultErrorMesssage = `${method} to ${fullUrl} returned ${response.statusCode}: ${statusText}`;
68
+ const defaultErrorMessage = `${method} to ${fullUrl} returned ${response.statusCode}: ${statusText}`;
50
69
  const duration = Date.now() - startTime;
51
- const errMessage = typeof errMapMessage === "function" ? errMapMessage(response) : errMapMessage || defaultErrorMesssage;
70
+ const errMessage = typeof errMapMessage === "function" ? errMapMessage(response) : errMapMessage || defaultErrorMessage;
52
71
  const error = new Error(errMessage);
53
72
  error.statusCode = response.statusCode;
54
73
  error.statusMessage = statusText;
@@ -165,16 +184,29 @@ function encodeRequestBody(body) {
165
184
  throw new Error("Unsupported body type");
166
185
  }
167
186
  async function readResponseBody(response, parseAs) {
168
- const contentType = response.headers["content-type"];
169
- switch (parseAs) {
170
- case "json":
171
- return response.body.json();
172
- case "text":
173
- return response.body.text();
174
- case "stream":
175
- return response.body;
176
- default:
177
- return contentType && contentType.includes("application/json") ? response.body.json() : response.body.text();
187
+ try {
188
+ const contentType = response.headers["content-type"];
189
+ if (+response.headers["content-length"] === 0) {
190
+ return void 0;
191
+ }
192
+ switch (parseAs) {
193
+ case "json":
194
+ return await response.body.json();
195
+ case "text":
196
+ return response.body.text();
197
+ case "stream":
198
+ return response.body;
199
+ default:
200
+ return contentType && contentType.includes("application/json") ? await response.body.json() : response.body.text();
201
+ }
202
+ } catch (error) {
203
+ throw_error_default(response.statusCode, {
204
+ description: "Error parsing the response body",
205
+ parseAs,
206
+ contentType: response.headers["content-type"],
207
+ bodyLength: +response.headers["content-length"],
208
+ error: error.message
209
+ });
178
210
  }
179
211
  }
180
212
  var get = (url, options) => request("GET", url, options);
@@ -182,12 +214,6 @@ var post = (url, body, options) => request("POST", url, { body, ...options });
182
214
  var put = (url, body, options) => request("PUT", url, { body, ...options });
183
215
  var del = (url, body, options) => request("DELETE", url, { body, ...options });
184
216
 
185
- // src/util/helpers.js
186
- import { randomUUID } from "crypto";
187
- var encode = (data) => Buffer.from(data, "utf-8").toString("base64");
188
- var decode = (base64Data) => Buffer.from(base64Data, "base64").toString("utf-8");
189
- var uuid = () => randomUUID();
190
-
191
217
  // src/util/references.js
192
218
  import { Readable as Readable2, Writable } from "stream";
193
219
  function expandReferences(state, ...args) {
@@ -264,22 +290,13 @@ var parse_date_default = (d, startDate) => {
264
290
  return d;
265
291
  };
266
292
 
267
- // src/util/throw-error.js
268
- var throw_error_default = (code, { description, fix, ...extras } = {}) => {
269
- const message = description ? `${code}: ${description}` : code;
270
- const e = new Error(message);
271
- e.code = code;
272
- if (description) {
273
- e.description = description;
274
- }
275
- if (fix) {
276
- e.fix = fix;
277
- }
278
- for (const key in extras) {
279
- e[key] = extras[key];
280
- }
281
- throw e;
282
- };
293
+ // src/util/base64.js
294
+ var encode = (data) => Buffer.from(data, "utf-8").toString("base64");
295
+ var decode = (base64Data) => Buffer.from(base64Data, "base64").toString("utf-8");
296
+
297
+ // src/util/uuid.js
298
+ import { randomUUID } from "crypto";
299
+ var uuid = () => randomUUID();
283
300
  export {
284
301
  ERROR_ABSOLUTE_URL,
285
302
  ERROR_URL_MISMATCH,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openfn/language-common",
3
- "version": "2.1.1",
3
+ "version": "2.2.1",
4
4
  "description": "Common Expressions for OpenFn",
5
5
  "homepage": "https://docs.openfn.org",
6
6
  "repository": {
@@ -39,7 +39,7 @@
39
39
  "csvtojson": "^2.0.10",
40
40
  "date-fns": "^2.25.0",
41
41
  "http-status-codes": "^2.3.0",
42
- "jsonpath-plus": "^10.0.0",
42
+ "jsonpath-plus": "^10.2.0",
43
43
  "lodash": "^4.17.21",
44
44
  "undici": "^5.28.4"
45
45
  },
package/types/http.d.ts CHANGED
@@ -8,7 +8,7 @@
8
8
  * @function
9
9
  * @public
10
10
  * @example <caption>Get with a query an oath token</caption>
11
- * get($.data.url, http.options({ query: $.query }).oath($.configuration.access_token)
11
+ * get($.data.url, http.options({ query: $.query }).oath($.configuration.access_token))
12
12
  */
13
13
  export function options(opts?: {}): any;
14
14
  /**
package/types/index.d.ts CHANGED
@@ -4,5 +4,6 @@ export * from './Adaptor';
4
4
  export * as beta from './beta';
5
5
  export * as http from './http';
6
6
  export * as dateFns from './dateFns';
7
+ export * as util from './util';
7
8
  import * as metadata from './metadata';
8
9
  export { metadata };
@@ -1,4 +1,4 @@
1
- declare type Entity = {
1
+ export declare type Entity = {
2
2
  name: string;
3
3
  type: string;
4
4
  label?: string;
@@ -8,4 +8,4 @@ declare type Entity = {
8
8
  meta?: Record<string, any>;
9
9
  addChild: (e: Entity, name?: string) => void;
10
10
  };
11
- declare type DataType = 'string' | 'boolean' | 'date';
11
+ export declare type DataType = 'string' | 'boolean' | 'date';
@@ -1,3 +1,2 @@
1
1
  export function encode(data: string): string;
2
2
  export function decode(base64Data: string): string;
3
- export function uuid(): string;
@@ -1,6 +1,6 @@
1
1
  export * from "./http";
2
- export * from "./helpers";
3
2
  export * from "./references";
4
- import parseDate from "./parse-date";
5
- import throwError from "./throw-error";
6
- export { parseDate, throwError };
3
+ export { default as parseDate } from "./parse-date";
4
+ export { default as throwError } from "./throw-error";
5
+ export { uuid } from "./uuid";
6
+ export { encode, decode } from "./base64";
@@ -0,0 +1 @@
1
+ export function uuid(): string;