lifecycleion 0.0.8 → 0.0.10
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 +45 -36
- package/dist/lib/domain-utils/domain-utils.cjs +1154 -0
- package/dist/lib/domain-utils/domain-utils.cjs.map +1 -0
- package/dist/lib/domain-utils/domain-utils.d.cts +210 -0
- package/dist/lib/domain-utils/domain-utils.d.ts +210 -0
- package/dist/lib/domain-utils/domain-utils.js +1112 -0
- package/dist/lib/domain-utils/domain-utils.js.map +1 -0
- package/dist/lib/http-client/index.cjs +5254 -0
- package/dist/lib/http-client/index.cjs.map +1 -0
- package/dist/lib/http-client/index.d.cts +372 -0
- package/dist/lib/http-client/index.d.ts +372 -0
- package/dist/lib/http-client/index.js +5207 -0
- package/dist/lib/http-client/index.js.map +1 -0
- package/dist/lib/http-client-mock/index.cjs +525 -0
- package/dist/lib/http-client-mock/index.cjs.map +1 -0
- package/dist/lib/http-client-mock/index.d.cts +129 -0
- package/dist/lib/http-client-mock/index.d.ts +129 -0
- package/dist/lib/http-client-mock/index.js +488 -0
- package/dist/lib/http-client-mock/index.js.map +1 -0
- package/dist/lib/http-client-node/index.cjs +1112 -0
- package/dist/lib/http-client-node/index.cjs.map +1 -0
- package/dist/lib/http-client-node/index.d.cts +43 -0
- package/dist/lib/http-client-node/index.d.ts +43 -0
- package/dist/lib/http-client-node/index.js +1075 -0
- package/dist/lib/http-client-node/index.js.map +1 -0
- package/dist/lib/http-client-xhr/index.cjs +323 -0
- package/dist/lib/http-client-xhr/index.cjs.map +1 -0
- package/dist/lib/http-client-xhr/index.d.cts +23 -0
- package/dist/lib/http-client-xhr/index.d.ts +23 -0
- package/dist/lib/http-client-xhr/index.js +286 -0
- package/dist/lib/http-client-xhr/index.js.map +1 -0
- package/dist/lib/lifecycle-manager/index.cjs +118 -61
- package/dist/lib/lifecycle-manager/index.cjs.map +1 -1
- package/dist/lib/lifecycle-manager/index.js +118 -61
- package/dist/lib/lifecycle-manager/index.js.map +1 -1
- package/dist/lib/lru-cache/index.cjs +274 -0
- package/dist/lib/lru-cache/index.cjs.map +1 -0
- package/dist/lib/lru-cache/index.d.cts +84 -0
- package/dist/lib/lru-cache/index.d.ts +84 -0
- package/dist/lib/lru-cache/index.js +249 -0
- package/dist/lib/lru-cache/index.js.map +1 -0
- package/dist/lib/retry-utils/index.d.cts +3 -23
- package/dist/lib/retry-utils/index.d.ts +3 -23
- package/dist/types-CUPvmYQ8.d.cts +868 -0
- package/dist/types-D_MywcG0.d.cts +23 -0
- package/dist/types-D_MywcG0.d.ts +23 -0
- package/dist/types-Hw2PUTIT.d.ts +868 -0
- package/package.json +45 -3
|
@@ -0,0 +1,525 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/lib/http-client-mock/index.ts
|
|
31
|
+
var http_client_mock_exports = {};
|
|
32
|
+
__export(http_client_mock_exports, {
|
|
33
|
+
MockAdapter: () => MockAdapter
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(http_client_mock_exports);
|
|
36
|
+
|
|
37
|
+
// src/lib/http-client/adapters/mock-adapter.ts
|
|
38
|
+
var import_find_my_way = __toESM(require("find-my-way"), 1);
|
|
39
|
+
var import_qs2 = __toESM(require("qs"), 1);
|
|
40
|
+
|
|
41
|
+
// src/lib/sleep.ts
|
|
42
|
+
async function sleep(time) {
|
|
43
|
+
return new Promise(function(resolve) {
|
|
44
|
+
setTimeout(function() {
|
|
45
|
+
resolve();
|
|
46
|
+
}, time);
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// src/lib/http-client/consts.ts
|
|
51
|
+
var REDIRECT_STATUS_CODES = /* @__PURE__ */ new Set([
|
|
52
|
+
// 301 Moved Permanently
|
|
53
|
+
301,
|
|
54
|
+
// 302 Found
|
|
55
|
+
302,
|
|
56
|
+
// 303 See Other
|
|
57
|
+
303,
|
|
58
|
+
// 307 Temporary Redirect
|
|
59
|
+
307,
|
|
60
|
+
// 308 Permanent Redirect
|
|
61
|
+
308
|
|
62
|
+
]);
|
|
63
|
+
|
|
64
|
+
// src/lib/http-client/utils.ts
|
|
65
|
+
var import_qs = __toESM(require("qs"), 1);
|
|
66
|
+
|
|
67
|
+
// src/lib/domain-utils/domain-utils.ts
|
|
68
|
+
var import_tldts = require("tldts");
|
|
69
|
+
|
|
70
|
+
// src/lib/domain-utils/helpers.ts
|
|
71
|
+
var import_tr46 = require("tr46");
|
|
72
|
+
var INTERNAL_PSEUDO_TLDS = Object.freeze(
|
|
73
|
+
/* @__PURE__ */ new Set(["localhost", "local", "test", "internal"])
|
|
74
|
+
);
|
|
75
|
+
|
|
76
|
+
// src/lib/domain-utils/domain-utils.ts
|
|
77
|
+
var import_tldts2 = require("tldts");
|
|
78
|
+
|
|
79
|
+
// src/lib/http-client/utils.ts
|
|
80
|
+
function resolveAbsoluteURL(url, baseURL) {
|
|
81
|
+
if (!url) {
|
|
82
|
+
return url;
|
|
83
|
+
}
|
|
84
|
+
try {
|
|
85
|
+
return new URL(url).href;
|
|
86
|
+
} catch {
|
|
87
|
+
}
|
|
88
|
+
if (baseURL) {
|
|
89
|
+
try {
|
|
90
|
+
const base = baseURL.endsWith("/") ? baseURL : `${baseURL}/`;
|
|
91
|
+
return new URL(url, base).href;
|
|
92
|
+
} catch {
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return url;
|
|
96
|
+
}
|
|
97
|
+
function parseContentType(contentTypeHeader) {
|
|
98
|
+
if (!contentTypeHeader) {
|
|
99
|
+
return "binary";
|
|
100
|
+
} else {
|
|
101
|
+
const lower = contentTypeHeader.trim().toLowerCase();
|
|
102
|
+
if (lower.includes("application/json") || lower.includes("+json")) {
|
|
103
|
+
return "json";
|
|
104
|
+
} else if (lower.startsWith("text/")) {
|
|
105
|
+
return "text";
|
|
106
|
+
} else if (lower.includes("application/x-www-form-urlencoded")) {
|
|
107
|
+
return "text";
|
|
108
|
+
} else {
|
|
109
|
+
return "binary";
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
function normalizeAdapterResponseHeaders(headers) {
|
|
114
|
+
const result = {};
|
|
115
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
116
|
+
const lower = key.toLowerCase();
|
|
117
|
+
if (lower === "set-cookie") {
|
|
118
|
+
const chunk = Array.isArray(value) ? value : [value];
|
|
119
|
+
const existing = result[lower];
|
|
120
|
+
if (existing === void 0) {
|
|
121
|
+
result[lower] = chunk;
|
|
122
|
+
} else {
|
|
123
|
+
const existingLines = Array.isArray(existing) ? existing : [existing];
|
|
124
|
+
result[lower] = [...existingLines, ...chunk];
|
|
125
|
+
}
|
|
126
|
+
} else {
|
|
127
|
+
result[lower] = Array.isArray(value) ? value[0] ?? "" : value;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return result;
|
|
131
|
+
}
|
|
132
|
+
function scalarHeader(headers, lowercaseName) {
|
|
133
|
+
const v = headers[lowercaseName];
|
|
134
|
+
if (v === void 0) {
|
|
135
|
+
return void 0;
|
|
136
|
+
}
|
|
137
|
+
return Array.isArray(v) ? v[0] : v;
|
|
138
|
+
}
|
|
139
|
+
function resolveDetectedRedirectURL(requestURL, status, headers, baseURL) {
|
|
140
|
+
if (![301, 302, 303, 307, 308].includes(status)) {
|
|
141
|
+
return void 0;
|
|
142
|
+
}
|
|
143
|
+
const location = scalarHeader(headers, "location");
|
|
144
|
+
if (!location) {
|
|
145
|
+
return void 0;
|
|
146
|
+
}
|
|
147
|
+
try {
|
|
148
|
+
const absoluteRequestURL = resolveAbsoluteURL(requestURL, baseURL);
|
|
149
|
+
return new URL(location, absoluteRequestURL).toString();
|
|
150
|
+
} catch {
|
|
151
|
+
return location;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
function isPlainJSONBodyObject(value) {
|
|
155
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) {
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
const prototype = Reflect.getPrototypeOf(value);
|
|
159
|
+
return prototype === Object.prototype || prototype === null;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// src/lib/http-client/adapters/mock-adapter.ts
|
|
163
|
+
var noop = () => {
|
|
164
|
+
};
|
|
165
|
+
var MockAdapter = class {
|
|
166
|
+
routes;
|
|
167
|
+
router;
|
|
168
|
+
config;
|
|
169
|
+
constructor(config) {
|
|
170
|
+
this.config = config ?? {};
|
|
171
|
+
this.router = (0, import_find_my_way.default)({
|
|
172
|
+
ignoreTrailingSlash: true,
|
|
173
|
+
ignoreDuplicateSlashes: false,
|
|
174
|
+
maxParamLength: 100
|
|
175
|
+
});
|
|
176
|
+
this.routes = buildRoutes(this.router);
|
|
177
|
+
}
|
|
178
|
+
getType() {
|
|
179
|
+
return "mock";
|
|
180
|
+
}
|
|
181
|
+
async send(request) {
|
|
182
|
+
const { requestURL, method, headers, body } = request;
|
|
183
|
+
const materializedHeaders = materializeMockRequestHeaders(headers);
|
|
184
|
+
if (request.signal?.aborted) {
|
|
185
|
+
throwAbortError();
|
|
186
|
+
}
|
|
187
|
+
request.onUploadProgress?.({ loaded: 0, total: 0, progress: 0 });
|
|
188
|
+
let path;
|
|
189
|
+
let queryString;
|
|
190
|
+
try {
|
|
191
|
+
const url = new URL(requestURL);
|
|
192
|
+
path = url.pathname;
|
|
193
|
+
queryString = url.search.slice(1);
|
|
194
|
+
} catch {
|
|
195
|
+
const qIdx = requestURL.indexOf("?");
|
|
196
|
+
if (qIdx >= 0) {
|
|
197
|
+
path = requestURL.slice(0, qIdx);
|
|
198
|
+
queryString = requestURL.slice(qIdx + 1);
|
|
199
|
+
} else {
|
|
200
|
+
path = requestURL;
|
|
201
|
+
queryString = "";
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
const query = import_qs2.default.parse(queryString);
|
|
205
|
+
const cookies = parseCookieHeader(materializedHeaders["cookie"]);
|
|
206
|
+
const match = this.router.find(method, path);
|
|
207
|
+
const params = match?.params ?? {};
|
|
208
|
+
const mockRequest = {
|
|
209
|
+
method,
|
|
210
|
+
path,
|
|
211
|
+
params,
|
|
212
|
+
query,
|
|
213
|
+
headers: materializedHeaders,
|
|
214
|
+
cookies,
|
|
215
|
+
body: body instanceof FormData ? extractFormData(body) : parseRequestBody(body, materializedHeaders["content-type"])
|
|
216
|
+
};
|
|
217
|
+
let mockResponse;
|
|
218
|
+
if (match === null) {
|
|
219
|
+
mockResponse = { status: 404, body: { message: "Not Found" } };
|
|
220
|
+
} else {
|
|
221
|
+
const handler = match.store;
|
|
222
|
+
try {
|
|
223
|
+
mockResponse = await awaitAbortable(
|
|
224
|
+
handler(mockRequest),
|
|
225
|
+
request.signal
|
|
226
|
+
);
|
|
227
|
+
} catch (handlerError) {
|
|
228
|
+
if (isInternalAbortError(handlerError)) {
|
|
229
|
+
throwAbortError();
|
|
230
|
+
}
|
|
231
|
+
if (this.config.onError) {
|
|
232
|
+
try {
|
|
233
|
+
mockResponse = await awaitAbortable(
|
|
234
|
+
this.config.onError(mockRequest, handlerError),
|
|
235
|
+
request.signal
|
|
236
|
+
);
|
|
237
|
+
} catch (error) {
|
|
238
|
+
if (isInternalAbortError(error)) {
|
|
239
|
+
throwAbortError();
|
|
240
|
+
}
|
|
241
|
+
mockResponse = {
|
|
242
|
+
status: 500,
|
|
243
|
+
body: { message: "Internal Server Error" }
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
} else {
|
|
247
|
+
mockResponse = {
|
|
248
|
+
status: 500,
|
|
249
|
+
body: { message: "Internal Server Error" }
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
const delay = mockResponse.delay ?? this.config.defaultDelay ?? 0;
|
|
255
|
+
if (delay > 0) {
|
|
256
|
+
if (request.signal) {
|
|
257
|
+
await sleepAbortable(delay, request.signal);
|
|
258
|
+
} else {
|
|
259
|
+
await sleep(delay);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
if (request.signal?.aborted) {
|
|
263
|
+
throwAbortError();
|
|
264
|
+
}
|
|
265
|
+
const responseBody = shouldOmitResponseBody(method, mockResponse.status) ? null : serializeResponseBody(mockResponse);
|
|
266
|
+
request.onUploadProgress?.({ loaded: 1, total: 1, progress: 1 });
|
|
267
|
+
request.onDownloadProgress?.({
|
|
268
|
+
loaded: responseBody?.length ?? 0,
|
|
269
|
+
total: responseBody?.length ?? 0,
|
|
270
|
+
progress: 1
|
|
271
|
+
});
|
|
272
|
+
const responseHeaders = {
|
|
273
|
+
...mockResponse.headers ?? {}
|
|
274
|
+
};
|
|
275
|
+
if (mockResponse.cookies) {
|
|
276
|
+
const setCookieEntries = cookiesToSetCookieHeaders(mockResponse.cookies);
|
|
277
|
+
if (setCookieEntries.length > 0) {
|
|
278
|
+
const existing = responseHeaders["set-cookie"];
|
|
279
|
+
const existingArr = existing === void 0 ? [] : Array.isArray(existing) ? existing : [existing];
|
|
280
|
+
responseHeaders["set-cookie"] = [...existingArr, ...setCookieEntries];
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
if (!hasHeader(responseHeaders, "content-type") && responseBody !== null) {
|
|
284
|
+
const ct = mockResponse.contentType ?? inferContentType(mockResponse.body);
|
|
285
|
+
if (ct === "json") {
|
|
286
|
+
responseHeaders["content-type"] = "application/json";
|
|
287
|
+
} else if (ct === "text") {
|
|
288
|
+
responseHeaders["content-type"] = "text/plain";
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
const normalizedResponseHeaders = normalizeAdapterResponseHeaders(responseHeaders);
|
|
292
|
+
const detectedRedirectURL = resolveDetectedRedirectURL(
|
|
293
|
+
request.requestURL,
|
|
294
|
+
mockResponse.status,
|
|
295
|
+
normalizedResponseHeaders
|
|
296
|
+
);
|
|
297
|
+
return {
|
|
298
|
+
status: mockResponse.status,
|
|
299
|
+
wasRedirectDetected: REDIRECT_STATUS_CODES.has(mockResponse.status),
|
|
300
|
+
...detectedRedirectURL ? { detectedRedirectURL } : {},
|
|
301
|
+
headers: normalizedResponseHeaders,
|
|
302
|
+
body: responseBody
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
};
|
|
306
|
+
function buildRoutes(router) {
|
|
307
|
+
function on(method, path, handler) {
|
|
308
|
+
try {
|
|
309
|
+
router.on(method, path, noop, handler);
|
|
310
|
+
} catch (error) {
|
|
311
|
+
if (isDuplicateRouteRegistrationError(error)) {
|
|
312
|
+
throw duplicateRouteRegistrationError(method, path, error);
|
|
313
|
+
}
|
|
314
|
+
throw error;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
return {
|
|
318
|
+
get: (path, handler) => on("GET", path, handler),
|
|
319
|
+
post: (path, handler) => on("POST", path, handler),
|
|
320
|
+
put: (path, handler) => on("PUT", path, handler),
|
|
321
|
+
patch: (path, handler) => on("PATCH", path, handler),
|
|
322
|
+
delete: (path, handler) => on("DELETE", path, handler),
|
|
323
|
+
head: (path, handler) => on("HEAD", path, handler),
|
|
324
|
+
clear: () => router.reset()
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
function isDuplicateRouteRegistrationError(error) {
|
|
328
|
+
return error instanceof Error && error.message.includes("already declared for route");
|
|
329
|
+
}
|
|
330
|
+
function duplicateRouteRegistrationError(method, path, cause) {
|
|
331
|
+
return new Error(
|
|
332
|
+
`[MockAdapter] Duplicate route registration for ${method} ${path}. Routes must be unique per method and normalized path.`,
|
|
333
|
+
{ cause }
|
|
334
|
+
);
|
|
335
|
+
}
|
|
336
|
+
function parseCookieHeader(cookieHeader) {
|
|
337
|
+
if (!cookieHeader) {
|
|
338
|
+
return {};
|
|
339
|
+
}
|
|
340
|
+
const cookies = {};
|
|
341
|
+
for (const part of cookieHeader.split(";")) {
|
|
342
|
+
const eqIdx = part.indexOf("=");
|
|
343
|
+
if (eqIdx < 0) {
|
|
344
|
+
continue;
|
|
345
|
+
}
|
|
346
|
+
const name = part.slice(0, eqIdx).trim();
|
|
347
|
+
const value = part.slice(eqIdx + 1).trim();
|
|
348
|
+
if (name) {
|
|
349
|
+
cookies[name] = value;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
return cookies;
|
|
353
|
+
}
|
|
354
|
+
function materializeMockRequestHeaders(headers) {
|
|
355
|
+
const result = {};
|
|
356
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
357
|
+
result[key] = Array.isArray(value) ? key.toLowerCase() === "cookie" ? value.join("; ") : value.join(", ") : value;
|
|
358
|
+
}
|
|
359
|
+
return result;
|
|
360
|
+
}
|
|
361
|
+
function cookiesToSetCookieHeaders(cookies) {
|
|
362
|
+
return Object.entries(cookies).map(([name, value]) => {
|
|
363
|
+
if (value === null) {
|
|
364
|
+
return `${name}=; Path=/; Max-Age=0`;
|
|
365
|
+
}
|
|
366
|
+
if (typeof value === "string") {
|
|
367
|
+
return `${name}=${value}; Path=/`;
|
|
368
|
+
}
|
|
369
|
+
return serializeMockCookieOptions(name, value);
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
function serializeMockCookieOptions(name, cookie) {
|
|
373
|
+
const parts = [
|
|
374
|
+
`${name}=${cookie.value}`,
|
|
375
|
+
`Path=${cookie.path ?? "/"}`
|
|
376
|
+
];
|
|
377
|
+
appendCookieAttribute(parts, "Max-Age", cookie.maxAge);
|
|
378
|
+
appendCookieAttribute(parts, "Domain", cookie.domain);
|
|
379
|
+
if (cookie.httpOnly) {
|
|
380
|
+
parts.push("HttpOnly");
|
|
381
|
+
}
|
|
382
|
+
if (cookie.secure) {
|
|
383
|
+
parts.push("Secure");
|
|
384
|
+
}
|
|
385
|
+
appendCookieAttribute(parts, "SameSite", cookie.sameSite);
|
|
386
|
+
return parts.join("; ");
|
|
387
|
+
}
|
|
388
|
+
function appendCookieAttribute(parts, name, value) {
|
|
389
|
+
if (value !== void 0 && value !== "") {
|
|
390
|
+
parts.push(`${name}=${value}`);
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
function extractFormData(fd) {
|
|
394
|
+
const fields = {};
|
|
395
|
+
const files = {};
|
|
396
|
+
for (const [key, value] of fd.entries()) {
|
|
397
|
+
if (typeof value === "string") {
|
|
398
|
+
fields[key] = value;
|
|
399
|
+
} else {
|
|
400
|
+
files[key] = value;
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
return { fields, files };
|
|
404
|
+
}
|
|
405
|
+
function parseRequestBody(body, contentType) {
|
|
406
|
+
if (body === null || body === void 0) {
|
|
407
|
+
return void 0;
|
|
408
|
+
}
|
|
409
|
+
const parsedContentType = parseContentType(contentType);
|
|
410
|
+
if (body instanceof Uint8Array) {
|
|
411
|
+
if (parsedContentType === "binary") {
|
|
412
|
+
return body;
|
|
413
|
+
} else if (parsedContentType === "json") {
|
|
414
|
+
const text = new TextDecoder().decode(body);
|
|
415
|
+
try {
|
|
416
|
+
return JSON.parse(text);
|
|
417
|
+
} catch {
|
|
418
|
+
return text;
|
|
419
|
+
}
|
|
420
|
+
} else if (parsedContentType === "text") {
|
|
421
|
+
return new TextDecoder().decode(body);
|
|
422
|
+
}
|
|
423
|
+
} else if (parsedContentType === "json") {
|
|
424
|
+
try {
|
|
425
|
+
return JSON.parse(body);
|
|
426
|
+
} catch {
|
|
427
|
+
return body;
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
return body;
|
|
431
|
+
}
|
|
432
|
+
function serializeResponseBody(response) {
|
|
433
|
+
const { body } = response;
|
|
434
|
+
if (body === void 0 || body === null) {
|
|
435
|
+
return null;
|
|
436
|
+
} else if (body instanceof Uint8Array) {
|
|
437
|
+
return body;
|
|
438
|
+
} else if (body instanceof ArrayBuffer) {
|
|
439
|
+
return new Uint8Array(body);
|
|
440
|
+
} else if (typeof body === "string") {
|
|
441
|
+
return new TextEncoder().encode(body);
|
|
442
|
+
} else if (Array.isArray(body) || isPlainJSONBodyObject(body)) {
|
|
443
|
+
return new TextEncoder().encode(JSON.stringify(body));
|
|
444
|
+
}
|
|
445
|
+
throw new Error(
|
|
446
|
+
"Unsupported mock response body type. Supported types: string, Uint8Array, ArrayBuffer, plain object, array, null, and undefined."
|
|
447
|
+
);
|
|
448
|
+
}
|
|
449
|
+
function inferContentType(body) {
|
|
450
|
+
if (typeof body === "string") {
|
|
451
|
+
return "text";
|
|
452
|
+
} else if (body instanceof Uint8Array || body instanceof ArrayBuffer) {
|
|
453
|
+
return "binary";
|
|
454
|
+
} else {
|
|
455
|
+
return "json";
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
function hasHeader(headers, name) {
|
|
459
|
+
return Object.keys(headers).some(
|
|
460
|
+
(k) => k.toLowerCase() === name.toLowerCase()
|
|
461
|
+
);
|
|
462
|
+
}
|
|
463
|
+
var InternalMockAbortError = class extends Error {
|
|
464
|
+
constructor() {
|
|
465
|
+
super("The operation was aborted.");
|
|
466
|
+
this.name = "AbortError";
|
|
467
|
+
}
|
|
468
|
+
};
|
|
469
|
+
function isInternalAbortError(error) {
|
|
470
|
+
return error instanceof InternalMockAbortError;
|
|
471
|
+
}
|
|
472
|
+
function throwAbortError() {
|
|
473
|
+
const err = new Error("The operation was aborted.");
|
|
474
|
+
err.name = "AbortError";
|
|
475
|
+
throw err;
|
|
476
|
+
}
|
|
477
|
+
function shouldOmitResponseBody(method, status) {
|
|
478
|
+
return method === "HEAD" || status === 204 || status === 304;
|
|
479
|
+
}
|
|
480
|
+
function awaitAbortable(value, signal) {
|
|
481
|
+
if (!signal) {
|
|
482
|
+
return Promise.resolve(value);
|
|
483
|
+
}
|
|
484
|
+
if (signal.aborted) {
|
|
485
|
+
throw new InternalMockAbortError();
|
|
486
|
+
}
|
|
487
|
+
return new Promise((resolve, reject) => {
|
|
488
|
+
const onAbort = () => {
|
|
489
|
+
signal.removeEventListener("abort", onAbort);
|
|
490
|
+
reject(new InternalMockAbortError());
|
|
491
|
+
};
|
|
492
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
493
|
+
Promise.resolve(value).then(
|
|
494
|
+
(result) => {
|
|
495
|
+
signal.removeEventListener("abort", onAbort);
|
|
496
|
+
resolve(result);
|
|
497
|
+
},
|
|
498
|
+
(error) => {
|
|
499
|
+
signal.removeEventListener("abort", onAbort);
|
|
500
|
+
reject(error instanceof Error ? error : new Error(String(error)));
|
|
501
|
+
}
|
|
502
|
+
);
|
|
503
|
+
});
|
|
504
|
+
}
|
|
505
|
+
function sleepAbortable(ms, signal) {
|
|
506
|
+
if (signal.aborted) {
|
|
507
|
+
throw new InternalMockAbortError();
|
|
508
|
+
}
|
|
509
|
+
return new Promise((resolve, reject) => {
|
|
510
|
+
const onAbort = () => {
|
|
511
|
+
clearTimeout(id);
|
|
512
|
+
reject(new InternalMockAbortError());
|
|
513
|
+
};
|
|
514
|
+
const id = setTimeout(() => {
|
|
515
|
+
signal.removeEventListener("abort", onAbort);
|
|
516
|
+
resolve();
|
|
517
|
+
}, ms);
|
|
518
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
519
|
+
});
|
|
520
|
+
}
|
|
521
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
522
|
+
0 && (module.exports = {
|
|
523
|
+
MockAdapter
|
|
524
|
+
});
|
|
525
|
+
//# sourceMappingURL=index.cjs.map
|