/** * @module HttpRouter */ import { type HttpMethod, type HttpReqSchemas, type IHttpReq, type RawFormData, type MultiStringInputs, type StringInputs, type FileDef, type FileInputs, type IValidatedHttpReq, type ReqInputs } from "../../http-router/contracts/_module.js"; /** * Configuration for creating an {@link HttpReq} from a standard Web API `Request`. * * Bundles the raw `Request` object with optional validation schemas and * pre-resolved path parameters so the resulting `HttpReq` can apply * type-safe validation on demand. * * IMPORT_PATH: `"@daiso-tech/core/http-router"` * @group Implementations */ export type FromWebReqSettings = { /** * The raw Web API `Request` object to wrap. */ request: Request; /** * @internal */ _rawParams?: StringInputs; }; /** * A variant of {@link TestReqBody} representing a JSON request body. * * Use this when you want to simulate an `application/json` payload * in tests. The `data` field accepts any value; it will be serialized * via `JSON.stringify` when the test {@link HttpReq} is constructed. * * IMPORT_PATH: `"@daiso-tech/core/http-router"` * @group Implementations */ export type TestReqJsonBody = { type: "application/json"; data: unknown; }; /** * A variant of {@link TestReqBody} representing a URL-encoded form body. * * Use this when you want to simulate an * `application/x-www-form-urlencoded` payload in tests. The `data` * field is a {@link StringInputs} record of form field name/value pairs. * * IMPORT_PATH: `"@daiso-tech/core/http-router"` * @group Implementations */ export type TestReqUrlEncodedBody = { type: "application/x-www-form-urlencoded"; data: StringInputs; }; /** * A variant of {@link TestReqBody} representing a multipart form data body. * * Use this when you want to simulate a `multipart/form-data` payload * in tests. The `data` object may contain: * - `fields` — a {@link StringInputs} record of form field name/value pairs. * - `files` — a record mapping file field names to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer | ArrayBuffer} * instances representing raw file content. * * IMPORT_PATH: `"@daiso-tech/core/http-router"` * @group Implementations */ export type TestReqMultipartFormDataBody = { type: "multipart/form-data"; data: { fields?: StringInputs; /** * A record mapping file field names to their raw content. * Accepts a single `ArrayBuffer` for single-file fields or * `ArrayBuffer[]` for multi-file fields. */ files?: Partial>>; }; }; /** * A variant of {@link TestReqBody} representing a custom/opaque request body. * * Use this when you want to simulate an arbitrary payload that does not * fit into the JSON, URL-encoded, or multipart categories. The `data` * field is passed through as-is when the test {@link HttpReq} is constructed. * * IMPORT_PATH: `"@daiso-tech/core/http-router"` * @group Implementations */ export type TestReqCustom = { type: "custom"; data: unknown; }; /** * A discriminated union representing the body of an HTTP request for * testing purposes. * * Each variant maps to a content type, allowing the body data to be * typed according to the selected format. * * IMPORT_PATH: `"@daiso-tech/core/http-router"` * @group Implementations */ export type TestReqBody = TestReqJsonBody | TestReqUrlEncodedBody | TestReqMultipartFormDataBody | TestReqCustom; /** * Configuration for creating an {@link HttpReq} instance for testing purposes. * * Allows you to construct a fully synthetic HTTP request by providing * mock values for every request data source — path parameters, search * parameters, headers, cookies, and body — along with optional validation * schemas. This is the primary way to create an {@link IHttpReq} in unit * tests without needing a real Web API `Request` object. * * @typeParam TReqJson - The type of the parsed JSON body. * @typeParam TReqFields - The type of the parsed form fields. * @typeParam TReqParams - The type of the parsed path parameters. * @typeParam TReqSearchParams - The type of the parsed query parameters. * @typeParam TReqHeaders - The type of the parsed headers. * @typeParam TReqFiles - The expected file upload definitions. * @typeParam TCookieData - A record mapping cookie names to their value types. * * IMPORT_PATH: `"@daiso-tech/core/http-router"` * @group Implementations */ export type TestReqSettings = { method: HttpMethod; /** * @default "https://test.local" */ hostname?: string; url: string; /** * Mock path parameters (e.g. `{ id: "42" }` for a route like `/users/:id`). */ params?: StringInputs; /** * Mock query/search parameters (e.g. `{ page: "1", tags: ["a", "b"] }`). */ searchParams?: MultiStringInputs; /** * Mock HTTP request headers. */ headers?: StringInputs; /** * Mock cookie values. */ cookies?: StringInputs; /** * Mock request body, structured by content type. * * Use the discriminated union variants to specify JSON, URL-encoded * form data, multipart form data (with optional file uploads), or a * custom payload. */ body?: TestReqBody; }; /** * The default implementation of {@link IHttpReq}. * * Provides typed access to all request data sources — JSON body, form fields, * path parameters, query parameters, headers, and the raw body. * * Use {@link HttpReq.fromWebReq} to create an instance from a standard Web API `Request` object. * * @typeParam TReqMethod - The HTTP method type. * @typeParam TReqJson - The type of the parsed JSON body. * @typeParam TReqFields - The type of the parsed form fields. * @typeParam TReqParams - The type of the parsed path parameters. * @typeParam TReqSearchParams - The type of the parsed query parameters. * @typeParam TReqHeaders - The type of the parsed headers. * @typeParam TReqFiles - The expected file upload definitions. * @typeParam TCookieData - A record mapping cookie names to their value types. * * IMPORT_PATH: `"@daiso-tech/core/http-router"` * @group Implementations */ export declare class HttpReq implements IHttpReq { private readonly request; private readonly rawParamsData; /** * Creates an `HttpReq` instance from a standard Web API `Request` object. * * @param req - The source `Request` object. * @returns A new `IHttpReq` instance wrapping the request. */ static fromWebReq(settings: FromWebReqSettings): IHttpReq; private static deserializeCookies; private static serializeCookies; private static buildTestUrl; private static buildTestHeaders; private static buildJsonTestBody; private static buildUrlEncodedTestBody; private static buildMultipartFormDataTestBody; private static buildTestBody; /** * Creates an `HttpReq` instance with synthetic data for testing. * * Unlike {@link HttpReq.fromWebReq}, this method does not require a * real Web API `Request` object. Instead, you provide mock values for * path parameters, search parameters, headers, cookies, and body * through {@link TestReqSettings}. The resulting `HttpReq` behaves * identically to one created from a real request, including validation * support. * * @param settings - Configuration for the test request, including mock * data and optional validation schemas. * @returns A new `IHttpReq` instance suitable for testing. */ static test(settings: TestReqSettings): IHttpReq; private constructor(); [Symbol.asyncIterator](): AsyncIterator; text(): Promise; get readableStream(): ReadableStream | null; get signal(): AbortSignal; rawCookies(): StringInputs; get method(): HttpMethod; get url(): string; rawJson(): Promise; rawFormData(): Promise; rawParams(): StringInputs; rawSearchParams(): MultiStringInputs; rawHeaders(): StringInputs; arrayBuffer(): Promise; bytes(): Promise; blob(): Promise; get webReq(): Request; withSchema>, TReqParams extends ReqInputs = Partial>, TReqSearchParams extends ReqInputs = Partial>, TReqHeaders extends ReqInputs = Partial>, TReqFiles extends FileInputs = Partial>, TCookieData extends StringInputs = Partial>>(schemas: HttpReqSchemas): IValidatedHttpReq; }