/** * Filters for request body extraction * @packageDocumentation */ /// import { Filter } from "../filter"; import { IsInstanceOf, Tuple } from "../types"; import { ParsedUrlQuery } from "querystring"; /** * Extracts the raw request body as a `Buffer` */ export declare const raw: Filter<[Buffer]>; /** * Extracts the request body as a string */ export declare const text: Filter<[string]>; /** * Possible values inside a schema for JSON body validation and extraction */ export declare type JsonSchema = typeof String | typeof Number | typeof Boolean | JsonSchema[] | { [key: string]: JsonSchema; } | { optional: true; type: JsonSchema; }; /** * Schema for JSON body validation and extraction */ export declare type RootJsonSchema = Tuple | { [key: string]: JsonSchema; }; /** * Converts a [[`RootJsonSchema`]] to the type it represents * * Single element tuples will be translated to arrays. */ declare type JsonMap = S extends [unknown] ? JsonMap<[S[0], []]>[0][] : { [K in keyof S]: true extends IsInstanceOf ? string : true extends IsInstanceOf ? number : true extends IsInstanceOf ? boolean : S[K] extends RootJsonSchema ? JsonMap : S[K] extends { optional: true; type: infer T; } ? T extends JsonSchema ? true extends IsInstanceOf ? string | undefined : true extends IsInstanceOf ? number | undefined : true extends IsInstanceOf ? boolean | undefined : T extends RootJsonSchema ? JsonMap | undefined : never : never : never; }; /** * Validates and extracts the request body as JSON following the provided schema * * ```ts * // Will match { "name": "Do the laundry", "done": false } * // Generic type annotation not required, only present for clarity's sake * const todo: Filter<[ * { name: string; description?: string; done: boolean } * ]> = json({ * name: String, * description: { optional: true, type: String }, * done: Boolean, * }); * ``` * * @param schema - Schema * @param extra - Whether to include extra fields not diescribed in the schema in the extracted object */ export declare function json(schema: T, extra?: boolean): Filter<[JsonMap]>; /** * Extracts the request body as JSON */ export declare const anyJson: Filter<[unknown]>; /** * Extracts the request body as `application/x-www-form-urlencoded` */ export declare const form: Filter<[ParsedUrlQuery]>; /** * `multipart/form-data` file */ export interface FormDataFile { /** * File contents */ data: Buffer; /** * File name */ filename: string; /** * Encoding of the data */ encoding: string; /** * Mime type of the file */ mime: string; } /** * `multipart/form-data` field */ export interface FormDataField { /** * Field value */ value: string; /** * Encoding of the value */ encoding: string; /** * Mime type of the value */ mime: string; } /** * `multipart/form-data` */ export interface FormData { /** * Files indexed by their field name */ files: { [key: string]: FormDataFile[]; }; /** * Fields indexed by their name */ fields: { [key: string]: FormDataField[]; }; } /** * Extracts the request body as `multipart/form-data` */ export declare const multipart: Filter<[FormData]>; export {};