@openfn/language-common 3.0.0 → 3.0.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
@@ -218,11 +218,46 @@ var getClient = (baseUrl, options2) => {
218
218
  }
219
219
  return clients.get(baseUrl);
220
220
  };
221
- var enableMockClient = (baseUrl) => {
221
+ var enableMockClient = (baseUrl, options2 = {}) => {
222
+ const { defaultContentType = "application/json" } = options2;
222
223
  const mockAgent = new import_undici.MockAgent({ connections: 1 });
223
224
  mockAgent.disableNetConnect();
224
225
  const client = mockAgent.get(baseUrl);
225
226
  if (!clients.has(baseUrl)) {
227
+ if (defaultContentType) {
228
+ const _intercept = client.intercept;
229
+ client.intercept = (...args) => {
230
+ const interceptor = _intercept.apply(client, args);
231
+ const _reply = interceptor.reply;
232
+ const ensureJsonHeader = (headers = {}) => {
233
+ const hasJsonHeader = Object.keys(headers).find(
234
+ (k) => /content-type/i.test(k)
235
+ );
236
+ if (!hasJsonHeader) {
237
+ headers["content-type"] = defaultContentType;
238
+ }
239
+ };
240
+ const reply = (...args2) => {
241
+ if (typeof args2[0] === "function") {
242
+ const response = _reply.apply(interceptor, args2);
243
+ if (response.body) {
244
+ response.headers ?? (response.headers = {});
245
+ ensureJsonHeader(response.headers);
246
+ }
247
+ return response;
248
+ } else {
249
+ const [code, data, options3 = {}] = args2;
250
+ if (data) {
251
+ options3.headers ?? (options3.headers = {});
252
+ ensureJsonHeader(options3.headers);
253
+ }
254
+ return _reply.call(interceptor, code, data, options3);
255
+ }
256
+ };
257
+ interceptor.reply = reply;
258
+ return interceptor;
259
+ };
260
+ }
226
261
  clients.set(baseUrl, client);
227
262
  }
228
263
  return client;
@@ -360,15 +395,15 @@ function encodeRequestBody(body) {
360
395
  throw new Error("Unsupported body type");
361
396
  }
362
397
  async function readResponseBody(response, parseAs) {
363
- var _a;
364
- const contentLength = parseInt(
365
- response.headers["content-length"] ?? ((_a = response.body) == null ? void 0 : _a.readableLength)
366
- );
398
+ let contentLength = -1;
399
+ if ("content-length" in response.headers) {
400
+ contentLength = parseInt(response.headers["content-length"]);
401
+ }
367
402
  const contentType = response.headers["content-type"];
403
+ if (contentLength === 0 || !contentType || response.statusCode === 204) {
404
+ return;
405
+ }
368
406
  try {
369
- if (Number.isNaN(contentLength) || contentLength === 0) {
370
- return void 0;
371
- }
372
407
  switch (parseAs) {
373
408
  case "json":
374
409
  return await response.body.json();
package/dist/index.js CHANGED
@@ -153,11 +153,46 @@ var getClient = (baseUrl, options2) => {
153
153
  }
154
154
  return clients.get(baseUrl);
155
155
  };
156
- var enableMockClient = (baseUrl) => {
156
+ var enableMockClient = (baseUrl, options2 = {}) => {
157
+ const { defaultContentType = "application/json" } = options2;
157
158
  const mockAgent = new MockAgent({ connections: 1 });
158
159
  mockAgent.disableNetConnect();
159
160
  const client = mockAgent.get(baseUrl);
160
161
  if (!clients.has(baseUrl)) {
162
+ if (defaultContentType) {
163
+ const _intercept = client.intercept;
164
+ client.intercept = (...args) => {
165
+ const interceptor = _intercept.apply(client, args);
166
+ const _reply = interceptor.reply;
167
+ const ensureJsonHeader = (headers = {}) => {
168
+ const hasJsonHeader = Object.keys(headers).find(
169
+ (k) => /content-type/i.test(k)
170
+ );
171
+ if (!hasJsonHeader) {
172
+ headers["content-type"] = defaultContentType;
173
+ }
174
+ };
175
+ const reply = (...args2) => {
176
+ if (typeof args2[0] === "function") {
177
+ const response = _reply.apply(interceptor, args2);
178
+ if (response.body) {
179
+ response.headers ?? (response.headers = {});
180
+ ensureJsonHeader(response.headers);
181
+ }
182
+ return response;
183
+ } else {
184
+ const [code, data, options3 = {}] = args2;
185
+ if (data) {
186
+ options3.headers ?? (options3.headers = {});
187
+ ensureJsonHeader(options3.headers);
188
+ }
189
+ return _reply.call(interceptor, code, data, options3);
190
+ }
191
+ };
192
+ interceptor.reply = reply;
193
+ return interceptor;
194
+ };
195
+ }
161
196
  clients.set(baseUrl, client);
162
197
  }
163
198
  return client;
@@ -295,15 +330,15 @@ function encodeRequestBody(body) {
295
330
  throw new Error("Unsupported body type");
296
331
  }
297
332
  async function readResponseBody(response, parseAs) {
298
- var _a;
299
- const contentLength = parseInt(
300
- response.headers["content-length"] ?? ((_a = response.body) == null ? void 0 : _a.readableLength)
301
- );
333
+ let contentLength = -1;
334
+ if ("content-length" in response.headers) {
335
+ contentLength = parseInt(response.headers["content-length"]);
336
+ }
302
337
  const contentType = response.headers["content-type"];
338
+ if (contentLength === 0 || !contentType || response.statusCode === 204) {
339
+ return;
340
+ }
303
341
  try {
304
- if (Number.isNaN(contentLength) || contentLength === 0) {
305
- return void 0;
306
- }
307
342
  switch (parseAs) {
308
343
  case "json":
309
344
  return await response.body.json();
package/dist/util.cjs CHANGED
@@ -125,11 +125,46 @@ var getClient = (baseUrl, options) => {
125
125
  }
126
126
  return clients.get(baseUrl);
127
127
  };
128
- var enableMockClient = (baseUrl) => {
128
+ var enableMockClient = (baseUrl, options = {}) => {
129
+ const { defaultContentType = "application/json" } = options;
129
130
  const mockAgent = new import_undici.MockAgent({ connections: 1 });
130
131
  mockAgent.disableNetConnect();
131
132
  const client = mockAgent.get(baseUrl);
132
133
  if (!clients.has(baseUrl)) {
134
+ if (defaultContentType) {
135
+ const _intercept = client.intercept;
136
+ client.intercept = (...args) => {
137
+ const interceptor = _intercept.apply(client, args);
138
+ const _reply = interceptor.reply;
139
+ const ensureJsonHeader = (headers = {}) => {
140
+ const hasJsonHeader = Object.keys(headers).find(
141
+ (k) => /content-type/i.test(k)
142
+ );
143
+ if (!hasJsonHeader) {
144
+ headers["content-type"] = defaultContentType;
145
+ }
146
+ };
147
+ const reply = (...args2) => {
148
+ if (typeof args2[0] === "function") {
149
+ const response = _reply.apply(interceptor, args2);
150
+ if (response.body) {
151
+ response.headers ?? (response.headers = {});
152
+ ensureJsonHeader(response.headers);
153
+ }
154
+ return response;
155
+ } else {
156
+ const [code, data, options2 = {}] = args2;
157
+ if (data) {
158
+ options2.headers ?? (options2.headers = {});
159
+ ensureJsonHeader(options2.headers);
160
+ }
161
+ return _reply.call(interceptor, code, data, options2);
162
+ }
163
+ };
164
+ interceptor.reply = reply;
165
+ return interceptor;
166
+ };
167
+ }
133
168
  clients.set(baseUrl, client);
134
169
  }
135
170
  return client;
@@ -267,15 +302,15 @@ function encodeRequestBody(body) {
267
302
  throw new Error("Unsupported body type");
268
303
  }
269
304
  async function readResponseBody(response, parseAs) {
270
- var _a;
271
- const contentLength = parseInt(
272
- response.headers["content-length"] ?? ((_a = response.body) == null ? void 0 : _a.readableLength)
273
- );
305
+ let contentLength = -1;
306
+ if ("content-length" in response.headers) {
307
+ contentLength = parseInt(response.headers["content-length"]);
308
+ }
274
309
  const contentType = response.headers["content-type"];
310
+ if (contentLength === 0 || !contentType || response.statusCode === 204) {
311
+ return;
312
+ }
275
313
  try {
276
- if (Number.isNaN(contentLength) || contentLength === 0) {
277
- return void 0;
278
- }
279
314
  switch (parseAs) {
280
315
  case "json":
281
316
  return await response.body.json();
package/dist/util.js CHANGED
@@ -76,11 +76,46 @@ var getClient = (baseUrl, options) => {
76
76
  }
77
77
  return clients.get(baseUrl);
78
78
  };
79
- var enableMockClient = (baseUrl) => {
79
+ var enableMockClient = (baseUrl, options = {}) => {
80
+ const { defaultContentType = "application/json" } = options;
80
81
  const mockAgent = new MockAgent({ connections: 1 });
81
82
  mockAgent.disableNetConnect();
82
83
  const client = mockAgent.get(baseUrl);
83
84
  if (!clients.has(baseUrl)) {
85
+ if (defaultContentType) {
86
+ const _intercept = client.intercept;
87
+ client.intercept = (...args) => {
88
+ const interceptor = _intercept.apply(client, args);
89
+ const _reply = interceptor.reply;
90
+ const ensureJsonHeader = (headers = {}) => {
91
+ const hasJsonHeader = Object.keys(headers).find(
92
+ (k) => /content-type/i.test(k)
93
+ );
94
+ if (!hasJsonHeader) {
95
+ headers["content-type"] = defaultContentType;
96
+ }
97
+ };
98
+ const reply = (...args2) => {
99
+ if (typeof args2[0] === "function") {
100
+ const response = _reply.apply(interceptor, args2);
101
+ if (response.body) {
102
+ response.headers ?? (response.headers = {});
103
+ ensureJsonHeader(response.headers);
104
+ }
105
+ return response;
106
+ } else {
107
+ const [code, data, options2 = {}] = args2;
108
+ if (data) {
109
+ options2.headers ?? (options2.headers = {});
110
+ ensureJsonHeader(options2.headers);
111
+ }
112
+ return _reply.call(interceptor, code, data, options2);
113
+ }
114
+ };
115
+ interceptor.reply = reply;
116
+ return interceptor;
117
+ };
118
+ }
84
119
  clients.set(baseUrl, client);
85
120
  }
86
121
  return client;
@@ -218,15 +253,15 @@ function encodeRequestBody(body) {
218
253
  throw new Error("Unsupported body type");
219
254
  }
220
255
  async function readResponseBody(response, parseAs) {
221
- var _a;
222
- const contentLength = parseInt(
223
- response.headers["content-length"] ?? ((_a = response.body) == null ? void 0 : _a.readableLength)
224
- );
256
+ let contentLength = -1;
257
+ if ("content-length" in response.headers) {
258
+ contentLength = parseInt(response.headers["content-length"]);
259
+ }
225
260
  const contentType = response.headers["content-type"];
261
+ if (contentLength === 0 || !contentType || response.statusCode === 204) {
262
+ return;
263
+ }
226
264
  try {
227
- if (Number.isNaN(contentLength) || contentLength === 0) {
228
- return void 0;
229
- }
230
265
  switch (parseAs) {
231
266
  case "json":
232
267
  return await response.body.json();
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@openfn/language-common",
3
3
  "label": "Common",
4
- "version": "3.0.0",
4
+ "version": "3.0.1",
5
5
  "description": "Common Expressions for OpenFn",
6
6
  "homepage": "https://docs.openfn.org",
7
7
  "repository": {
@@ -28,7 +28,7 @@ export function makeBasicAuthHeader(username: any, password: any): {
28
28
  Authorization: string;
29
29
  };
30
30
  export function logResponse(response: any): any;
31
- export function enableMockClient(baseUrl: any): import("undici/types/mock-interceptor").Interceptable;
31
+ export function enableMockClient(baseUrl: any, options?: {}): import("undici/types/mock-interceptor").Interceptable;
32
32
  export const ERROR_ABSOLUTE_URL: "Absolute URLs not suppored";
33
33
  export function assertRelativeUrl(path: any): void;
34
34
  export const ERROR_URL_MISMATCH: "Target origin does not match baseUrl origin";