@openfn/language-common 2.0.0-rc2 → 2.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/README.md +7 -7
- package/ast.json +1602 -0
- package/dist/index.cjs +690 -4941
- package/dist/index.js +611 -4921
- package/dist/metadata.cjs +59 -0
- package/dist/metadata.js +35 -0
- package/dist/util.cjs +322 -0
- package/dist/util.js +276 -0
- package/package.json +51 -57
- package/types/Adaptor.d.ts +429 -0
- package/types/beta.d.ts +21 -0
- package/types/dateFns.d.ts +1 -0
- package/types/http.d.ts +129 -0
- package/types/index.d.ts +8 -0
- package/types/metadata.d.ts +11 -0
- package/types/util/http.d.ts +76 -0
- package/types/util/index.d.ts +4 -0
- package/types/util/parse-date.d.ts +2 -0
- package/types/util/references.d.ts +2 -0
- package/dist/language-common.d.ts +0 -685
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
|
|
19
|
+
// src/metadata.js
|
|
20
|
+
var metadata_exports = {};
|
|
21
|
+
__export(metadata_exports, {
|
|
22
|
+
createEntity: () => createEntity
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(metadata_exports);
|
|
25
|
+
var createEntity = (name, type, props = {}, children) => {
|
|
26
|
+
const { label, datatype, ...meta } = props;
|
|
27
|
+
const e = {
|
|
28
|
+
name,
|
|
29
|
+
type,
|
|
30
|
+
addChild: function(e2, name2) {
|
|
31
|
+
if (!this.children) {
|
|
32
|
+
this.children = name2 ? {} : [];
|
|
33
|
+
}
|
|
34
|
+
if (name2) {
|
|
35
|
+
if (Array.isArray(this.children)) {
|
|
36
|
+
throw new Error("Cannot add a named entity to child array");
|
|
37
|
+
}
|
|
38
|
+
this.children[name2] = e2;
|
|
39
|
+
} else {
|
|
40
|
+
this.children.push(e2);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
if (label) {
|
|
45
|
+
e.label = label;
|
|
46
|
+
}
|
|
47
|
+
if (datatype) {
|
|
48
|
+
e.datatype = datatype;
|
|
49
|
+
}
|
|
50
|
+
if (children) {
|
|
51
|
+
e.children = children;
|
|
52
|
+
}
|
|
53
|
+
e.meta = meta || {};
|
|
54
|
+
return e;
|
|
55
|
+
};
|
|
56
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
57
|
+
0 && (module.exports = {
|
|
58
|
+
createEntity
|
|
59
|
+
});
|
package/dist/metadata.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// src/metadata.js
|
|
2
|
+
var createEntity = (name, type, props = {}, children) => {
|
|
3
|
+
const { label, datatype, ...meta } = props;
|
|
4
|
+
const e = {
|
|
5
|
+
name,
|
|
6
|
+
type,
|
|
7
|
+
addChild: function(e2, name2) {
|
|
8
|
+
if (!this.children) {
|
|
9
|
+
this.children = name2 ? {} : [];
|
|
10
|
+
}
|
|
11
|
+
if (name2) {
|
|
12
|
+
if (Array.isArray(this.children)) {
|
|
13
|
+
throw new Error("Cannot add a named entity to child array");
|
|
14
|
+
}
|
|
15
|
+
this.children[name2] = e2;
|
|
16
|
+
} else {
|
|
17
|
+
this.children.push(e2);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
if (label) {
|
|
22
|
+
e.label = label;
|
|
23
|
+
}
|
|
24
|
+
if (datatype) {
|
|
25
|
+
e.datatype = datatype;
|
|
26
|
+
}
|
|
27
|
+
if (children) {
|
|
28
|
+
e.children = children;
|
|
29
|
+
}
|
|
30
|
+
e.meta = meta || {};
|
|
31
|
+
return e;
|
|
32
|
+
};
|
|
33
|
+
export {
|
|
34
|
+
createEntity
|
|
35
|
+
};
|
package/dist/util.cjs
ADDED
|
@@ -0,0 +1,322 @@
|
|
|
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/util/index.js
|
|
26
|
+
var util_exports = {};
|
|
27
|
+
__export(util_exports, {
|
|
28
|
+
ERROR_ABSOLUTE_URL: () => ERROR_ABSOLUTE_URL,
|
|
29
|
+
ERROR_URL_MISMATCH: () => ERROR_URL_MISMATCH,
|
|
30
|
+
assertRelativeUrl: () => assertRelativeUrl,
|
|
31
|
+
del: () => del,
|
|
32
|
+
enableMockClient: () => enableMockClient,
|
|
33
|
+
expandReferences: () => expandReferences,
|
|
34
|
+
get: () => get,
|
|
35
|
+
logResponse: () => logResponse,
|
|
36
|
+
makeBasicAuthHeader: () => makeBasicAuthHeader,
|
|
37
|
+
normalizeOauthConfig: () => normalizeOauthConfig,
|
|
38
|
+
parseDate: () => parse_date_default,
|
|
39
|
+
parseUrl: () => parseUrl,
|
|
40
|
+
post: () => post,
|
|
41
|
+
put: () => put,
|
|
42
|
+
request: () => request
|
|
43
|
+
});
|
|
44
|
+
module.exports = __toCommonJS(util_exports);
|
|
45
|
+
|
|
46
|
+
// src/util/http.js
|
|
47
|
+
var import_undici = require("undici");
|
|
48
|
+
var import_http_status_codes = require("http-status-codes");
|
|
49
|
+
var import_node_stream = require("stream");
|
|
50
|
+
var import_node_querystring = __toESM(require("querystring"), 1);
|
|
51
|
+
var import_node_path = __toESM(require("path"), 1);
|
|
52
|
+
var clients = /* @__PURE__ */ new Map();
|
|
53
|
+
var makeBasicAuthHeader = (username, password) => {
|
|
54
|
+
const buff = Buffer.from(`${username}:${password}`);
|
|
55
|
+
const credentials = buff.toString("base64");
|
|
56
|
+
return { Authorization: `Basic ${credentials}` };
|
|
57
|
+
};
|
|
58
|
+
var logResponse = (response) => {
|
|
59
|
+
const { method, url, statusCode, duration } = response;
|
|
60
|
+
if (method && url && duration && statusCode) {
|
|
61
|
+
const message = `${method} ${url} - ${statusCode} in ${duration}ms`;
|
|
62
|
+
if (response instanceof Error) {
|
|
63
|
+
console.error(message);
|
|
64
|
+
console.error("response body: ");
|
|
65
|
+
console.error(response.body || "[no body]");
|
|
66
|
+
} else {
|
|
67
|
+
console.log(message);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return response;
|
|
71
|
+
};
|
|
72
|
+
var getClient = (baseUrl, options) => {
|
|
73
|
+
const { tls } = options;
|
|
74
|
+
if (!clients.has(baseUrl)) {
|
|
75
|
+
clients.set(baseUrl, new import_undici.Client(baseUrl, { connect: tls }));
|
|
76
|
+
}
|
|
77
|
+
return clients.get(baseUrl);
|
|
78
|
+
};
|
|
79
|
+
var enableMockClient = (baseUrl) => {
|
|
80
|
+
const mockAgent = new import_undici.MockAgent({ connections: 1 });
|
|
81
|
+
mockAgent.disableNetConnect();
|
|
82
|
+
const client = mockAgent.get(baseUrl);
|
|
83
|
+
if (!clients.has(baseUrl)) {
|
|
84
|
+
clients.set(baseUrl, client);
|
|
85
|
+
}
|
|
86
|
+
return client;
|
|
87
|
+
};
|
|
88
|
+
var assertOK = async (response, errorMap, fullUrl, method, startTime) => {
|
|
89
|
+
const errMapMessage = errorMap[response.statusCode];
|
|
90
|
+
const isError = typeof errMapMessage === "boolean" ? errMapMessage : errMapMessage || response.statusCode >= 400;
|
|
91
|
+
if (isError) {
|
|
92
|
+
const body = await readResponseBody(response);
|
|
93
|
+
const statusText = (0, import_http_status_codes.getReasonPhrase)(response.statusCode);
|
|
94
|
+
const defaultErrorMesssage = `${method} to ${fullUrl} returned ${response.statusCode}: ${statusText}`;
|
|
95
|
+
const duration = Date.now() - startTime;
|
|
96
|
+
const errMessage = typeof errMapMessage === "function" ? errMapMessage(response) : errMapMessage || defaultErrorMesssage;
|
|
97
|
+
const error = new Error(errMessage);
|
|
98
|
+
error.statusCode = response.statusCode;
|
|
99
|
+
error.statusMessage = statusText;
|
|
100
|
+
error.url = fullUrl;
|
|
101
|
+
error.duration = duration;
|
|
102
|
+
error.method = method;
|
|
103
|
+
error.body = body;
|
|
104
|
+
error.headers = response.headers;
|
|
105
|
+
throw error;
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
var ERROR_ABSOLUTE_URL = "Absolute URLs not suppored";
|
|
109
|
+
var assertRelativeUrl = (path2) => {
|
|
110
|
+
if (/https?:\/\//.test(path2)) {
|
|
111
|
+
const e = new Error("UNEXPECTED_ABSOLUTE_URL");
|
|
112
|
+
e.code = "UNEXPECTED_ABSOLUTE_URL";
|
|
113
|
+
e.description = "An absolute URL was provided (https://...) but only a path (/a/b/c) is supported";
|
|
114
|
+
e.url = path2;
|
|
115
|
+
e.fix = "Remove the protocol, domain and origin from the provided URL. Maybe you need to use the generic HTTP helper functions instead?";
|
|
116
|
+
throw e;
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
var ERROR_URL_MISMATCH = "Target origin does not match baseUrl origin";
|
|
120
|
+
var parseUrl = (pathOrUrl = "", baseUrl) => {
|
|
121
|
+
let fullUrl;
|
|
122
|
+
if (/https?:\/\//.test(pathOrUrl)) {
|
|
123
|
+
fullUrl = new URL(pathOrUrl);
|
|
124
|
+
if (baseUrl) {
|
|
125
|
+
const base = new URL(baseUrl);
|
|
126
|
+
if (fullUrl.origin !== base.origin) {
|
|
127
|
+
const e = new Error(ERROR_URL_MISMATCH);
|
|
128
|
+
e.code = "BASE_URL_MISMATCH";
|
|
129
|
+
e.description = `A request was attempted to an absolute URL, but a different base URL was specified. This is a potential security violation.`;
|
|
130
|
+
e.target = pathOrUrl;
|
|
131
|
+
e.baseUrl = baseUrl;
|
|
132
|
+
e.fix = "Try using a generic HTTP function to access the target URL";
|
|
133
|
+
throw e;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
} else if (baseUrl) {
|
|
137
|
+
fullUrl = new URL(import_node_path.default.join(baseUrl, pathOrUrl));
|
|
138
|
+
} else {
|
|
139
|
+
new URL(pathOrUrl);
|
|
140
|
+
}
|
|
141
|
+
return {
|
|
142
|
+
url: fullUrl.toString(),
|
|
143
|
+
baseUrl: fullUrl.origin,
|
|
144
|
+
path: fullUrl.pathname,
|
|
145
|
+
query: import_node_querystring.default.parse(fullUrl.searchParams.toString())
|
|
146
|
+
};
|
|
147
|
+
};
|
|
148
|
+
async function request(method, fullUrlOrPath, options = {}) {
|
|
149
|
+
const startTime = Date.now();
|
|
150
|
+
const {
|
|
151
|
+
url,
|
|
152
|
+
baseUrl,
|
|
153
|
+
path: path2,
|
|
154
|
+
query: urlQuery
|
|
155
|
+
} = parseUrl(fullUrlOrPath, options.baseUrl);
|
|
156
|
+
const {
|
|
157
|
+
headers = {},
|
|
158
|
+
query: optionQuery = {},
|
|
159
|
+
body,
|
|
160
|
+
errors = {},
|
|
161
|
+
timeout = 3e5,
|
|
162
|
+
tls = {},
|
|
163
|
+
parseAs = "auto",
|
|
164
|
+
maxRedirections
|
|
165
|
+
} = options;
|
|
166
|
+
const client = getClient(baseUrl, { tls });
|
|
167
|
+
const response = await client.request({
|
|
168
|
+
path: path2,
|
|
169
|
+
query: {
|
|
170
|
+
...optionQuery,
|
|
171
|
+
...urlQuery
|
|
172
|
+
},
|
|
173
|
+
method,
|
|
174
|
+
headers,
|
|
175
|
+
body: encodeRequestBody(body),
|
|
176
|
+
throwOnError: false,
|
|
177
|
+
maxRedirections,
|
|
178
|
+
bodyTimeout: timeout,
|
|
179
|
+
headersTimeout: timeout,
|
|
180
|
+
origin: baseUrl
|
|
181
|
+
});
|
|
182
|
+
const statusText = (0, import_http_status_codes.getReasonPhrase)(response.statusCode);
|
|
183
|
+
await assertOK(response, errors, url, method, startTime);
|
|
184
|
+
const responseBody = await readResponseBody(response, parseAs);
|
|
185
|
+
const endTime = Date.now();
|
|
186
|
+
const duration = endTime - startTime;
|
|
187
|
+
return {
|
|
188
|
+
url,
|
|
189
|
+
method,
|
|
190
|
+
statusCode: response.statusCode,
|
|
191
|
+
statusMessage: statusText,
|
|
192
|
+
headers: response.headers,
|
|
193
|
+
body: responseBody,
|
|
194
|
+
duration
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
function encodeRequestBody(body) {
|
|
198
|
+
if (!body) {
|
|
199
|
+
return void 0;
|
|
200
|
+
}
|
|
201
|
+
if (Buffer.isBuffer(body) || body instanceof import_node_stream.Readable || typeof body === "string") {
|
|
202
|
+
return body;
|
|
203
|
+
}
|
|
204
|
+
if (typeof body === "object") {
|
|
205
|
+
if (Symbol.asyncIterator in Object(body) || Symbol.iterator in Object(body) || body instanceof FormData) {
|
|
206
|
+
return body;
|
|
207
|
+
}
|
|
208
|
+
return JSON.stringify(body);
|
|
209
|
+
}
|
|
210
|
+
throw new Error("Unsupported body type");
|
|
211
|
+
}
|
|
212
|
+
async function readResponseBody(response, parseAs) {
|
|
213
|
+
const contentType = response.headers["content-type"];
|
|
214
|
+
switch (parseAs) {
|
|
215
|
+
case "json":
|
|
216
|
+
return response.body.json();
|
|
217
|
+
case "text":
|
|
218
|
+
return response.body.text();
|
|
219
|
+
case "stream":
|
|
220
|
+
return response.body;
|
|
221
|
+
default:
|
|
222
|
+
return contentType && contentType.includes("application/json") ? response.body.json() : response.body.text();
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
var get = (url, options) => request("GET", url, options);
|
|
226
|
+
var post = (url, body, options) => request("POST", url, { body, ...options });
|
|
227
|
+
var put = (url, body, options) => request("PUT", url, { body, ...options });
|
|
228
|
+
var del = (url, body, options) => request("DELETE", url, { body, ...options });
|
|
229
|
+
|
|
230
|
+
// src/util/references.js
|
|
231
|
+
var import_node_stream2 = require("stream");
|
|
232
|
+
function expandReferences(state, ...args) {
|
|
233
|
+
return args.map((value) => expandReference(state, value));
|
|
234
|
+
}
|
|
235
|
+
var isStream = (value) => {
|
|
236
|
+
if (value && typeof value == "object") {
|
|
237
|
+
if (value instanceof import_node_stream2.Readable || value instanceof import_node_stream2.Writable) {
|
|
238
|
+
return true;
|
|
239
|
+
}
|
|
240
|
+
if (value.pipeTo || value.pipe) {
|
|
241
|
+
return true;
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
return false;
|
|
245
|
+
};
|
|
246
|
+
function expandReference(state, value) {
|
|
247
|
+
if (Buffer.isBuffer(value)) {
|
|
248
|
+
return value;
|
|
249
|
+
}
|
|
250
|
+
if (Array.isArray(value)) {
|
|
251
|
+
return value.map((v) => expandReference(state, v));
|
|
252
|
+
}
|
|
253
|
+
if (typeof value == "object" && !!value && !isStream(value)) {
|
|
254
|
+
return Object.keys(value).reduce((acc, key) => {
|
|
255
|
+
return { ...acc, [key]: expandReference(state, value[key]) };
|
|
256
|
+
}, {});
|
|
257
|
+
}
|
|
258
|
+
if (typeof value == "function") {
|
|
259
|
+
return expandReference(state, value(state));
|
|
260
|
+
}
|
|
261
|
+
return value;
|
|
262
|
+
}
|
|
263
|
+
function normalizeOauthConfig(configuration) {
|
|
264
|
+
const { access_token, accessToken } = configuration;
|
|
265
|
+
if (access_token && accessToken)
|
|
266
|
+
throw new Error(
|
|
267
|
+
'Both "accessToken" & "access_token" keys found in configuration; please use only "access_token" for OAuth2 credentials.'
|
|
268
|
+
);
|
|
269
|
+
if (access_token)
|
|
270
|
+
return { ...configuration, accessToken: access_token };
|
|
271
|
+
console.log(
|
|
272
|
+
'Key "access_token" not found in state.configuration;',
|
|
273
|
+
"is this a standard OAuth 2.0 JSON credential?"
|
|
274
|
+
);
|
|
275
|
+
if (accessToken)
|
|
276
|
+
console.log('Using "accessToken" from state.configuration');
|
|
277
|
+
return configuration;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// src/util/parse-date.js
|
|
281
|
+
var import_date_fns = require("date-fns");
|
|
282
|
+
var parse_date_default = (d, startDate) => {
|
|
283
|
+
try {
|
|
284
|
+
if (d === "start") {
|
|
285
|
+
return startDate;
|
|
286
|
+
} else if (d === "now" || d === "end") {
|
|
287
|
+
return new Date();
|
|
288
|
+
} else if (d === "today") {
|
|
289
|
+
return (0, import_date_fns.startOfToday)();
|
|
290
|
+
} else if (d === "yesterday") {
|
|
291
|
+
return (0, import_date_fns.startOfYesterday)();
|
|
292
|
+
} else if (/(hours? ago)$/.test(d)) {
|
|
293
|
+
const [diff] = d.match(/\d+/);
|
|
294
|
+
return (0, import_date_fns.subHours)(new Date(), diff);
|
|
295
|
+
} else if (/(days? ago)$/.test(d)) {
|
|
296
|
+
const [diff] = d.match(/\d+/);
|
|
297
|
+
return (0, import_date_fns.startOfDay)((0, import_date_fns.subDays)(new Date(), diff));
|
|
298
|
+
}
|
|
299
|
+
} catch (e) {
|
|
300
|
+
console.log(`Error converting ${d} into a date`);
|
|
301
|
+
console.log(e);
|
|
302
|
+
}
|
|
303
|
+
return d;
|
|
304
|
+
};
|
|
305
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
306
|
+
0 && (module.exports = {
|
|
307
|
+
ERROR_ABSOLUTE_URL,
|
|
308
|
+
ERROR_URL_MISMATCH,
|
|
309
|
+
assertRelativeUrl,
|
|
310
|
+
del,
|
|
311
|
+
enableMockClient,
|
|
312
|
+
expandReferences,
|
|
313
|
+
get,
|
|
314
|
+
logResponse,
|
|
315
|
+
makeBasicAuthHeader,
|
|
316
|
+
normalizeOauthConfig,
|
|
317
|
+
parseDate,
|
|
318
|
+
parseUrl,
|
|
319
|
+
post,
|
|
320
|
+
put,
|
|
321
|
+
request
|
|
322
|
+
});
|
package/dist/util.js
ADDED
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
// src/util/http.js
|
|
2
|
+
import { Client, MockAgent } from "undici";
|
|
3
|
+
import { getReasonPhrase } from "http-status-codes";
|
|
4
|
+
import { Readable } from "stream";
|
|
5
|
+
import querystring from "querystring";
|
|
6
|
+
import path from "path";
|
|
7
|
+
var clients = /* @__PURE__ */ new Map();
|
|
8
|
+
var makeBasicAuthHeader = (username, password) => {
|
|
9
|
+
const buff = Buffer.from(`${username}:${password}`);
|
|
10
|
+
const credentials = buff.toString("base64");
|
|
11
|
+
return { Authorization: `Basic ${credentials}` };
|
|
12
|
+
};
|
|
13
|
+
var logResponse = (response) => {
|
|
14
|
+
const { method, url, statusCode, duration } = response;
|
|
15
|
+
if (method && url && duration && statusCode) {
|
|
16
|
+
const message = `${method} ${url} - ${statusCode} in ${duration}ms`;
|
|
17
|
+
if (response instanceof Error) {
|
|
18
|
+
console.error(message);
|
|
19
|
+
console.error("response body: ");
|
|
20
|
+
console.error(response.body || "[no body]");
|
|
21
|
+
} else {
|
|
22
|
+
console.log(message);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return response;
|
|
26
|
+
};
|
|
27
|
+
var getClient = (baseUrl, options) => {
|
|
28
|
+
const { tls } = options;
|
|
29
|
+
if (!clients.has(baseUrl)) {
|
|
30
|
+
clients.set(baseUrl, new Client(baseUrl, { connect: tls }));
|
|
31
|
+
}
|
|
32
|
+
return clients.get(baseUrl);
|
|
33
|
+
};
|
|
34
|
+
var enableMockClient = (baseUrl) => {
|
|
35
|
+
const mockAgent = new MockAgent({ connections: 1 });
|
|
36
|
+
mockAgent.disableNetConnect();
|
|
37
|
+
const client = mockAgent.get(baseUrl);
|
|
38
|
+
if (!clients.has(baseUrl)) {
|
|
39
|
+
clients.set(baseUrl, client);
|
|
40
|
+
}
|
|
41
|
+
return client;
|
|
42
|
+
};
|
|
43
|
+
var assertOK = async (response, errorMap, fullUrl, method, startTime) => {
|
|
44
|
+
const errMapMessage = errorMap[response.statusCode];
|
|
45
|
+
const isError = typeof errMapMessage === "boolean" ? errMapMessage : errMapMessage || response.statusCode >= 400;
|
|
46
|
+
if (isError) {
|
|
47
|
+
const body = await readResponseBody(response);
|
|
48
|
+
const statusText = getReasonPhrase(response.statusCode);
|
|
49
|
+
const defaultErrorMesssage = `${method} to ${fullUrl} returned ${response.statusCode}: ${statusText}`;
|
|
50
|
+
const duration = Date.now() - startTime;
|
|
51
|
+
const errMessage = typeof errMapMessage === "function" ? errMapMessage(response) : errMapMessage || defaultErrorMesssage;
|
|
52
|
+
const error = new Error(errMessage);
|
|
53
|
+
error.statusCode = response.statusCode;
|
|
54
|
+
error.statusMessage = statusText;
|
|
55
|
+
error.url = fullUrl;
|
|
56
|
+
error.duration = duration;
|
|
57
|
+
error.method = method;
|
|
58
|
+
error.body = body;
|
|
59
|
+
error.headers = response.headers;
|
|
60
|
+
throw error;
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
var ERROR_ABSOLUTE_URL = "Absolute URLs not suppored";
|
|
64
|
+
var assertRelativeUrl = (path2) => {
|
|
65
|
+
if (/https?:\/\//.test(path2)) {
|
|
66
|
+
const e = new Error("UNEXPECTED_ABSOLUTE_URL");
|
|
67
|
+
e.code = "UNEXPECTED_ABSOLUTE_URL";
|
|
68
|
+
e.description = "An absolute URL was provided (https://...) but only a path (/a/b/c) is supported";
|
|
69
|
+
e.url = path2;
|
|
70
|
+
e.fix = "Remove the protocol, domain and origin from the provided URL. Maybe you need to use the generic HTTP helper functions instead?";
|
|
71
|
+
throw e;
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
var ERROR_URL_MISMATCH = "Target origin does not match baseUrl origin";
|
|
75
|
+
var parseUrl = (pathOrUrl = "", baseUrl) => {
|
|
76
|
+
let fullUrl;
|
|
77
|
+
if (/https?:\/\//.test(pathOrUrl)) {
|
|
78
|
+
fullUrl = new URL(pathOrUrl);
|
|
79
|
+
if (baseUrl) {
|
|
80
|
+
const base = new URL(baseUrl);
|
|
81
|
+
if (fullUrl.origin !== base.origin) {
|
|
82
|
+
const e = new Error(ERROR_URL_MISMATCH);
|
|
83
|
+
e.code = "BASE_URL_MISMATCH";
|
|
84
|
+
e.description = `A request was attempted to an absolute URL, but a different base URL was specified. This is a potential security violation.`;
|
|
85
|
+
e.target = pathOrUrl;
|
|
86
|
+
e.baseUrl = baseUrl;
|
|
87
|
+
e.fix = "Try using a generic HTTP function to access the target URL";
|
|
88
|
+
throw e;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
} else if (baseUrl) {
|
|
92
|
+
fullUrl = new URL(path.join(baseUrl, pathOrUrl));
|
|
93
|
+
} else {
|
|
94
|
+
new URL(pathOrUrl);
|
|
95
|
+
}
|
|
96
|
+
return {
|
|
97
|
+
url: fullUrl.toString(),
|
|
98
|
+
baseUrl: fullUrl.origin,
|
|
99
|
+
path: fullUrl.pathname,
|
|
100
|
+
query: querystring.parse(fullUrl.searchParams.toString())
|
|
101
|
+
};
|
|
102
|
+
};
|
|
103
|
+
async function request(method, fullUrlOrPath, options = {}) {
|
|
104
|
+
const startTime = Date.now();
|
|
105
|
+
const {
|
|
106
|
+
url,
|
|
107
|
+
baseUrl,
|
|
108
|
+
path: path2,
|
|
109
|
+
query: urlQuery
|
|
110
|
+
} = parseUrl(fullUrlOrPath, options.baseUrl);
|
|
111
|
+
const {
|
|
112
|
+
headers = {},
|
|
113
|
+
query: optionQuery = {},
|
|
114
|
+
body,
|
|
115
|
+
errors = {},
|
|
116
|
+
timeout = 3e5,
|
|
117
|
+
tls = {},
|
|
118
|
+
parseAs = "auto",
|
|
119
|
+
maxRedirections
|
|
120
|
+
} = options;
|
|
121
|
+
const client = getClient(baseUrl, { tls });
|
|
122
|
+
const response = await client.request({
|
|
123
|
+
path: path2,
|
|
124
|
+
query: {
|
|
125
|
+
...optionQuery,
|
|
126
|
+
...urlQuery
|
|
127
|
+
},
|
|
128
|
+
method,
|
|
129
|
+
headers,
|
|
130
|
+
body: encodeRequestBody(body),
|
|
131
|
+
throwOnError: false,
|
|
132
|
+
maxRedirections,
|
|
133
|
+
bodyTimeout: timeout,
|
|
134
|
+
headersTimeout: timeout,
|
|
135
|
+
origin: baseUrl
|
|
136
|
+
});
|
|
137
|
+
const statusText = 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 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
|
+
import { Readable as Readable2, Writable } from "stream";
|
|
187
|
+
function expandReferences(state, ...args) {
|
|
188
|
+
return args.map((value) => expandReference(state, value));
|
|
189
|
+
}
|
|
190
|
+
var isStream = (value) => {
|
|
191
|
+
if (value && typeof value == "object") {
|
|
192
|
+
if (value instanceof Readable2 || value instanceof Writable) {
|
|
193
|
+
return true;
|
|
194
|
+
}
|
|
195
|
+
if (value.pipeTo || value.pipe) {
|
|
196
|
+
return true;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
return false;
|
|
200
|
+
};
|
|
201
|
+
function expandReference(state, value) {
|
|
202
|
+
if (Buffer.isBuffer(value)) {
|
|
203
|
+
return value;
|
|
204
|
+
}
|
|
205
|
+
if (Array.isArray(value)) {
|
|
206
|
+
return value.map((v) => expandReference(state, v));
|
|
207
|
+
}
|
|
208
|
+
if (typeof value == "object" && !!value && !isStream(value)) {
|
|
209
|
+
return Object.keys(value).reduce((acc, key) => {
|
|
210
|
+
return { ...acc, [key]: expandReference(state, value[key]) };
|
|
211
|
+
}, {});
|
|
212
|
+
}
|
|
213
|
+
if (typeof value == "function") {
|
|
214
|
+
return expandReference(state, value(state));
|
|
215
|
+
}
|
|
216
|
+
return value;
|
|
217
|
+
}
|
|
218
|
+
function normalizeOauthConfig(configuration) {
|
|
219
|
+
const { access_token, accessToken } = configuration;
|
|
220
|
+
if (access_token && accessToken)
|
|
221
|
+
throw new Error(
|
|
222
|
+
'Both "accessToken" & "access_token" keys found in configuration; please use only "access_token" for OAuth2 credentials.'
|
|
223
|
+
);
|
|
224
|
+
if (access_token)
|
|
225
|
+
return { ...configuration, accessToken: access_token };
|
|
226
|
+
console.log(
|
|
227
|
+
'Key "access_token" not found in state.configuration;',
|
|
228
|
+
"is this a standard OAuth 2.0 JSON credential?"
|
|
229
|
+
);
|
|
230
|
+
if (accessToken)
|
|
231
|
+
console.log('Using "accessToken" from state.configuration');
|
|
232
|
+
return configuration;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// src/util/parse-date.js
|
|
236
|
+
import { startOfToday, startOfYesterday, subHours, subDays, startOfDay } from "date-fns";
|
|
237
|
+
var parse_date_default = (d, startDate) => {
|
|
238
|
+
try {
|
|
239
|
+
if (d === "start") {
|
|
240
|
+
return startDate;
|
|
241
|
+
} else if (d === "now" || d === "end") {
|
|
242
|
+
return new Date();
|
|
243
|
+
} else if (d === "today") {
|
|
244
|
+
return startOfToday();
|
|
245
|
+
} else if (d === "yesterday") {
|
|
246
|
+
return startOfYesterday();
|
|
247
|
+
} else if (/(hours? ago)$/.test(d)) {
|
|
248
|
+
const [diff] = d.match(/\d+/);
|
|
249
|
+
return subHours(new Date(), diff);
|
|
250
|
+
} else if (/(days? ago)$/.test(d)) {
|
|
251
|
+
const [diff] = d.match(/\d+/);
|
|
252
|
+
return startOfDay(subDays(new Date(), diff));
|
|
253
|
+
}
|
|
254
|
+
} catch (e) {
|
|
255
|
+
console.log(`Error converting ${d} into a date`);
|
|
256
|
+
console.log(e);
|
|
257
|
+
}
|
|
258
|
+
return d;
|
|
259
|
+
};
|
|
260
|
+
export {
|
|
261
|
+
ERROR_ABSOLUTE_URL,
|
|
262
|
+
ERROR_URL_MISMATCH,
|
|
263
|
+
assertRelativeUrl,
|
|
264
|
+
del,
|
|
265
|
+
enableMockClient,
|
|
266
|
+
expandReferences,
|
|
267
|
+
get,
|
|
268
|
+
logResponse,
|
|
269
|
+
makeBasicAuthHeader,
|
|
270
|
+
normalizeOauthConfig,
|
|
271
|
+
parse_date_default as parseDate,
|
|
272
|
+
parseUrl,
|
|
273
|
+
post,
|
|
274
|
+
put,
|
|
275
|
+
request
|
|
276
|
+
};
|