Middleware
The fetch middleware that transforms requests and responses.
Middleware transforms a request before the client sends it, or a response after it returns. A job lists middleware under config.fetcher.middleware, applied in order. This page catalogs the available kinds and their parameters.
How middleware is configured
Each entry in config.fetcher.middleware is an object with a kind and a params object. Middleware is applied in the order listed. Some kinds act on the outgoing request, and some can act on either the request or the response, selected with an on field.
config:
fetcher:
middleware:
- kind: set_language
params:
languages:
- [en-US, 1.0]mutate_path
Rewrites the request path when it matches a path pattern. The paths map keys are path patterns and the values are the replacement paths.
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
paths | map of path pattern to string | yes | none | Each matching path pattern is rewritten to its mapped replacement path. |
middleware:
- kind: mutate_path
params:
paths:
/old/*: /newmutate_method
Changes the HTTP method for requests whose path matches a pattern.
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
paths | map of path pattern to method | yes | none | Each matching path pattern uses its mapped HTTP method. |
middleware:
- kind: mutate_method
params:
paths:
/search/*: POSTmutate_headers
Adds or overrides headers on the request or the response.
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
on | request or response | yes | none | Whether the headers are applied to the request or the response. |
headers | list of name and value pairs | yes | none | The headers to set, each as a two-element [name, value] list. |
middleware:
- kind: mutate_headers
params:
on: request
headers:
- [X-Api-Key, <your-api-key>]
- [Accept, application/json]mutate_json_body
Transforms a JSON request or response body using an expression. The expression is written in CEL (Common Expression Language).
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
on | request or response | yes | none | Whether the transform applies to the request or the response body. |
expression | string | yes | none | A CEL expression that produces the new JSON body. |
middleware:
- kind: mutate_json_body
params:
on: response
expression: "body.data"set_language
Sets the request's preferred languages, which become the Accept-Language header. Each entry pairs a language tag with a quality weight between 0 and 1.
| Field | Type | Required | Default | Description |
|---|---|---|---|---|
languages | list of tag and weight pairs | yes | none | The preferred languages, each as a two-element [tag, weight] list. |
middleware:
- kind: set_language
params:
languages:
- [en-US, 1.0]
- [en, 0.8]