Skip to content

Middleware

Middleware with phantom output type.

The Output parameter tracks what this middleware adds to context at compile time. The _output brand is phantom — it exists only in the type system for inference, not at runtime.

Created via the middleware factory. Attached to commands via CommandBuilder.middleware().

Signatures

ts
type Middleware<Output extends Record<string, unknown>> = MiddlewareImpl & { _output: Output; };

Examples

ts
interface User { id: string; name: string }

const auth = middleware<{ user: User }>(async ({ next }) => {
  const user = await getUser();
  if (!user) throw new CLIError('Not authenticated', { code: 'AUTH_REQUIRED' });
  return next({ user });
});

See Also

Released under the MIT License.