@sgedda/mockifyer-fetch 1.8.100 → 1.8.104
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/clients/fetch-client.d.ts +4 -0
- package/dist/clients/fetch-client.js +50 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.js +161 -41
- package/dist/metro-sync-middleware.d.ts +10 -0
- package/dist/metro-sync-middleware.js +163 -23
- package/dist/react-native.d.ts +4 -0
- package/dist/utils/resolve-patched-fetch-request.d.ts +24 -0
- package/dist/utils/resolve-patched-fetch-request.js +91 -0
- package/package.json +2 -2
- package/src/clients/fetch-client.ts +66 -1
- package/src/index.ts +196 -41
- package/src/metro-sync-middleware.ts +199 -23
- package/src/react-native.ts +4 -0
- package/src/utils/resolve-patched-fetch-request.ts +105 -0
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolve `fetch(input, init)` into the HTTPClient shape used by the global fetch patch.
|
|
3
|
+
*
|
|
4
|
+
* Native fetch reads method/headers/body from a `Request` argument when `init` omits them.
|
|
5
|
+
* The patch historically only inspected `init`, so `fetch(new Request(url, { method, headers, body }))`
|
|
6
|
+
* was treated as GET with no headers or body.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export interface ResolvedPatchedFetchRequest {
|
|
10
|
+
url: string;
|
|
11
|
+
method: string;
|
|
12
|
+
headers: Record<string, string>;
|
|
13
|
+
body: unknown;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function isRequest(input: unknown): input is Request {
|
|
17
|
+
return typeof Request !== 'undefined' && input instanceof Request;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function headersToRecord(headers: HeadersInit | undefined): Record<string, string> {
|
|
21
|
+
const out: Record<string, string> = {};
|
|
22
|
+
if (!headers) {
|
|
23
|
+
return out;
|
|
24
|
+
}
|
|
25
|
+
if (headers instanceof Headers) {
|
|
26
|
+
headers.forEach((value, key) => {
|
|
27
|
+
out[key] = value;
|
|
28
|
+
});
|
|
29
|
+
return out;
|
|
30
|
+
}
|
|
31
|
+
if (Array.isArray(headers)) {
|
|
32
|
+
for (const [key, value] of headers) {
|
|
33
|
+
out[key] = value;
|
|
34
|
+
}
|
|
35
|
+
return out;
|
|
36
|
+
}
|
|
37
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
38
|
+
if (value !== undefined && value !== null) {
|
|
39
|
+
out[key] = String(value);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return out;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Parse JSON text when it looks like an object/array; keep invalid JSON as the original string
|
|
47
|
+
* so the request still proceeds (native fetch does not throw on unparsable bodies).
|
|
48
|
+
*/
|
|
49
|
+
export function decodeFetchBodyText(text: string): unknown {
|
|
50
|
+
const start = text.trimStart();
|
|
51
|
+
if (start.startsWith('{') || start.startsWith('[')) {
|
|
52
|
+
try {
|
|
53
|
+
return JSON.parse(text);
|
|
54
|
+
} catch {
|
|
55
|
+
return text;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return text;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function decodeFetchBodyInit(body: BodyInit): unknown {
|
|
62
|
+
if (typeof body === 'string') {
|
|
63
|
+
return decodeFetchBodyText(body);
|
|
64
|
+
}
|
|
65
|
+
return body;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function urlFromFetchInput(input: RequestInfo | URL): string {
|
|
69
|
+
if (typeof input === 'string') {
|
|
70
|
+
return input;
|
|
71
|
+
}
|
|
72
|
+
if (input instanceof URL) {
|
|
73
|
+
return input.toString();
|
|
74
|
+
}
|
|
75
|
+
return input.url;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Method, headers, and body as native `fetch(input, init)` would use them.
|
|
80
|
+
* `init` fields override the `Request` argument (Fetch spec). Relative URL strings
|
|
81
|
+
* are left unchanged — `new Request('/path')` throws in Node without a base URL.
|
|
82
|
+
*/
|
|
83
|
+
export async function resolvePatchedFetchRequest(
|
|
84
|
+
input: RequestInfo | URL,
|
|
85
|
+
init?: RequestInit
|
|
86
|
+
): Promise<ResolvedPatchedFetchRequest> {
|
|
87
|
+
const url = urlFromFetchInput(input);
|
|
88
|
+
const method = String(init?.method || (isRequest(input) ? input.method : 'GET') || 'GET');
|
|
89
|
+
|
|
90
|
+
const headers = headersToRecord(
|
|
91
|
+
init?.headers !== undefined ? init.headers : isRequest(input) ? input.headers : undefined
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
let body: unknown;
|
|
95
|
+
if (init !== undefined && init.body !== undefined && init.body !== null) {
|
|
96
|
+
body = decodeFetchBodyInit(init.body);
|
|
97
|
+
} else if (isRequest(input) && method.toUpperCase() !== 'GET' && method.toUpperCase() !== 'HEAD') {
|
|
98
|
+
const text = await input.clone().text();
|
|
99
|
+
body = text ? decodeFetchBodyText(text) : undefined;
|
|
100
|
+
} else {
|
|
101
|
+
body = undefined;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return { url, method, headers, body };
|
|
105
|
+
}
|