msw 2.12.11 → 2.12.13
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/config/scripts/postinstall.js +2 -2
- package/lib/core/{HttpResponse-Cw4ELwIN.d.mts → HttpResponse-Be4eT3x6.d.mts} +8 -2
- package/lib/core/{HttpResponse-CVs3ngx3.d.ts → HttpResponse-Dj6ibgFJ.d.ts} +8 -2
- package/lib/core/HttpResponse.d.mts +1 -1
- package/lib/core/HttpResponse.d.ts +1 -1
- package/lib/core/HttpResponse.js +53 -11
- package/lib/core/HttpResponse.js.map +1 -1
- package/lib/core/HttpResponse.mjs +53 -11
- package/lib/core/HttpResponse.mjs.map +1 -1
- package/lib/core/SetupApi.d.mts +1 -1
- package/lib/core/SetupApi.d.ts +1 -1
- package/lib/core/getResponse.d.mts +1 -1
- package/lib/core/getResponse.d.ts +1 -1
- package/lib/core/graphql.d.mts +1 -1
- package/lib/core/graphql.d.ts +1 -1
- package/lib/core/handlers/GraphQLHandler.d.mts +1 -1
- package/lib/core/handlers/GraphQLHandler.d.ts +1 -1
- package/lib/core/handlers/GraphQLHandler.js +29 -1
- package/lib/core/handlers/GraphQLHandler.js.map +1 -1
- package/lib/core/handlers/GraphQLHandler.mjs +29 -1
- package/lib/core/handlers/GraphQLHandler.mjs.map +1 -1
- package/lib/core/handlers/HttpHandler.d.mts +1 -1
- package/lib/core/handlers/HttpHandler.d.ts +1 -1
- package/lib/core/handlers/RequestHandler.d.mts +1 -1
- package/lib/core/handlers/RequestHandler.d.ts +1 -1
- package/lib/core/handlers/RequestHandler.js.map +1 -1
- package/lib/core/handlers/RequestHandler.mjs.map +1 -1
- package/lib/core/http.d.mts +1 -1
- package/lib/core/http.d.ts +1 -1
- package/lib/core/index.d.mts +3 -3
- package/lib/core/index.d.ts +3 -3
- package/lib/core/index.js +6 -4
- package/lib/core/index.js.map +1 -1
- package/lib/core/index.mjs +8 -3
- package/lib/core/index.mjs.map +1 -1
- package/lib/core/passthrough.d.mts +1 -1
- package/lib/core/passthrough.d.ts +1 -1
- package/lib/core/sse.d.mts +1 -1
- package/lib/core/sse.d.ts +1 -1
- package/lib/core/sse.js +1 -1
- package/lib/core/sse.js.map +1 -1
- package/lib/core/sse.mjs +1 -1
- package/lib/core/sse.mjs.map +1 -1
- package/lib/core/utils/HttpResponse/decorators.d.mts +1 -1
- package/lib/core/utils/HttpResponse/decorators.d.ts +1 -1
- package/lib/core/utils/executeHandlers.d.mts +1 -1
- package/lib/core/utils/executeHandlers.d.ts +1 -1
- package/lib/core/utils/handleRequest.d.mts +1 -1
- package/lib/core/utils/handleRequest.d.ts +1 -1
- package/lib/core/utils/internal/isHandlerKind.d.mts +1 -1
- package/lib/core/utils/internal/isHandlerKind.d.ts +1 -1
- package/lib/core/utils/internal/parseGraphQLRequest.d.mts +1 -1
- package/lib/core/utils/internal/parseGraphQLRequest.d.ts +1 -1
- package/lib/core/utils/internal/parseGraphQLRequest.js +1 -1
- package/lib/core/utils/internal/parseGraphQLRequest.js.map +1 -1
- package/lib/core/utils/internal/parseGraphQLRequest.mjs +1 -1
- package/lib/core/utils/internal/parseGraphQLRequest.mjs.map +1 -1
- package/lib/core/utils/internal/parseMultipartData.d.mts +1 -1
- package/lib/core/utils/internal/parseMultipartData.d.ts +1 -1
- package/lib/core/utils/internal/parseMultipartData.js +3 -1
- package/lib/core/utils/internal/parseMultipartData.js.map +1 -1
- package/lib/core/utils/internal/parseMultipartData.mjs +3 -1
- package/lib/core/utils/internal/parseMultipartData.mjs.map +1 -1
- package/lib/core/utils/internal/requestHandlerUtils.d.mts +1 -1
- package/lib/core/utils/internal/requestHandlerUtils.d.ts +1 -1
- package/lib/core/utils/request/getAllAcceptedMimeTypes.d.mts +14 -0
- package/lib/core/utils/request/getAllAcceptedMimeTypes.d.ts +14 -0
- package/lib/core/utils/request/getAllAcceptedMimeTypes.js +61 -0
- package/lib/core/utils/request/getAllAcceptedMimeTypes.js.map +1 -0
- package/lib/core/utils/request/getAllAcceptedMimeTypes.mjs +41 -0
- package/lib/core/utils/request/getAllAcceptedMimeTypes.mjs.map +1 -0
- package/lib/core/ws/WebSocketIndexedDBClientStore.js +1 -1
- package/lib/core/ws/WebSocketIndexedDBClientStore.js.map +1 -1
- package/lib/core/ws/WebSocketIndexedDBClientStore.mjs +1 -1
- package/lib/core/ws/WebSocketIndexedDBClientStore.mjs.map +1 -1
- package/lib/core/ws/handleWebSocketEvent.d.mts +1 -1
- package/lib/core/ws/handleWebSocketEvent.d.ts +1 -1
- package/lib/iife/index.js +262 -159
- package/lib/iife/index.js.map +1 -1
- package/lib/mockServiceWorker.js +1 -1
- package/package.json +2 -2
- package/src/core/HttpResponse.test.ts +25 -9
- package/src/core/HttpResponse.ts +62 -10
- package/src/core/handlers/GraphQLHandler.ts +54 -2
- package/src/core/handlers/RequestHandler.ts +1 -1
- package/src/core/index.ts +8 -3
- package/src/core/sse.ts +1 -1
- package/src/core/utils/internal/parseGraphQLRequest.ts +1 -1
- package/src/core/utils/internal/parseMultipartData.ts +3 -1
- package/src/core/utils/request/getAllAcceptedMimeTypes.test.ts +86 -0
- package/src/core/utils/request/getAllAcceptedMimeTypes.ts +70 -0
- package/src/core/ws/WebSocketIndexedDBClientStore.ts +1 -2
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import fs from 'node:fs'
|
|
2
2
|
import path from 'node:path'
|
|
3
|
-
import {
|
|
3
|
+
import { execFileSync } from 'node:child_process'
|
|
4
4
|
|
|
5
5
|
// When executing the "postinstall" script, the "process.cwd" equals
|
|
6
6
|
// the package directory, not the parent project where the package is installed.
|
|
@@ -23,7 +23,7 @@ function postInstall() {
|
|
|
23
23
|
* @note Call the "init" command directly. It will now copy the worker script
|
|
24
24
|
* to all saved paths in "msw.workerDirectory"
|
|
25
25
|
*/
|
|
26
|
-
|
|
26
|
+
execFileSync(process.execPath, [cliExecutable, 'init'], {
|
|
27
27
|
cwd: parentPackageCwd,
|
|
28
28
|
})
|
|
29
29
|
} catch (error) {
|
|
@@ -131,6 +131,11 @@ declare class GraphQLHandler extends RequestHandler<GraphQLHandlerInfo, GraphQLR
|
|
|
131
131
|
request: Request;
|
|
132
132
|
parsedResult: GraphQLRequestParsedResult;
|
|
133
133
|
}): Promise<boolean>;
|
|
134
|
+
run(args: {
|
|
135
|
+
request: StrictRequest<any>;
|
|
136
|
+
requestId: string;
|
|
137
|
+
resolutionContext?: ResponseResolutionContext;
|
|
138
|
+
}): Promise<RequestHandlerExecutionResult<GraphQLRequestParsedResult> | null>;
|
|
134
139
|
private matchOperationName;
|
|
135
140
|
protected extendResolverArgs(args: {
|
|
136
141
|
request: Request;
|
|
@@ -174,7 +179,7 @@ interface RequestHandlerArgs<HandlerInfo, HandlerOptions extends RequestHandlerO
|
|
|
174
179
|
interface RequestHandlerOptions {
|
|
175
180
|
once?: boolean;
|
|
176
181
|
}
|
|
177
|
-
interface RequestHandlerExecutionResult<ParsedResult extends
|
|
182
|
+
interface RequestHandlerExecutionResult<ParsedResult extends object | undefined> {
|
|
178
183
|
handler: RequestHandler;
|
|
179
184
|
parsedResult?: ParsedResult;
|
|
180
185
|
request: Request;
|
|
@@ -265,6 +270,7 @@ interface StrictRequest<BodyType extends JsonBodyType> extends Request {
|
|
|
265
270
|
* @deprecated Please use {@link HttpResponse} instead.
|
|
266
271
|
*/
|
|
267
272
|
type StrictResponse<BodyType extends DefaultBodyType> = HttpResponse<BodyType>;
|
|
273
|
+
declare const kDefaultContentType: unique symbol;
|
|
268
274
|
/**
|
|
269
275
|
* A drop-in replacement for the standard `Response` class
|
|
270
276
|
* to allow additional features, like mocking the response `Set-Cookie` header.
|
|
@@ -329,4 +335,4 @@ declare class HttpResponse<BodyType extends DefaultBodyType> extends FetchRespon
|
|
|
329
335
|
static formData(body?: FormData, init?: HttpResponseInit): HttpResponse<FormData>;
|
|
330
336
|
}
|
|
331
337
|
|
|
332
|
-
export { type AsyncResponseResolverReturnType as A,
|
|
338
|
+
export { type AsyncResponseResolverReturnType as A, type GraphQLHandlerNameSelector as B, type GraphQLHandlerInfo as C, type DefaultBodyType as D, type GraphQLRequestParsedResult as E, type GraphQLCustomPredicateResult as F, GraphQLHandler as G, HttpResponse as H, isDocumentNode as I, type JsonBodyType as J, type RequestHandlerInternalInfo as K, type RequestHandlerArgs as L, type MaybeAsyncResponseResolverReturnType as M, type RequestHandlerExecutionResult as N, bodyType as O, type ParsedGraphQLRequest as P, type DefaultUnsafeFetchResponse as Q, RequestHandler as R, type StrictRequest as S, kDefaultContentType as T, type ResponseResolver as a, type ResponseResolverReturnType as b, type RequestHandlerOptions as c, type DefaultRequestMultipartBody as d, type ResponseResolverInfo as e, type GraphQLQuery as f, type GraphQLVariables as g, type GraphQLRequestBody as h, type GraphQLResponseBody as i, type GraphQLJsonRequestBody as j, type GraphQLOperationType as k, type GraphQLCustomPredicate as l, type ResponseResolutionContext as m, type HttpResponseInit as n, type StrictResponse as o, type GraphQLPredicate as p, type GraphQLResolverExtras as q, type RequestHandlerDefaultInfo as r, type HandlersExecutionResult as s, executeHandlers as t, type ParsedGraphQLQuery as u, parseDocumentNode as v, type GraphQLParsedOperationsMap as w, type GraphQLMultipartRequestBody as x, parseGraphQLRequest as y, type DocumentTypeDecoration as z };
|
|
@@ -131,6 +131,11 @@ declare class GraphQLHandler extends RequestHandler<GraphQLHandlerInfo, GraphQLR
|
|
|
131
131
|
request: Request;
|
|
132
132
|
parsedResult: GraphQLRequestParsedResult;
|
|
133
133
|
}): Promise<boolean>;
|
|
134
|
+
run(args: {
|
|
135
|
+
request: StrictRequest<any>;
|
|
136
|
+
requestId: string;
|
|
137
|
+
resolutionContext?: ResponseResolutionContext;
|
|
138
|
+
}): Promise<RequestHandlerExecutionResult<GraphQLRequestParsedResult> | null>;
|
|
134
139
|
private matchOperationName;
|
|
135
140
|
protected extendResolverArgs(args: {
|
|
136
141
|
request: Request;
|
|
@@ -174,7 +179,7 @@ interface RequestHandlerArgs<HandlerInfo, HandlerOptions extends RequestHandlerO
|
|
|
174
179
|
interface RequestHandlerOptions {
|
|
175
180
|
once?: boolean;
|
|
176
181
|
}
|
|
177
|
-
interface RequestHandlerExecutionResult<ParsedResult extends
|
|
182
|
+
interface RequestHandlerExecutionResult<ParsedResult extends object | undefined> {
|
|
178
183
|
handler: RequestHandler;
|
|
179
184
|
parsedResult?: ParsedResult;
|
|
180
185
|
request: Request;
|
|
@@ -265,6 +270,7 @@ interface StrictRequest<BodyType extends JsonBodyType> extends Request {
|
|
|
265
270
|
* @deprecated Please use {@link HttpResponse} instead.
|
|
266
271
|
*/
|
|
267
272
|
type StrictResponse<BodyType extends DefaultBodyType> = HttpResponse<BodyType>;
|
|
273
|
+
declare const kDefaultContentType: unique symbol;
|
|
268
274
|
/**
|
|
269
275
|
* A drop-in replacement for the standard `Response` class
|
|
270
276
|
* to allow additional features, like mocking the response `Set-Cookie` header.
|
|
@@ -329,4 +335,4 @@ declare class HttpResponse<BodyType extends DefaultBodyType> extends FetchRespon
|
|
|
329
335
|
static formData(body?: FormData, init?: HttpResponseInit): HttpResponse<FormData>;
|
|
330
336
|
}
|
|
331
337
|
|
|
332
|
-
export { type AsyncResponseResolverReturnType as A,
|
|
338
|
+
export { type AsyncResponseResolverReturnType as A, type GraphQLHandlerNameSelector as B, type GraphQLHandlerInfo as C, type DefaultBodyType as D, type GraphQLRequestParsedResult as E, type GraphQLCustomPredicateResult as F, GraphQLHandler as G, HttpResponse as H, isDocumentNode as I, type JsonBodyType as J, type RequestHandlerInternalInfo as K, type RequestHandlerArgs as L, type MaybeAsyncResponseResolverReturnType as M, type RequestHandlerExecutionResult as N, bodyType as O, type ParsedGraphQLRequest as P, type DefaultUnsafeFetchResponse as Q, RequestHandler as R, type StrictRequest as S, kDefaultContentType as T, type ResponseResolver as a, type ResponseResolverReturnType as b, type RequestHandlerOptions as c, type DefaultRequestMultipartBody as d, type ResponseResolverInfo as e, type GraphQLQuery as f, type GraphQLVariables as g, type GraphQLRequestBody as h, type GraphQLResponseBody as i, type GraphQLJsonRequestBody as j, type GraphQLOperationType as k, type GraphQLCustomPredicate as l, type ResponseResolutionContext as m, type HttpResponseInit as n, type StrictResponse as o, type GraphQLPredicate as p, type GraphQLResolverExtras as q, type RequestHandlerDefaultInfo as r, type HandlersExecutionResult as s, executeHandlers as t, type ParsedGraphQLQuery as u, parseDocumentNode as v, type GraphQLParsedOperationsMap as w, type GraphQLMultipartRequestBody as x, parseGraphQLRequest as y, type DocumentTypeDecoration as z };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import '@mswjs/interceptors';
|
|
2
|
-
export {
|
|
2
|
+
export { Q as DefaultUnsafeFetchResponse, H as HttpResponse, n as HttpResponseInit, S as StrictRequest, o as StrictResponse, O as bodyType, T as kDefaultContentType } from './HttpResponse-Be4eT3x6.mjs';
|
|
3
3
|
import './typeUtils.mjs';
|
|
4
4
|
import './utils/internal/isIterable.mjs';
|
|
5
5
|
import 'graphql';
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import '@mswjs/interceptors';
|
|
2
|
-
export {
|
|
2
|
+
export { Q as DefaultUnsafeFetchResponse, H as HttpResponse, n as HttpResponseInit, S as StrictRequest, o as StrictResponse, O as bodyType, T as kDefaultContentType } from './HttpResponse-Dj6ibgFJ.js';
|
|
3
3
|
import './typeUtils.js';
|
|
4
4
|
import './utils/internal/isIterable.js';
|
|
5
5
|
import 'graphql';
|
package/lib/core/HttpResponse.js
CHANGED
|
@@ -19,12 +19,14 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
19
19
|
var HttpResponse_exports = {};
|
|
20
20
|
__export(HttpResponse_exports, {
|
|
21
21
|
HttpResponse: () => HttpResponse,
|
|
22
|
-
bodyType: () => bodyType
|
|
22
|
+
bodyType: () => bodyType,
|
|
23
|
+
kDefaultContentType: () => kDefaultContentType
|
|
23
24
|
});
|
|
24
25
|
module.exports = __toCommonJS(HttpResponse_exports);
|
|
25
26
|
var import_interceptors = require("@mswjs/interceptors");
|
|
26
27
|
var import_decorators = require("./utils/HttpResponse/decorators");
|
|
27
28
|
const bodyType = Symbol("bodyType");
|
|
29
|
+
const kDefaultContentType = Symbol.for("kDefaultContentType");
|
|
28
30
|
class HttpResponse extends import_interceptors.FetchResponse {
|
|
29
31
|
[bodyType] = null;
|
|
30
32
|
constructor(body, init) {
|
|
@@ -43,7 +45,8 @@ class HttpResponse extends import_interceptors.FetchResponse {
|
|
|
43
45
|
*/
|
|
44
46
|
static text(body, init) {
|
|
45
47
|
const responseInit = (0, import_decorators.normalizeResponseInit)(init);
|
|
46
|
-
|
|
48
|
+
const hasExplicitContentType = responseInit.headers.has("Content-Type");
|
|
49
|
+
if (!hasExplicitContentType) {
|
|
47
50
|
responseInit.headers.set("Content-Type", "text/plain");
|
|
48
51
|
}
|
|
49
52
|
if (!responseInit.headers.has("Content-Length")) {
|
|
@@ -52,7 +55,14 @@ class HttpResponse extends import_interceptors.FetchResponse {
|
|
|
52
55
|
body ? new Blob([body]).size.toString() : "0"
|
|
53
56
|
);
|
|
54
57
|
}
|
|
55
|
-
|
|
58
|
+
const response = new HttpResponse(body, responseInit);
|
|
59
|
+
if (!hasExplicitContentType) {
|
|
60
|
+
Object.defineProperty(response, kDefaultContentType, {
|
|
61
|
+
value: true,
|
|
62
|
+
enumerable: false
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
return response;
|
|
56
66
|
}
|
|
57
67
|
/**
|
|
58
68
|
* Create a `Response` with a `Content-Type: "application/json"` body.
|
|
@@ -62,7 +72,8 @@ class HttpResponse extends import_interceptors.FetchResponse {
|
|
|
62
72
|
*/
|
|
63
73
|
static json(body, init) {
|
|
64
74
|
const responseInit = (0, import_decorators.normalizeResponseInit)(init);
|
|
65
|
-
|
|
75
|
+
const hasExplicitContentType = responseInit.headers.has("Content-Type");
|
|
76
|
+
if (!hasExplicitContentType) {
|
|
66
77
|
responseInit.headers.set("Content-Type", "application/json");
|
|
67
78
|
}
|
|
68
79
|
const responseText = JSON.stringify(body);
|
|
@@ -72,7 +83,14 @@ class HttpResponse extends import_interceptors.FetchResponse {
|
|
|
72
83
|
responseText ? new Blob([responseText]).size.toString() : "0"
|
|
73
84
|
);
|
|
74
85
|
}
|
|
75
|
-
|
|
86
|
+
const response = new HttpResponse(responseText, responseInit);
|
|
87
|
+
if (!hasExplicitContentType) {
|
|
88
|
+
Object.defineProperty(response, kDefaultContentType, {
|
|
89
|
+
value: true,
|
|
90
|
+
enumerable: false
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
return response;
|
|
76
94
|
}
|
|
77
95
|
/**
|
|
78
96
|
* Create a `Response` with a `Content-Type: "application/xml"` body.
|
|
@@ -82,10 +100,18 @@ class HttpResponse extends import_interceptors.FetchResponse {
|
|
|
82
100
|
*/
|
|
83
101
|
static xml(body, init) {
|
|
84
102
|
const responseInit = (0, import_decorators.normalizeResponseInit)(init);
|
|
85
|
-
|
|
103
|
+
const hasExplicitContentType = responseInit.headers.has("Content-Type");
|
|
104
|
+
if (!hasExplicitContentType) {
|
|
86
105
|
responseInit.headers.set("Content-Type", "text/xml");
|
|
87
106
|
}
|
|
88
|
-
|
|
107
|
+
const response = new HttpResponse(body, responseInit);
|
|
108
|
+
if (!hasExplicitContentType) {
|
|
109
|
+
Object.defineProperty(response, kDefaultContentType, {
|
|
110
|
+
value: true,
|
|
111
|
+
enumerable: false
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
return response;
|
|
89
115
|
}
|
|
90
116
|
/**
|
|
91
117
|
* Create a `Response` with a `Content-Type: "text/html"` body.
|
|
@@ -95,10 +121,18 @@ class HttpResponse extends import_interceptors.FetchResponse {
|
|
|
95
121
|
*/
|
|
96
122
|
static html(body, init) {
|
|
97
123
|
const responseInit = (0, import_decorators.normalizeResponseInit)(init);
|
|
98
|
-
|
|
124
|
+
const hasExplicitContentType = responseInit.headers.has("Content-Type");
|
|
125
|
+
if (!hasExplicitContentType) {
|
|
99
126
|
responseInit.headers.set("Content-Type", "text/html");
|
|
100
127
|
}
|
|
101
|
-
|
|
128
|
+
const response = new HttpResponse(body, responseInit);
|
|
129
|
+
if (!hasExplicitContentType) {
|
|
130
|
+
Object.defineProperty(response, kDefaultContentType, {
|
|
131
|
+
value: true,
|
|
132
|
+
enumerable: false
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
return response;
|
|
102
136
|
}
|
|
103
137
|
/**
|
|
104
138
|
* Create a `Response` with an `ArrayBuffer` body.
|
|
@@ -111,13 +145,21 @@ class HttpResponse extends import_interceptors.FetchResponse {
|
|
|
111
145
|
*/
|
|
112
146
|
static arrayBuffer(body, init) {
|
|
113
147
|
const responseInit = (0, import_decorators.normalizeResponseInit)(init);
|
|
114
|
-
|
|
148
|
+
const hasExplicitContentType = responseInit.headers.has("Content-Type");
|
|
149
|
+
if (!hasExplicitContentType) {
|
|
115
150
|
responseInit.headers.set("Content-Type", "application/octet-stream");
|
|
116
151
|
}
|
|
117
152
|
if (body && !responseInit.headers.has("Content-Length")) {
|
|
118
153
|
responseInit.headers.set("Content-Length", body.byteLength.toString());
|
|
119
154
|
}
|
|
120
|
-
|
|
155
|
+
const response = new HttpResponse(body, responseInit);
|
|
156
|
+
if (!hasExplicitContentType) {
|
|
157
|
+
Object.defineProperty(response, kDefaultContentType, {
|
|
158
|
+
value: true,
|
|
159
|
+
enumerable: false
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
return response;
|
|
121
163
|
}
|
|
122
164
|
/**
|
|
123
165
|
* Create a `Response` with a `FormData` body.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/core/HttpResponse.ts"],"sourcesContent":["import { FetchResponse } from '@mswjs/interceptors'\nimport type { DefaultBodyType, JsonBodyType } from './handlers/RequestHandler'\nimport type { NoInfer } from './typeUtils'\nimport {\n decorateResponse,\n normalizeResponseInit,\n} from './utils/HttpResponse/decorators'\n\nexport interface HttpResponseInit extends ResponseInit {\n type?: ResponseType\n}\n\nexport const bodyType: unique symbol = Symbol('bodyType')\nexport type DefaultUnsafeFetchResponse = Response & {\n [bodyType]?: never\n}\n\nexport interface StrictRequest<BodyType extends JsonBodyType> extends Request {\n json(): Promise<BodyType>\n clone(): StrictRequest<BodyType>\n}\n\n/**\n * Opaque `Response` type that supports strict body type.\n *\n * @deprecated Please use {@link HttpResponse} instead.\n */\nexport type StrictResponse<BodyType extends DefaultBodyType> =\n HttpResponse<BodyType>\n\n/**\n * A drop-in replacement for the standard `Response` class\n * to allow additional features, like mocking the response `Set-Cookie` header.\n *\n * @example\n * new HttpResponse('Hello world', { status: 201 })\n * HttpResponse.json({ name: 'John' })\n * HttpResponse.formData(form)\n *\n * @see {@link https://mswjs.io/docs/api/http-response `HttpResponse` API reference}\n */\nexport class HttpResponse<\n BodyType extends DefaultBodyType,\n> extends FetchResponse {\n readonly [bodyType]: BodyType = null as any\n\n constructor(body?: NoInfer<BodyType> | null, init?: HttpResponseInit) {\n const responseInit = normalizeResponseInit(init)\n super(body as BodyInit, responseInit)\n decorateResponse(this, responseInit)\n }\n\n static error(): HttpResponse<any> {\n return super.error() as HttpResponse<any>\n }\n\n /**\n * Create a `Response` with a `Content-Type: \"text/plain\"` body.\n * @example\n * HttpResponse.text('hello world')\n * HttpResponse.text('Error', { status: 500 })\n */\n static text<BodyType extends string>(\n body?: NoInfer<BodyType> | null,\n init?: HttpResponseInit,\n ): HttpResponse<BodyType> {\n const responseInit = normalizeResponseInit(init)\n\n if (!responseInit.headers.has('Content-Type')) {\n responseInit.headers.set('Content-Type', 'text/plain')\n }\n\n // Automatically set the \"Content-Length\" response header\n // for non-empty text responses. This enforces consistency and\n // brings mocked responses closer to production.\n if (!responseInit.headers.has('Content-Length')) {\n responseInit.headers.set(\n 'Content-Length',\n body ? new Blob([body]).size.toString() : '0',\n )\n }\n\n return new HttpResponse(body, responseInit)\n }\n\n /**\n * Create a `Response` with a `Content-Type: \"application/json\"` body.\n * @example\n * HttpResponse.json({ firstName: 'John' })\n * HttpResponse.json({ error: 'Not Authorized' }, { status: 401 })\n */\n static json<BodyType extends JsonBodyType>(\n body?: NoInfer<BodyType> | null | undefined,\n init?: HttpResponseInit,\n ): HttpResponse<BodyType> {\n const responseInit = normalizeResponseInit(init)\n\n if (!responseInit.headers.has('Content-Type')) {\n responseInit.headers.set('Content-Type', 'application/json')\n }\n\n /**\n * @note TypeScript is incorrect here.\n * Stringifying undefined will return undefined.\n */\n const responseText = JSON.stringify(body) as string | undefined\n\n if (!responseInit.headers.has('Content-Length')) {\n responseInit.headers.set(\n 'Content-Length',\n responseText ? new Blob([responseText]).size.toString() : '0',\n )\n }\n\n return new HttpResponse(responseText as BodyType, responseInit)\n }\n\n /**\n * Create a `Response` with a `Content-Type: \"application/xml\"` body.\n * @example\n * HttpResponse.xml(`<user name=\"John\" />`)\n * HttpResponse.xml(`<article id=\"abc-123\" />`, { status: 201 })\n */\n static xml<BodyType extends string>(\n body?: BodyType | null,\n init?: HttpResponseInit,\n ): HttpResponse<BodyType> {\n const responseInit = normalizeResponseInit(init)\n\n if (!responseInit.headers.has('Content-Type')) {\n responseInit.headers.set('Content-Type', 'text/xml')\n }\n\n return new HttpResponse(body, responseInit)\n }\n\n /**\n * Create a `Response` with a `Content-Type: \"text/html\"` body.\n * @example\n * HttpResponse.html(`<p class=\"author\">Jane Doe</p>`)\n * HttpResponse.html(`<main id=\"abc-123\">Main text</main>`, { status: 201 })\n */\n static html<BodyType extends string>(\n body?: BodyType | null,\n init?: HttpResponseInit,\n ): HttpResponse<BodyType> {\n const responseInit = normalizeResponseInit(init)\n\n if (!responseInit.headers.has('Content-Type')) {\n responseInit.headers.set('Content-Type', 'text/html')\n }\n\n return new HttpResponse(body, responseInit)\n }\n\n /**\n * Create a `Response` with an `ArrayBuffer` body.\n * @example\n * const buffer = new ArrayBuffer(3)\n * const view = new Uint8Array(buffer)\n * view.set([1, 2, 3])\n *\n * HttpResponse.arrayBuffer(buffer)\n */\n static arrayBuffer<BodyType extends ArrayBuffer | SharedArrayBuffer>(\n body?: BodyType,\n init?: HttpResponseInit,\n ): HttpResponse<BodyType> {\n const responseInit = normalizeResponseInit(init)\n\n if (!responseInit.headers.has('Content-Type')) {\n responseInit.headers.set('Content-Type', 'application/octet-stream')\n }\n\n if (body && !responseInit.headers.has('Content-Length')) {\n responseInit.headers.set('Content-Length', body.byteLength.toString())\n }\n\n return new HttpResponse(body, responseInit)\n }\n\n /**\n * Create a `Response` with a `FormData` body.\n * @example\n * const data = new FormData()\n * data.set('name', 'Alice')\n *\n * HttpResponse.formData(data)\n */\n static formData(\n body?: FormData,\n init?: HttpResponseInit,\n ): HttpResponse<FormData> {\n return new HttpResponse(body, normalizeResponseInit(init))\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,0BAA8B;AAG9B,wBAGO;AAMA,MAAM,WAA0B,OAAO,UAAU;AA6BjD,MAAM,qBAEH,kCAAc;AAAA,EACtB,CAAU,QAAQ,IAAc;AAAA,EAEhC,YAAY,MAAiC,MAAyB;AACpE,UAAM,mBAAe,yCAAsB,IAAI;AAC/C,UAAM,MAAkB,YAAY;AACpC,4CAAiB,MAAM,YAAY;AAAA,EACrC;AAAA,EAEA,OAAO,QAA2B;AAChC,WAAO,MAAM,MAAM;AAAA,EACrB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,OAAO,KACL,MACA,MACwB;AACxB,UAAM,mBAAe,yCAAsB,IAAI;AAE/C,QAAI,CAAC,aAAa,QAAQ,IAAI,cAAc,GAAG;AAC7C,mBAAa,QAAQ,IAAI,gBAAgB,YAAY;AAAA,IACvD;AAKA,QAAI,CAAC,aAAa,QAAQ,IAAI,gBAAgB,GAAG;AAC/C,mBAAa,QAAQ;AAAA,QACnB;AAAA,QACA,OAAO,IAAI,KAAK,CAAC,IAAI,CAAC,EAAE,KAAK,SAAS,IAAI;AAAA,MAC5C;AAAA,IACF;AAEA,WAAO,IAAI,aAAa,MAAM,YAAY;AAAA,EAC5C;AAAA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Q,CAAC,aAAa,QAAQ,IAAI,gBAAgB,GAAG;AACvD,mBAAa,QAAQ,IAAI,kBAAkB,KAAK,WAAW,SAAS,CAAC;AAAA,IACvE;AAEA,WAAO,IAAI,aAAa,MAAM,YAAY;AAAA,EAC5C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA,OAAO,SACL,MACA,MACwB;AACxB,WAAO,IAAI,aAAa,UAAM,yCAAsB,IAAI,CAAC;AAAA,EAC3D;AACF;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../src/core/HttpResponse.ts"],"sourcesContent":["import { FetchResponse } from '@mswjs/interceptors'\nimport type { DefaultBodyType, JsonBodyType } from './handlers/RequestHandler'\nimport type { NoInfer } from './typeUtils'\nimport {\n decorateResponse,\n normalizeResponseInit,\n} from './utils/HttpResponse/decorators'\n\nexport interface HttpResponseInit extends ResponseInit {\n type?: ResponseType\n}\n\nexport const bodyType: unique symbol = Symbol('bodyType')\nexport type DefaultUnsafeFetchResponse = Response & {\n [bodyType]?: never\n}\n\nexport interface StrictRequest<BodyType extends JsonBodyType> extends Request {\n json(): Promise<BodyType>\n clone(): StrictRequest<BodyType>\n}\n\n/**\n * Opaque `Response` type that supports strict body type.\n *\n * @deprecated Please use {@link HttpResponse} instead.\n */\nexport type StrictResponse<BodyType extends DefaultBodyType> =\n HttpResponse<BodyType>\n\nexport const kDefaultContentType = Symbol.for('kDefaultContentType')\n\n/**\n * A drop-in replacement for the standard `Response` class\n * to allow additional features, like mocking the response `Set-Cookie` header.\n *\n * @example\n * new HttpResponse('Hello world', { status: 201 })\n * HttpResponse.json({ name: 'John' })\n * HttpResponse.formData(form)\n *\n * @see {@link https://mswjs.io/docs/api/http-response `HttpResponse` API reference}\n */\nexport class HttpResponse<\n BodyType extends DefaultBodyType,\n> extends FetchResponse {\n readonly [bodyType]: BodyType = null as any\n\n constructor(body?: NoInfer<BodyType> | null, init?: HttpResponseInit) {\n const responseInit = normalizeResponseInit(init)\n super(body as BodyInit, responseInit)\n decorateResponse(this, responseInit)\n }\n\n static error(): HttpResponse<any> {\n return super.error() as HttpResponse<any>\n }\n\n /**\n * Create a `Response` with a `Content-Type: \"text/plain\"` body.\n * @example\n * HttpResponse.text('hello world')\n * HttpResponse.text('Error', { status: 500 })\n */\n static text<BodyType extends string>(\n body?: NoInfer<BodyType> | null,\n init?: HttpResponseInit,\n ): HttpResponse<BodyType> {\n const responseInit = normalizeResponseInit(init)\n const hasExplicitContentType = responseInit.headers.has('Content-Type')\n\n if (!hasExplicitContentType) {\n responseInit.headers.set('Content-Type', 'text/plain')\n }\n\n // Automatically set the \"Content-Length\" response header\n // for non-empty text responses. This enforces consistency and\n // brings mocked responses closer to production.\n if (!responseInit.headers.has('Content-Length')) {\n responseInit.headers.set(\n 'Content-Length',\n body ? new Blob([body]).size.toString() : '0',\n )\n }\n\n const response = new HttpResponse(body, responseInit)\n\n if (!hasExplicitContentType) {\n Object.defineProperty(response, kDefaultContentType, {\n value: true,\n enumerable: false,\n })\n }\n\n return response\n }\n\n /**\n * Create a `Response` with a `Content-Type: \"application/json\"` body.\n * @example\n * HttpResponse.json({ firstName: 'John' })\n * HttpResponse.json({ error: 'Not Authorized' }, { status: 401 })\n */\n static json<BodyType extends JsonBodyType>(\n body?: NoInfer<BodyType> | null | undefined,\n init?: HttpResponseInit,\n ): HttpResponse<BodyType> {\n const responseInit = normalizeResponseInit(init)\n const hasExplicitContentType = responseInit.headers.has('Content-Type')\n\n if (!hasExplicitContentType) {\n responseInit.headers.set('Content-Type', 'application/json')\n }\n\n /**\n * @note TypeScript is incorrect here.\n * Stringifying undefined will return undefined.\n */\n const responseText = JSON.stringify(body) as string | undefined\n\n if (!responseInit.headers.has('Content-Length')) {\n responseInit.headers.set(\n 'Content-Length',\n responseText ? new Blob([responseText]).size.toString() : '0',\n )\n }\n\n const response = new HttpResponse(responseText, responseInit)\n\n if (!hasExplicitContentType) {\n Object.defineProperty(response, kDefaultContentType, {\n value: true,\n enumerable: false,\n })\n }\n\n return response as HttpResponse<BodyType>\n }\n\n /**\n * Create a `Response` with a `Content-Type: \"application/xml\"` body.\n * @example\n * HttpResponse.xml(`<user name=\"John\" />`)\n * HttpResponse.xml(`<article id=\"abc-123\" />`, { status: 201 })\n */\n static xml<BodyType extends string>(\n body?: BodyType | null,\n init?: HttpResponseInit,\n ): HttpResponse<BodyType> {\n const responseInit = normalizeResponseInit(init)\n const hasExplicitContentType = responseInit.headers.has('Content-Type')\n\n if (!hasExplicitContentType) {\n responseInit.headers.set('Content-Type', 'text/xml')\n }\n\n const response = new HttpResponse(body, responseInit)\n\n if (!hasExplicitContentType) {\n Object.defineProperty(response, kDefaultContentType, {\n value: true,\n enumerable: false,\n })\n }\n\n return response as HttpResponse<BodyType>\n }\n\n /**\n * Create a `Response` with a `Content-Type: \"text/html\"` body.\n * @example\n * HttpResponse.html(`<p class=\"author\">Jane Doe</p>`)\n * HttpResponse.html(`<main id=\"abc-123\">Main text</main>`, { status: 201 })\n */\n static html<BodyType extends string>(\n body?: BodyType | null,\n init?: HttpResponseInit,\n ): HttpResponse<BodyType> {\n const responseInit = normalizeResponseInit(init)\n const hasExplicitContentType = responseInit.headers.has('Content-Type')\n\n if (!hasExplicitContentType) {\n responseInit.headers.set('Content-Type', 'text/html')\n }\n\n const response = new HttpResponse(body, responseInit)\n\n if (!hasExplicitContentType) {\n Object.defineProperty(response, kDefaultContentType, {\n value: true,\n enumerable: false,\n })\n }\n\n return response as HttpResponse<BodyType>\n }\n\n /**\n * Create a `Response` with an `ArrayBuffer` body.\n * @example\n * const buffer = new ArrayBuffer(3)\n * const view = new Uint8Array(buffer)\n * view.set([1, 2, 3])\n *\n * HttpResponse.arrayBuffer(buffer)\n */\n static arrayBuffer<BodyType extends ArrayBuffer | SharedArrayBuffer>(\n body?: BodyType,\n init?: HttpResponseInit,\n ): HttpResponse<BodyType> {\n const responseInit = normalizeResponseInit(init)\n const hasExplicitContentType = responseInit.headers.has('Content-Type')\n\n if (!hasExplicitContentType) {\n responseInit.headers.set('Content-Type', 'application/octet-stream')\n }\n\n if (body && !responseInit.headers.has('Content-Length')) {\n responseInit.headers.set('Content-Length', body.byteLength.toString())\n }\n\n const response = new HttpResponse(body, responseInit)\n\n if (!hasExplicitContentType) {\n Object.defineProperty(response, kDefaultContentType, {\n value: true,\n enumerable: false,\n })\n }\n\n return response as HttpResponse<BodyType>\n }\n\n /**\n * Create a `Response` with a `FormData` body.\n * @example\n * const data = new FormData()\n * data.set('name', 'Alice')\n *\n * HttpResponse.formData(data)\n */\n static formData(\n body?: FormData,\n init?: HttpResponseInit,\n ): HttpResponse<FormData> {\n return new HttpResponse(body, normalizeResponseInit(init))\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,0BAA8B;AAG9B,wBAGO;AAMA,MAAM,WAA0B,OAAO,UAAU;AAkBjD,MAAM,sBAAsB,OAAO,IAAI,qBAAqB;AAa5D,MAAM,qBAEH,kCAAc;AAAA,EACtB,CAAU,QAAQ,IAAc;AAAA,EAEhC,YAAY,MAAiC,MAAyB;AACpE,UAAM,mBAAe,yCAAsB,IAAI;AAC/C,UAAM,MAAkB,YAAY;AACpC,4CAAiB,MAAM,YAAY;AAAA,EACrC;AAAA,EAEA,OAAO,QAA2B;AAChC,WAAO,MAAM,MAAM;AAAA,EACrB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,OAAO,KACL,MACA,MACwB;AACxB,UAAM,mBAAe,yCAAsB,IAAI;AAC/C,UAAM,yBAAyB,aAAa,QAAQ,IAAI,cAAc;AAEtE,QAAI,CAAC,wBAAwB;AAC3B,mBAAa,QAAQ,IAAI,gBAAgB,YAAY;AAAA,IACvD;AAKA,QAAI,CAAC,aAAa,QAAQ,IAAI,gBAAgB,GAAG;AAC/C,mBAAa,QAAQ;AAAA,QACnB;AAAA,QACA,OAAO,IAAI,KAAK,CAAC,IAAI,CAAC,EAAE,KA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@ import {
|
|
|
4
4
|
normalizeResponseInit
|
|
5
5
|
} from './utils/HttpResponse/decorators.mjs';
|
|
6
6
|
const bodyType = Symbol("bodyType");
|
|
7
|
+
const kDefaultContentType = Symbol.for("kDefaultContentType");
|
|
7
8
|
class HttpResponse extends FetchResponse {
|
|
8
9
|
[bodyType] = null;
|
|
9
10
|
constructor(body, init) {
|
|
@@ -22,7 +23,8 @@ class HttpResponse extends FetchResponse {
|
|
|
22
23
|
*/
|
|
23
24
|
static text(body, init) {
|
|
24
25
|
const responseInit = normalizeResponseInit(init);
|
|
25
|
-
|
|
26
|
+
const hasExplicitContentType = responseInit.headers.has("Content-Type");
|
|
27
|
+
if (!hasExplicitContentType) {
|
|
26
28
|
responseInit.headers.set("Content-Type", "text/plain");
|
|
27
29
|
}
|
|
28
30
|
if (!responseInit.headers.has("Content-Length")) {
|
|
@@ -31,7 +33,14 @@ class HttpResponse extends FetchResponse {
|
|
|
31
33
|
body ? new Blob([body]).size.toString() : "0"
|
|
32
34
|
);
|
|
33
35
|
}
|
|
34
|
-
|
|
36
|
+
const response = new HttpResponse(body, responseInit);
|
|
37
|
+
if (!hasExplicitContentType) {
|
|
38
|
+
Object.defineProperty(response, kDefaultContentType, {
|
|
39
|
+
value: true,
|
|
40
|
+
enumerable: false
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
return response;
|
|
35
44
|
}
|
|
36
45
|
/**
|
|
37
46
|
* Create a `Response` with a `Content-Type: "application/json"` body.
|
|
@@ -41,7 +50,8 @@ class HttpResponse extends FetchResponse {
|
|
|
41
50
|
*/
|
|
42
51
|
static json(body, init) {
|
|
43
52
|
const responseInit = normalizeResponseInit(init);
|
|
44
|
-
|
|
53
|
+
const hasExplicitContentType = responseInit.headers.has("Content-Type");
|
|
54
|
+
if (!hasExplicitContentType) {
|
|
45
55
|
responseInit.headers.set("Content-Type", "application/json");
|
|
46
56
|
}
|
|
47
57
|
const responseText = JSON.stringify(body);
|
|
@@ -51,7 +61,14 @@ class HttpResponse extends FetchResponse {
|
|
|
51
61
|
responseText ? new Blob([responseText]).size.toString() : "0"
|
|
52
62
|
);
|
|
53
63
|
}
|
|
54
|
-
|
|
64
|
+
const response = new HttpResponse(responseText, responseInit);
|
|
65
|
+
if (!hasExplicitContentType) {
|
|
66
|
+
Object.defineProperty(response, kDefaultContentType, {
|
|
67
|
+
value: true,
|
|
68
|
+
enumerable: false
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
return response;
|
|
55
72
|
}
|
|
56
73
|
/**
|
|
57
74
|
* Create a `Response` with a `Content-Type: "application/xml"` body.
|
|
@@ -61,10 +78,18 @@ class HttpResponse extends FetchResponse {
|
|
|
61
78
|
*/
|
|
62
79
|
static xml(body, init) {
|
|
63
80
|
const responseInit = normalizeResponseInit(init);
|
|
64
|
-
|
|
81
|
+
const hasExplicitContentType = responseInit.headers.has("Content-Type");
|
|
82
|
+
if (!hasExplicitContentType) {
|
|
65
83
|
responseInit.headers.set("Content-Type", "text/xml");
|
|
66
84
|
}
|
|
67
|
-
|
|
85
|
+
const response = new HttpResponse(body, responseInit);
|
|
86
|
+
if (!hasExplicitContentType) {
|
|
87
|
+
Object.defineProperty(response, kDefaultContentType, {
|
|
88
|
+
value: true,
|
|
89
|
+
enumerable: false
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
return response;
|
|
68
93
|
}
|
|
69
94
|
/**
|
|
70
95
|
* Create a `Response` with a `Content-Type: "text/html"` body.
|
|
@@ -74,10 +99,18 @@ class HttpResponse extends FetchResponse {
|
|
|
74
99
|
*/
|
|
75
100
|
static html(body, init) {
|
|
76
101
|
const responseInit = normalizeResponseInit(init);
|
|
77
|
-
|
|
102
|
+
const hasExplicitContentType = responseInit.headers.has("Content-Type");
|
|
103
|
+
if (!hasExplicitContentType) {
|
|
78
104
|
responseInit.headers.set("Content-Type", "text/html");
|
|
79
105
|
}
|
|
80
|
-
|
|
106
|
+
const response = new HttpResponse(body, responseInit);
|
|
107
|
+
if (!hasExplicitContentType) {
|
|
108
|
+
Object.defineProperty(response, kDefaultContentType, {
|
|
109
|
+
value: true,
|
|
110
|
+
enumerable: false
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
return response;
|
|
81
114
|
}
|
|
82
115
|
/**
|
|
83
116
|
* Create a `Response` with an `ArrayBuffer` body.
|
|
@@ -90,13 +123,21 @@ class HttpResponse extends FetchResponse {
|
|
|
90
123
|
*/
|
|
91
124
|
static arrayBuffer(body, init) {
|
|
92
125
|
const responseInit = normalizeResponseInit(init);
|
|
93
|
-
|
|
126
|
+
const hasExplicitContentType = responseInit.headers.has("Content-Type");
|
|
127
|
+
if (!hasExplicitContentType) {
|
|
94
128
|
responseInit.headers.set("Content-Type", "application/octet-stream");
|
|
95
129
|
}
|
|
96
130
|
if (body && !responseInit.headers.has("Content-Length")) {
|
|
97
131
|
responseInit.headers.set("Content-Length", body.byteLength.toString());
|
|
98
132
|
}
|
|
99
|
-
|
|
133
|
+
const response = new HttpResponse(body, responseInit);
|
|
134
|
+
if (!hasExplicitContentType) {
|
|
135
|
+
Object.defineProperty(response, kDefaultContentType, {
|
|
136
|
+
value: true,
|
|
137
|
+
enumerable: false
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
return response;
|
|
100
141
|
}
|
|
101
142
|
/**
|
|
102
143
|
* Create a `Response` with a `FormData` body.
|
|
@@ -112,6 +153,7 @@ class HttpResponse extends FetchResponse {
|
|
|
112
153
|
}
|
|
113
154
|
export {
|
|
114
155
|
HttpResponse,
|
|
115
|
-
bodyType
|
|
156
|
+
bodyType,
|
|
157
|
+
kDefaultContentType
|
|
116
158
|
};
|
|
117
159
|
//# sourceMappingURL=HttpResponse.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/core/HttpResponse.ts"],"sourcesContent":["import { FetchResponse } from '@mswjs/interceptors'\nimport type { DefaultBodyType, JsonBodyType } from './handlers/RequestHandler'\nimport type { NoInfer } from './typeUtils'\nimport {\n decorateResponse,\n normalizeResponseInit,\n} from './utils/HttpResponse/decorators'\n\nexport interface HttpResponseInit extends ResponseInit {\n type?: ResponseType\n}\n\nexport const bodyType: unique symbol = Symbol('bodyType')\nexport type DefaultUnsafeFetchResponse = Response & {\n [bodyType]?: never\n}\n\nexport interface StrictRequest<BodyType extends JsonBodyType> extends Request {\n json(): Promise<BodyType>\n clone(): StrictRequest<BodyType>\n}\n\n/**\n * Opaque `Response` type that supports strict body type.\n *\n * @deprecated Please use {@link HttpResponse} instead.\n */\nexport type StrictResponse<BodyType extends DefaultBodyType> =\n HttpResponse<BodyType>\n\n/**\n * A drop-in replacement for the standard `Response` class\n * to allow additional features, like mocking the response `Set-Cookie` header.\n *\n * @example\n * new HttpResponse('Hello world', { status: 201 })\n * HttpResponse.json({ name: 'John' })\n * HttpResponse.formData(form)\n *\n * @see {@link https://mswjs.io/docs/api/http-response `HttpResponse` API reference}\n */\nexport class HttpResponse<\n BodyType extends DefaultBodyType,\n> extends FetchResponse {\n readonly [bodyType]: BodyType = null as any\n\n constructor(body?: NoInfer<BodyType> | null, init?: HttpResponseInit) {\n const responseInit = normalizeResponseInit(init)\n super(body as BodyInit, responseInit)\n decorateResponse(this, responseInit)\n }\n\n static error(): HttpResponse<any> {\n return super.error() as HttpResponse<any>\n }\n\n /**\n * Create a `Response` with a `Content-Type: \"text/plain\"` body.\n * @example\n * HttpResponse.text('hello world')\n * HttpResponse.text('Error', { status: 500 })\n */\n static text<BodyType extends string>(\n body?: NoInfer<BodyType> | null,\n init?: HttpResponseInit,\n ): HttpResponse<BodyType> {\n const responseInit = normalizeResponseInit(init)\n\n if (!responseInit.headers.has('Content-Type')) {\n responseInit.headers.set('Content-Type', 'text/plain')\n }\n\n // Automatically set the \"Content-Length\" response header\n // for non-empty text responses. This enforces consistency and\n // brings mocked responses closer to production.\n if (!responseInit.headers.has('Content-Length')) {\n responseInit.headers.set(\n 'Content-Length',\n body ? new Blob([body]).size.toString() : '0',\n )\n }\n\n return new HttpResponse(body, responseInit)\n }\n\n /**\n * Create a `Response` with a `Content-Type: \"application/json\"` body.\n * @example\n * HttpResponse.json({ firstName: 'John' })\n * HttpResponse.json({ error: 'Not Authorized' }, { status: 401 })\n */\n static json<BodyType extends JsonBodyType>(\n body?: NoInfer<BodyType> | null | undefined,\n init?: HttpResponseInit,\n ): HttpResponse<BodyType> {\n const responseInit = normalizeResponseInit(init)\n\n if (!responseInit.headers.has('Content-Type')) {\n responseInit.headers.set('Content-Type', 'application/json')\n }\n\n /**\n * @note TypeScript is incorrect here.\n * Stringifying undefined will return undefined.\n */\n const responseText = JSON.stringify(body) as string | undefined\n\n if (!responseInit.headers.has('Content-Length')) {\n responseInit.headers.set(\n 'Content-Length',\n responseText ? new Blob([responseText]).size.toString() : '0',\n )\n }\n\n return new HttpResponse(responseText as BodyType, responseInit)\n }\n\n /**\n * Create a `Response` with a `Content-Type: \"application/xml\"` body.\n * @example\n * HttpResponse.xml(`<user name=\"John\" />`)\n * HttpResponse.xml(`<article id=\"abc-123\" />`, { status: 201 })\n */\n static xml<BodyType extends string>(\n body?: BodyType | null,\n init?: HttpResponseInit,\n ): HttpResponse<BodyType> {\n const responseInit = normalizeResponseInit(init)\n\n if (!responseInit.headers.has('Content-Type')) {\n responseInit.headers.set('Content-Type', 'text/xml')\n }\n\n return new HttpResponse(body, responseInit)\n }\n\n /**\n * Create a `Response` with a `Content-Type: \"text/html\"` body.\n * @example\n * HttpResponse.html(`<p class=\"author\">Jane Doe</p>`)\n * HttpResponse.html(`<main id=\"abc-123\">Main text</main>`, { status: 201 })\n */\n static html<BodyType extends string>(\n body?: BodyType | null,\n init?: HttpResponseInit,\n ): HttpResponse<BodyType> {\n const responseInit = normalizeResponseInit(init)\n\n if (!responseInit.headers.has('Content-Type')) {\n responseInit.headers.set('Content-Type', 'text/html')\n }\n\n return new HttpResponse(body, responseInit)\n }\n\n /**\n * Create a `Response` with an `ArrayBuffer` body.\n * @example\n * const buffer = new ArrayBuffer(3)\n * const view = new Uint8Array(buffer)\n * view.set([1, 2, 3])\n *\n * HttpResponse.arrayBuffer(buffer)\n */\n static arrayBuffer<BodyType extends ArrayBuffer | SharedArrayBuffer>(\n body?: BodyType,\n init?: HttpResponseInit,\n ): HttpResponse<BodyType> {\n const responseInit = normalizeResponseInit(init)\n\n if (!responseInit.headers.has('Content-Type')) {\n responseInit.headers.set('Content-Type', 'application/octet-stream')\n }\n\n if (body && !responseInit.headers.has('Content-Length')) {\n responseInit.headers.set('Content-Length', body.byteLength.toString())\n }\n\n return new HttpResponse(body, responseInit)\n }\n\n /**\n * Create a `Response` with a `FormData` body.\n * @example\n * const data = new FormData()\n * data.set('name', 'Alice')\n *\n * HttpResponse.formData(data)\n */\n static formData(\n body?: FormData,\n init?: HttpResponseInit,\n ): HttpResponse<FormData> {\n return new HttpResponse(body, normalizeResponseInit(init))\n }\n}\n"],"mappings":"AAAA,SAAS,qBAAqB;AAG9B;AAAA,EACE;AAAA,EACA;AAAA,OACK;AAMA,MAAM,WAA0B,OAAO,UAAU;AA6BjD,MAAM,qBAEH,cAAc;AAAA,EACtB,CAAU,QAAQ,IAAc;AAAA,EAEhC,YAAY,MAAiC,MAAyB;AACpE,UAAM,eAAe,sBAAsB,IAAI;AAC/C,UAAM,MAAkB,YAAY;AACpC,qBAAiB,MAAM,YAAY;AAAA,EACrC;AAAA,EAEA,OAAO,QAA2B;AAChC,WAAO,MAAM,MAAM;AAAA,EACrB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,OAAO,KACL,MACA,MACwB;AACxB,UAAM,eAAe,sBAAsB,IAAI;AAE/C,QAAI,CAAC,aAAa,QAAQ,IAAI,cAAc,GAAG;AAC7C,mBAAa,QAAQ,IAAI,gBAAgB,YAAY;AAAA,IACvD;AAKA,QAAI,CAAC,aAAa,QAAQ,IAAI,gBAAgB,GAAG;AAC/C,mBAAa,QAAQ;AAAA,QACnB;AAAA,QACA,OAAO,IAAI,KAAK,CAAC,IAAI,CAAC,EA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AQ,IAAI,cAAc,GAAG;AAC7C,mBAAa,QAAQ,IAAI,gBAAgB,0BAA0B;AAAA,IACrE;AAEA,QAAI,QAAQ,CAAC,aAAa,QAAQ,IAAI,gBAAgB,GAAG;AACvD,mBAAa,QAAQ,IAAI,kBAAkB,KAAK,WAAW,SAAS,CAAC;AAAA,IACvE;AAEA,WAAO,IAAI,aAAa,MAAM,YAAY;AAAA,EAC5C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA,OAAO,SACL,MACA,MACwB;AACxB,WAAO,IAAI,aAAa,MAAM,sBAAsB,IAAI,CAAC;AAAA,EAC3D;AACF;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../src/core/HttpResponse.ts"],"sourcesContent":["import { FetchResponse } from '@mswjs/interceptors'\nimport type { DefaultBodyType, JsonBodyType } from './handlers/RequestHandler'\nimport type { NoInfer } from './typeUtils'\nimport {\n decorateResponse,\n normalizeResponseInit,\n} from './utils/HttpResponse/decorators'\n\nexport interface HttpResponseInit extends ResponseInit {\n type?: ResponseType\n}\n\nexport const bodyType: unique symbol = Symbol('bodyType')\nexport type DefaultUnsafeFetchResponse = Response & {\n [bodyType]?: never\n}\n\nexport interface StrictRequest<BodyType extends JsonBodyType> extends Request {\n json(): Promise<BodyType>\n clone(): StrictRequest<BodyType>\n}\n\n/**\n * Opaque `Response` type that supports strict body type.\n *\n * @deprecated Please use {@link HttpResponse} instead.\n */\nexport type StrictResponse<BodyType extends DefaultBodyType> =\n HttpResponse<BodyType>\n\nexport const kDefaultContentType = Symbol.for('kDefaultContentType')\n\n/**\n * A drop-in replacement for the standard `Response` class\n * to allow additional features, like mocking the response `Set-Cookie` header.\n *\n * @example\n * new HttpResponse('Hello world', { status: 201 })\n * HttpResponse.json({ name: 'John' })\n * HttpResponse.formData(form)\n *\n * @see {@link https://mswjs.io/docs/api/http-response `HttpResponse` API reference}\n */\nexport class HttpResponse<\n BodyType extends DefaultBodyType,\n> extends FetchResponse {\n readonly [bodyType]: BodyType = null as any\n\n constructor(body?: NoInfer<BodyType> | null, init?: HttpResponseInit) {\n const responseInit = normalizeResponseInit(init)\n super(body as BodyInit, responseInit)\n decorateResponse(this, responseInit)\n }\n\n static error(): HttpResponse<any> {\n return super.error() as HttpResponse<any>\n }\n\n /**\n * Create a `Response` with a `Content-Type: \"text/plain\"` body.\n * @example\n * HttpResponse.text('hello world')\n * HttpResponse.text('Error', { status: 500 })\n */\n static text<BodyType extends string>(\n body?: NoInfer<BodyType> | null,\n init?: HttpResponseInit,\n ): HttpResponse<BodyType> {\n const responseInit = normalizeResponseInit(init)\n const hasExplicitContentType = responseInit.headers.has('Content-Type')\n\n if (!hasExplicitContentType) {\n responseInit.headers.set('Content-Type', 'text/plain')\n }\n\n // Automatically set the \"Content-Length\" response header\n // for non-empty text responses. This enforces consistency and\n // brings mocked responses closer to production.\n if (!responseInit.headers.has('Content-Length')) {\n responseInit.headers.set(\n 'Content-Length',\n body ? new Blob([body]).size.toString() : '0',\n )\n }\n\n const response = new HttpResponse(body, responseInit)\n\n if (!hasExplicitContentType) {\n Object.defineProperty(response, kDefaultContentType, {\n value: true,\n enumerable: false,\n })\n }\n\n return response\n }\n\n /**\n * Create a `Response` with a `Content-Type: \"application/json\"` body.\n * @example\n * HttpResponse.json({ firstName: 'John' })\n * HttpResponse.json({ error: 'Not Authorized' }, { status: 401 })\n */\n static json<BodyType extends JsonBodyType>(\n body?: NoInfer<BodyType> | null | undefined,\n init?: HttpResponseInit,\n ): HttpResponse<BodyType> {\n const responseInit = normalizeResponseInit(init)\n const hasExplicitContentType = responseInit.headers.has('Content-Type')\n\n if (!hasExplicitContentType) {\n responseInit.headers.set('Content-Type', 'application/json')\n }\n\n /**\n * @note TypeScript is incorrect here.\n * Stringifying undefined will return undefined.\n */\n const responseText = JSON.stringify(body) as string | undefined\n\n if (!responseInit.headers.has('Content-Length')) {\n responseInit.headers.set(\n 'Content-Length',\n responseText ? new Blob([responseText]).size.toString() : '0',\n )\n }\n\n const response = new HttpResponse(responseText, responseInit)\n\n if (!hasExplicitContentType) {\n Object.defineProperty(response, kDefaultContentType, {\n value: true,\n enumerable: false,\n })\n }\n\n return response as HttpResponse<BodyType>\n }\n\n /**\n * Create a `Response` with a `Content-Type: \"application/xml\"` body.\n * @example\n * HttpResponse.xml(`<user name=\"John\" />`)\n * HttpResponse.xml(`<article id=\"abc-123\" />`, { status: 201 })\n */\n static xml<BodyType extends string>(\n body?: BodyType | null,\n init?: HttpResponseInit,\n ): HttpResponse<BodyType> {\n const responseInit = normalizeResponseInit(init)\n const hasExplicitContentType = responseInit.headers.has('Content-Type')\n\n if (!hasExplicitContentType) {\n responseInit.headers.set('Content-Type', 'text/xml')\n }\n\n const response = new HttpResponse(body, responseInit)\n\n if (!hasExplicitContentType) {\n Object.defineProperty(response, kDefaultContentType, {\n value: true,\n enumerable: false,\n })\n }\n\n return response as HttpResponse<BodyType>\n }\n\n /**\n * Create a `Response` with a `Content-Type: \"text/html\"` body.\n * @example\n * HttpResponse.html(`<p class=\"author\">Jane Doe</p>`)\n * HttpResponse.html(`<main id=\"abc-123\">Main text</main>`, { status: 201 })\n */\n static html<BodyType extends string>(\n body?: BodyType | null,\n init?: HttpResponseInit,\n ): HttpResponse<BodyType> {\n const responseInit = normalizeResponseInit(init)\n const hasExplicitContentType = responseInit.headers.has('Content-Type')\n\n if (!hasExplicitContentType) {\n responseInit.headers.set('Content-Type', 'text/html')\n }\n\n const response = new HttpResponse(body, responseInit)\n\n if (!hasExplicitContentType) {\n Object.defineProperty(response, kDefaultContentType, {\n value: true,\n enumerable: false,\n })\n }\n\n return response as HttpResponse<BodyType>\n }\n\n /**\n * Create a `Response` with an `ArrayBuffer` body.\n * @example\n * const buffer = new ArrayBuffer(3)\n * const view = new Uint8Array(buffer)\n * view.set([1, 2, 3])\n *\n * HttpResponse.arrayBuffer(buffer)\n */\n static arrayBuffer<BodyType extends ArrayBuffer | SharedArrayBuffer>(\n body?: BodyType,\n init?: HttpResponseInit,\n ): HttpResponse<BodyType> {\n const responseInit = normalizeResponseInit(init)\n const hasExplicitContentType = responseInit.headers.has('Content-Type')\n\n if (!hasExplicitContentType) {\n responseInit.headers.set('Content-Type', 'application/octet-stream')\n }\n\n if (body && !responseInit.headers.has('Content-Length')) {\n responseInit.headers.set('Content-Length', body.byteLength.toString())\n }\n\n const response = new HttpResponse(body, responseInit)\n\n if (!hasExplicitContentType) {\n Object.defineProperty(response, kDefaultContentType, {\n value: true,\n enumerable: false,\n })\n }\n\n return response as HttpResponse<BodyType>\n }\n\n /**\n * Create a `Response` with a `FormData` body.\n * @example\n * const data = new FormData()\n * data.set('name', 'Alice')\n *\n * HttpResponse.formData(data)\n */\n static formData(\n body?: FormData,\n init?: HttpResponseInit,\n ): HttpResponse<FormData> {\n return new HttpResponse(body, normalizeResponseInit(init))\n }\n}\n"],"mappings":"AAAA,SAAS,qBAAqB;AAG9B;AAAA,EACE;AAAA,EACA;AAAA,OACK;AAMA,MAAM,WAA0B,OAAO,UAAU;AAkBjD,MAAM,sBAAsB,OAAO,IAAI,qBAAqB;AAa5D,MAAM,qBAEH,cAAc;AAAA,EACtB,CAAU,QAAQ,IAAc;AAAA,EAEhC,YAAY,MAAiC,MAAyB;AACpE,UAAM,eAAe,sBAAsB,IAAI;AAC/C,UAAM,MAAkB,YAAY;AACpC,qBAAiB,MAAM,YAAY;AAAA,EACrC;AAAA,EAEA,OAAO,QAA2B;AAChC,WAAO,MAAM,MAAM;AAAA,EACrB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,OAAO,KACL,MACA,MACwB;AACxB,UAAM,eAAe,sBAAsB,IAAI;AAC/C,UAAM,yBAAyB,aAAa,QAAQ,IAAI,cAAc;AAEtE,QAAI,CAAC,wBAAwB;AAC3B,mBAAa,QAAQ,IAAI,gBAAgB,YAAY;AAAA,IACvD;AAKA,QAAI,CAAC,aAAa,QAAQ,IAAI,gBAAgB,GAAG;AAC/C,mBAAa,QAAQ;AAAA,Q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,CAAC;AAAA,EAC3D;AACF;","names":[]}
|
package/lib/core/SetupApi.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { EventMap, Emitter } from 'strict-event-emitter';
|
|
2
|
-
import { R as RequestHandler } from './HttpResponse-
|
|
2
|
+
import { R as RequestHandler } from './HttpResponse-Be4eT3x6.mjs';
|
|
3
3
|
import { LifeCycleEventEmitter } from './sharedOptions.mjs';
|
|
4
4
|
import { Disposable } from './utils/internal/Disposable.mjs';
|
|
5
5
|
import { WebSocketHandler } from './handlers/WebSocketHandler.mjs';
|
package/lib/core/SetupApi.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { EventMap, Emitter } from 'strict-event-emitter';
|
|
2
|
-
import { R as RequestHandler } from './HttpResponse-
|
|
2
|
+
import { R as RequestHandler } from './HttpResponse-Dj6ibgFJ.js';
|
|
3
3
|
import { LifeCycleEventEmitter } from './sharedOptions.js';
|
|
4
4
|
import { Disposable } from './utils/internal/Disposable.js';
|
|
5
5
|
import { WebSocketHandler } from './handlers/WebSocketHandler.js';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { R as RequestHandler, m as ResponseResolutionContext } from './HttpResponse-
|
|
1
|
+
import { R as RequestHandler, m as ResponseResolutionContext } from './HttpResponse-Be4eT3x6.mjs';
|
|
2
2
|
import '@mswjs/interceptors';
|
|
3
3
|
import './utils/internal/isIterable.mjs';
|
|
4
4
|
import './typeUtils.mjs';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { R as RequestHandler, m as ResponseResolutionContext } from './HttpResponse-
|
|
1
|
+
import { R as RequestHandler, m as ResponseResolutionContext } from './HttpResponse-Dj6ibgFJ.js';
|
|
2
2
|
import '@mswjs/interceptors';
|
|
3
3
|
import './utils/internal/isIterable.js';
|
|
4
4
|
import './typeUtils.js';
|
package/lib/core/graphql.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { f as GraphQLQuery, g as GraphQLVariables,
|
|
1
|
+
import { f as GraphQLQuery, g as GraphQLVariables, p as GraphQLPredicate, a as ResponseResolver, q as GraphQLResolverExtras, i as GraphQLResponseBody, c as RequestHandlerOptions, G as GraphQLHandler } from './HttpResponse-Be4eT3x6.mjs';
|
|
2
2
|
import { Path } from './utils/matching/matchRequestUrl.mjs';
|
|
3
3
|
import '@mswjs/interceptors';
|
|
4
4
|
import './utils/internal/isIterable.mjs';
|
package/lib/core/graphql.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { f as GraphQLQuery, g as GraphQLVariables,
|
|
1
|
+
import { f as GraphQLQuery, g as GraphQLVariables, p as GraphQLPredicate, a as ResponseResolver, q as GraphQLResolverExtras, i as GraphQLResponseBody, c as RequestHandlerOptions, G as GraphQLHandler } from './HttpResponse-Dj6ibgFJ.js';
|
|
2
2
|
import { Path } from './utils/matching/matchRequestUrl.js';
|
|
3
3
|
import '@mswjs/interceptors';
|
|
4
4
|
import './utils/internal/isIterable.js';
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import 'graphql';
|
|
2
|
-
export {
|
|
2
|
+
export { z as DocumentTypeDecoration, l as GraphQLCustomPredicate, F as GraphQLCustomPredicateResult, G as GraphQLHandler, C as GraphQLHandlerInfo, B as GraphQLHandlerNameSelector, j as GraphQLJsonRequestBody, k as GraphQLOperationType, p as GraphQLPredicate, f as GraphQLQuery, h as GraphQLRequestBody, E as GraphQLRequestParsedResult, q as GraphQLResolverExtras, i as GraphQLResponseBody, g as GraphQLVariables, I as isDocumentNode } from '../HttpResponse-Be4eT3x6.mjs';
|
|
3
3
|
import '../utils/matching/matchRequestUrl.mjs';
|
|
4
4
|
import '@mswjs/interceptors';
|
|
5
5
|
import '../utils/internal/isIterable.mjs';
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import 'graphql';
|
|
2
|
-
export {
|
|
2
|
+
export { z as DocumentTypeDecoration, l as GraphQLCustomPredicate, F as GraphQLCustomPredicateResult, G as GraphQLHandler, C as GraphQLHandlerInfo, B as GraphQLHandlerNameSelector, j as GraphQLJsonRequestBody, k as GraphQLOperationType, p as GraphQLPredicate, f as GraphQLQuery, h as GraphQLRequestBody, E as GraphQLRequestParsedResult, q as GraphQLResolverExtras, i as GraphQLResponseBody, g as GraphQLVariables, I as isDocumentNode } from '../HttpResponse-Dj6ibgFJ.js';
|
|
3
3
|
import '../utils/matching/matchRequestUrl.js';
|
|
4
4
|
import '@mswjs/interceptors';
|
|
5
5
|
import '../utils/internal/isIterable.js';
|
|
@@ -22,6 +22,7 @@ __export(GraphQLHandler_exports, {
|
|
|
22
22
|
isDocumentNode: () => isDocumentNode
|
|
23
23
|
});
|
|
24
24
|
module.exports = __toCommonJS(GraphQLHandler_exports);
|
|
25
|
+
var import_outvariant = require("outvariant");
|
|
25
26
|
var import_graphql = require("graphql");
|
|
26
27
|
var import_RequestHandler = require("./RequestHandler");
|
|
27
28
|
var import_getTimestamp = require("../utils/logging/getTimestamp");
|
|
@@ -33,7 +34,8 @@ var import_parseGraphQLRequest = require("../utils/internal/parseGraphQLRequest"
|
|
|
33
34
|
var import_toPublicUrl = require("../utils/request/toPublicUrl");
|
|
34
35
|
var import_devUtils = require("../utils/internal/devUtils");
|
|
35
36
|
var import_getRequestCookies = require("../utils/request/getRequestCookies");
|
|
36
|
-
var
|
|
37
|
+
var import_HttpResponse = require("../HttpResponse");
|
|
38
|
+
var import_getAllAcceptedMimeTypes = require("../utils/request/getAllAcceptedMimeTypes");
|
|
37
39
|
function isDocumentNode(value) {
|
|
38
40
|
if (value == null) {
|
|
39
41
|
return false;
|
|
@@ -157,6 +159,32 @@ Consider naming this operation or using "graphql.operation()" request handler to
|
|
|
157
159
|
});
|
|
158
160
|
return args.parsedResult.match.matches && hasMatchingOperationType && hasMatchingOperationName;
|
|
159
161
|
}
|
|
162
|
+
async run(args) {
|
|
163
|
+
const result = await super.run(args);
|
|
164
|
+
if (result?.response == null) {
|
|
165
|
+
return result;
|
|
166
|
+
}
|
|
167
|
+
if (!(import_HttpResponse.kDefaultContentType in result.response)) {
|
|
168
|
+
return result;
|
|
169
|
+
}
|
|
170
|
+
const acceptedMimeTypes = (0, import_getAllAcceptedMimeTypes.getAllAcceptedMimeTypes)(
|
|
171
|
+
args.request.headers.get("accept")
|
|
172
|
+
);
|
|
173
|
+
if (acceptedMimeTypes.length === 0) {
|
|
174
|
+
return result;
|
|
175
|
+
}
|
|
176
|
+
const graphqlResponseIndex = acceptedMimeTypes.indexOf(
|
|
177
|
+
"application/graphql-response+json"
|
|
178
|
+
);
|
|
179
|
+
const jsonIndex = acceptedMimeTypes.indexOf("application/json");
|
|
180
|
+
if (graphqlResponseIndex !== -1 && (jsonIndex === -1 || graphqlResponseIndex <= jsonIndex)) {
|
|
181
|
+
result.response.headers.set(
|
|
182
|
+
"content-type",
|
|
183
|
+
"application/graphql-response+json"
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
return result;
|
|
187
|
+
}
|
|
160
188
|
async matchOperationName(args) {
|
|
161
189
|
if (typeof this.info.operationName === "function") {
|
|
162
190
|
const customPredicateResult = await this.info.operationName({
|