SkipToResponse
Sourceimport { SkipToResponse } from "@prestojs/rest";
This can be passed to next
in middleware to skip all subsequent middleware in the chain, the call
to fetch
and resolve instead to the promise
passed in.
This is useful for middleware like dedupeInFlightRequestsMiddleware where if a duplicate request is detected it just needs to wait for the original request to finish and return the same Response object.
It can also be used to change how fetch
is performed (batchMiddleware
uses this).
For example this just calls a completely different URL and uses the response object from that:
function customFetchMiddleware(next, urlConfig, requestInit, context) {return next(new SkipToResponse(return fetch('/somewhere-else/');));}
Note that this completely bypasses any subsequent middleware in the chain (including response handling) so any security-related middleware should come before this.
API
Constructor
new SkipToResponse(promise)
SourceParameter | Type | Description | |
---|---|---|---|
* | promise | Promise<Response> | The promise to resolve to instead of calling |