-
-
Notifications
You must be signed in to change notification settings - Fork 533
/
Copy pathHttpResponse.ts
184 lines (159 loc) · 5.26 KB
/
HttpResponse.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import type { DefaultBodyType, JsonBodyType } from './handlers/RequestHandler'
import type { NoInfer } from './typeUtils'
import {
decorateResponse,
normalizeResponseInit,
} from './utils/HttpResponse/decorators'
export interface HttpResponseInit extends ResponseInit {
type?: ResponseType
}
declare const bodyType: unique symbol
export interface StrictRequest<BodyType extends DefaultBodyType>
extends Request {
json(): Promise<BodyType>
}
/**
* Opaque `Response` type that supports strict body type.
*/
export interface StrictResponse<BodyType extends DefaultBodyType>
extends Response {
readonly [bodyType]: BodyType
}
/**
* A drop-in replacement for the standard `Response` class
* to allow additional features, like mocking the response `Set-Cookie` header.
*
* @example
* new HttpResponse('Hello world', { status: 201 })
* HttpResponse.json({ name: 'John' })
* HttpResponse.formData(form)
*
* @see {@link https://mswjs.io/docs/api/http-response `HttpResponse` API reference}
*/
export class HttpResponse extends Response {
constructor(body?: BodyInit | null, init?: HttpResponseInit) {
const responseInit = normalizeResponseInit(init)
super(body, responseInit)
decorateResponse(this, responseInit)
}
/**
* Create a `Response` with a `Content-Type: "text/plain"` body.
* @example
* HttpResponse.text('hello world')
* HttpResponse.text('Error', { status: 500 })
*/
static text<BodyType extends string>(
body?: NoInfer<BodyType> | null,
init?: HttpResponseInit,
): StrictResponse<BodyType> {
const responseInit = normalizeResponseInit(init)
if (!responseInit.headers.has('Content-Type')) {
responseInit.headers.set('Content-Type', 'text/plain')
}
// Automatically set the "Content-Length" response header
// for non-empty text responses. This enforces consistency and
// brings mocked responses closer to production.
if (!responseInit.headers.has('Content-Length')) {
responseInit.headers.set(
'Content-Length',
body ? new Blob([body]).size.toString() : '0',
)
}
return new HttpResponse(body, responseInit) as StrictResponse<BodyType>
}
/**
* Create a `Response` with a `Content-Type: "application/json"` body.
* @example
* HttpResponse.json({ firstName: 'John' })
* HttpResponse.json({ error: 'Not Authorized' }, { status: 401 })
*/
static json<BodyType extends JsonBodyType>(
body?: NoInfer<BodyType> | null,
init?: HttpResponseInit,
): StrictResponse<BodyType> {
const responseInit = normalizeResponseInit(init)
if (!responseInit.headers.has('Content-Type')) {
responseInit.headers.set('Content-Type', 'application/json')
}
/**
* @note TypeScript is incorrect here.
* Stringifying undefined will return undefined.
*/
const responseText = JSON.stringify(body) as string | undefined
if (!responseInit.headers.has('Content-Length')) {
responseInit.headers.set(
'Content-Length',
responseText ? new Blob([responseText]).size.toString() : '0',
)
}
return new HttpResponse(
responseText,
responseInit,
) as StrictResponse<BodyType>
}
/**
* Create a `Response` with a `Content-Type: "application/xml"` body.
* @example
* HttpResponse.xml(`<user name="John" />`)
* HttpResponse.xml(`<article id="abc-123" />`, { status: 201 })
*/
static xml<BodyType extends string>(
body?: BodyType | null,
init?: HttpResponseInit,
): Response {
const responseInit = normalizeResponseInit(init)
if (!responseInit.headers.has('Content-Type')) {
responseInit.headers.set('Content-Type', 'text/xml')
}
return new HttpResponse(body, responseInit)
}
/**
* Create a `Response` with a `Content-Type: "text/html"` body.
* @example
* HttpResponse.html(`<p class="author">Jane Doe</p>`)
* HttpResponse.html(`<main id="abc-123">Main text</main>`, { status: 201 })
*/
static html<BodyType extends string>(
body?: BodyType | null,
init?: HttpResponseInit,
): Response {
const responseInit = normalizeResponseInit(init)
if (!responseInit.headers.has('Content-Type')) {
responseInit.headers.set('Content-Type', 'text/html')
}
return new HttpResponse(body, responseInit)
}
/**
* Create a `Response` with an `ArrayBuffer` body.
* @example
* const buffer = new ArrayBuffer(3)
* const view = new Uint8Array(buffer)
* view.set([1, 2, 3])
*
* HttpResponse.arrayBuffer(buffer)
*/
static arrayBuffer(
body?: ArrayBuffer | SharedArrayBuffer,
init?: HttpResponseInit,
): Response {
const responseInit = normalizeResponseInit(init)
if (!responseInit.headers.has('Content-Type')) {
responseInit.headers.set('Content-Type', 'application/octet-stream')
}
if (body && !responseInit.headers.has('Content-Length')) {
responseInit.headers.set('Content-Length', body.byteLength.toString())
}
return new HttpResponse(body, responseInit)
}
/**
* Create a `Response` with a `FormData` body.
* @example
* const data = new FormData()
* data.set('name', 'Alice')
*
* HttpResponse.formData(data)
*/
static formData(body?: FormData, init?: HttpResponseInit): Response {
return new HttpResponse(body, normalizeResponseInit(init))
}
}