response-standardizes
Version:
Standardized HTTP status response handlers for Express.js
76 lines (68 loc) • 4.33 kB
TypeScript
import { Response } from "express";
export interface StandardResponse<T = any> {
success: boolean;
message: string;
data?: T;
error?: string;
statusCode: number;
}
export interface ResponseFunctions {
createResponse<T = any>(
res: Response | null,
status: number,
success: boolean,
message: string,
data?: T
): StandardResponse<T> | Response;
// 1xx Informational
ResponseContinue(res: Response | null, message?: string, data?: any): any;
ResponseSwitchingProtocols(res: Response | null, message?: string, data?: any): any;
ResponseProcessing(res: Response | null, message?: string, data?: any): any;
ResponseEarlyHints(res: Response | null, message?: string, data?: any): any;
// 2xx Success
ResponseSuccess(res: Response | null, message?: string, data?: any): any;
ResponseCreated(res: Response | null, message?: string, data?: any): any;
ResponseAccepted(res: Response | null, message?: string, data?: any): any;
ResponseNonAuthoritativeInformation(res: Response | null, message?: string, data?: any): any;
ResponseNoContent(res: Response | null, message?: string, data?: any): any;
ResponseResetContent(res: Response | null, message?: string, data?: any): any;
ResponsePartialContent(res: Response | null, message?: string, data?: any): any;
// 3xx Redirection
ResponseMultipleChoices(res: Response | null, message?: string, data?: any): any;
ResponseMovedPermanently(res: Response | null, message?: string, data?: any): any;
ResponseFound(res: Response | null, message?: string, data?: any): any;
ResponseSeeOther(res: Response | null, message?: string, data?: any): any;
ResponseNotModified(res: Response | null, message?: string, data?: any): any;
ResponseTemporaryRedirect(res: Response | null, message?: string, data?: any): any;
ResponsePermanentRedirect(res: Response | null, message?: string, data?: any): any;
// 4xx Client Errors
ResponseBadRequest(res: Response | null, message?: string, data?: any): any;
ResponseUnauthorized(res: Response | null, message?: string, data?: any): any;
ResponsePaymentRequired(res: Response | null, message?: string, data?: any): any;
ResponseForbidden(res: Response | null, message?: string, data?: any): any;
ResponseNotFound(res: Response | null, message?: string, data?: any): any;
ResponseMethodNotAllowed(res: Response | null, message?: string, data?: any): any;
ResponseNotAcceptable(res: Response | null, message?: string, data?: any): any;
ResponseProxyAuthenticationRequired(res: Response | null, message?: string, data?: any): any;
ResponseRequestTimeout(res: Response | null, message?: string, data?: any): any;
ResponseConflict(res: Response | null, message?: string, data?: any): any;
ResponseGone(res: Response | null, message?: string, data?: any): any;
ResponseLengthRequired(res: Response | null, message?: string, data?: any): any;
ResponsePreconditionFailed(res: Response | null, message?: string, data?: any): any;
ResponsePayloadTooLarge(res: Response | null, message?: string, data?: any): any;
ResponseUriTooLong(res: Response | null, message?: string, data?: any): any;
ResponseUnsupportedMediaType(res: Response | null, message?: string, data?: any): any;
ResponseRangeNotSatisfiable(res: Response | null, message?: string, data?: any): any;
ResponseExpectationFailed(res: Response | null, message?: string, data?: any): any;
ResponseImATeapot(res: Response | null, message?: string, data?: any): any;
ResponseUnprocessableEntity(res: Response | null, message?: string, data?: any): any;
ResponseTooManyRequests(res: Response | null, message?: string, data?: any): any;
// 5xx Server Errors
ResponseInternalServerError(res: Response | null, message?: string, data?: any): any;
ResponseNotImplemented(res: Response | null, message?: string, data?: any): any;
ResponseBadGateway(res: Response | null, message?: string, data?: any): any;
ResponseServiceUnavailable(res: Response | null, message?: string, data?: any): any;
ResponseGatewayTimeout(res: Response | null, message?: string, data?: any): any;
ResponseHttpVersionNotSupported(res: Response | null, message?: string, data?: any): any;
}
export function createAppLogger(): any;