Closed kettanaito closed 2 months ago
interface RequestController {
// Instruct the interceptor to perform the request as-is.
passthrough(): void
// Use the given response as the mock response for the request.
respondWith(response: Response): void
// Use the given error as the request error.
errorWith(error?: Error): void
// Abort the request.
// In some clients (ClientRequest) it's synonymous to ".errorWith()",
// in others (XMLHttpRequest) it's different.
abort(): void
}
The request controller is exposed in the request
listener argument:
interceptor.on('request', ({ request, controller }) => {
controller.respondWith(new Response('hello world'))
})
The request controller can only handle the request once. Once any of its methods are called, calling methods on it again must throw an error (the same way calling request.respondWith()
multiple times right now throws an error).
This has been released in v0.33.0!
Make sure to always update to the latest version (npm i @mswjs/interceptors@latest
) to get the newest features and bug fixes.
Predictable release automation by @ossjs/release.
Instead of adding the
respondWith()
custom method on the Fetch APIRequest
representation of the intercepted request, expose that method alongside the request in the listener argument object.This also resembles the Service Worker's
fetch
event a bit more closely. Not that it has ever the intention but it's a nice little detail.