Skip to content

Middleware

Since 1.4.0

Middleware function type.

Signature

typescript
type Middleware<C extends Record<string, any> = object> = (
  ctx: Context<C>,
  next: () => Promise<void>
) => Promise<void>

Parameters

ParameterDescription
CContext type

Description

Middleware function that can modify context and call next middleware.

Examples

Basic Usage

typescript
import type { Middleware, Context } from 'uni-types'

type AuthMiddleware = Middleware<{ user: { id: string } }>

const auth: AuthMiddleware = async (ctx, next) => {
  ctx.user = await authenticate(ctx.request)
  await next()
}

Released under the MIT License.