-
-
Notifications
You must be signed in to change notification settings - Fork 536
/
Copy pathbypass.ts
36 lines (30 loc) · 1.22 KB
/
bypass.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
import { invariant } from 'outvariant'
export type BypassRequestInput = string | URL | Request
/**
* Creates a `Request` instance that will always be ignored by MSW.
*
* @example
* import { bypass } from 'msw'
*
* fetch(bypass('/resource'))
* fetch(bypass(new URL('/resource', 'https://example.com)))
* fetch(bypass(new Request('https://example.com/resource')))
*
* @see {@link https://mswjs.io/docs/api/bypass `bypass()` API reference}
*/
export function bypass(input: BypassRequestInput, init?: RequestInit): Request {
const request = input instanceof Request ? input : new Request(input, init)
invariant(
!request.bodyUsed,
'Failed to create a bypassed request to "%s %s": given request instance already has its body read. Make sure to clone the intercepted request if you wish to read its body before bypassing it.',
request.method,
request.url,
)
const requestClone = request.clone()
// Set the internal header that would instruct MSW
// to bypass this request from any further request matching.
// Unlike "passthrough()", bypass is meant for performing
// additional requests within pending request resolution.
requestClone.headers.set('x-msw-intention', 'bypass')
return requestClone
}