UNPKG

http-reply

Version:

A lightweight Node.js utility for sending consistent, standardized HTTP responses across your API endpoints

59 lines (57 loc) 2.1 kB
type ResponseLike = { status: (code: number) => any; json: (body: any) => any; type?: (mime: string) => any; send: (body?: any) => any; } | { code: (code: number) => any; send: (body?: any) => any; type?: (mime: string) => any; }; type AdapterFunction = (res: any, statusCode: number, payload: any) => any; interface HttpReplyConfig { includeTimestamp?: boolean; includeCode?: boolean; includeMessage?: boolean; includeError?: boolean; includeMetaData?: boolean; enableLogging?: boolean; stringify?: boolean; customFields?: Record<string, any>; dateFormat?: "iso" | "unix"; adapter?: AdapterFunction | null; } interface HttpReplyOptions { message?: string; data?: any; metaData?: Record<string, any>; code?: number; error?: any; extra?: Record<string, any>; } declare class HttpReply { private config; constructor(config?: HttpReplyConfig); private _sendResponse; response(res: ResponseLike, opts: HttpReplyOptions): any; success(res: ResponseLike, opts?: HttpReplyOptions): any; created(res: ResponseLike, opts?: HttpReplyOptions): any; accepted(res: ResponseLike, opts?: HttpReplyOptions): any; noContent(res: ResponseLike, opts?: { message?: string; code?: number; extra?: Record<string, any>; }): any; error(res: ResponseLike, opts?: HttpReplyOptions): any; rejected(res: ResponseLike, opts?: HttpReplyOptions): any; conflict(res: ResponseLike, opts?: HttpReplyOptions): any; badRequest(res: ResponseLike, opts?: HttpReplyOptions): any; tooManyRequests(res: ResponseLike, opts?: HttpReplyOptions): any; notImplemented(res: ResponseLike, opts?: HttpReplyOptions): any; serviceUnavailable(res: ResponseLike, opts?: HttpReplyOptions): any; forbidden(res: ResponseLike, opts?: HttpReplyOptions): any; unauthorized(res: ResponseLike, opts?: HttpReplyOptions): any; notFound(res: ResponseLike, opts?: HttpReplyOptions): any; static createMethodShortcuts(): void; } export { HttpReply, HttpReply as default };