@openfn/language-http 4.0.2 → 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.js +381 -0
- package/package.json +1 -1
package/dist/index.js
ADDED
|
@@ -0,0 +1,381 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __export = (target, all) => {
|
|
3
|
+
for (var name in all)
|
|
4
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
// src/Adaptor.js
|
|
8
|
+
var Adaptor_exports = {};
|
|
9
|
+
__export(Adaptor_exports, {
|
|
10
|
+
alterState: () => alterState,
|
|
11
|
+
arrayToString: () => arrayToString,
|
|
12
|
+
axios: () => axios,
|
|
13
|
+
combine: () => combine,
|
|
14
|
+
dataPath: () => dataPath,
|
|
15
|
+
dataValue: () => dataValue,
|
|
16
|
+
dateFns: () => dateFns,
|
|
17
|
+
del: () => del,
|
|
18
|
+
each: () => each,
|
|
19
|
+
execute: () => execute,
|
|
20
|
+
field: () => field,
|
|
21
|
+
fields: () => fields,
|
|
22
|
+
fn: () => fn,
|
|
23
|
+
get: () => get,
|
|
24
|
+
http: () => http2,
|
|
25
|
+
humanProper: () => humanProper,
|
|
26
|
+
lastReferenceValue: () => lastReferenceValue,
|
|
27
|
+
merge: () => merge,
|
|
28
|
+
parseCSV: () => parseCSV,
|
|
29
|
+
parseXML: () => parseXML,
|
|
30
|
+
patch: () => patch,
|
|
31
|
+
post: () => post,
|
|
32
|
+
put: () => put,
|
|
33
|
+
request: () => request,
|
|
34
|
+
scrubEmojis: () => scrubEmojis,
|
|
35
|
+
sourceValue: () => sourceValue,
|
|
36
|
+
splitKeys: () => splitKeys,
|
|
37
|
+
toArray: () => toArray
|
|
38
|
+
});
|
|
39
|
+
|
|
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;
|
|
59
|
+
}
|
|
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
|
+
}
|
|
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
|
+
}
|
|
81
|
+
}
|
|
82
|
+
if (error)
|
|
83
|
+
return error;
|
|
84
|
+
delete response.request;
|
|
85
|
+
delete response.connection;
|
|
86
|
+
return new Error(safeStringify(response, null, 2));
|
|
87
|
+
}
|
|
88
|
+
function tryJson(data) {
|
|
89
|
+
if (typeof data === "string") {
|
|
90
|
+
try {
|
|
91
|
+
return JSON.parse(data);
|
|
92
|
+
} catch (e) {
|
|
93
|
+
return { body: data };
|
|
94
|
+
}
|
|
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]);
|
|
109
|
+
});
|
|
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 };
|
|
117
|
+
}
|
|
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
|
+
};
|
|
141
|
+
return finalConfig;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// 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 fs from "fs";
|
|
155
|
+
import parse from "csv-parse";
|
|
156
|
+
import tough from "tough-cookie";
|
|
157
|
+
import {
|
|
158
|
+
alterState,
|
|
159
|
+
arrayToString,
|
|
160
|
+
combine,
|
|
161
|
+
dataPath,
|
|
162
|
+
dataValue,
|
|
163
|
+
dateFns,
|
|
164
|
+
each,
|
|
165
|
+
field,
|
|
166
|
+
fields,
|
|
167
|
+
fn,
|
|
168
|
+
http as http2,
|
|
169
|
+
humanProper,
|
|
170
|
+
lastReferenceValue,
|
|
171
|
+
merge,
|
|
172
|
+
scrubEmojis,
|
|
173
|
+
sourceValue,
|
|
174
|
+
splitKeys,
|
|
175
|
+
toArray
|
|
176
|
+
} from "@openfn/language-common";
|
|
177
|
+
var { axios } = http;
|
|
178
|
+
function execute(...operations) {
|
|
179
|
+
const initialState = {
|
|
180
|
+
references: [],
|
|
181
|
+
data: null
|
|
182
|
+
};
|
|
183
|
+
return (state) => {
|
|
184
|
+
return commonExecute(...operations)({ ...initialState, ...state });
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
var cookiejar = new tough.CookieJar();
|
|
188
|
+
var Cookie = tough.Cookie;
|
|
189
|
+
axios.interceptors.request.use((config) => {
|
|
190
|
+
cookiejar == null ? void 0 : cookiejar.getCookies(config.url, (err, cookies) => {
|
|
191
|
+
config.headers.cookie = cookies == null ? void 0 : cookies.join("; ");
|
|
192
|
+
});
|
|
193
|
+
return config;
|
|
194
|
+
});
|
|
195
|
+
function handleCookies(response) {
|
|
196
|
+
var _a, _b;
|
|
197
|
+
const { config, data, headers } = response;
|
|
198
|
+
if (config.keepCookie) {
|
|
199
|
+
let cookies;
|
|
200
|
+
let keepCookies = [];
|
|
201
|
+
if (headers["set-cookie"]) {
|
|
202
|
+
if (headers["set-cookie"] instanceof Array) {
|
|
203
|
+
cookies = (_a = headers["set-cookie"]) == null ? void 0 : _a.map(Cookie.parse);
|
|
204
|
+
} else {
|
|
205
|
+
cookies = [Cookie.parse(headers["set-cookie"])];
|
|
206
|
+
}
|
|
207
|
+
(_b = headers["set-cookie"]) == null ? void 0 : _b.forEach((c) => {
|
|
208
|
+
cookiejar.setCookie(Cookie.parse(c), config.url, (err, cookie) => {
|
|
209
|
+
keepCookies == null ? void 0 : keepCookies.push(cookie == null ? void 0 : cookie.cookieString());
|
|
210
|
+
});
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
const extendableData = Array.isArray(data) ? { body: data } : data;
|
|
214
|
+
return {
|
|
215
|
+
...response,
|
|
216
|
+
data: {
|
|
217
|
+
...extendableData,
|
|
218
|
+
__cookie: (keepCookies == null ? void 0 : keepCookies.length) === 1 ? keepCookies[0] : keepCookies,
|
|
219
|
+
__headers: response.headers
|
|
220
|
+
}
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
return response;
|
|
224
|
+
}
|
|
225
|
+
function handleResponse(state, response) {
|
|
226
|
+
console.log(
|
|
227
|
+
response.config.method.toUpperCase(),
|
|
228
|
+
"request succeeded with",
|
|
229
|
+
response.status,
|
|
230
|
+
"\u2713"
|
|
231
|
+
);
|
|
232
|
+
const compatibleResp = {
|
|
233
|
+
...response,
|
|
234
|
+
httpStatus: response.status,
|
|
235
|
+
message: response.statusText,
|
|
236
|
+
data: tryJson(response.data)
|
|
237
|
+
};
|
|
238
|
+
const respWithCookies = handleCookies(compatibleResp);
|
|
239
|
+
return {
|
|
240
|
+
...composeNextState(state, respWithCookies.data),
|
|
241
|
+
response: respWithCookies
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
function handleCallback(state, callback) {
|
|
245
|
+
if (callback)
|
|
246
|
+
return callback(state);
|
|
247
|
+
return state;
|
|
248
|
+
}
|
|
249
|
+
function get(path, params, callback) {
|
|
250
|
+
return (state) => {
|
|
251
|
+
const resolvedPath = expandReferences(path)(state);
|
|
252
|
+
const resolvedParams = http.expandRequestReferences(params)(state);
|
|
253
|
+
const url = setUrl(state.configuration, resolvedPath);
|
|
254
|
+
const auth = setAuth(
|
|
255
|
+
state.configuration,
|
|
256
|
+
(resolvedParams == null ? void 0 : resolvedParams.authentication) ?? (resolvedParams == null ? void 0 : resolvedParams.auth)
|
|
257
|
+
);
|
|
258
|
+
const config = mapToAxiosConfig({ ...resolvedParams, url, auth });
|
|
259
|
+
return http.get(config)(state).then((response) => handleResponse(state, response)).then((nextState) => handleCallback(nextState, callback));
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
function post(path, params, callback) {
|
|
263
|
+
return (state) => {
|
|
264
|
+
const resolvedPath = expandReferences(path)(state);
|
|
265
|
+
const resolvedParams = http.expandRequestReferences(params)(state);
|
|
266
|
+
const url = setUrl(state.configuration, resolvedPath);
|
|
267
|
+
const auth = setAuth(
|
|
268
|
+
state.configuration,
|
|
269
|
+
(resolvedParams == null ? void 0 : resolvedParams.authentication) ?? (resolvedParams == null ? void 0 : resolvedParams.auth)
|
|
270
|
+
);
|
|
271
|
+
const config = mapToAxiosConfig({ ...resolvedParams, url, auth });
|
|
272
|
+
return http.post(config)(state).then((response) => handleResponse(state, response)).then((nextState) => handleCallback(nextState, callback));
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
function put(path, params, callback) {
|
|
276
|
+
return (state) => {
|
|
277
|
+
const resolvedPath = expandReferences(path)(state);
|
|
278
|
+
const resolvedParams = http.expandRequestReferences(params)(state);
|
|
279
|
+
const url = setUrl(state.configuration, resolvedPath);
|
|
280
|
+
const auth = setAuth(
|
|
281
|
+
state.configuration,
|
|
282
|
+
(resolvedParams == null ? void 0 : resolvedParams.authentication) ?? (resolvedParams == null ? void 0 : resolvedParams.auth)
|
|
283
|
+
);
|
|
284
|
+
const config = mapToAxiosConfig({ ...resolvedParams, url, auth });
|
|
285
|
+
return http.put(config)(state).then((response) => handleResponse(state, response)).then((nextState) => handleCallback(nextState, callback));
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
function patch(path, params, callback) {
|
|
289
|
+
return (state) => {
|
|
290
|
+
const resolvedPath = expandReferences(path)(state);
|
|
291
|
+
const resolvedParams = http.expandRequestReferences(params)(state);
|
|
292
|
+
const url = setUrl(state.configuration, resolvedPath);
|
|
293
|
+
const auth = setAuth(
|
|
294
|
+
state.configuration,
|
|
295
|
+
(resolvedParams == null ? void 0 : resolvedParams.authentication) ?? (resolvedParams == null ? void 0 : resolvedParams.auth)
|
|
296
|
+
);
|
|
297
|
+
const config = mapToAxiosConfig({ ...resolvedParams, url, auth });
|
|
298
|
+
return http.patch(config)(state).then((response) => handleResponse(state, response)).then((nextState) => handleCallback(nextState, callback));
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
function del(path, params, callback) {
|
|
302
|
+
return (state) => {
|
|
303
|
+
const resolvedPath = expandReferences(path)(state);
|
|
304
|
+
const resolvedParams = http.expandRequestReferences(params)(state);
|
|
305
|
+
const url = setUrl(state.configuration, resolvedPath);
|
|
306
|
+
const auth = setAuth(
|
|
307
|
+
state.configuration,
|
|
308
|
+
(resolvedParams == null ? void 0 : resolvedParams.authentication) ?? resolvedParams.auth
|
|
309
|
+
);
|
|
310
|
+
const config = mapToAxiosConfig({ ...resolvedParams, url, auth });
|
|
311
|
+
return http.delete(config)(state).then((response) => handleResponse(state, response)).then((nextState) => handleCallback(nextState, callback));
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
function parseXML(body, script) {
|
|
315
|
+
return (state) => {
|
|
316
|
+
const $ = cheerio.load(body);
|
|
317
|
+
cheerioTableparser($);
|
|
318
|
+
if (script) {
|
|
319
|
+
const result = script($);
|
|
320
|
+
try {
|
|
321
|
+
const r = JSON.parse(result);
|
|
322
|
+
return composeNextState(state, r);
|
|
323
|
+
} catch (e) {
|
|
324
|
+
return composeNextState(state, { body: result });
|
|
325
|
+
}
|
|
326
|
+
} else {
|
|
327
|
+
return composeNextState(state, { body });
|
|
328
|
+
}
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
function parseCSV(target, config) {
|
|
332
|
+
return (state) => {
|
|
333
|
+
return new Promise((resolve, reject) => {
|
|
334
|
+
var csvData = [];
|
|
335
|
+
try {
|
|
336
|
+
fs.readFileSync(target);
|
|
337
|
+
fs.createReadStream(target).pipe(parse(config)).on("data", (csvrow) => {
|
|
338
|
+
console.log(csvrow);
|
|
339
|
+
csvData.push(csvrow);
|
|
340
|
+
}).on("end", () => {
|
|
341
|
+
console.log(csvData);
|
|
342
|
+
resolve(composeNextState(state, csvData));
|
|
343
|
+
});
|
|
344
|
+
} catch (err) {
|
|
345
|
+
var csvString;
|
|
346
|
+
if (typeof target === "string") {
|
|
347
|
+
csvString = target;
|
|
348
|
+
} else {
|
|
349
|
+
csvString = expandReferences(target)(state);
|
|
350
|
+
}
|
|
351
|
+
csvData = parse(csvString, config, (err2, output) => {
|
|
352
|
+
console.log(output);
|
|
353
|
+
resolve(composeNextState(state, output));
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
});
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
function request(params) {
|
|
360
|
+
return (state) => {
|
|
361
|
+
const resolvedParams = http.expandRequestReferences(params)(state);
|
|
362
|
+
return new Promise((resolve, reject) => {
|
|
363
|
+
nodeRequest(resolvedParams, (error, response, body) => {
|
|
364
|
+
error = assembleError({ error, response, resolvedParams });
|
|
365
|
+
error && reject(error);
|
|
366
|
+
console.log(
|
|
367
|
+
"\u2713 Request succeeded. (The response body available in state.)"
|
|
368
|
+
);
|
|
369
|
+
const resp = tryJson(body);
|
|
370
|
+
resolve(resp);
|
|
371
|
+
});
|
|
372
|
+
});
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
// src/index.js
|
|
377
|
+
var src_default = Adaptor_exports;
|
|
378
|
+
export {
|
|
379
|
+
Adaptor_exports as Adaptor,
|
|
380
|
+
src_default as default
|
|
381
|
+
};
|