@openfn/language-http 5.1.0 → 6.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/dist/index.cjs CHANGED
@@ -25,354 +25,183 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
25
25
  // src/index.js
26
26
  var src_exports = {};
27
27
  __export(src_exports, {
28
- alterState: () => import_language_common2.alterState,
29
- arrayToString: () => import_language_common2.arrayToString,
30
- axios: () => axios,
31
- combine: () => import_language_common2.combine,
32
- dataPath: () => import_language_common2.dataPath,
33
- dataValue: () => import_language_common2.dataValue,
34
- dateFns: () => import_language_common2.dateFns,
28
+ alterState: () => import_language_common3.alterState,
29
+ arrayToString: () => import_language_common3.arrayToString,
30
+ chunk: () => import_language_common3.chunk,
31
+ combine: () => import_language_common3.combine,
32
+ dataPath: () => import_language_common3.dataPath,
33
+ dataValue: () => import_language_common3.dataValue,
34
+ dateFns: () => import_language_common3.dateFns,
35
35
  default: () => src_default,
36
36
  del: () => del,
37
- each: () => import_language_common2.each,
37
+ each: () => import_language_common3.each,
38
38
  execute: () => execute,
39
- field: () => import_language_common2.field,
40
- fields: () => import_language_common2.fields,
41
- fn: () => import_language_common2.fn,
39
+ field: () => import_language_common3.field,
40
+ fields: () => import_language_common3.fields,
41
+ fn: () => import_language_common3.fn,
42
42
  get: () => get,
43
- http: () => import_language_common2.http,
44
- humanProper: () => import_language_common2.humanProper,
45
- lastReferenceValue: () => import_language_common2.lastReferenceValue,
46
- merge: () => import_language_common2.merge,
47
- parseCsv: () => import_language_common2.parseCsv,
43
+ humanProper: () => import_language_common3.humanProper,
44
+ lastReferenceValue: () => import_language_common3.lastReferenceValue,
45
+ merge: () => import_language_common3.merge,
46
+ parseCsv: () => import_language_common3.parseCsv,
48
47
  parseXML: () => parseXML,
49
48
  patch: () => patch,
50
49
  post: () => post,
51
50
  put: () => put,
52
- request: () => request,
53
- scrubEmojis: () => import_language_common2.scrubEmojis,
54
- sourceValue: () => import_language_common2.sourceValue,
55
- splitKeys: () => import_language_common2.splitKeys,
56
- toArray: () => import_language_common2.toArray
51
+ request: () => request2,
52
+ scrubEmojis: () => import_language_common3.scrubEmojis,
53
+ sourceValue: () => import_language_common3.sourceValue,
54
+ splitKeys: () => import_language_common3.splitKeys,
55
+ toArray: () => import_language_common3.toArray
57
56
  });
58
57
  module.exports = __toCommonJS(src_exports);
59
58
 
60
59
  // src/Adaptor.js
61
60
  var Adaptor_exports = {};
62
61
  __export(Adaptor_exports, {
63
- alterState: () => import_language_common2.alterState,
64
- arrayToString: () => import_language_common2.arrayToString,
65
- axios: () => axios,
66
- combine: () => import_language_common2.combine,
67
- dataPath: () => import_language_common2.dataPath,
68
- dataValue: () => import_language_common2.dataValue,
69
- dateFns: () => import_language_common2.dateFns,
62
+ alterState: () => import_language_common3.alterState,
63
+ arrayToString: () => import_language_common3.arrayToString,
64
+ chunk: () => import_language_common3.chunk,
65
+ combine: () => import_language_common3.combine,
66
+ dataPath: () => import_language_common3.dataPath,
67
+ dataValue: () => import_language_common3.dataValue,
68
+ dateFns: () => import_language_common3.dateFns,
70
69
  del: () => del,
71
- each: () => import_language_common2.each,
70
+ each: () => import_language_common3.each,
72
71
  execute: () => execute,
73
- field: () => import_language_common2.field,
74
- fields: () => import_language_common2.fields,
75
- fn: () => import_language_common2.fn,
72
+ field: () => import_language_common3.field,
73
+ fields: () => import_language_common3.fields,
74
+ fn: () => import_language_common3.fn,
76
75
  get: () => get,
77
- http: () => import_language_common2.http,
78
- humanProper: () => import_language_common2.humanProper,
79
- lastReferenceValue: () => import_language_common2.lastReferenceValue,
80
- merge: () => import_language_common2.merge,
81
- parseCsv: () => import_language_common2.parseCsv,
76
+ humanProper: () => import_language_common3.humanProper,
77
+ lastReferenceValue: () => import_language_common3.lastReferenceValue,
78
+ merge: () => import_language_common3.merge,
79
+ parseCsv: () => import_language_common3.parseCsv,
82
80
  parseXML: () => parseXML,
83
81
  patch: () => patch,
84
82
  post: () => post,
85
83
  put: () => put,
86
- request: () => request,
87
- scrubEmojis: () => import_language_common2.scrubEmojis,
88
- sourceValue: () => import_language_common2.sourceValue,
89
- splitKeys: () => import_language_common2.splitKeys,
90
- toArray: () => import_language_common2.toArray
84
+ request: () => request2,
85
+ scrubEmojis: () => import_language_common3.scrubEmojis,
86
+ sourceValue: () => import_language_common3.sourceValue,
87
+ splitKeys: () => import_language_common3.splitKeys,
88
+ toArray: () => import_language_common3.toArray
91
89
  });
90
+ var import_language_common2 = require("@openfn/language-common");
91
+ var import_cheerio = __toESM(require("cheerio"), 1);
92
+ var import_cheerio_tableparser = __toESM(require("cheerio-tableparser"), 1);
92
93
 
93
94
  // src/Utils.js
94
- var import_form_data = __toESM(require("form-data"), 1);
95
- var import_fp = __toESM(require("lodash/fp"), 1);
96
- var import_fast_safe_stringify = __toESM(require("fast-safe-stringify"), 1);
97
- var { isEmpty } = import_fp.default;
98
- function setUrl(configuration, path) {
99
- const baseUrl = configuration == null ? void 0 : configuration.baseUrl;
100
- if (isValidHttpUrl(path))
101
- return path;
102
- if (baseUrl)
103
- return `${baseUrl.replace(/\/$/, "")}/${path.replace(/^\/+/g, "")}`;
104
- return path;
105
- }
106
- function isValidHttpUrl(string) {
107
- let url;
108
- try {
109
- url = new URL(string);
110
- } catch (_2) {
111
- return false;
95
+ var import_language_common = require("@openfn/language-common");
96
+ var import_util = require("@openfn/language-common/util");
97
+ function addBasicAuth(configuration = {}, headers) {
98
+ const { username, password } = configuration;
99
+ if (username && password) {
100
+ Object.assign(headers, (0, import_util.makeBasicAuthHeader)(username, password));
112
101
  }
113
- return url.protocol === "http:" || url.protocol === "https:";
114
- }
115
- function setAuth(configuration, manualAuth) {
116
- if (manualAuth)
117
- return manualAuth;
118
- else if (configuration && configuration.username)
119
- return {
120
- username: configuration.username,
121
- password: configuration.password
122
- };
123
- else
124
- return null;
125
102
  }
126
- function assembleError({ response, error, params }) {
127
- var _a;
128
- if (response) {
129
- const customCodes = (_a = params == null ? void 0 : params.options) == null ? void 0 : _a.successCodes;
130
- const status = (response == null ? void 0 : response.status) || (response == null ? void 0 : response.statusCode);
131
- if ((customCodes || [200, 201, 202]).indexOf(status) > -1) {
132
- return false;
133
- }
103
+ function encodeFormBody(data) {
104
+ const form = new FormData();
105
+ for (const [key, value] of Object.entries(data)) {
106
+ form.append(key, value);
134
107
  }
135
- if (error)
136
- return error;
137
- delete response.request;
138
- delete response.connection;
139
- return new Error((0, import_fast_safe_stringify.default)(response, null, 2));
108
+ return form;
140
109
  }
141
- function tryJson(data) {
142
- if (typeof data === "string") {
143
- try {
144
- return JSON.parse(data);
145
- } catch (e) {
146
- return { body: data };
110
+ function request(method, path, params, callback = (s) => s) {
111
+ return (state) => {
112
+ var _a;
113
+ const [resolvedPath, resolvedParams = {}] = (0, import_util.expandReferences)(
114
+ state,
115
+ path,
116
+ params
117
+ );
118
+ let { body, headers = {} } = resolvedParams;
119
+ if (resolvedParams.json) {
120
+ console.warn(
121
+ "WARNING: The `json` option has been deprecated. Use `body` instead"
122
+ );
123
+ body = resolvedParams.json;
147
124
  }
148
- }
149
- return data;
150
- }
151
- function mapToAxiosConfig(requestConfig) {
152
- let form = null;
153
- const formData = (requestConfig == null ? void 0 : requestConfig.formData) || (requestConfig == null ? void 0 : requestConfig.form);
154
- let headers = requestConfig == null ? void 0 : requestConfig.headers;
155
- if ((requestConfig == null ? void 0 : requestConfig.gzip) === true) {
156
- headers = { ...headers, "Accept-Encoding": "gzip, deflate" };
157
- }
158
- if (!isEmpty(formData)) {
159
- form = new import_form_data.default();
160
- Object.entries(formData).forEach((element) => {
161
- form.append(element[0], element[1]);
162
- });
163
- const formHeaders = form.getHeaders();
164
- headers = { ...headers, ...formHeaders };
165
- }
166
- if (requestConfig == null ? void 0 : requestConfig.json) {
167
- headers = { ...headers, "Content-type": "application/json" };
168
- if (typeof (requestConfig == null ? void 0 : requestConfig.json) === "object") {
169
- requestConfig = { ...requestConfig, body: requestConfig == null ? void 0 : requestConfig.json };
125
+ if (resolvedParams.form) {
126
+ body = encodeFormBody(resolvedParams.form);
170
127
  }
171
- }
172
- const finalConfig = {
173
- ...requestConfig,
174
- url: (requestConfig == null ? void 0 : requestConfig.url) ?? (requestConfig == null ? void 0 : requestConfig.uri),
175
- headers,
176
- params: {
177
- ...requestConfig == null ? void 0 : requestConfig.params,
178
- ...requestConfig == null ? void 0 : requestConfig.qs,
179
- ...requestConfig == null ? void 0 : requestConfig.query
180
- },
181
- data: (requestConfig == null ? void 0 : requestConfig.data) ?? ((requestConfig == null ? void 0 : requestConfig.body) || form),
182
- auth: (requestConfig == null ? void 0 : requestConfig.auth) ?? (requestConfig == null ? void 0 : requestConfig.authentication),
183
- responseType: (requestConfig == null ? void 0 : requestConfig.responseType) ?? "json",
184
- responseEncoding: (requestConfig == null ? void 0 : requestConfig.responseEncoding) ?? (requestConfig == null ? void 0 : requestConfig.encoding),
185
- validateStatus: (status) => {
186
- var _a;
187
- const customCodes = (_a = requestConfig == null ? void 0 : requestConfig.options) == null ? void 0 : _a.successCodes;
188
- if (customCodes)
189
- return customCodes.includes(status);
190
- return status >= 200 && status < 300;
191
- },
192
- maxRedirects: (requestConfig == null ? void 0 : requestConfig.maxRedirects) ?? ((requestConfig == null ? void 0 : requestConfig.followAllRedirects) === false ? 0 : 5)
128
+ const baseUrl = (_a = state.configuration) == null ? void 0 : _a.baseUrl;
129
+ addBasicAuth(state.configuration, headers);
130
+ const maxRedirections = resolvedParams.maxRedirections ?? (resolvedParams.followAllRedirects === false ? 0 : 5);
131
+ const tls = resolvedParams.tls ?? resolvedParams.agentOptions;
132
+ if (resolvedParams.agentOptions) {
133
+ console.warn(
134
+ "WARNING: The `agentOptions` option has been deprecated. Use `tls` instead"
135
+ );
136
+ }
137
+ const options = {
138
+ ...resolvedParams,
139
+ headers,
140
+ baseUrl,
141
+ body,
142
+ tls,
143
+ maxRedirections
144
+ };
145
+ return (0, import_util.request)(method, resolvedPath, options).then((response) => {
146
+ (0, import_util.logResponse)(response);
147
+ return {
148
+ ...(0, import_language_common.composeNextState)(state, response.body),
149
+ response
150
+ };
151
+ }).then(callback).catch((err) => {
152
+ (0, import_util.logResponse)(err);
153
+ throw err;
154
+ });
193
155
  };
194
- return finalConfig;
195
156
  }
196
157
 
197
158
  // src/Adaptor.js
198
- var import_language_common = require("@openfn/language-common");
199
- var import_request = __toESM(require("request"), 1);
200
- var import_cheerio = __toESM(require("cheerio"), 1);
201
- var import_cheerio_tableparser = __toESM(require("cheerio-tableparser"), 1);
202
- var import_tough_cookie = __toESM(require("tough-cookie"), 1);
203
- var import_language_common2 = require("@openfn/language-common");
204
- var { axios } = import_language_common.http;
159
+ var import_language_common3 = require("@openfn/language-common");
205
160
  function execute(...operations) {
206
161
  const initialState = {
207
162
  references: [],
208
163
  data: null
209
164
  };
210
165
  return (state) => {
211
- return (0, import_language_common.execute)(...operations)({ ...initialState, ...state });
212
- };
213
- }
214
- var cookiejar = new import_tough_cookie.default.CookieJar();
215
- var Cookie = import_tough_cookie.default.Cookie;
216
- axios.interceptors.request.use((config) => {
217
- cookiejar == null ? void 0 : cookiejar.getCookies(config.url, (err, cookies) => {
218
- config.headers.cookie = cookies == null ? void 0 : cookies.join("; ");
219
- });
220
- return config;
221
- });
222
- function handleCookies(response) {
223
- var _a, _b;
224
- const { config, data, headers } = response;
225
- if (config.keepCookie) {
226
- let cookies;
227
- let keepCookies = [];
228
- if (headers["set-cookie"]) {
229
- if (headers["set-cookie"] instanceof Array) {
230
- cookies = (_a = headers["set-cookie"]) == null ? void 0 : _a.map(Cookie.parse);
231
- } else {
232
- cookies = [Cookie.parse(headers["set-cookie"])];
233
- }
234
- (_b = headers["set-cookie"]) == null ? void 0 : _b.forEach((c) => {
235
- cookiejar.setCookie(Cookie.parse(c), config.url, (err, cookie) => {
236
- keepCookies == null ? void 0 : keepCookies.push(cookie == null ? void 0 : cookie.cookieString());
237
- });
238
- });
239
- }
240
- const extendableData = Array.isArray(data) ? { body: data } : data;
241
- return {
242
- ...response,
243
- data: {
244
- ...extendableData,
245
- __cookie: (keepCookies == null ? void 0 : keepCookies.length) === 1 ? keepCookies[0] : keepCookies,
246
- __headers: response.headers
247
- }
248
- };
249
- }
250
- return response;
251
- }
252
- function handleResponse(state, response) {
253
- console.log(
254
- response.config.method.toUpperCase(),
255
- "request succeeded with",
256
- response.status,
257
- "\u2713"
258
- );
259
- const { method, path, host, protocol, _headers } = response.request;
260
- response.request = { method, path, host, protocol, _headers };
261
- const compatibleResp = {
262
- ...response,
263
- httpStatus: response.status,
264
- message: response.statusText,
265
- data: tryJson(response.data)
266
- };
267
- const respWithCookies = handleCookies(compatibleResp);
268
- return {
269
- ...(0, import_language_common.composeNextState)(state, respWithCookies.data),
270
- response: respWithCookies
166
+ return (0, import_language_common2.execute)(...operations)({ ...initialState, ...state });
271
167
  };
272
168
  }
273
- function handleCallback(state, callback) {
274
- if (callback)
275
- return callback(state);
276
- return state;
169
+ function request2(method, path, params, callback) {
170
+ return request(method, path, params, callback);
277
171
  }
278
172
  function get(path, params, callback) {
279
- return (state) => {
280
- const resolvedPath = (0, import_language_common.expandReferences)(path)(state);
281
- const resolvedParams = import_language_common.http.expandRequestReferences(params)(state);
282
- const url = setUrl(state.configuration, resolvedPath);
283
- const auth = setAuth(
284
- state.configuration,
285
- (resolvedParams == null ? void 0 : resolvedParams.authentication) ?? (resolvedParams == null ? void 0 : resolvedParams.auth)
286
- );
287
- const config = mapToAxiosConfig({ ...resolvedParams, url, auth });
288
- return import_language_common.http.get(config)(state).then((response) => handleResponse(state, response)).then((nextState) => handleCallback(nextState, callback));
289
- };
173
+ return request("GET", path, params, callback);
290
174
  }
291
175
  function post(path, params, callback) {
292
- return (state) => {
293
- const resolvedPath = (0, import_language_common.expandReferences)(path)(state);
294
- const resolvedParams = import_language_common.http.expandRequestReferences(params)(state);
295
- const url = setUrl(state.configuration, resolvedPath);
296
- const auth = setAuth(
297
- state.configuration,
298
- (resolvedParams == null ? void 0 : resolvedParams.authentication) ?? (resolvedParams == null ? void 0 : resolvedParams.auth)
299
- );
300
- const config = mapToAxiosConfig({ ...resolvedParams, url, auth });
301
- return import_language_common.http.post(config)(state).then((response) => handleResponse(state, response)).then((nextState) => handleCallback(nextState, callback));
302
- };
176
+ return request("POST", path, params, callback);
303
177
  }
304
178
  function put(path, params, callback) {
305
- return (state) => {
306
- const resolvedPath = (0, import_language_common.expandReferences)(path)(state);
307
- const resolvedParams = import_language_common.http.expandRequestReferences(params)(state);
308
- const url = setUrl(state.configuration, resolvedPath);
309
- const auth = setAuth(
310
- state.configuration,
311
- (resolvedParams == null ? void 0 : resolvedParams.authentication) ?? (resolvedParams == null ? void 0 : resolvedParams.auth)
312
- );
313
- const config = mapToAxiosConfig({ ...resolvedParams, url, auth });
314
- return import_language_common.http.put(config)(state).then((response) => handleResponse(state, response)).then((nextState) => handleCallback(nextState, callback));
315
- };
179
+ return request("PUT", path, params, callback);
316
180
  }
317
181
  function patch(path, params, callback) {
318
- return (state) => {
319
- const resolvedPath = (0, import_language_common.expandReferences)(path)(state);
320
- const resolvedParams = import_language_common.http.expandRequestReferences(params)(state);
321
- const url = setUrl(state.configuration, resolvedPath);
322
- const auth = setAuth(
323
- state.configuration,
324
- (resolvedParams == null ? void 0 : resolvedParams.authentication) ?? (resolvedParams == null ? void 0 : resolvedParams.auth)
325
- );
326
- const config = mapToAxiosConfig({ ...resolvedParams, url, auth });
327
- return import_language_common.http.patch(config)(state).then((response) => handleResponse(state, response)).then((nextState) => handleCallback(nextState, callback));
328
- };
182
+ return request("PATCH", path, params, callback);
329
183
  }
330
184
  function del(path, params, callback) {
331
- return (state) => {
332
- const resolvedPath = (0, import_language_common.expandReferences)(path)(state);
333
- const resolvedParams = import_language_common.http.expandRequestReferences(params)(state);
334
- const url = setUrl(state.configuration, resolvedPath);
335
- const auth = setAuth(
336
- state.configuration,
337
- (resolvedParams == null ? void 0 : resolvedParams.authentication) ?? resolvedParams.auth
338
- );
339
- const config = mapToAxiosConfig({ ...resolvedParams, url, auth });
340
- return import_language_common.http.delete(config)(state).then((response) => handleResponse(state, response)).then((nextState) => handleCallback(nextState, callback));
341
- };
185
+ return request("DELETE", path, params, callback);
342
186
  }
343
187
  function parseXML(body, script) {
344
188
  return (state) => {
345
- const $ = import_cheerio.default.load(body);
189
+ const resolvedBody = (0, import_language_common2.expandReferences)(body)(state);
190
+ const $ = import_cheerio.default.load(resolvedBody);
346
191
  (0, import_cheerio_tableparser.default)($);
347
192
  if (script) {
348
193
  const result = script($);
349
194
  try {
350
195
  const r = JSON.parse(result);
351
- return (0, import_language_common.composeNextState)(state, r);
196
+ return (0, import_language_common2.composeNextState)(state, r);
352
197
  } catch (e) {
353
- return (0, import_language_common.composeNextState)(state, { body: result });
198
+ return (0, import_language_common2.composeNextState)(state, { body: result });
354
199
  }
355
200
  } else {
356
- return (0, import_language_common.composeNextState)(state, { body });
201
+ return (0, import_language_common2.composeNextState)(state, { body: resolvedBody });
357
202
  }
358
203
  };
359
204
  }
360
- function request(params) {
361
- return (state) => {
362
- const resolvedParams = import_language_common.http.expandRequestReferences(params)(state);
363
- return new Promise((resolve, reject) => {
364
- (0, import_request.default)(resolvedParams, (err, response, body) => {
365
- const error = assembleError({ error: err, response, resolvedParams });
366
- error && reject(error);
367
- console.log(
368
- "\u2713 Request succeeded. (The response body available in state.)"
369
- );
370
- const resp = tryJson(body);
371
- resolve(resp);
372
- });
373
- });
374
- };
375
- }
376
205
 
377
206
  // src/index.js
378
207
  var src_default = Adaptor_exports;
@@ -380,7 +209,7 @@ var src_default = Adaptor_exports;
380
209
  0 && (module.exports = {
381
210
  alterState,
382
211
  arrayToString,
383
- axios,
212
+ chunk,
384
213
  combine,
385
214
  dataPath,
386
215
  dataValue,
@@ -392,7 +221,6 @@ var src_default = Adaptor_exports;
392
221
  fields,
393
222
  fn,
394
223
  get,
395
- http,
396
224
  humanProper,
397
225
  lastReferenceValue,
398
226
  merge,