@sigiljs/sigil
Version:
TypeScript-first Node.js HTTP framework offering schema-driven routing, modifier-based middleware, plugin extensibility, and flexible response templating
58 lines (57 loc) • 2.05 kB
TypeScript
/**
* Represents the body of an incoming HTTP request.
* Provides accessors for JSON, text, Buffer, Blob, and URLSearchParams representations.
*
* @template Json expected JSON type (default `any`).
*/
export default class IncomingBody<Json = any> {
#private;
/** Size of the body in bytes. */
readonly size: number;
/**
* Constructs an IncomingBody instance.
*
* @param buffer original body content. Can be a Buffer, string, object, or null.
* Objects will be converted into strings.
*/
constructor(buffer: Buffer | string | object | null);
/**
* Parses and returns the body as JSON.
* Caches parsed value for future calls.
*
* @template T type to parse into when `Json` generic is not a record.
* @template N whether the result should be non-nullable.
* @template R resolved return type based on `Json` and `T`.
* @param nonNullable if `true`, enforces non-nullability in parsing.
* @returns parsed JSON of type `R` or `null` if parsing fails and `nonNullable` is `false`.
*/
json<T extends Record<string, any> = Record<string, any>, N extends boolean = false, R = Json extends Record<any, any> ? Json : T>(nonNullable?: N): N extends true ? R : (R | null);
/**
* Returns the body decoded as a UTF-8 string.
* Caches the result for future calls.
*
* @returns body text.
*/
text(): string;
/**
* Returns the raw Buffer of the body.
*
* @returns body Buffer.
*/
buffer(): Buffer;
/**
* Returns the body as a Blob with the specified MIME type.
* Caches the result for future calls.
*
* @param mimeType MIME type for the Blob (e.g., "application/json").
* @returns blob representing the body.
*/
blob(mimeType: string): Blob;
/**
* Parses the body as URL search parameters.
* Caches the result for future calls.
*
* @returns URLSearchParams instance built from the body string.
*/
urlSearchParams(): URLSearchParams;
}