ufiber
Version:
Next-gen webserver for node-js developer
194 lines (193 loc) • 4.65 kB
TypeScript
import { kCtxReq, kMatch } from "../consts.js";
import { CustomHeader, Handler, RequestHeader, Result, RouterRoute } from "../types.js";
import { UwsReadable } from "./readable.js";
import { FormData, FormOption } from "../utils/body.js";
import { HttpRequest, HttpResponse } from "../../uws";
//#region src/http/request.d.ts
type ReqData = {
body: Partial<{
json: any;
text: string;
blob: Blob;
arrayBuffer: ArrayBuffer;
formData: FormData;
}>;
headers: Record<string, any>;
routeIndex: number;
};
type Options = {
req: HttpRequest;
res: HttpResponse;
isSSL: boolean;
methods?: string[];
bodyLimit?: number;
};
declare class Request {
#private;
readonly req: HttpRequest;
readonly res: HttpResponse;
/**
* The URL pathname (without host or query).
*
* Always begins with `/`.
*
* @example
* "/users/15"
*/
path: string;
/**
* HTTP method in uppercase (e.g. `POST`, `GET`)
*/
method: string;
/**
* The raw query string including the leading `?`, or empty string if none.
*
* @example
* "?page=2&limit=10"
* ""
*/
urlQuery: string;
isSSL: boolean;
[kCtxReq]: ReqData;
[kMatch]: Result<[Handler, RouterRoute]>;
constructor({
req,
res,
bodyLimit,
methods,
isSSL
}: Options);
protected destroy(): void;
/**
* The full request URL including protocol, host, pathname, and query.
*
* @example
* "http://localhost:3000/users/15?active=true"
*/
get url(): string;
/**
* Returns a single query param or the full query object.
*
* @example
* req.query('q') // "hello"
* req.query() // { q: "hello", page: "1" }
*/
query(): Record<string, string>;
query(key: string): string | undefined;
/**
* Returns array values for a query param or all params as arrays.
*
* @example
* req.queries('tag') // ["a", "b"]
* req.queries() // { tag: ["a","b"] }
*/
queries(): Record<string, string[]>;
queries(key: string): string[] | undefined;
/**
* Returns the value of a named route parameter.
*
* @example
* ```ts
* ctx.param('id'); // "123"
* ```
*/
param(field: string): string | undefined;
/**
* Returns an object containing all route parameters for the current route.
*
* @example
* ```ts
* ctx.params(); // { id: "123", name: "John" }
* ```
*/
params(): Record<string, string>;
/**
* Returns raw header pairs in their original order.
*/
get rawHeaders(): string[];
/**
* Returns a specific request header,
* or all headers if no name is provided.
*
* @example
* ```ts
* req.getHeader('content-type'); // => "application/json"
* req.getHeader(); // => { host: "localhost:3000", ... }
* ```
*/
getHeader(field: RequestHeader): string | undefined;
getHeader(field: string): string | undefined;
getHeader(): Record<RequestHeader | (string & CustomHeader), string>;
/**
* Returns a readable stream of the request body.
*
* @example
* ```ts
* const file = fs.createWriteStream('upload.bin');
* ctx.stream.pipe(file);
* ```
*/
get stream(): UwsReadable;
/**
* Reads and returns the request body as a UTF-8 string.
*
* @example
* ```ts
* const text = await ctx.textParse();
* console.log('Body:', text);
* ```
*/
textParse(): Promise<string>;
/**
* Reads and returns the request body as an ArrayBuffer.
*
* @example
* ```ts
* const arrayBuffer = await ctx.arrayBuffer();
* console.log(arrayBuffer.byteLength);
* ```
*/
arrayBuffer(): Promise<ArrayBuffer>;
/**
* Reads the body as a Blob (Node 18+).
*
* @example
* ```ts
* const blob = await ctx.blobParse();
* console.log('Blob size:', blob.size);
* ```
*
* @returns {Promise<Blob>}
*/
blobParse(): Promise<Blob>;
/**
* Parses and returns the request body as JSON.
*
* @template T
* @returns {Promise<T>} The parsed JSON body.
* @throws {SyntaxError} If body is empty or malformed.
*
* @example
* ```ts
* const data = await ctx.jsonParse();
* console.log(data.user, data.email);
* ```
*/
jsonParse<T = any>(): Promise<T>;
/**
* Parses form submissions (URL-encoded or multipart/form-data).
*
* @param {FormOption} [options] - Optional multipart parser settings.
* @returns {Promise<FormData>}
* @throws {TypeError} If content type is unsupported.
*
* @example
* ```ts
* const form = await ctx.formParse();
* console.log(form.get('username'));
* ```
*/
formParse(options?: FormOption): Promise<FormData>;
}
//#endregion
export { Request };