diff --git a/src/content/docs/en/reference/api-reference.mdx b/src/content/docs/en/reference/api-reference.mdx index 96b578a0f9e419..a9acce5bc8124c 100644 --- a/src/content/docs/en/reference/api-reference.mdx +++ b/src/content/docs/en/reference/api-reference.mdx @@ -1458,6 +1458,8 @@ Middleware allows you to intercept requests and responses and inject behaviors d ### `onRequest()` +**Type:** `(context: APIContext, next: MiddlewareNext) => Promise | Response | Promise | void` + A required exported function from `src/middleware.js` that will be called before rendering every page or API route. It accepts two optional arguments: [context](#contextlocals) and [next()](#next). `onRequest()` must return a `Response`: either directly, or by calling `next()`. ```js title="src/middleware.js" @@ -1471,6 +1473,8 @@ export function onRequest (context, next) { ### `next()` +**Type:** `(rewritePayload?: RewritePayload) => Promise` + A function that intercepts (reads and modifies) the `Response` of a `Request` or calls the "next" middleware in the chain and returns a `Response`. For example, this function could modify the HTML body of a response. This is an optional argument of `onRequest()`, and may provide the required `Response` returned by the middleware. @@ -1478,6 +1482,8 @@ This is an optional argument of `onRequest()`, and may provide the required `Res ### `sequence()` +**Type:** `(...handlers: MiddlewareHandler[]) => MiddlewareHandler` + A function that accepts middleware functions as arguments, and will execute them in the order in which they are passed. ```js title="src/middleware.js" @@ -1492,12 +1498,20 @@ export const onRequest = sequence(validation, auth, greeting); ### `createContext()` +

+ +**Type:** `(context: CreateContext) => APIContext` + A low-level API to create an [`APIContext`](#endpoint-context)to be passed to an Astro middleware `onRequest()` function. This function can be used by integrations/adapters to programmatically execute the Astro middleware. ### `trySerializeLocals()` +

+ +**Type:** `(value: unknown) => string` + A low-level API that takes in any value and tries to return a serialized version (a string) of it. If the value cannot be serialized, the function will throw a runtime error. ## Internationalization (`astro:i18n`)