/** * @stacksjs/router - Stacks Router * * A thin wrapper around bun-router that adds Stacks-specific * action/controller resolution for string-based route handlers. * * All routing functionality comes directly from bun-router. */ // Side-import the EnhancedRequest module augmentation so every consumer // of @stacksjs/router gets the typed Stacks markers (_corsConfig, // _requestId, _authenticatedUser, …) and Laravel-style macros (input, // all, has, file, user, …) without `as any` casts. See // `./request-augmentation.ts` and stacksjs/stacks#1863 T-3. import './request-augmentation' export { readJsonBody, readTextBody, RequestBodyError } from './body' export { parseJsonText } from './json' export type { JsonParseOptions } from './json' // Re-export everything from bun-router (includes response factory) export * from '@stacksjs/bun-router' // Re-export the augmentation types so userland can refer to the marker // surface explicitly when needed. export type { AuthenticatedUser, StacksRequestExtensions, StacksRequestMacros, StacksRequestMarkers } from './request-augmentation' /** * Augmentation target: the types an application gives the request. * * `user` is the type of the signed-in user the Auth middleware stamps on * `request._authenticatedUser`. `@stacksjs/auth` registers the User row * `User.find` returns, so an app's own User model comes through. With * nothing registered, the user is anything with an `id`. * * Declared here, in the entry, rather than beside the markers: an * augmentation merges into the module it names, and an interface that is only * re-exported from here would get a second, unrelated declaration that the * markers never see (the same trap bun-router's `RouterTypeRegistry` notes). * * @example * ```ts * declare module '@stacksjs/router' { * interface RequestTypeRegistry { * user: UserModel * } * } * ``` */ // eslint-disable-next-line ts/no-empty-object-type -- augmentation target; empty by design export interface RequestTypeRegistry {} // Export Stacks-specific action resolver and URL helper export { assertRouteMiddlewareResolvable, configureViewDirectories, clearCsrfModuleCache, clearMiddlewareCache, createStacksRouter, disableViewRouting, enhanceRequest, findUnresolvableRouteMiddleware, installMiddlewareHotReload, loadMiddlewareHandlers, resetBootHooks, resetShutdownHooks, route, runBootHooks, runShutdownHooks, serve, serverResponse, url, warnOnMultipleRouterInstances } from './stacks-router' export type { BootHook, ShutdownHook, StacksRouterConfig } from './stacks-router' // Export request context helpers export { cacheRequestQuery, clearCurrentRequest, getCurrentRequest, getTraceId, request, runWithRequest, setCurrentRequest, withTraceId } from './request-context' // Export Middleware class for defining route middleware export { defineMiddleware, Middleware } from './middleware' export type { MiddlewareAliases, MiddlewareClasses, MiddlewareClassName, MiddlewareConfig, Request } from './middleware' // Export route loader export type { RootMountedAppRoute } from './route-loader' export type { DefaultRouteBundleSelection } from './route-loader' export { appRouteRegistry, bundleMounts, DEFAULT_ROUTE_BUNDLE_FEATURES, DEFAULT_ROUTE_REGISTRY, listRootMountedAppRoutes, loadRoutes, mountedDefaultRouteBundles } from './route-loader' // Export route registry types — owned here rather than in app/Routes.ts // so the path doesn't depend on a 5-level relative reach across the // framework defaults tree (stacksjs/stacks#1863, T-10). export type { RouteDefinition, RouteRegistry } from './route-types' // Export error handler utilities export { clearTrackedQueries, createErrorResponse, createMiddlewareErrorResponse, createNotFoundResponse, createValidationErrorResponse, getQueryShapeCounts, trackQuery, } from './error-handler' // Export route introspection helpers export { isUnmatchedRouteResponse, listNamedRoutes, listRedirectRoutes, listRegisteredRoutes, routeParams } from './stacks-router' export { DASHBOARD_ROUTE_GROUPS, dashboardGroupMiddleware, describeUnknownDashboardGroups, resolveDashboardAccess } from './dashboard-access' export type { DashboardAccess, DashboardRouteGroupPrefix } from './dashboard-access' export type { UrlParams } from './stacks-router' // Registering an action by import rather than by name: the seam that lets a // route hand over the action object it already has, instead of a string that // only a runtime `import()` can make sense of. `listRegisteredRoutes()` reports // it, which is how the OpenAPI generator reads such a route's schema without a // file path to import. export { isRouterAction, provideActions, provideMiddleware, resolveActionHandler, resolveActionValidations, wrapAction } from './stacks-router' export { withRouteAdmission } from './stacks-router' export type { RouteAdmission, WithheldRoute } from './stacks-router' export type { ChainableRoute, RedirectRoute, RedirectStatus, RouteMonitorOption, RouterAction, StacksHandler, StacksRouterInstance } from './stacks-router' /* * Typed routes, bound to this application's router. * * The builder, the route-map contract and the client itself all come from * `@stacksjs/bun-router` (re-exported above by the star export at the top of * this file) - `createTypedClient`, `TypedRouter`, `RoutesOf`, `defineEndpoint` * and the rest are available from here without being named again. Only the * binding to the `route` singleton is specific to Stacks, and that is what * `./typed-router` adds; it deliberately shadows the star-exported name. */ export { createTypedRouter } from './typed-router' // Route-model binding (stacksjs/stacks#2231): what lets `can:view,site` // reach `SitePolicy.view(user, site)` instead of handing the policy layer a // raw path string it can never match a policy against. export { clearRouteModelBindings, defineRouteModelBinding, resolveRouteModel, routeModelBindings, setRouteModelFallback, type RouteModelContext, type RouteModelResolution, type RouteModelResolver, } from './route-model-binding' // Export JSON-vs-HTML negotiation predicate so userland can short-circuit // the same decision the framework makes in formatResult / error-handler. export { isApiRequest, JSON_CONTENT_TYPE } from './api-shape' // Export action-level rate limiting helpers export { rateLimit, rateLimitStatus, clearRateLimit, resetRateLimiters } from './rate-limit' // `clientAddress` here shadows bun-router's (re-exported above) with one that // reads `config.security.trustedProxies`. export { cacheRedisConnection, clientAddress, fallbackStorage, rateLimitScale, resetSharedRateLimitStorage, scaledLimit, sharedRateLimitStorage, trustedProxies, useRateLimitStorage } from './security-settings' // `registerPeerSource` here shadows bun-router's with one that also records // the socket's port, for `peerSocket` (the web application firewall's REMOTE_PORT). export { peerSocket, registerPeerSource } from './peer-socket' // Export path-param sanitization helper (stacksjs/stacks#1870 R-12). // Defense-in-depth for actions that interpolate route params into // filesystem paths; the helper enforces no-traversal / no-absolute / // no-null-byte / length ceiling at a single chokepoint. export { PathParamError, safePathParam, sanitizePathParam } from './path-sanitize' export type { PathParamRejection, SanitizePathParamOptions } from './path-sanitize' // Shared dependency probes used by the public load-balancer endpoint and // authenticated operational dashboards. export { checkApplicationHealth, runHealthProbes } from './health' export type { ApplicationHealthCheck, ApplicationHealthResult, HealthProbe, HealthProbeOptions } from './health' // Export the streaming-response helper for SSE / NDJSON / chunked // binary returns (stacksjs/stacks#1870 R-4). Actions can return // `stream(asyncGen, { type: 'sse' })` and the router pipes it back // with the right Content-Type + no-cache headers. export { stream } from './stacks-router' export type { StreamOptions } from './stacks-router' // Refuse a browser form from inside `handle()`: `{ error }` for a caller that // asked for data, its page with the message for a browser (stacksjs/stacks#3311). export { FORM_REFUSAL, formRefusalOf, refuse } from './refuse' export type { FormRefusal, RefuseOptions } from './refuse' // Signed-URL helpers — HMAC over the URL + optional expiry so single- // use links (email verify, password reset, unsubscribe) can be handed // out without long-lived bearer tokens. Pair `signedUrl(...)` with the // `signed` middleware (or call `verifySignedUrl(req.url)` directly). // See stacksjs/stacks#1870 R-7. export { signedUrl, signUrl, verifySignedUrl, verifySignedUrlMiddleware } from './signed-url' export type { SignedUrlOptions, SignedUrlVerifyResult } from './signed-url' // Encryption-at-rest wrapper for any bun-router SessionStore // (stacksjs/stacks#1878 Se-4). Opt-in: wrap your existing store // instance so session payloads are AES-GCM encrypted via APP_KEY // before being persisted. export { EncryptedSessionStore } from './encrypted-session-store' export type { EncryptedSessionStoreOptions } from './encrypted-session-store' // Session driver factory (stacksjs/stacks#1889, F-2 from #1874). // Builds a SessionStore from the Stacks config — picks the right // driver from `config.session.driver`, optionally wraps with // EncryptedSessionStore. Re-exports all four bun-router store // classes so callers can assemble custom stacks manually too. export { createSessionStore, createStacksSessionStore, DatabaseSessionStore, FileSessionStore, MemorySessionStore, RedisSessionStore, } from './session-factory' export type { RedisClient, SessionConfig, SessionData, SessionStore, StacksSessionConfig, } from './session-factory'