UNPKG

jetpath

Version:

A performance-first cross-runtime API framework without the boilerplate

210 lines (209 loc) 6.78 kB
import { type IncomingMessage } from 'node:http'; import { type Stream } from 'node:stream'; import type { FileOptions, HTTPBody, JetContext, jetOptions, JetPluginExecutorInitParams, JetRoute, methods, SchemaDefinition, SchemaType, ValidationOptions } from './types.js'; import type { BunFile } from 'bun'; export declare class JetPlugin { plugin: any; constructor(plugin: { executor: Function; server?: any; }); setup(init: JetPluginExecutorInitParams): any; } export declare class LOG { static colors: { reset: string; bright: string; dim: string; underscore: string; blink: string; reverse: string; hidden: string; fgBlack: string; fgRed: string; fgGreen: string; fgYellow: string; fgBlue: string; fgMagenta: string; fgCyan: string; fgWhite: string; bgBlack: string; bgRed: string; bgGreen: string; bgYellow: string; bgBlue: string; bgMagenta: string; bgCyan: string; bgWhite: string; }; static print(message: any, color: string): void; static log(message: string, type: 'info' | 'warn' | 'error' | 'success'): void; } export interface CookieOptions { path?: string; domain?: string; secure?: boolean; httpOnly?: boolean; sameSite?: 'strict' | 'lax' | 'none'; maxAge?: number; expires?: Date; } declare class ctxState { state: Record<string, any>; } export declare class Context { code: number; request: Request | IncomingMessage | undefined; params: Record<string, any> | undefined; /** * @internal */ $_internal_query: Record<string, any> | undefined; /** * @internal */ $_internal_body?: Record<string, any>; path: string | undefined; connection?: JetSocket; method: methods | undefined; handler: JetRoute | null; __jet_pool: boolean; plugins: Record<string, Function>; get state(): Record<string, any>; payload?: string; _2: Record<string, string>; _3?: Stream; _6: boolean; res?: any; _7: ctxState; constructor(); send(data: unknown, statusCode?: number, contentType?: string, validate?: boolean): void; redirect(url: string): void; get(field: string): string | undefined; set(field: string, value: string): void; getCookie(name: string): string | undefined; getCookies(): Record<string, string>; setCookie(name: string, value: string, options?: CookieOptions): void; clearCookie(name: string, options?: CookieOptions): void; sendStream(stream: Stream | string | BunFile, config?: { folder?: string; ContentType: string; }): void; download(stream: string | BunFile, config?: { folder?: string; ContentType: string; }): void; sendResponse(Response?: Response): void; upgrade(): void | never; parse<Type extends any = Record<string, any>>(options?: { maxBodySize?: number; contentType?: string; validate?: boolean; }): Promise<Type>; parseQuery<Type extends any = Record<string, any>>(options?: { validate?: boolean; }): Promise<Type>; } export declare class JetSocket { private listeners; addEventListener(event: 'message' | 'close' | 'drain' | 'open', listener: (...param: any[]) => void): void; /** * @internal */ __binder(eventName: 'message' | 'close' | 'drain' | 'open', data: any): void; } /** * Schema builder classes */ export declare class SchemaBuilder { protected def: SchemaDefinition; constructor(type: SchemaType, options?: ValidationOptions); required(err?: string): this; optional(err?: string): this; default(value: any): this; validate(fn: (value: any) => boolean | string): this; regex(pattern: RegExp, err?: string): this; getDefinition(): SchemaDefinition; } export declare class StringSchema extends SchemaBuilder { constructor(options?: ValidationOptions); email(err?: string): this; min(length: number, err?: string): this; max(length: number, err?: string): this; url(err?: string): this; } export declare class NumberSchema extends SchemaBuilder { constructor(options?: ValidationOptions); min(value: number, err?: string): this; max(value: number, err?: string): this; integer(err?: string): this; positive(err?: string): this; negative(err?: string): this; } export declare class BooleanSchema extends SchemaBuilder { constructor(); } export declare class ArraySchema extends SchemaBuilder { constructor(elementSchema?: SchemaBuilder); min(length: number, err?: string): this; max(length: number, err?: string): this; nonempty(err?: string): this; } export declare class ObjectSchema extends SchemaBuilder { constructor(shape?: Record<string, SchemaBuilder>); shape(shape: Record<string, SchemaBuilder>): this; } export declare class DateSchema extends SchemaBuilder { constructor(); min(date: Date | string, err?: string): this; max(date: Date | string, err?: string): this; future(err?: string): this; past(err?: string): this; } export declare class FileSchema extends SchemaBuilder { constructor(options?: FileOptions); maxSize(bytes: number, err?: string): this; mimeType(types: string | string[], err?: string): this; } export declare class SchemaCompiler { static compile(schema: Record<string, SchemaBuilder>): HTTPBody<any>; } declare class TrieNode { children: Map<any, any>; parameterChild?: TrieNode; paramName?: string; wildcardChild?: TrieNode; handler?: JetRoute; constructor(); } /** * Represents the Trie data structure for storing and matching URL routes. */ export declare class Trie { root: TrieNode; method: string; hashmap: Record<string, JetRoute>; constructor(method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'OPTIONS' | 'HEAD' | 'CONNECT' | 'TRACE'); /** * Inserts a route path and its associated handler into the Trie. */ insert(path: string, handler: JetRoute): void; get_responder(req: IncomingMessage | Request, res: any): Context | undefined; } export declare class JetServer { private options; constructor(options?: jetOptions); private _run; runBare(func: JetRoute): Promise<{ code: number; body: any; headers: Record<string, string>; }>; runWithCTX(func: JetRoute, ctx: JetContext<any, any>): Promise<{ code: number; body: any; headers: Record<string, string>; }>; createCTX(req: Request, res: Response, path: string, handler: JetRoute, params: Record<string, any>): JetContext; } export {};