@stacksjs/router
Version:
The Stacks framework router.
72 lines (71 loc) • 2.98 kB
TypeScript
import type { FileInfo } from '@stacksjs/bun-router';
/**
* Stacks-specific marker fields attached to the request by the
* router itself and the framework's default middleware.
*
* Markers are deliberately prefixed with `_` so they can't collide
* with userland keys on the request, and their lifetimes are bounded
* by the request's lifetime — they're never persisted.
*/
export declare interface StacksRequestMarkers {
_corsConfig?: unknown
_forceJson?: boolean
_skipCsrf?: boolean
_compress?: boolean
_middlewareParams?: Record<string, string>
_requestId?: string
_startNs?: bigint
_authenticatedUser?: unknown
_currentAccessToken?: unknown
_bodyParsed?: boolean
}
/**
* Laravel-style request-input macros that Stacks attaches in
* `enhanceRequest` (router/src/stacks-router.ts). These shadow some
* of bun-router's `RequestMacroMethods` with Stacks-specific
* implementations (more permissive `T = any` generics so action
* callers don't have to specify the return type for every read).
*
* Listed here as part of the augmentation so call sites like
* `request.input(key)` type-check without `as any`.
*/
export declare interface StacksRequestMacros {
input?: <T = unknown>(key: string, defaultValue?: T) => T
get?: <T = unknown>(key: string, defaultValue?: T) => T
all?: () => Record<string, unknown>
only?: <T extends Record<string, unknown>>(keys: string[]) => T
except?: <T extends Record<string, unknown>>(keys: string[]) => T
has?: (key: string | string[]) => boolean
hasAny?: (keys: string[]) => boolean
missing?: (key: string) => boolean
filled?: (key: string) => boolean
integer?: (key: string, defaultValue?: number) => number
float?: (key: string, defaultValue?: number) => number
boolean?: (key: string, defaultValue?: boolean) => boolean
string?: (key: string, defaultValue?: string) => string
array?: <T = unknown>(key: string, defaultValue?: T[]) => T[]
file?: (key: string) => FileInfo | null
files?: (key: string) => FileInfo[]
hasFile?: (key: string) => boolean
allFiles?: () => Record<string, FileInfo | FileInfo[]>
getFiles?: () => Record<string, FileInfo | FileInfo[]>
user?: () => Promise<unknown>
userToken?: () => Promise<unknown>
tokenCan?: (ability: string) => Promise<boolean>
tokenCant?: (ability: string) => Promise<boolean>
can?: (ability: string, ...args: unknown[]) => Promise<boolean>
cannot?: (ability: string, ...args: unknown[]) => Promise<boolean>
authorize?: (ability: string, ...args: unknown[]) => Promise<void>
}
/**
* Union of Stacks markers + macros — useful as a single type alias for
* places that previously cast to `any`.
*/
export type StacksRequestExtensions = StacksRequestMarkers & StacksRequestMacros;
declare module '@stacksjs/bun-router' {
interface EnhancedRequest extends StacksRequestMarkers {
allFiles?: StacksRequestMacros['allFiles']
tokenCan?: StacksRequestMacros['tokenCan']
can?: StacksRequestMacros['can']
}
}