UNPKG

ufiber

Version:

Next-gen webserver for node-js developer

163 lines (162 loc) 3.96 kB
import { kCtxRes } from "../consts.js"; import { HttpStatusCode, RedirectStatusCode } from "../status.js"; import { BaseMime, ResponseHeader } from "../types.js"; import { Request } from "./request.js"; import { HttpRequest, HttpResponse, RecognizedString } from "../../uws"; //#region src/http/context.d.ts interface SetHeaders { (name: 'content-type', value?: BaseMime, append?: boolean): void; (name: ResponseHeader, value?: string, append?: boolean): void; (name: string, value?: string, append?: boolean): void; } type ResData = { vars?: Map<string, unknown>; headers: Record<string, string | string[]>; statusCode?: HttpStatusCode; headerSent: boolean; aborts: Array<() => void>; }; type Options = { req: HttpRequest; res: HttpResponse; isSSL: boolean; appName?: string; bodyLimit?: number; methods?: string[]; }; /** * ⚡ Unified high-performance Context for uWebSockets.js * * Combines both Request + Response APIs into one streamlined object. * Inspired by Fiber GO, Hono, and Oak — but built for uWS zero-copy speed. * * @example * ```ts * app.get('/users/:id', async (ctx) => { * const id = ctx.param('id'); * const data = await ctx.jsonParse(); * ctx.status(200).json({ id, data }); * }); * ``` */ declare class Context extends Request { /** Whether the request was aborted by the client */ aborted: boolean; /** Whether response has already been sent */ finished: boolean; readonly [kCtxRes]: ResData; constructor({ req, res, appName, isSSL, bodyLimit, methods }: Options); /** * Register callback for when client disconnects * * @example * ```ts * ctx.onAbort(() => db.release()); * ``` */ onAbort(fn: () => void): void; /** * Store transient data between middlewares * * @example * ```ts * ctx.set('user', { id: 1 }); * ``` */ set<T>(key: string, value: T): this; /** * Retrieve stored middleware data * * @example * ```ts * const user = ctx.get<{ id: number }>('user'); * ``` */ get<T>(key: string): T | undefined; /** * Set HTTP status code * Chainable, so you can call: ctx.status(201).json({...}) * * @example * ```ts * ctx.status(201).json({ created: true }); * ctx.status(404).text('Not Found'); * ``` */ status: (code: HttpStatusCode) => this; /** * setter response headers with type safety * * @example * ```ts * ctx.setHeader('Content-Type', 'application/json'); * ctx.setHeader('x-custom', 'value', true); // append * ``` */ setHeader: SetHeaders; writeHeaders(): void; writeStatus(): void; /** * End response with optional body * * @example * ```ts * ctx.end('Hello World'); * ``` */ end(body?: RecognizedString, cb?: () => void): void; /** * Send plain text response * Automatically sets Content-Type to text/plain with UTF-8 * * @example * ```ts * ctx.text('Hello World'); * ctx.status(200).text('Success'); * ``` */ text(body: string, status?: HttpStatusCode): void; /** * Send JSON response * Automatically stringifies and sets Content-Type * * @example * ```ts * ctx.json({ users: [...] }); * ctx.json({ error: 'Not Found' }, 404); * ``` */ json(body: any, status?: HttpStatusCode): void; /** * Send HTML response * Automatically sets Content-Type to text/html * * @example * ```ts * ctx.html('<h1>Welcome</h1>'); * ctx.html('<p>Error</p>', 500); * ``` */ html(body: string, status?: HttpStatusCode): void; /** * Redirect to another URL * Sends Location header with empty body (browser handles the redirect) * * @example * ```ts * ctx.redirect('/login'); * ctx.redirect('/home', 301); // Permanent redirect * ctx.redirect('https://example.com'); * ``` */ redirect(url: string, status?: RedirectStatusCode): void; } //#endregion export { Context };