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