UNPKG

@trifrost/core

Version:

Blazingly fast, runtime-agnostic server framework for modern edge and node environments

167 lines (166 loc) 5.92 kB
import { type TriFrostCache } from './modules/Cache'; import { type TriFrostCookieOptions } from './modules/Cookies'; import { TriFrostRateLimit, type TriFrostRateLimitLimitFunction } from './modules/RateLimit/_RateLimit'; import { TriFrostRootLogger, type TriFrostLoggerExporter } from './modules/Logger'; import { Router } from './routing/Router'; import { type TriFrostRuntime } from './runtimes/types'; import { type TriFrostMiddleware, type TriFrostGrouperHandler, type TriFrostHandler, type TriFrostRouteHandler, type PathParam, type TriFrostRouteBuilderHandler } from './types/routing'; import { type TriFrostContextIdOptions } from './types/context'; import { type TriFrostBodyParserOptions } from './utils/BodyParser/types'; import { Lazy, type LazyInitFn } from './utils/Lazy'; import { type createScript } from './modules'; import { type CssGeneric, type CssInstance } from './modules/JSX/style/use'; type AppOptions<Env extends Record<string, any>> = { /** * Global cookie defaults * (defaults to { * path: '/', * secure: true, * httpOnly: true, * sameSite: 'Strict', * }) */ cookies?: Partial<TriFrostCookieOptions>; /** * Rate Limiter instance */ rateLimit?: LazyInitFn<TriFrostRateLimit, Env>; /** * Cache instance */ cache?: LazyInitFn<TriFrostCache<Env>, Env>; /** * Tracing Setup */ tracing?: { /** * Logger to use * @note If none are provided we internally fallback to a Logger with runtime-default exporter */ exporters?: LazyInitFn<TriFrostLoggerExporter[] | TriFrostLoggerExporter, Env>; /** * Request ID options for use in traces/log and distributed tracing */ requestId?: TriFrostContextIdOptions; }; /** * Global environment for the app */ env?: Partial<Env>; /** * Custom Runtime * @note If not provided we will automatically determine the environment */ runtime?: TriFrostRuntime; /** * Maximum timeout in milliseconds globally * @note defaults to 30 000 */ timeout?: number | null; client?: { script?: ReturnType<typeof createScript>['script']; css?: CssGeneric<any>; }; }; declare class App<Env extends Record<string, any>, State extends Record<string, unknown> = {}> extends Router<Env, State> { /** * MARK: Private Vars */ protected runtime: TriFrostRuntime | null; protected logger: TriFrostRootLogger<Env> | null; protected exporters: LazyInitFn<TriFrostLoggerExporter[] | TriFrostLoggerExporter, Env> | null; protected requestId: TriFrostContextIdOptions | null; protected running: boolean; protected cookies: { config: Partial<TriFrostCookieOptions>; }; protected cache: Lazy<TriFrostCache<Env>, Env>; protected css: CssInstance<any, any, any, any> | null; protected script: ReturnType<typeof createScript>['script'] | null; protected env: Env; /** * MARK: Constructor */ constructor(options?: AppOptions<Env>); /** * MARK: Getters */ /** * Whether or not the server is running (started) or not */ get isRunning(): boolean; /** * MARK: Methods */ /** * Boot the app, configure the runtime and any runtime-specific bits * (such as listening for traffic, configuring workerd, etc) */ boot(options?: { port?: number; }): Promise<this>; /** * Stop the runtime and shutdown the app instance */ shutdown(): boolean; /** * MARK: Routing */ /** * Add a router or middleware to the router */ use<Patch extends Record<string, unknown> = {}>(val: TriFrostMiddleware<Env, State, Patch>): App<Env, State & Patch>; /** * Attach a rate limit to the middleware chain */ limit(limit: number | TriFrostRateLimitLimitFunction<Env, State>): this; /** * Configure body parser options */ bodyParser(options: TriFrostBodyParserOptions | null): this; /** * Add a subrouter with dynamic path handling. */ group<Path extends string = string>(path: Path, handler: TriFrostGrouperHandler<Env, State & PathParam<Path>>): this; /** * Add a subroute with a builder approach */ route<Path extends string = string>(path: Path, handler: TriFrostRouteBuilderHandler<Env, State & PathParam<Path>>): this; /** * Configure a catch-all not found handler for subroutes of this router * * @param {Handler} handler - Handler to run */ onNotFound(handler: TriFrostHandler<Env, State>): this; /** * Configure a catch-all error handler for subroutes of this router * * @param {Handler} handler - Handler to run */ onError(handler: TriFrostHandler<Env, State>): this; /** * Configure a HTTP Get route */ get<Path extends string = string>(path: Path, handler: TriFrostRouteHandler<Env, State & PathParam<Path>>): this; /** * Configure a HTTP Post route */ post<Path extends string = string>(path: Path, handler: TriFrostRouteHandler<Env, State & PathParam<Path>>): this; /** * Configure a HTTP Patch route */ patch<Path extends string = string>(path: Path, handler: TriFrostRouteHandler<Env, State & PathParam<Path>>): this; /** * Configure a HTTP Put route */ put<Path extends string = string>(path: Path, handler: TriFrostRouteHandler<Env, State & PathParam<Path>>): this; /** * Configure a HTTP Delete route */ del<Path extends string = string>(path: Path, handler: TriFrostRouteHandler<Env, State & PathParam<Path>>): this; /** * Configure a health route */ health<Path extends string = string>(path: Path, handler: TriFrostHandler<Env, State & PathParam<Path>>): this; } export { App, App as default };