@sgedda/mockifyer-fetch 1.8.46 → 1.8.71
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.js +2 -0
- package/dist/index.js +132 -40
- package/dist/metro-sync-middleware.d.ts +13 -2
- package/dist/metro-sync-middleware.js +921 -153
- package/dist/utils/fetch-response-body.d.ts +9 -0
- package/dist/utils/fetch-response-body.js +35 -0
- package/package.json +2 -2
- package/src/clients/fetch-client.ts +4 -0
- package/src/index.ts +177 -45
- package/src/metro-sync-middleware.ts +1375 -253
- package/src/utils/fetch-response-body.ts +33 -0
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Fetch "null body status" codes — Response body must be null.
|
|
3
|
+
* Passing '' / JSON (including JSON.stringify(null) → "null") throws in undici:
|
|
4
|
+
* `TypeError: Response constructor: Invalid response status code 204`
|
|
5
|
+
*
|
|
6
|
+
* @see https://fetch.spec.whatwg.org/#null-body-status
|
|
7
|
+
*/
|
|
8
|
+
const FETCH_NULL_BODY_STATUSES = new Set([101, 103, 204, 205, 304]);
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Whether `status` forbids a Response body under the Fetch spec.
|
|
12
|
+
*/
|
|
13
|
+
export function isFetchNullBodyStatus(status: number): boolean {
|
|
14
|
+
return FETCH_NULL_BODY_STATUSES.has(status);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Body argument for `new Response(body, { status })`.
|
|
19
|
+
* Returns `null` for null-body statuses so Node/undici does not throw.
|
|
20
|
+
*/
|
|
21
|
+
export function bodyInitForFetchResponse(status: number, data: unknown): BodyInit | null {
|
|
22
|
+
if (isFetchNullBodyStatus(status)) {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
if (typeof data === 'string') {
|
|
26
|
+
return data;
|
|
27
|
+
}
|
|
28
|
+
// JSON.stringify(undefined) is undefined — Response treats nullish as empty body
|
|
29
|
+
if (data === undefined) {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
return JSON.stringify(data);
|
|
33
|
+
}
|