Skip to content

Commit

Permalink
fix(api-reference): add missing Types and Since in Middleware section
Browse files Browse the repository at this point in the history
* Add missing Since (see withastro/astro#7578 for
`createContext` and `trySerializeLocals`
* Add missing types (see `packages/astro/src/core/build/types.ts` and
`packages/astro/src/@types/astro.ts`)
  • Loading branch information
ArmandPhilippot committed Jul 29, 2024
1 parent e5500c4 commit 1aa413b
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/content/docs/en/reference/api-reference.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -1458,6 +1458,8 @@ Middleware allows you to intercept requests and responses and inject behaviors d

### `onRequest()`

**Type:** `(context: APIContext, next: MiddlewareNext) => Promise<Response> | Response | Promise<void> | 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"
Expand All @@ -1471,13 +1473,17 @@ export function onRequest (context, next) {

### `next()`

**Type:** `(rewritePayload?: RewritePayload) => Promise<Response>`

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.


### `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"
Expand All @@ -1492,12 +1498,20 @@ export const onRequest = sequence(validation, auth, greeting);

### `createContext()`

<p><Since v="2.8.0" /></p>

**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()`

<p><Since v="2.8.0" /></p>

**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`)
Expand Down

0 comments on commit 1aa413b

Please sign in to comment.