MiddlewareContext

Source
import { MiddlewareContext } from "@prestojs/rest";

An instance of this is passed as the fourth parameter to middleware functions.

API

new MiddlewareContext(options)

Source

Should not be instantiated manually. This is created by Endpoint as part of processing a request.

ParameterTypeDescription
*options__type

Methods

Listen for the start of the call to fetch. The callback will be called immediately after fetch is called and is passed the Promise returned by fetch.

This is useful for middleware like dedupeInFlightRequestsMiddleware that need access to the fetch Promise directly. It can then cache the promise and subsequently pass it to SkipToResponse to make other calls to an Endpoint use the same fetch Response.

ParameterTypeDescription
*callback
Function

The callback that will be called when fetch starts. It will be passed the Promise returned by fetch

Function

Mark a Response as decoded.

You likely don't need this; use decodeBody on Endpoint instead. This is useful in cases where you want to bypass the normal Endpoint decodeBody functionality (for example in batchMiddleware).

ParameterTypeDescription
*responseResponse

The response object returned from fetch.

*decodedBodyPromise<T>

The decoded body extracted from response.

void

Called internally by Endpoint to notify when fetch has started.

Should not be called elsewhere.

ParameterTypeDescription
*fetchPromisePromise<Response>
void

Properties

The endpoint the middleware is executing within.

Function to re-execute the Endpoint. Accepts no arguments - it replays the request with identical arguments as the first time. This will go through the full middleware cycle again. This is useful for middleware that may replay a request after an initial failure, eg. if user isn't authenticated on initial attempt.

KeyTypeDescription
headersHeadersInit|Record

Any headers to add to the request. You can unset default headers that might be specified in the default Endpoint.defaultConfig.requestInit by setting the value to undefined.

paginatorPaginatorInterface|null

The paginator instance to use. This can be provided in the constructor to use by default for all executions of this endpoint or provided for each call to the endpoint.

Only applicable if paginationMiddleware has been added to the Endpoint.

queryQuery
urlArgsRecord

The original options passed to execute

The state as of when the last middleware in the chain was processed.

This contains the url, response, decodedBody and result.

The options used to execute the endpoint with