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