function withSecurityMetadata
Since 0.2.0
withSecurityMetadata<T extends MiddlewareFunction>(
middleware: T,
): T

Brands a middleware function with the security it enforces, so a documentation generator can read it without importing the plugin that produced it.

The function is branded in place and returned, so identity is preserved and the brand costs no wrapper frame per request. The property is symbol-keyed and non-enumerable, so it is invisible to Object.keys, JSON.stringify and spread, and the middleware behaves exactly as it did.

Examples

Example 1

export function requireAuth(): MiddlewareFunction {
  return withSecurityMetadata(async (ctx, next) => {
    if (!ctx.request.user) return void ctx.response.status(401).json({ error: 'Unauthorized' });
    await next();
  }, { authenticated: true });
}

Type Parameters

Parameters

middleware: T

The middleware to brand

What the middleware enforces

Return Type

The same middleware reference, branded

Usage

import { withSecurityMetadata } from "common/src/index.ts";