milliparsec
Version:
tiniest body parser in the universe
72 lines (71 loc) • 2.54 kB
TypeScript
import { Buffer } from 'node:buffer';
import type { IncomingMessage, ServerResponse as Response } from 'node:http';
type NextFunction = (err?: any) => void;
/**
* Request extension with a body
*/
export type ReqWithBody<T = any> = IncomingMessage & {
body?: T;
};
export declare const hasBody: (method: string) => boolean;
export type LimitErrorFn = (limit: number) => Error;
export type ParserOptions = Partial<{
/**
* Limit payload size (in bytes)
* @default '100KB'
*/
payloadLimit: number;
/**
* Custom error function for payload limit
*/
payloadLimitErrorFn: LimitErrorFn;
}>;
export declare const p: <T = any>(fn: (body: Buffer) => void, payloadLimit?: number, payloadLimitErrorFn?: LimitErrorFn) => (req: ReqWithBody<T>, _res: Response, next?: (err?: any) => void) => Promise<void>;
/**
* Parse payload with a custom function
* @param fn
*/
declare const custom: <T = any>(fn: (body: Buffer) => any) => (req: ReqWithBody, _res: Response, next?: NextFunction) => Promise<void>;
/**
* Parse JSON payload
* @param options
*/
declare const json: ({ payloadLimit, payloadLimitErrorFn }?: ParserOptions) => (req: ReqWithBody, res: Response, next?: NextFunction) => Promise<void>;
/**
* Parse raw payload
* @param options
*/
declare const raw: ({ payloadLimit, payloadLimitErrorFn }?: ParserOptions) => (req: ReqWithBody, _res: Response, next?: NextFunction) => Promise<void>;
/**
* Stringify request payload
* @param param0
* @returns
*/
declare const text: ({ payloadLimit, payloadLimitErrorFn }?: ParserOptions) => (req: ReqWithBody, _res: Response, next?: NextFunction) => Promise<void>;
/**
* Parse urlencoded payload
* @param options
*/
declare const urlencoded: ({ payloadLimit, payloadLimitErrorFn }?: ParserOptions) => (req: ReqWithBody, _res: Response, next?: NextFunction) => Promise<void>;
type MultipartOptions = Partial<{
/**
* Limit number of files
*/
fileCountLimit: number;
/**
* Limit file size (in bytes)
*/
fileSizeLimit: number;
/**
* Custom error function for file size limit
*/
fileSizeLimitErrorFn: LimitErrorFn;
}>;
/**
* Parse multipart form data (supports files as well)
*
* Does not restrict total payload size by default.
* @param options
*/
declare const multipart: ({ payloadLimit, payloadLimitErrorFn, ...opts }?: MultipartOptions & ParserOptions) => (req: ReqWithBody, res: Response, next?: NextFunction) => Promise<void>;
export { custom, json, raw, text, urlencoded, multipart };