flaska/flaska.d.ts
2024-11-18 04:02:22 +00:00

147 lines
4.5 KiB
TypeScript

import { IncomingMessage, ServerResponse, Server } from 'node:http'
import { Stats } from 'node:fs'
type Maybe<T> = T | null | undefined
export const MimeTypeDb: Record<string, Array<string>>
export type Logger = {
fatal: (...data: any[]) => void,
error: (...data: any[]) => void,
warn: (...data: any[]) => void,
info: (...data: any[]) => void,
debug: (...data: any[]) => void,
trace: (...data: any[]) => void,
log: (...data: any[]) => void,
}
export type Method = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'OPTIONS' | 'PATCH' | 'HEAD'
export type Context = {
log: Logger,
req: IncomingMessage,
res: ServerResponse<InstanceType<IncomingMessage>>,
method: Method,
url: string,
search: string,
state: Record<string, any>,
status: number,
query: URLSearchParams,
body: any,
type: string | null,
length: number | null,
headers: Record<string, string>,
aborted?: boolean,
}
export interface BodyHasHandler = { handleRequest: (ctx: Context) => any }
export type HandlerSync = (ctx: Context) => void
export type HandlerAsync = (ctx: Context) => Promise<void>
export type Handler = HandlerSync | HandlerAsync
export function QueryHandler(): Handler
export function JsonHandler(options: { sizeLimit?: number | null } = {}): Handler
export function CorsHandler(options: {
allowedMethod?: Maybe<string>,
allowedOrigins?: Maybe<string[]>,
allowedHeaders?: Maybe<string | boolean>,
credentials?: Maybe<boolean>,
exposeHeaders?: Maybe<string>,
maxAge?: Maybe<string>,
} = {}): Handler
export type File = { name: string, path: string, filename: string }
export class HttpError {
constructor(statusCode: number, message: string, body: any)
status: number
body: any
}
export class FileResponse implements BodyHasHandler {
constructor(filepath: string, stat: Stats)
filepath: string
stat: Stats
handleRequst(ctx: Context): any
}
export function FormidableHandler(formidable: any, options: {
parseFields?: Maybe<boolean>,
uploadDir?: Maybe<string>,
filename?: Maybe<(file: File) => string>,
maxFileSize?: Maybe<number>,
maxFieldSize?: Maybe<number>,
maxFields?: Maybe<number>,
rename?: Maybe<boolean>,
} = {}): Handler
export class RouterError {
constructor(route1: any, route2: any, message: string)
routeA: any
routeB: any
}
export type FlaskaRouterMatch = {
path: {
path: string,
handlers: Handler[],
},
params: Record<string, string>,
}
export class FlaskaRouter {
constructor()
addRoute(path: string, handler: Handler): void
addRoute(path: string, middlewares: Handler | Handler[], handler: Handler): void
compile(): void
match(url: string): FlaskaRouterMatch | null
}
export type FlaskaOptions = {
defaultHeaders?: Maybe<Record<string, string>>,
log?: Maybe<Logger>,
nonce?: Maybe<Array<'child-src' | 'connect-src' | 'default-src' | 'fenced-frame-src' | 'font-src' | 'frame-src' | 'img-src' | 'manifest-src' | 'media-src' | 'object-src' | 'prefetch-src' | 'script-src' | 'script-src-elem' | 'script-src-attr' | 'style-src' | 'style-src-elem' | 'style-src-attr' | 'worker-src' | 'base-uri' | 'sandbox' | 'form-action' | 'frame-ancestors' | 'require-trusted-types-for' | 'trusted-types' | 'upgrade-insecure-requests'>>,
nonceCacheLength: Maybe<number>,
appendHeaders?: Maybe<Record<string, string>>,
}
export class Flaska {
log: Logger
server: Server
routers: {
GET: FlaskaRouter,
POST: FlaskaRouter,
PUT: FlaskaRouter,
DELETE: FlaskaRouter,
OPTIONS: FlaskaRouter,
PATCH: FlaskaRouter,
HEAD: FlaskaRouter,
}
get: FlaskaRouter['addRoute']
post: FlaskaRouter['addRoute']
put: FlaskaRouter['addRoute']
delete: FlaskaRouter['addRoute']
options: FlaskaRouter['addRoute']
patch: FlaskaRouter['addRoute']
head: FlaskaRouter['addRoute']
constructor(options: FlaskaOptions)
devMode(): void
on404(handler: Handler): void
onerror(handler: Handler): void
onreqerror(handler: Handler): void
onreserror(handler: Handler): void
before(handler: HandlerSync): void
beforeAsync(handler: HandlerAsync): void
after(handler: HandlerSync): void
afterAsync(handler: HandlerAsync): void
listen(port: number): void
listen(port: number, ip: string): void
listen(port: number, cb: () => void): void
listen(port: number, ip:string, cb: () => void): void
listenAsync(port: number): Promise<void>
listenAsync(port: number, ip: string): Promise<void>
listenAsync(port: number, cb: () => void): Promise<void>
listenAsync(port: number, ip:string, cb: () => void): Promise<void>
closeAsync(): Promise<void>
}