@openfn/language-common 1.11.0 → 1.12.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 +33 -11
- package/dist/index.js +27 -6
- package/dist/metadata.cjs +1 -1
- package/dist/metadata.js +1 -1
- package/dist/util.cjs +176 -4
- package/dist/util.js +165 -4
- package/package.json +5 -4
- package/types/Adaptor.d.ts +1 -1
- package/types/util/http.d.ts +72 -0
- package/types/util/index.d.ts +2 -0
- package/types/util/references.d.ts +2 -0
- package/types/util.d.ts +0 -10
- /package/types/{http.d.ts → http.deprecated.d.ts} +0 -0
package/dist/index.cjs
CHANGED
|
@@ -42,7 +42,7 @@ __export(src_exports, {
|
|
|
42
42
|
field: () => field,
|
|
43
43
|
fields: () => fields,
|
|
44
44
|
fn: () => fn,
|
|
45
|
-
http: () =>
|
|
45
|
+
http: () => http_deprecated_exports,
|
|
46
46
|
humanProper: () => humanProper,
|
|
47
47
|
index: () => index,
|
|
48
48
|
join: () => join,
|
|
@@ -80,7 +80,7 @@ __export(Adaptor_exports, {
|
|
|
80
80
|
field: () => field,
|
|
81
81
|
fields: () => fields,
|
|
82
82
|
fn: () => fn,
|
|
83
|
-
http: () =>
|
|
83
|
+
http: () => http_deprecated_exports,
|
|
84
84
|
humanProper: () => humanProper,
|
|
85
85
|
index: () => index,
|
|
86
86
|
join: () => join,
|
|
@@ -101,17 +101,22 @@ var import_curry = __toESM(require("lodash/fp/curry.js"), 1);
|
|
|
101
101
|
var import_fromPairs = __toESM(require("lodash/fp/fromPairs.js"), 1);
|
|
102
102
|
var import_jsonpath_plus = require("jsonpath-plus");
|
|
103
103
|
var import_csv_parse = require("csv-parse");
|
|
104
|
-
var
|
|
105
|
-
var
|
|
104
|
+
var import_node_stream3 = require("stream");
|
|
105
|
+
var import_undici2 = require("undici");
|
|
106
106
|
|
|
107
|
-
// src/util.js
|
|
107
|
+
// src/util/http.js
|
|
108
|
+
var import_undici = require("undici");
|
|
109
|
+
var import_http_status_codes = require("http-status-codes");
|
|
108
110
|
var import_node_stream = require("stream");
|
|
111
|
+
|
|
112
|
+
// src/util/references.js
|
|
113
|
+
var import_node_stream2 = require("stream");
|
|
109
114
|
function expandReferences(state, ...args) {
|
|
110
115
|
return args.map((value) => expandReference(state, value));
|
|
111
116
|
}
|
|
112
117
|
var isStream = (value) => {
|
|
113
118
|
if (value && typeof value == "object") {
|
|
114
|
-
if (value instanceof
|
|
119
|
+
if (value instanceof import_node_stream2.Readable || value instanceof import_node_stream2.Writable) {
|
|
115
120
|
return true;
|
|
116
121
|
}
|
|
117
122
|
if (value.pipeTo || value.pipe) {
|
|
@@ -121,6 +126,9 @@ var isStream = (value) => {
|
|
|
121
126
|
return false;
|
|
122
127
|
};
|
|
123
128
|
function expandReference(state, value) {
|
|
129
|
+
if (Buffer.isBuffer(value)) {
|
|
130
|
+
return value;
|
|
131
|
+
}
|
|
124
132
|
if (Array.isArray(value)) {
|
|
125
133
|
return value.map((v) => expandReference(state, v));
|
|
126
134
|
}
|
|
@@ -166,9 +174,9 @@ function each(dataSource, operation) {
|
|
|
166
174
|
};
|
|
167
175
|
}
|
|
168
176
|
|
|
169
|
-
// src/http.js
|
|
170
|
-
var
|
|
171
|
-
__export(
|
|
177
|
+
// src/http.deprecated.js
|
|
178
|
+
var http_deprecated_exports = {};
|
|
179
|
+
__export(http_deprecated_exports, {
|
|
172
180
|
axios: () => import_axios.default,
|
|
173
181
|
delete: () => del,
|
|
174
182
|
expandRequestReferences: () => expandRequestReferences,
|
|
@@ -181,6 +189,13 @@ __export(http_exports, {
|
|
|
181
189
|
});
|
|
182
190
|
var import_axios = __toESM(require("axios"), 1);
|
|
183
191
|
var import_https = __toESM(require("https"), 1);
|
|
192
|
+
var printDeprecationNotice = (name) => {
|
|
193
|
+
if (process.env.NODE_ENV !== "production") {
|
|
194
|
+
console.warn(
|
|
195
|
+
`The common.http.\xAC${name} function has been deprecated. This adaptor should migrate to use common.util.http instead.`
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
};
|
|
184
199
|
function expandRequestReferences(params) {
|
|
185
200
|
const [toKeep, toExpand] = splitKeys(params || {}, [
|
|
186
201
|
"agentOptions",
|
|
@@ -214,42 +229,49 @@ function withAgent(params) {
|
|
|
214
229
|
}
|
|
215
230
|
function get(requestParams) {
|
|
216
231
|
return (state) => {
|
|
232
|
+
printDeprecationNotice("get");
|
|
217
233
|
const params = expandRequestReferences(requestParams)(state);
|
|
218
234
|
return (0, import_axios.default)({ method: "get", ...withAgent(params) });
|
|
219
235
|
};
|
|
220
236
|
}
|
|
221
237
|
function post(requestParams) {
|
|
222
238
|
return (state) => {
|
|
239
|
+
printDeprecationNotice("post");
|
|
223
240
|
const params = expandRequestReferences(requestParams)(state);
|
|
224
241
|
return (0, import_axios.default)({ method: "post", ...withAgent(params) });
|
|
225
242
|
};
|
|
226
243
|
}
|
|
227
244
|
function del(requestParams) {
|
|
228
245
|
return (state) => {
|
|
246
|
+
printDeprecationNotice("del");
|
|
229
247
|
const params = expandRequestReferences(requestParams)(state);
|
|
230
248
|
return (0, import_axios.default)({ method: "delete", ...withAgent(params) });
|
|
231
249
|
};
|
|
232
250
|
}
|
|
233
251
|
function head(requestParams) {
|
|
234
252
|
return (state) => {
|
|
253
|
+
printDeprecationNotice("head");
|
|
235
254
|
const params = expandRequestReferences(requestParams)(state);
|
|
236
255
|
return (0, import_axios.default)({ method: "head", ...withAgent(params) });
|
|
237
256
|
};
|
|
238
257
|
}
|
|
239
258
|
function put(requestParams) {
|
|
240
259
|
return (state) => {
|
|
260
|
+
printDeprecationNotice("put");
|
|
241
261
|
const params = expandRequestReferences(requestParams)(state);
|
|
242
262
|
return (0, import_axios.default)({ method: "put", ...withAgent(params) });
|
|
243
263
|
};
|
|
244
264
|
}
|
|
245
265
|
function patch(requestParams) {
|
|
246
266
|
return (state) => {
|
|
267
|
+
printDeprecationNotice("patch");
|
|
247
268
|
const params = expandRequestReferences(requestParams)(state);
|
|
248
269
|
return (0, import_axios.default)({ method: "patch", ...withAgent(params) });
|
|
249
270
|
};
|
|
250
271
|
}
|
|
251
272
|
function options(requestParams) {
|
|
252
273
|
return (state) => {
|
|
274
|
+
printDeprecationNotice("options");
|
|
253
275
|
const params = expandRequestReferences(requestParams)(state);
|
|
254
276
|
return (0, import_axios.default)({ method: "options", ...withAgent(params) });
|
|
255
277
|
};
|
|
@@ -467,7 +489,7 @@ var getParser = (csvData, options2) => {
|
|
|
467
489
|
}
|
|
468
490
|
let stream = csvData;
|
|
469
491
|
if (csvData instanceof ReadableStream) {
|
|
470
|
-
stream =
|
|
492
|
+
stream = import_node_stream3.Readable.from(csvData);
|
|
471
493
|
}
|
|
472
494
|
return stream.pipe((0, import_csv_parse.parse)(options2));
|
|
473
495
|
};
|
|
@@ -568,7 +590,7 @@ function validate(schema = "schema", data = "data") {
|
|
|
568
590
|
if (typeof schemaOrUrl === "string") {
|
|
569
591
|
try {
|
|
570
592
|
const url = new URL(schemaOrUrl);
|
|
571
|
-
const response = await (0,
|
|
593
|
+
const response = await (0, import_undici2.request)(url);
|
|
572
594
|
return response.body.json();
|
|
573
595
|
} catch (e) {
|
|
574
596
|
if (e instanceof TypeError) {
|
package/dist/index.js
CHANGED
|
@@ -23,7 +23,7 @@ __export(Adaptor_exports, {
|
|
|
23
23
|
field: () => field,
|
|
24
24
|
fields: () => fields,
|
|
25
25
|
fn: () => fn,
|
|
26
|
-
http: () =>
|
|
26
|
+
http: () => http_deprecated_exports,
|
|
27
27
|
humanProper: () => humanProper,
|
|
28
28
|
index: () => index,
|
|
29
29
|
join: () => join,
|
|
@@ -47,7 +47,11 @@ import { parse as parse2 } from "csv-parse";
|
|
|
47
47
|
import { Readable as Readable2 } from "stream";
|
|
48
48
|
import { request } from "undici";
|
|
49
49
|
|
|
50
|
-
// src/util.js
|
|
50
|
+
// src/util/http.js
|
|
51
|
+
import { Client, MockAgent } from "undici";
|
|
52
|
+
import { getReasonPhrase } from "http-status-codes";
|
|
53
|
+
|
|
54
|
+
// src/util/references.js
|
|
51
55
|
import { Readable, Writable } from "stream";
|
|
52
56
|
function expandReferences(state, ...args) {
|
|
53
57
|
return args.map((value) => expandReference(state, value));
|
|
@@ -64,6 +68,9 @@ var isStream = (value) => {
|
|
|
64
68
|
return false;
|
|
65
69
|
};
|
|
66
70
|
function expandReference(state, value) {
|
|
71
|
+
if (Buffer.isBuffer(value)) {
|
|
72
|
+
return value;
|
|
73
|
+
}
|
|
67
74
|
if (Array.isArray(value)) {
|
|
68
75
|
return value.map((v) => expandReference(state, v));
|
|
69
76
|
}
|
|
@@ -109,9 +116,9 @@ function each(dataSource, operation) {
|
|
|
109
116
|
};
|
|
110
117
|
}
|
|
111
118
|
|
|
112
|
-
// src/http.js
|
|
113
|
-
var
|
|
114
|
-
__export(
|
|
119
|
+
// src/http.deprecated.js
|
|
120
|
+
var http_deprecated_exports = {};
|
|
121
|
+
__export(http_deprecated_exports, {
|
|
115
122
|
axios: () => axios,
|
|
116
123
|
delete: () => del,
|
|
117
124
|
expandRequestReferences: () => expandRequestReferences,
|
|
@@ -124,6 +131,13 @@ __export(http_exports, {
|
|
|
124
131
|
});
|
|
125
132
|
import axios from "axios";
|
|
126
133
|
import https from "https";
|
|
134
|
+
var printDeprecationNotice = (name) => {
|
|
135
|
+
if (process.env.NODE_ENV !== "production") {
|
|
136
|
+
console.warn(
|
|
137
|
+
`The common.http.\xAC${name} function has been deprecated. This adaptor should migrate to use common.util.http instead.`
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
};
|
|
127
141
|
function expandRequestReferences(params) {
|
|
128
142
|
const [toKeep, toExpand] = splitKeys(params || {}, [
|
|
129
143
|
"agentOptions",
|
|
@@ -157,42 +171,49 @@ function withAgent(params) {
|
|
|
157
171
|
}
|
|
158
172
|
function get(requestParams) {
|
|
159
173
|
return (state) => {
|
|
174
|
+
printDeprecationNotice("get");
|
|
160
175
|
const params = expandRequestReferences(requestParams)(state);
|
|
161
176
|
return axios({ method: "get", ...withAgent(params) });
|
|
162
177
|
};
|
|
163
178
|
}
|
|
164
179
|
function post(requestParams) {
|
|
165
180
|
return (state) => {
|
|
181
|
+
printDeprecationNotice("post");
|
|
166
182
|
const params = expandRequestReferences(requestParams)(state);
|
|
167
183
|
return axios({ method: "post", ...withAgent(params) });
|
|
168
184
|
};
|
|
169
185
|
}
|
|
170
186
|
function del(requestParams) {
|
|
171
187
|
return (state) => {
|
|
188
|
+
printDeprecationNotice("del");
|
|
172
189
|
const params = expandRequestReferences(requestParams)(state);
|
|
173
190
|
return axios({ method: "delete", ...withAgent(params) });
|
|
174
191
|
};
|
|
175
192
|
}
|
|
176
193
|
function head(requestParams) {
|
|
177
194
|
return (state) => {
|
|
195
|
+
printDeprecationNotice("head");
|
|
178
196
|
const params = expandRequestReferences(requestParams)(state);
|
|
179
197
|
return axios({ method: "head", ...withAgent(params) });
|
|
180
198
|
};
|
|
181
199
|
}
|
|
182
200
|
function put(requestParams) {
|
|
183
201
|
return (state) => {
|
|
202
|
+
printDeprecationNotice("put");
|
|
184
203
|
const params = expandRequestReferences(requestParams)(state);
|
|
185
204
|
return axios({ method: "put", ...withAgent(params) });
|
|
186
205
|
};
|
|
187
206
|
}
|
|
188
207
|
function patch(requestParams) {
|
|
189
208
|
return (state) => {
|
|
209
|
+
printDeprecationNotice("patch");
|
|
190
210
|
const params = expandRequestReferences(requestParams)(state);
|
|
191
211
|
return axios({ method: "patch", ...withAgent(params) });
|
|
192
212
|
};
|
|
193
213
|
}
|
|
194
214
|
function options(requestParams) {
|
|
195
215
|
return (state) => {
|
|
216
|
+
printDeprecationNotice("options");
|
|
196
217
|
const params = expandRequestReferences(requestParams)(state);
|
|
197
218
|
return axios({ method: "options", ...withAgent(params) });
|
|
198
219
|
};
|
|
@@ -554,7 +575,7 @@ export {
|
|
|
554
575
|
field,
|
|
555
576
|
fields,
|
|
556
577
|
fn,
|
|
557
|
-
|
|
578
|
+
http_deprecated_exports as http,
|
|
558
579
|
humanProper,
|
|
559
580
|
index,
|
|
560
581
|
join,
|
package/dist/metadata.cjs
CHANGED
|
@@ -32,7 +32,7 @@ var createEntity = (name, type, props = {}, children) => {
|
|
|
32
32
|
this.children = name2 ? {} : [];
|
|
33
33
|
}
|
|
34
34
|
if (name2) {
|
|
35
|
-
if (isArray(this.children)) {
|
|
35
|
+
if (Array.isArray(this.children)) {
|
|
36
36
|
throw new Error("Cannot add a named entity to child array");
|
|
37
37
|
}
|
|
38
38
|
this.children[name2] = e2;
|
package/dist/metadata.js
CHANGED
|
@@ -9,7 +9,7 @@ var createEntity = (name, type, props = {}, children) => {
|
|
|
9
9
|
this.children = name2 ? {} : [];
|
|
10
10
|
}
|
|
11
11
|
if (name2) {
|
|
12
|
-
if (isArray(this.children)) {
|
|
12
|
+
if (Array.isArray(this.children)) {
|
|
13
13
|
throw new Error("Cannot add a named entity to child array");
|
|
14
14
|
}
|
|
15
15
|
this.children[name2] = e2;
|
package/dist/util.cjs
CHANGED
|
@@ -16,20 +16,180 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
16
16
|
};
|
|
17
17
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
18
|
|
|
19
|
-
// src/util.js
|
|
19
|
+
// src/util/index.js
|
|
20
20
|
var util_exports = {};
|
|
21
21
|
__export(util_exports, {
|
|
22
|
+
del: () => del,
|
|
23
|
+
enableMockClient: () => enableMockClient,
|
|
22
24
|
expandReferences: () => expandReferences,
|
|
23
|
-
|
|
25
|
+
get: () => get,
|
|
26
|
+
logResponse: () => logResponse,
|
|
27
|
+
makeBasicAuthHeader: () => makeBasicAuthHeader,
|
|
28
|
+
normalizeOauthConfig: () => normalizeOauthConfig,
|
|
29
|
+
parseUrl: () => parseUrl,
|
|
30
|
+
post: () => post,
|
|
31
|
+
put: () => put,
|
|
32
|
+
request: () => request
|
|
24
33
|
});
|
|
25
34
|
module.exports = __toCommonJS(util_exports);
|
|
35
|
+
|
|
36
|
+
// src/util/http.js
|
|
37
|
+
var import_undici = require("undici");
|
|
38
|
+
var import_http_status_codes = require("http-status-codes");
|
|
26
39
|
var import_node_stream = require("stream");
|
|
40
|
+
var clients = /* @__PURE__ */ new Map();
|
|
41
|
+
var makeBasicAuthHeader = (username, password) => {
|
|
42
|
+
const buff = Buffer.from(`${username}:${password}`);
|
|
43
|
+
const credentials = buff.toString("base64");
|
|
44
|
+
return { Authorization: `Basic ${credentials}` };
|
|
45
|
+
};
|
|
46
|
+
var logResponse = (response) => {
|
|
47
|
+
const { method, url, statusCode, duration } = response;
|
|
48
|
+
if (method && url && duration && statusCode) {
|
|
49
|
+
const message = `${method} ${url} - ${statusCode} in ${duration}ms`;
|
|
50
|
+
if (response instanceof Error) {
|
|
51
|
+
console.error(message);
|
|
52
|
+
console.error("response body: ");
|
|
53
|
+
console.error(response.body || "[no body]");
|
|
54
|
+
} else {
|
|
55
|
+
console.log(message);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
var getClient = (baseUrl, options) => {
|
|
60
|
+
const { tls } = options;
|
|
61
|
+
if (!clients.has(baseUrl)) {
|
|
62
|
+
clients.set(baseUrl, new import_undici.Client(baseUrl, { connect: tls }));
|
|
63
|
+
}
|
|
64
|
+
return clients.get(baseUrl);
|
|
65
|
+
};
|
|
66
|
+
var enableMockClient = (baseUrl) => {
|
|
67
|
+
const mockAgent = new import_undici.MockAgent({ connections: 1 });
|
|
68
|
+
const client = mockAgent.get(baseUrl);
|
|
69
|
+
if (!clients.has(baseUrl)) {
|
|
70
|
+
clients.set(baseUrl, client);
|
|
71
|
+
}
|
|
72
|
+
return client;
|
|
73
|
+
};
|
|
74
|
+
var assertOK = async (response, errorMap, fullUrl, method, startTime) => {
|
|
75
|
+
const errMapMessage = errorMap[response.statusCode];
|
|
76
|
+
const isError = typeof errMapMessage === "boolean" ? errMapMessage : errMapMessage || response.statusCode >= 400;
|
|
77
|
+
if (isError) {
|
|
78
|
+
const body = await readResponseBody(response);
|
|
79
|
+
const statusText = (0, import_http_status_codes.getReasonPhrase)(response.statusCode);
|
|
80
|
+
const defaultErrorMesssage = `${method} to ${fullUrl} returned ${response.statusCode}: ${statusText}`;
|
|
81
|
+
const duration = Date.now() - startTime;
|
|
82
|
+
const errMessage = typeof errMapMessage === "function" ? errMapMessage(response) : errMapMessage || defaultErrorMesssage;
|
|
83
|
+
const error = new Error(errMessage);
|
|
84
|
+
error.statusCode = response.statusCode;
|
|
85
|
+
error.statusMessage = statusText;
|
|
86
|
+
error.url = fullUrl;
|
|
87
|
+
error.duration = duration;
|
|
88
|
+
error.method = method;
|
|
89
|
+
error.body = body;
|
|
90
|
+
error.headers = response.headers;
|
|
91
|
+
throw error;
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
var parseUrl = (pathOrUrl = "", baseUrl) => {
|
|
95
|
+
let fullUrl;
|
|
96
|
+
if (/https?:\/\//.test(pathOrUrl)) {
|
|
97
|
+
fullUrl = pathOrUrl;
|
|
98
|
+
} else if (baseUrl) {
|
|
99
|
+
const base = baseUrl.endsWith("/") ? baseUrl : baseUrl + "/";
|
|
100
|
+
const path = pathOrUrl.startsWith("/") ? pathOrUrl.substring(1) : pathOrUrl;
|
|
101
|
+
fullUrl = base + path;
|
|
102
|
+
} else {
|
|
103
|
+
new URL(pathOrUrl);
|
|
104
|
+
}
|
|
105
|
+
const url = new URL(fullUrl);
|
|
106
|
+
return {
|
|
107
|
+
url: url.toString(),
|
|
108
|
+
baseUrl: url.origin,
|
|
109
|
+
path: url.pathname
|
|
110
|
+
};
|
|
111
|
+
};
|
|
112
|
+
async function request(method, fullUrlOrPath, options = {}) {
|
|
113
|
+
const startTime = Date.now();
|
|
114
|
+
const { url, baseUrl, path } = parseUrl(fullUrlOrPath, options.baseUrl);
|
|
115
|
+
const {
|
|
116
|
+
headers = {},
|
|
117
|
+
query = {},
|
|
118
|
+
body,
|
|
119
|
+
errors = {},
|
|
120
|
+
timeout = 3e5,
|
|
121
|
+
tls = {},
|
|
122
|
+
parseAs = "auto",
|
|
123
|
+
maxRedirections
|
|
124
|
+
} = options;
|
|
125
|
+
const client = getClient(baseUrl, { tls });
|
|
126
|
+
const response = await client.request({
|
|
127
|
+
path,
|
|
128
|
+
query,
|
|
129
|
+
method,
|
|
130
|
+
headers,
|
|
131
|
+
body: encodeRequestBody(body),
|
|
132
|
+
throwOnError: false,
|
|
133
|
+
maxRedirections,
|
|
134
|
+
bodyTimeout: timeout,
|
|
135
|
+
headersTimeout: timeout
|
|
136
|
+
});
|
|
137
|
+
const statusText = (0, import_http_status_codes.getReasonPhrase)(response.statusCode);
|
|
138
|
+
await assertOK(response, errors, url, method, startTime);
|
|
139
|
+
const responseBody = await readResponseBody(response, parseAs);
|
|
140
|
+
const endTime = Date.now();
|
|
141
|
+
const duration = endTime - startTime;
|
|
142
|
+
return {
|
|
143
|
+
url,
|
|
144
|
+
method,
|
|
145
|
+
statusCode: response.statusCode,
|
|
146
|
+
statusMessage: statusText,
|
|
147
|
+
headers: response.headers,
|
|
148
|
+
body: responseBody,
|
|
149
|
+
duration
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
function encodeRequestBody(body) {
|
|
153
|
+
if (!body) {
|
|
154
|
+
return void 0;
|
|
155
|
+
}
|
|
156
|
+
if (Buffer.isBuffer(body) || body instanceof import_node_stream.Readable || typeof body === "string") {
|
|
157
|
+
return body;
|
|
158
|
+
}
|
|
159
|
+
if (typeof body === "object") {
|
|
160
|
+
if (Symbol.asyncIterator in Object(body) || Symbol.iterator in Object(body) || body instanceof FormData) {
|
|
161
|
+
return body;
|
|
162
|
+
}
|
|
163
|
+
return JSON.stringify(body);
|
|
164
|
+
}
|
|
165
|
+
throw new Error("Unsupported body type");
|
|
166
|
+
}
|
|
167
|
+
async function readResponseBody(response, parseAs) {
|
|
168
|
+
const contentType = response.headers["content-type"];
|
|
169
|
+
switch (parseAs) {
|
|
170
|
+
case "json":
|
|
171
|
+
return response.body.json();
|
|
172
|
+
case "text":
|
|
173
|
+
return response.body.text();
|
|
174
|
+
case "stream":
|
|
175
|
+
return response.body;
|
|
176
|
+
default:
|
|
177
|
+
return contentType && contentType.includes("application/json") ? response.body.json() : response.body.text();
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
var get = (url, options) => request("GET", url, options);
|
|
181
|
+
var post = (url, body, options) => request("POST", url, { body, ...options });
|
|
182
|
+
var put = (url, body, options) => request("PUT", url, { body, ...options });
|
|
183
|
+
var del = (url, body, options) => request("DELETE", url, { body, ...options });
|
|
184
|
+
|
|
185
|
+
// src/util/references.js
|
|
186
|
+
var import_node_stream2 = require("stream");
|
|
27
187
|
function expandReferences(state, ...args) {
|
|
28
188
|
return args.map((value) => expandReference(state, value));
|
|
29
189
|
}
|
|
30
190
|
var isStream = (value) => {
|
|
31
191
|
if (value && typeof value == "object") {
|
|
32
|
-
if (value instanceof
|
|
192
|
+
if (value instanceof import_node_stream2.Readable || value instanceof import_node_stream2.Writable) {
|
|
33
193
|
return true;
|
|
34
194
|
}
|
|
35
195
|
if (value.pipeTo || value.pipe) {
|
|
@@ -39,6 +199,9 @@ var isStream = (value) => {
|
|
|
39
199
|
return false;
|
|
40
200
|
};
|
|
41
201
|
function expandReference(state, value) {
|
|
202
|
+
if (Buffer.isBuffer(value)) {
|
|
203
|
+
return value;
|
|
204
|
+
}
|
|
42
205
|
if (Array.isArray(value)) {
|
|
43
206
|
return value.map((v) => expandReference(state, v));
|
|
44
207
|
}
|
|
@@ -70,6 +233,15 @@ function normalizeOauthConfig(configuration) {
|
|
|
70
233
|
}
|
|
71
234
|
// Annotate the CommonJS export names for ESM import in node:
|
|
72
235
|
0 && (module.exports = {
|
|
236
|
+
del,
|
|
237
|
+
enableMockClient,
|
|
73
238
|
expandReferences,
|
|
74
|
-
|
|
239
|
+
get,
|
|
240
|
+
logResponse,
|
|
241
|
+
makeBasicAuthHeader,
|
|
242
|
+
normalizeOauthConfig,
|
|
243
|
+
parseUrl,
|
|
244
|
+
post,
|
|
245
|
+
put,
|
|
246
|
+
request
|
|
75
247
|
});
|
package/dist/util.js
CHANGED
|
@@ -1,11 +1,160 @@
|
|
|
1
|
-
// src/util.js
|
|
2
|
-
import {
|
|
1
|
+
// src/util/http.js
|
|
2
|
+
import { Client, MockAgent } from "undici";
|
|
3
|
+
import { getReasonPhrase } from "http-status-codes";
|
|
4
|
+
import { Readable } from "stream";
|
|
5
|
+
var clients = /* @__PURE__ */ new Map();
|
|
6
|
+
var makeBasicAuthHeader = (username, password) => {
|
|
7
|
+
const buff = Buffer.from(`${username}:${password}`);
|
|
8
|
+
const credentials = buff.toString("base64");
|
|
9
|
+
return { Authorization: `Basic ${credentials}` };
|
|
10
|
+
};
|
|
11
|
+
var logResponse = (response) => {
|
|
12
|
+
const { method, url, statusCode, duration } = response;
|
|
13
|
+
if (method && url && duration && statusCode) {
|
|
14
|
+
const message = `${method} ${url} - ${statusCode} in ${duration}ms`;
|
|
15
|
+
if (response instanceof Error) {
|
|
16
|
+
console.error(message);
|
|
17
|
+
console.error("response body: ");
|
|
18
|
+
console.error(response.body || "[no body]");
|
|
19
|
+
} else {
|
|
20
|
+
console.log(message);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
var getClient = (baseUrl, options) => {
|
|
25
|
+
const { tls } = options;
|
|
26
|
+
if (!clients.has(baseUrl)) {
|
|
27
|
+
clients.set(baseUrl, new Client(baseUrl, { connect: tls }));
|
|
28
|
+
}
|
|
29
|
+
return clients.get(baseUrl);
|
|
30
|
+
};
|
|
31
|
+
var enableMockClient = (baseUrl) => {
|
|
32
|
+
const mockAgent = new MockAgent({ connections: 1 });
|
|
33
|
+
const client = mockAgent.get(baseUrl);
|
|
34
|
+
if (!clients.has(baseUrl)) {
|
|
35
|
+
clients.set(baseUrl, client);
|
|
36
|
+
}
|
|
37
|
+
return client;
|
|
38
|
+
};
|
|
39
|
+
var assertOK = async (response, errorMap, fullUrl, method, startTime) => {
|
|
40
|
+
const errMapMessage = errorMap[response.statusCode];
|
|
41
|
+
const isError = typeof errMapMessage === "boolean" ? errMapMessage : errMapMessage || response.statusCode >= 400;
|
|
42
|
+
if (isError) {
|
|
43
|
+
const body = await readResponseBody(response);
|
|
44
|
+
const statusText = getReasonPhrase(response.statusCode);
|
|
45
|
+
const defaultErrorMesssage = `${method} to ${fullUrl} returned ${response.statusCode}: ${statusText}`;
|
|
46
|
+
const duration = Date.now() - startTime;
|
|
47
|
+
const errMessage = typeof errMapMessage === "function" ? errMapMessage(response) : errMapMessage || defaultErrorMesssage;
|
|
48
|
+
const error = new Error(errMessage);
|
|
49
|
+
error.statusCode = response.statusCode;
|
|
50
|
+
error.statusMessage = statusText;
|
|
51
|
+
error.url = fullUrl;
|
|
52
|
+
error.duration = duration;
|
|
53
|
+
error.method = method;
|
|
54
|
+
error.body = body;
|
|
55
|
+
error.headers = response.headers;
|
|
56
|
+
throw error;
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
var parseUrl = (pathOrUrl = "", baseUrl) => {
|
|
60
|
+
let fullUrl;
|
|
61
|
+
if (/https?:\/\//.test(pathOrUrl)) {
|
|
62
|
+
fullUrl = pathOrUrl;
|
|
63
|
+
} else if (baseUrl) {
|
|
64
|
+
const base = baseUrl.endsWith("/") ? baseUrl : baseUrl + "/";
|
|
65
|
+
const path = pathOrUrl.startsWith("/") ? pathOrUrl.substring(1) : pathOrUrl;
|
|
66
|
+
fullUrl = base + path;
|
|
67
|
+
} else {
|
|
68
|
+
new URL(pathOrUrl);
|
|
69
|
+
}
|
|
70
|
+
const url = new URL(fullUrl);
|
|
71
|
+
return {
|
|
72
|
+
url: url.toString(),
|
|
73
|
+
baseUrl: url.origin,
|
|
74
|
+
path: url.pathname
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
async function request(method, fullUrlOrPath, options = {}) {
|
|
78
|
+
const startTime = Date.now();
|
|
79
|
+
const { url, baseUrl, path } = parseUrl(fullUrlOrPath, options.baseUrl);
|
|
80
|
+
const {
|
|
81
|
+
headers = {},
|
|
82
|
+
query = {},
|
|
83
|
+
body,
|
|
84
|
+
errors = {},
|
|
85
|
+
timeout = 3e5,
|
|
86
|
+
tls = {},
|
|
87
|
+
parseAs = "auto",
|
|
88
|
+
maxRedirections
|
|
89
|
+
} = options;
|
|
90
|
+
const client = getClient(baseUrl, { tls });
|
|
91
|
+
const response = await client.request({
|
|
92
|
+
path,
|
|
93
|
+
query,
|
|
94
|
+
method,
|
|
95
|
+
headers,
|
|
96
|
+
body: encodeRequestBody(body),
|
|
97
|
+
throwOnError: false,
|
|
98
|
+
maxRedirections,
|
|
99
|
+
bodyTimeout: timeout,
|
|
100
|
+
headersTimeout: timeout
|
|
101
|
+
});
|
|
102
|
+
const statusText = getReasonPhrase(response.statusCode);
|
|
103
|
+
await assertOK(response, errors, url, method, startTime);
|
|
104
|
+
const responseBody = await readResponseBody(response, parseAs);
|
|
105
|
+
const endTime = Date.now();
|
|
106
|
+
const duration = endTime - startTime;
|
|
107
|
+
return {
|
|
108
|
+
url,
|
|
109
|
+
method,
|
|
110
|
+
statusCode: response.statusCode,
|
|
111
|
+
statusMessage: statusText,
|
|
112
|
+
headers: response.headers,
|
|
113
|
+
body: responseBody,
|
|
114
|
+
duration
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
function encodeRequestBody(body) {
|
|
118
|
+
if (!body) {
|
|
119
|
+
return void 0;
|
|
120
|
+
}
|
|
121
|
+
if (Buffer.isBuffer(body) || body instanceof Readable || typeof body === "string") {
|
|
122
|
+
return body;
|
|
123
|
+
}
|
|
124
|
+
if (typeof body === "object") {
|
|
125
|
+
if (Symbol.asyncIterator in Object(body) || Symbol.iterator in Object(body) || body instanceof FormData) {
|
|
126
|
+
return body;
|
|
127
|
+
}
|
|
128
|
+
return JSON.stringify(body);
|
|
129
|
+
}
|
|
130
|
+
throw new Error("Unsupported body type");
|
|
131
|
+
}
|
|
132
|
+
async function readResponseBody(response, parseAs) {
|
|
133
|
+
const contentType = response.headers["content-type"];
|
|
134
|
+
switch (parseAs) {
|
|
135
|
+
case "json":
|
|
136
|
+
return response.body.json();
|
|
137
|
+
case "text":
|
|
138
|
+
return response.body.text();
|
|
139
|
+
case "stream":
|
|
140
|
+
return response.body;
|
|
141
|
+
default:
|
|
142
|
+
return contentType && contentType.includes("application/json") ? response.body.json() : response.body.text();
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
var get = (url, options) => request("GET", url, options);
|
|
146
|
+
var post = (url, body, options) => request("POST", url, { body, ...options });
|
|
147
|
+
var put = (url, body, options) => request("PUT", url, { body, ...options });
|
|
148
|
+
var del = (url, body, options) => request("DELETE", url, { body, ...options });
|
|
149
|
+
|
|
150
|
+
// src/util/references.js
|
|
151
|
+
import { Readable as Readable2, Writable } from "stream";
|
|
3
152
|
function expandReferences(state, ...args) {
|
|
4
153
|
return args.map((value) => expandReference(state, value));
|
|
5
154
|
}
|
|
6
155
|
var isStream = (value) => {
|
|
7
156
|
if (value && typeof value == "object") {
|
|
8
|
-
if (value instanceof
|
|
157
|
+
if (value instanceof Readable2 || value instanceof Writable) {
|
|
9
158
|
return true;
|
|
10
159
|
}
|
|
11
160
|
if (value.pipeTo || value.pipe) {
|
|
@@ -15,6 +164,9 @@ var isStream = (value) => {
|
|
|
15
164
|
return false;
|
|
16
165
|
};
|
|
17
166
|
function expandReference(state, value) {
|
|
167
|
+
if (Buffer.isBuffer(value)) {
|
|
168
|
+
return value;
|
|
169
|
+
}
|
|
18
170
|
if (Array.isArray(value)) {
|
|
19
171
|
return value.map((v) => expandReference(state, v));
|
|
20
172
|
}
|
|
@@ -45,6 +197,15 @@ function normalizeOauthConfig(configuration) {
|
|
|
45
197
|
return configuration;
|
|
46
198
|
}
|
|
47
199
|
export {
|
|
200
|
+
del,
|
|
201
|
+
enableMockClient,
|
|
48
202
|
expandReferences,
|
|
49
|
-
|
|
203
|
+
get,
|
|
204
|
+
logResponse,
|
|
205
|
+
makeBasicAuthHeader,
|
|
206
|
+
normalizeOauthConfig,
|
|
207
|
+
parseUrl,
|
|
208
|
+
post,
|
|
209
|
+
put,
|
|
210
|
+
request
|
|
50
211
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openfn/language-common",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.12.0",
|
|
4
4
|
"description": "Common Expressions for OpenFn",
|
|
5
5
|
"homepage": "https://docs.openfn.org",
|
|
6
6
|
"repository": {
|
|
@@ -38,9 +38,10 @@
|
|
|
38
38
|
"csv-parse": "^5.4.0",
|
|
39
39
|
"csvtojson": "^2.0.10",
|
|
40
40
|
"date-fns": "^2.25.0",
|
|
41
|
+
"http-status-codes": "^2.3.0",
|
|
41
42
|
"jsonpath-plus": "^4.0.0",
|
|
42
43
|
"lodash": "^4.17.19",
|
|
43
|
-
"undici": "^5.
|
|
44
|
+
"undici": "^5.23.0"
|
|
44
45
|
},
|
|
45
46
|
"devDependencies": {
|
|
46
47
|
"@openfn/buildtools": "^1.0.2",
|
|
@@ -55,8 +56,8 @@
|
|
|
55
56
|
"main": "dist/index.cjs",
|
|
56
57
|
"scripts": {
|
|
57
58
|
"build": "pnpm clean && build-adaptor common",
|
|
58
|
-
"test": "mocha --experimental-specifier-resolution=node --no-warnings",
|
|
59
|
-
"test:watch": "mocha -w --experimental-specifier-resolution=node --no-warnings",
|
|
59
|
+
"test": "mocha --experimental-specifier-resolution=node --no-warnings --recursive",
|
|
60
|
+
"test:watch": "mocha -w --experimental-specifier-resolution=node --no-warnings --recursive",
|
|
60
61
|
"clean": "rimraf dist types docs",
|
|
61
62
|
"pack": "pnpm pack --pack-destination ../../dist",
|
|
62
63
|
"lint": "eslint src"
|
package/types/Adaptor.d.ts
CHANGED
|
@@ -348,7 +348,7 @@ export function parseCsv(csvData: string | Stream, parsingOptions?: any, callbac
|
|
|
348
348
|
*/
|
|
349
349
|
export function validate(schema?: string | object, data?: string | object): Operation;
|
|
350
350
|
export * as beta from "./beta";
|
|
351
|
-
export * as http from "./http";
|
|
351
|
+
export * as http from "./http.deprecated";
|
|
352
352
|
export * as dateFns from "./dateFns";
|
|
353
353
|
/**
|
|
354
354
|
* Scopes an array of data based on a JSONPath.
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `request` is a helper function that sends HTTP requests and returns the response
|
|
3
|
+
* body, headers, and status code.
|
|
4
|
+
* Use the error map to provide custom error messages or get hold of the response in case of errors.
|
|
5
|
+
* @param method - The HTTP method to use for the request (e.g., "GET", "POST", "PUT", "DELETE", etc.).
|
|
6
|
+
* @param fullUrlOrPath - The full or partial URL for the request.
|
|
7
|
+
* @param [options] - The `options` parameter is an object that contains additional configuration
|
|
8
|
+
* options for the request.
|
|
9
|
+
* @returns an object with the following properties:
|
|
10
|
+
* - method: the request method
|
|
11
|
+
* - url: the request url
|
|
12
|
+
* - code: the status code of the response
|
|
13
|
+
* - headers: the headers of the response
|
|
14
|
+
* - body: the body of the response
|
|
15
|
+
* - message: the status text of the response
|
|
16
|
+
* - duration: the response time
|
|
17
|
+
*/
|
|
18
|
+
export function request(method: any, fullUrlOrPath: any, options?: {}): Promise<{
|
|
19
|
+
url: string;
|
|
20
|
+
method: any;
|
|
21
|
+
statusCode: any;
|
|
22
|
+
statusMessage: string;
|
|
23
|
+
headers: any;
|
|
24
|
+
body: any;
|
|
25
|
+
duration: number;
|
|
26
|
+
}>;
|
|
27
|
+
export function makeBasicAuthHeader(username: any, password: any): {
|
|
28
|
+
Authorization: string;
|
|
29
|
+
};
|
|
30
|
+
export function logResponse(response: any): void;
|
|
31
|
+
export function enableMockClient(baseUrl: any): import("undici/types/mock-interceptor").Interceptable;
|
|
32
|
+
export function parseUrl(pathOrUrl: string, baseUrl: any): {
|
|
33
|
+
url: string;
|
|
34
|
+
baseUrl: string;
|
|
35
|
+
path: string;
|
|
36
|
+
};
|
|
37
|
+
export function get(url: any, options: any): Promise<{
|
|
38
|
+
url: string;
|
|
39
|
+
method: any;
|
|
40
|
+
statusCode: any;
|
|
41
|
+
statusMessage: string;
|
|
42
|
+
headers: any;
|
|
43
|
+
body: any;
|
|
44
|
+
duration: number;
|
|
45
|
+
}>;
|
|
46
|
+
export function post(url: any, body: any, options: any): Promise<{
|
|
47
|
+
url: string;
|
|
48
|
+
method: any;
|
|
49
|
+
statusCode: any;
|
|
50
|
+
statusMessage: string;
|
|
51
|
+
headers: any;
|
|
52
|
+
body: any;
|
|
53
|
+
duration: number;
|
|
54
|
+
}>;
|
|
55
|
+
export function put(url: any, body: any, options: any): Promise<{
|
|
56
|
+
url: string;
|
|
57
|
+
method: any;
|
|
58
|
+
statusCode: any;
|
|
59
|
+
statusMessage: string;
|
|
60
|
+
headers: any;
|
|
61
|
+
body: any;
|
|
62
|
+
duration: number;
|
|
63
|
+
}>;
|
|
64
|
+
export function del(url: any, body: any, options: any): Promise<{
|
|
65
|
+
url: string;
|
|
66
|
+
method: any;
|
|
67
|
+
statusCode: any;
|
|
68
|
+
statusMessage: string;
|
|
69
|
+
headers: any;
|
|
70
|
+
body: any;
|
|
71
|
+
duration: number;
|
|
72
|
+
}>;
|
package/types/util.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* General-purpose utility functions
|
|
3
|
-
*
|
|
4
|
-
* These are designed more for use in adaptor code than job code
|
|
5
|
-
* (but we could choose to export util from common)
|
|
6
|
-
*
|
|
7
|
-
* None of these functions are operation factories
|
|
8
|
-
*/
|
|
9
|
-
export function expandReferences(state: any, ...args: any[]): any[];
|
|
10
|
-
export function normalizeOauthConfig(configuration: any): any;
|
|
File without changes
|