Reference
@muze-nl/metro-middleware reference
Middleware is added to a Metro client with .with():
json(options)
Adds Accept: application/json when missing, serializes plain object request bodies to JSON for non-GET/HEAD requests, and parses JSON responses into response.data.
Options: contentType, accept, reviver, replacer, space.
thrower(options)
Throws an Error when response.ok is false. The original response is available as error.cause. You can pass status handlers:
getdata()
For OK responses with response.data, returns the data directly. Otherwise returns the response.
retry(options)
Retries temporary failures. By default it retries GET, HEAD, and OPTIONS responses with status 408, 425, 429, 500, 502, 503, or 504, and it does not retry abort or timeout errors.
Common options: attempts, delay, factor, maxDelay, jitter, methods, status, respectRetryAfter, respectRateLimit, when, onError, sleep, random.
A number may be used as shorthand:
timeout(options)
Aborts a request after a delay. A number is shorthand for { ms }. ms may also be a function receiving the request.
Exports timeout.timeoutError(ms) as a helper on the default function.
abort(options)
Adds or combines an external abort signal with the request signal. options may be an AbortSignal, a function returning one, or { signal }.
Exports abort.combineSignals(...signals) and abort.abortError(message) as helpers on the default function.
backoff(options)
Remembers server backoff hints and waits before later requests in the same scope. It understands Retry-After, RateLimit-Reset with RateLimit-Remaining: 0, and combined RateLimit values with r=0 and t=seconds.
Common options: store, scope, statuses, maxDelay, sleep, now.
Stores:
Helpers on the default function include responseBackoffDelay, parseRetryAfter, parseRateLimitReset, parseCombinedRateLimit, memoryBackoffStore, localStorageBackoffStore, and sleep.
echoMock()
Returns a 200 OK Metro response that echoes the request URL, headers, and body. Useful in tests.
errorMock()
Returns HTTP error responses for paths such as /400/, /401/, /403/, /404/, /429/, and common 5xx paths. Useful in tests.
Default export
The default export is an object containing the named middleware factories.
Writing your own middleware
For middleware invariants and examples, see Writing Metro middleware.