ezy-response
Version:
Simplified and standardized Express.js response handling with clean, one-liner syntax for success, error, custom, and stream-based responses
123 lines (106 loc) • 3.6 kB
TypeScript
import { Request, Response, NextFunction } from 'express';
declare module 'express-serve-static-core' {
interface Response {
success(options?: SuccessOptions): Response;
error(options?: ErrorOptions): Response;
custom(options: CustomOptions): Response;
paginated(options: PaginatedOptions): Response;
noContent(message?: string): Response;
created(options: CreatedOptions): Response;
validationError(options: ValidationErrorOptions): Response;
unauthorized(message?: string): Response;
forbidden(message?: string): Response;
notFound(message?: string): Response;
conflict(message?: string): Response;
rateLimitExceeded(options?: RateLimitOptions): Response;
streamFile(options: StreamFileOptions): Response;
serverError(message?: string, error?: any): Response;
}
}
export interface SuccessOptions {
message?: string;
data?: any;
meta?: Record<string, any>;
statusCode?: number;
}
export interface ErrorOptions {
message?: string;
error?: any;
code?: string;
statusCode?: number;
meta?: Record<string, any>;
}
export interface CustomOptions {
statusCode: number;
data: any;
headers?: Record<string, string>;
}
export interface PaginationMeta {
page: number;
limit: number;
total: number;
totalPages: number;
hasNext: boolean;
hasPrev: boolean;
}
export interface PaginatedOptions {
data: any;
pagination: PaginationMeta;
message?: string;
}
export interface CreatedOptions {
data: any;
message?: string;
location?: string;
}
export interface ValidationErrorOptions {
errors: Array<{
field?: string;
message: string;
code?: string;
}>;
message?: string;
}
export interface RateLimitOptions {
message?: string;
retryAfter?: number;
}
export interface StreamFileOptions {
filePath: string;
filename?: string;
mimeType?: string;
inline?: boolean;
}
export interface EzyResponseOptions {
enableCompression?: boolean;
}
export interface StandardResponse {
success: boolean;
message: string;
timestamp: string;
data?: any;
meta?: Record<string, any>;
code?: string;
error?: any;
}
export interface PaginatedResponse extends StandardResponse {
pagination: PaginationMeta;
}
declare function ezyResponse(
options?: EzyResponseOptions
): (req: Request, res: Response, next: NextFunction) => void;
export default ezyResponse;
export declare const success: (this: Response, options?: SuccessOptions) => Response;
export declare const error: (this: Response, options?: ErrorOptions) => Response;
export declare const custom: (this: Response, options: CustomOptions) => Response;
export declare const paginated: (this: Response, options: PaginatedOptions) => Response;
export declare const noContent: (this: Response, message?: string) => Response;
export declare const created: (this: Response, options: CreatedOptions) => Response;
export declare const validationError: (this: Response, options: ValidationErrorOptions) => Response;
export declare const unauthorized: (this: Response, message?: string) => Response;
export declare const forbidden: (this: Response, message?: string) => Response;
export declare const notFound: (this: Response, message?: string) => Response;
export declare const conflict: (this: Response, message?: string) => Response;
export declare const rateLimitExceeded: (this: Response, options?: RateLimitOptions) => Response;
export declare const streamFile: (this: Response, options: StreamFileOptions) => Response;
export declare const serverError: (this: Response, message?: string, error?: any) => Response;