express-pro-toolkit 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +426 -0
- package/index.js +28 -0
- package/package.json +55 -0
- package/src/AppError.js +175 -0
- package/src/asyncHandler.js +48 -0
- package/src/correlationId.js +58 -0
- package/src/errorHandler.js +153 -0
- package/src/httpStatus.js +48 -0
- package/src/notFoundHandler.js +34 -0
- package/src/requestLogger.js +119 -0
- package/src/responseHelpers.js +108 -0
- package/types/index.d.ts +169 -0
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { Request, Response, NextFunction, ErrorRequestHandler, RequestHandler } from 'express';
|
|
2
|
+
|
|
3
|
+
/* ─── AppError ────────────────────────────────────────────────────── */
|
|
4
|
+
|
|
5
|
+
export interface AppErrorOptions {
|
|
6
|
+
code?: string;
|
|
7
|
+
errors?: any[];
|
|
8
|
+
isOperational?: boolean;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export class AppError extends Error {
|
|
12
|
+
readonly name: 'AppError';
|
|
13
|
+
readonly statusCode: number;
|
|
14
|
+
readonly code?: string;
|
|
15
|
+
readonly errors: any[] | null;
|
|
16
|
+
readonly isOperational: boolean;
|
|
17
|
+
readonly timestamp: number;
|
|
18
|
+
|
|
19
|
+
constructor(message: string, statusCode?: number, options?: AppErrorOptions);
|
|
20
|
+
|
|
21
|
+
static badRequest(message?: string, options?: AppErrorOptions): AppError;
|
|
22
|
+
static unauthorized(message?: string, options?: AppErrorOptions): AppError;
|
|
23
|
+
static forbidden(message?: string, options?: AppErrorOptions): AppError;
|
|
24
|
+
static notFound(message?: string, options?: AppErrorOptions): AppError;
|
|
25
|
+
static conflict(message?: string, options?: AppErrorOptions): AppError;
|
|
26
|
+
static validation(errors: any[], message?: string): AppError;
|
|
27
|
+
static tooManyRequests(message?: string, options?: AppErrorOptions): AppError;
|
|
28
|
+
static internal(message?: string, options?: AppErrorOptions): AppError;
|
|
29
|
+
|
|
30
|
+
toJSON(): object;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/* ─── asyncHandler ────────────────────────────────────────────────── */
|
|
34
|
+
|
|
35
|
+
export function asyncHandler<T extends (...args: any[]) => any>(fn: T): T;
|
|
36
|
+
|
|
37
|
+
/* ─── errorHandler ────────────────────────────────────────────────── */
|
|
38
|
+
|
|
39
|
+
export interface ErrorLogPayload {
|
|
40
|
+
err: Error;
|
|
41
|
+
statusCode: number;
|
|
42
|
+
method: string;
|
|
43
|
+
url: string;
|
|
44
|
+
requestId: string | null;
|
|
45
|
+
message: string;
|
|
46
|
+
stack: string | null;
|
|
47
|
+
timestamp: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface ErrorHandlerOptions {
|
|
51
|
+
includeStack?: boolean;
|
|
52
|
+
onError?: (payload: ErrorLogPayload) => void;
|
|
53
|
+
defaultStatusCode?: number;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function createErrorHandler(options?: ErrorHandlerOptions): ErrorRequestHandler;
|
|
57
|
+
|
|
58
|
+
export const errorHandler: ErrorRequestHandler;
|
|
59
|
+
|
|
60
|
+
/* ─── requestLogger ───────────────────────────────────────────────── */
|
|
61
|
+
|
|
62
|
+
export interface RequestLogInfo {
|
|
63
|
+
method: string;
|
|
64
|
+
url: string;
|
|
65
|
+
status: number;
|
|
66
|
+
duration: number;
|
|
67
|
+
timestamp: string;
|
|
68
|
+
requestId: string | null;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export interface RequestLoggerOptions {
|
|
72
|
+
log?: (info: RequestLogInfo) => void;
|
|
73
|
+
skip?: (req: Request, res: Response) => boolean;
|
|
74
|
+
colorize?: boolean;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function createRequestLogger(options?: RequestLoggerOptions): RequestHandler;
|
|
78
|
+
|
|
79
|
+
export const requestLogger: RequestHandler;
|
|
80
|
+
|
|
81
|
+
/* ─── correlationId ───────────────────────────────────────────────── */
|
|
82
|
+
|
|
83
|
+
export interface CorrelationIdOptions {
|
|
84
|
+
header?: string;
|
|
85
|
+
generator?: () => string;
|
|
86
|
+
setResponseHeader?: boolean;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function correlationId(options?: CorrelationIdOptions): RequestHandler;
|
|
90
|
+
|
|
91
|
+
/* ─── notFoundHandler ─────────────────────────────────────────────── */
|
|
92
|
+
|
|
93
|
+
export interface NotFoundHandlerOptions {
|
|
94
|
+
message?: string;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function notFoundHandler(options?: NotFoundHandlerOptions): RequestHandler;
|
|
98
|
+
|
|
99
|
+
/* ─── responseHelpers ─────────────────────────────────────────────── */
|
|
100
|
+
|
|
101
|
+
export interface PaginationMeta {
|
|
102
|
+
page: number;
|
|
103
|
+
limit: number;
|
|
104
|
+
total: number;
|
|
105
|
+
totalPages: number;
|
|
106
|
+
hasNextPage: boolean;
|
|
107
|
+
hasPrevPage: boolean;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function sendSuccess(
|
|
111
|
+
res: Response,
|
|
112
|
+
data: any,
|
|
113
|
+
message?: string,
|
|
114
|
+
statusCode?: number,
|
|
115
|
+
meta?: object,
|
|
116
|
+
): void;
|
|
117
|
+
|
|
118
|
+
export function sendError(
|
|
119
|
+
res: Response,
|
|
120
|
+
message?: string,
|
|
121
|
+
statusCode?: number,
|
|
122
|
+
errors?: any[] | null,
|
|
123
|
+
code?: string,
|
|
124
|
+
): void;
|
|
125
|
+
|
|
126
|
+
export function sendPaginated(
|
|
127
|
+
res: Response,
|
|
128
|
+
items: any[],
|
|
129
|
+
pagination: { page: number; limit: number; total: number },
|
|
130
|
+
message?: string,
|
|
131
|
+
): void;
|
|
132
|
+
|
|
133
|
+
/* ─── httpStatus ──────────────────────────────────────────────────── */
|
|
134
|
+
|
|
135
|
+
export const httpStatus: {
|
|
136
|
+
readonly OK: 200;
|
|
137
|
+
readonly CREATED: 201;
|
|
138
|
+
readonly ACCEPTED: 202;
|
|
139
|
+
readonly NO_CONTENT: 204;
|
|
140
|
+
readonly MOVED_PERMANENTLY: 301;
|
|
141
|
+
readonly FOUND: 302;
|
|
142
|
+
readonly NOT_MODIFIED: 304;
|
|
143
|
+
readonly TEMPORARY_REDIRECT: 307;
|
|
144
|
+
readonly PERMANENT_REDIRECT: 308;
|
|
145
|
+
readonly BAD_REQUEST: 400;
|
|
146
|
+
readonly UNAUTHORIZED: 401;
|
|
147
|
+
readonly FORBIDDEN: 403;
|
|
148
|
+
readonly NOT_FOUND: 404;
|
|
149
|
+
readonly METHOD_NOT_ALLOWED: 405;
|
|
150
|
+
readonly CONFLICT: 409;
|
|
151
|
+
readonly GONE: 410;
|
|
152
|
+
readonly UNPROCESSABLE_ENTITY: 422;
|
|
153
|
+
readonly TOO_MANY_REQUESTS: 429;
|
|
154
|
+
readonly INTERNAL_SERVER_ERROR: 500;
|
|
155
|
+
readonly NOT_IMPLEMENTED: 501;
|
|
156
|
+
readonly BAD_GATEWAY: 502;
|
|
157
|
+
readonly SERVICE_UNAVAILABLE: 503;
|
|
158
|
+
readonly GATEWAY_TIMEOUT: 504;
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
/* ─── Express augmentation ────────────────────────────────────────── */
|
|
162
|
+
|
|
163
|
+
declare global {
|
|
164
|
+
namespace Express {
|
|
165
|
+
interface Request {
|
|
166
|
+
id?: string;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|