@sgedda/mockifyer-fetch 1.8.46 → 1.8.47
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.js
CHANGED
|
@@ -38,6 +38,7 @@ const mockifyer_core_2 = require("@sgedda/mockifyer-core");
|
|
|
38
38
|
const core_proxy_1 = require("./core-proxy");
|
|
39
39
|
const proxy_upstream_tls_insecure_1 = require("@sgedda/mockifyer-core/utils/proxy-upstream-tls-insecure");
|
|
40
40
|
const fetch_client_1 = require("./clients/fetch-client");
|
|
41
|
+
const fetch_response_body_1 = require("./utils/fetch-response-body");
|
|
41
42
|
class MockifyerClass {
|
|
42
43
|
/** Best-effort dashboard network log (skipped when traffic goes through `proxy.baseUrl`). */
|
|
43
44
|
logNetworkEvent(partial, correlation) {
|
|
@@ -1374,8 +1375,7 @@ function setupMockifyer(config) {
|
|
|
1374
1375
|
}
|
|
1375
1376
|
});
|
|
1376
1377
|
}
|
|
1377
|
-
|
|
1378
|
-
return new Response(responseBody, {
|
|
1378
|
+
return new Response((0, fetch_response_body_1.bodyInitForFetchResponse)(response.status, response.data), {
|
|
1379
1379
|
status: response.status,
|
|
1380
1380
|
statusText: response.statusText || '',
|
|
1381
1381
|
headers: responseHeaders
|
|
@@ -1387,11 +1387,9 @@ function setupMockifyer(config) {
|
|
|
1387
1387
|
Object.entries(error.response.headers || {}).forEach(([key, value]) => {
|
|
1388
1388
|
responseHeaders.set(key, String(value));
|
|
1389
1389
|
});
|
|
1390
|
-
const
|
|
1391
|
-
|
|
1392
|
-
:
|
|
1393
|
-
return new Response(errorBody, {
|
|
1394
|
-
status: error.response.status,
|
|
1390
|
+
const errorStatus = error.response.status;
|
|
1391
|
+
return new Response((0, fetch_response_body_1.bodyInitForFetchResponse)(errorStatus, error.response.data), {
|
|
1392
|
+
status: errorStatus,
|
|
1395
1393
|
statusText: error.response.statusText || '',
|
|
1396
1394
|
headers: responseHeaders
|
|
1397
1395
|
});
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether `status` forbids a Response body under the Fetch spec.
|
|
3
|
+
*/
|
|
4
|
+
export declare function isFetchNullBodyStatus(status: number): boolean;
|
|
5
|
+
/**
|
|
6
|
+
* Body argument for `new Response(body, { status })`.
|
|
7
|
+
* Returns `null` for null-body statuses so Node/undici does not throw.
|
|
8
|
+
*/
|
|
9
|
+
export declare function bodyInitForFetchResponse(status: number, data: unknown): BodyInit | null;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isFetchNullBodyStatus = isFetchNullBodyStatus;
|
|
4
|
+
exports.bodyInitForFetchResponse = bodyInitForFetchResponse;
|
|
5
|
+
/**
|
|
6
|
+
* Fetch "null body status" codes — Response body must be null.
|
|
7
|
+
* Passing '' / JSON (including JSON.stringify(null) → "null") throws in undici:
|
|
8
|
+
* `TypeError: Response constructor: Invalid response status code 204`
|
|
9
|
+
*
|
|
10
|
+
* @see https://fetch.spec.whatwg.org/#null-body-status
|
|
11
|
+
*/
|
|
12
|
+
const FETCH_NULL_BODY_STATUSES = new Set([101, 103, 204, 205, 304]);
|
|
13
|
+
/**
|
|
14
|
+
* Whether `status` forbids a Response body under the Fetch spec.
|
|
15
|
+
*/
|
|
16
|
+
function isFetchNullBodyStatus(status) {
|
|
17
|
+
return FETCH_NULL_BODY_STATUSES.has(status);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Body argument for `new Response(body, { status })`.
|
|
21
|
+
* Returns `null` for null-body statuses so Node/undici does not throw.
|
|
22
|
+
*/
|
|
23
|
+
function bodyInitForFetchResponse(status, data) {
|
|
24
|
+
if (isFetchNullBodyStatus(status)) {
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
if (typeof data === 'string') {
|
|
28
|
+
return data;
|
|
29
|
+
}
|
|
30
|
+
// JSON.stringify(undefined) is undefined — Response treats nullish as empty body
|
|
31
|
+
if (data === undefined) {
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
return JSON.stringify(data);
|
|
35
|
+
}
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -100,6 +100,7 @@ import {
|
|
|
100
100
|
import { resolveProxyUpstreamTlsInsecure } from '@sgedda/mockifyer-core/utils/proxy-upstream-tls-insecure';
|
|
101
101
|
|
|
102
102
|
import { FetchHTTPClient } from './clients/fetch-client';
|
|
103
|
+
import { bodyInitForFetchResponse } from './utils/fetch-response-body';
|
|
103
104
|
|
|
104
105
|
class MockifyerClass {
|
|
105
106
|
private config: MockifyerConfig;
|
|
@@ -1719,9 +1720,7 @@ export function setupMockifyer(config: MockifyerConfig): MockifyerInstance {
|
|
|
1719
1720
|
});
|
|
1720
1721
|
}
|
|
1721
1722
|
|
|
1722
|
-
|
|
1723
|
-
|
|
1724
|
-
return new Response(responseBody, {
|
|
1723
|
+
return new Response(bodyInitForFetchResponse(response.status, response.data), {
|
|
1725
1724
|
status: response.status,
|
|
1726
1725
|
statusText: response.statusText || '',
|
|
1727
1726
|
headers: responseHeaders
|
|
@@ -1732,13 +1731,10 @@ export function setupMockifyer(config: MockifyerConfig): MockifyerInstance {
|
|
|
1732
1731
|
Object.entries(error.response.headers || {}).forEach(([key, value]) => {
|
|
1733
1732
|
responseHeaders.set(key, String(value));
|
|
1734
1733
|
});
|
|
1735
|
-
|
|
1736
|
-
const
|
|
1737
|
-
|
|
1738
|
-
:
|
|
1739
|
-
|
|
1740
|
-
return new Response(errorBody, {
|
|
1741
|
-
status: error.response.status,
|
|
1734
|
+
|
|
1735
|
+
const errorStatus = error.response.status;
|
|
1736
|
+
return new Response(bodyInitForFetchResponse(errorStatus, error.response.data), {
|
|
1737
|
+
status: errorStatus,
|
|
1742
1738
|
statusText: error.response.statusText || '',
|
|
1743
1739
|
headers: responseHeaders
|
|
1744
1740
|
});
|
|
@@ -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
|
+
}
|