liaisev5.3.1
/MiddlewareContext

Reference

MiddlewareContext

This page lists what a middleware receives: ctx, then next. Look here when you write a middleware and need to know what a field holds, or whether changing it changes what is sent.

A middleware is a Middleware: (ctx, next) => Promise<Result<unknown>>. Writing middleware shows how to write one.

ctx

ctx is a MiddlewareContext:

PropertyTypeWhat it holds
request.methodstringThe HTTP method, such as 'GET'.
request.urlstringThe full URL, with path params and query string filled in.
request.pathstringThe path template, such as '/users/:id'.
request.paramsunknownThe params as the caller passed them.
request.headersHeadersThe merged headers. A middleware can add, change or remove them.
request.fetchOptionsFetchOptions | undefinedThe client’s, the endpoint’s and the call’s fetchOptions, merged into a fresh object for each call. A middleware can change a field or assign a new object. method, headers, body and signal in it are ignored. Optional in the type only: liaise always sets it, so write ctx.request.fetchOptions!.credentials = 'include', or assign a new object.
request.bodyunknownThe serialized body: a JSON string for an object or array of params, or the body as you passed it, such as a FormData. null when there is none.
request.signalAbortSignal | undefinedThe caller’s signal combined with the call’s deadline, or undefined when there is neither. fetch receives it, merged with the dedupe signal under dedupe. Under share it is the signal this caller waits on, because the shared request has its own. A middleware can replace it (Signals in middleware).
requestNamestringThe endpoint’s key, such as 'getUser'.

liaise reads method, url, headers, body, signal and fetchOptions from ctx.request when it calls fetch, so a middleware that changes one of them changes what is sent. How a replaced signal works with share and dedupe is under Signal-replacing middleware.

On a GraphQL client, request.method is always 'POST', and changing it changes nothing. request.url and request.path are both the endpoint, request.params holds the variables, request.body is the JSON string of { query, variables }, and requestName is the operation’s key, such as 'getCategory'.

next

next is a MiddlewareNext: () => Promise<Result<unknown>>. It runs the rest of the chain, ending in fetch, and resolves to its Result.

  • Call it once to pass the call on, then return its Result or a changed one.
  • Call it again to try again. Each call runs everything after this middleware again, as retryMiddleware does.
  • Don’t call it to answer early, for example from a cache. Return a Result of your own.
  • A middleware that throws ends the call with kind: 'middleware', unless it rethrows liaise’s own abort or timeout reason (Abort classification).
  • A next() called after the call has settled sends nothing and returns the Result the caller already has (Timeout backstop).

Next: Built-in middleware options lists the options of retries, the cache and logging.

esc