UNPKG

@stacksjs/stx

Version:

A performant UI Framework. Powered by Bun.

107 lines (105 loc) 3.32 kB
import path from 'node:path'; import type { StxOptions } from './types'; /** * Render a STX template with server-side data * * @param template - Template path relative to viewsDir, or inline template string * @param data - Data to pass to the template * @param options - Render options */ export declare function render(template: string, data?: Record<string, unknown>, options?: { viewsDir?: string layoutsDir?: string stxOptions?: StxOptions ctx?: RequestContext }): Promise<Response>; /** * Create a STX server application */ export declare function createApp(config?: AppConfig): void; /** Session data store */ export declare interface SessionData { } /** Session interface */ export declare interface Session { id: string data: SessionData get(key: string): T | undefined set(key: string, value: unknown): void delete(key: string): void clear(): void has(key: string): boolean flash(key: string, value?: unknown): unknown | undefined regenerate(): void destroy(): void } /** Pluggable session store interface for production-ready backends (Redis, DB, etc.) */ export declare interface SessionStore { get(sessionId: string): Promise<SessionData | null> set(sessionId: string, data: SessionData, ttlSeconds: number): Promise<void> delete(sessionId: string): Promise<void> } /** Request context passed to route handlers */ export declare interface RequestContext { request: Request url: URL params: Record<string, string> query: Record<string, string> session: Session csrfToken: string formData: () => Promise<Record<string, string | File>> json: <T = unknown>() => Promise<T> cookies: Record<string, string> setCookie: (name: string, value: string, options?: CookieOptions) => void responseHeaders: Headers flash: (key: string, value: unknown) => void getFlash: (key: string) => unknown errors: ValidationErrors old: (key: string) => string isAuthenticated: () => boolean user: () => unknown } /** Cookie options */ export declare interface CookieOptions { maxAge?: number expires?: Date path?: string domain?: string secure?: boolean httpOnly?: boolean sameSite?: 'Strict' | 'Lax' | 'None' } /** Validation errors */ export declare interface ValidationErrors { } /** App configuration */ export declare interface AppConfig { viewsDir?: string layoutsDir?: string defaultLayout?: string sessionSecret?: string sessionCookieName?: string sessionTtl?: number stxOptions?: StxOptions csrfCookieName?: string csrfEnabled?: boolean sessionStore?: SessionStore } /** Route definition */ declare interface Route { method: string pattern: string handler: RouteHandler middleware: Middleware[] } /** Route handler */ export type RouteHandler = (ctx: RequestContext) => Response | Promise<Response> /** Middleware function */ export type Middleware = (ctx: RequestContext, next: () => Promise<Response>) => Response | Promise<Response> /** Default in-memory session store. Use a custom SessionStore (Redis, DB) for production. */ export declare class MemorySessionStore implements SessionStore { private store: any; get(sessionId: string): Promise<SessionData | null>; set(sessionId: string, data: SessionData, ttlSeconds: number): Promise<void>; delete(sessionId: string): Promise<void>; }