-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.ts
More file actions
542 lines (493 loc) · 12.1 KB
/
index.ts
File metadata and controls
542 lines (493 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
/**
* HTTP Response Utilities Module
*
* This module provides helper functions for sending consistent API responses.
* All responses follow a standard format for both success and error cases.
*
* Standard Success Response Format:
* {
* "ok": true,
* "data": { ... },
* "meta": { ... } // optional, for pagination etc.
* }
*
* Standard Error Response Format:
* {
* "ok": false,
* "error": {
* "code": "ERROR_CODE",
* "message": "Human-readable message",
* "details": { ... },
* "requestId": "uuid"
* }
* }
*
* Best Practices Implemented:
* - Consistent response structure across all endpoints
* - Type-safe response functions
* - Automatic request ID inclusion
* - Proper HTTP status codes
* - Detailed error information for debugging
*/
import type { Response, Request } from 'express';
import type { SuccessResponse, ErrorResponse, HttpStatus, ErrorCode, ResponseMeta } from '../types/index.js';
/**
* ============================================
* Response Types
* ============================================
*/
/**
* Error details object
*/
export interface ErrorDetails {
[key: string]: unknown;
}
/**
* Error response options
*/
export interface ErrorOptions {
/** Additional error details */
details?: ErrorDetails;
/** Override HTTP status code */
status?: number;
}
/**
* ============================================
* Success Response Helpers
* ============================================
*/
/**
* Send a successful response
*
* @param res - Express response object
* @param data - Response payload
* @param status - HTTP status code (default: 200)
* @param meta - Optional metadata (pagination, etc.)
*
* @example
* ```ts
* ok(res, { message: 'Success' }, 200);
* // { "ok": true, "data": { "message": "Success" } }
*
* ok(res, { items: [] }, 200, { page: 1, total: 100 });
* // { "ok": true, "data": { "items": [] }, "meta": { "page": 1, "total": 100 } }
* ```
*/
export function ok<T>(
res: Response,
data: T,
status: number = 200,
meta?: ResponseMeta
): Response {
const responseBody: SuccessResponse<T> = {
ok: true,
data,
};
if (meta) {
responseBody.meta = meta;
}
return res.status(status).json(responseBody);
}
/**
* Send a 201 Created response
* Use this when a new resource is created
*
* @example
* ```ts
* created(res, { id: 'res_123', ...reservation }, 201);
* ```
*/
export function created<T>(res: Response, data: T, meta?: ResponseMeta): Response {
return ok(res, data, 201, meta);
}
/**
* Send a 200 OK response with no data
* Use this for DELETE operations or updates where no data is returned
*
* @example
* ```ts
* noContent(res);
* ```
*/
export function noContent(res: Response): Response {
return res.status(204).send();
}
/**
* Send an accepted response
* Use this when a request is accepted for processing but not completed yet
*
* @example
* ```ts
* accepted(res, { jobId: 'job_123', status: 'processing' });
* ```
*/
export function accepted<T>(res: Response, data: T): Response {
return ok(res, data, 202);
}
/**
* ============================================
* Error Response Helpers
* ============================================
*/
/**
* Send an error response
*
* @param res - Express response object
* @param req - Express request object (for request ID)
* @param code - Application error code
* @param message - Human-readable error message
* @param status - HTTP status code (default: 400)
* @param details - Additional error details
*
* @example
* ```ts
* fail(res, req, 'VALIDATION_ERROR', 'Invalid input', 400, { field: 'email' });
* ```
*/
export function fail(
res: Response,
req: Request,
code: ErrorCode,
message: string,
status: number = 400,
details?: ErrorDetails
): Response {
const responseBody: ErrorResponse = {
ok: false,
error: {
code,
message,
},
};
// Add details if provided
if (details) {
responseBody.error.details = details;
}
// Add request ID if available
if ((req as any).requestId) {
responseBody.error.requestId = (req as any).requestId;
}
return res.status(status).json(responseBody);
}
/**
* 400 Bad Request - Invalid request from client
*
* @example
* ```ts
* badRequest(res, req, 'VALIDATION_ERROR', 'Email is required');
* ```
*/
export function badRequest(
res: Response,
req: Request,
code: ErrorCode = 'VALIDATION_ERROR',
message: string,
details?: ErrorDetails
): Response {
return fail(res, req, code, message, 400, details);
}
/**
* 401 Unauthorized - Authentication required
*
* @example
* ```ts
* unauthorized(res, req, 'Authentication required');
* ```
*/
export function unauthorized(res: Response, req: Request, message: string = 'Unauthorized'): Response {
return fail(res, req, 'UNAUTHORIZED', message, 401);
}
/**
* 403 Forbidden - Insufficient permissions
*
* @example
* ```ts
* forbidden(res, req, 'You do not have access to this resource');
* ```
*/
export function forbidden(res: Response, req: Request, message: string = 'Forbidden'): Response {
return fail(res, req, 'FORBIDDEN', message, 403);
}
/**
* 404 Not Found - Resource doesn't exist
*
* @example
* ```ts
* notFound(res, req, 'ITEM_NOT_FOUND', 'Item not found', { itemId: 'item_123' });
* ```
*/
export function notFound(
res: Response,
req: Request,
code: ErrorCode = 'NOT_FOUND',
message: string = 'Resource not found',
details?: ErrorDetails
): Response {
return fail(res, req, code, message, 404, details);
}
/**
* 409 Conflict - Request conflicts with current state
*
* Commonly used for:
* - Duplicate resources
* - Out of stock
* - Invalid state transitions
*
* @example
* ```ts
* conflict(res, req, 'OUT_OF_STOCK', 'Not enough items available', { available: 2 });
* ```
*/
export function conflict(
res: Response,
req: Request,
code: ErrorCode = 'CONFLICT',
message: string,
details?: ErrorDetails
): Response {
return fail(res, req, code, message, 409, details);
}
/**
* 422 Unprocessable Entity - Valid request but semantically incorrect
*
* @example
* ```ts
* unprocessable(res, req, 'Cannot confirm cancelled reservation');
* ```
*/
export function unprocessable(
res: Response,
req: Request,
code: ErrorCode = 'UNPROCESSABLE_ENTITY',
message: string,
details?: ErrorDetails
): Response {
return fail(res, req, code, message, 422, details);
}
/**
* 429 Too Many Requests - Rate limited
*
* @example
* ```ts
* tooManyRequests(res, req, 'RATE_LIMITED', 'Too many requests, try again later');
* ```
*/
export function tooManyRequests(
res: Response,
req: Request,
code: ErrorCode = 'RATE_LIMITED',
message: string = 'Too many requests',
details?: ErrorDetails
): Response {
return fail(res, req, code, message, 429, details);
}
/**
* 500 Internal Server Error - Unexpected server error
*
* @example
* ```ts
* internalError(res, req, 'DATABASE_ERROR', 'Failed to save reservation');
* ```
*/
export function internalError(
res: Response,
req: Request,
code: ErrorCode = 'INTERNAL_ERROR',
message: string = 'Internal server error',
details?: ErrorDetails
): Response {
return fail(res, req, code, message, 500, details);
}
/**
* 503 Service Unavailable - Service is down
*
* @example
* ```ts
* serviceUnavailable(res, req, 'Service temporarily unavailable');
* ```
*/
export function serviceUnavailable(
res: Response,
req: Request,
message: string = 'Service temporarily unavailable',
details?: ErrorDetails
): Response {
return fail(res, req, 'SERVICE_UNAVAILABLE', message, 503, details);
}
/**
* ============================================
* Specialized Error Helpers
* ============================================
*/
/**
* Validation error helper
* Formats Zod validation errors consistently
*
* @param res - Express response
* @param req - Express request
* @param issues - Zod error issues
* @param message - Override error message
*/
export function validationError(
res: Response,
req: Request,
issues: Array<{ field: string; message: string }>,
message: string = 'Request validation failed'
): Response {
return badRequest(res, req, 'VALIDATION_ERROR', message, { issues });
}
/**
* Database error helper
* Logs the error internally but returns generic message to client
*
* @param res - Express response
* @param req - Express request
* @param error - Original error (for logging)
* @param message - User-facing message
*/
export function databaseError(
res: Response,
req: Request,
error: unknown,
message: string = 'Database operation failed'
): Response {
// Log the actual error for debugging
console.error('[Database Error]', error);
// Return generic message to client
return internalError(res, req, 'DATABASE_ERROR', message);
}
/**
* ============================================
* Response Builder Pattern
* ============================================
*/
/**
* Response builder class for chaining response operations
* Useful for complex responses with multiple conditions
*
* @example
* ```ts
* return ResponseBuilder.success(res)
* .data({ id: 'res_123' })
* .status(201)
* .meta({ version: '1' })
* .send();
* ```
*/
export class ResponseBuilder {
constructor(
private res: Response,
private req?: Request
) {}
/**
* Create a success response builder
*/
static success(res: Response, req?: Request): ResponseBuilder {
return new ResponseBuilder(res, req);
}
/**
* Create an error response builder
*/
static error(res: Response, req: Request): ResponseBuilder {
return new ResponseBuilder(res, req);
}
/** Set response data */
data<T>(data: T): this {
this.res.locals.data = data;
return this;
}
/** Set response status */
status(status: number): this {
this.res.locals.status = status;
return this;
}
/** Set response metadata */
meta(meta: ResponseMeta): this {
this.res.locals.meta = meta;
return this;
}
/** Set error code */
code(code: ErrorCode): this {
this.res.locals.errorCode = code;
return this;
}
/** Set error message */
message(message: string): this {
this.res.locals.errorMessage = message;
return this;
}
/** Set error details */
details(details: ErrorDetails): this {
this.res.locals.errorDetails = details;
return this;
}
/** Send the response */
send(): Response {
const data = this.res.locals.data;
const status = this.res.locals.status || 200;
const meta = this.res.locals.meta;
const errorCode = this.res.locals.errorCode;
const errorMessage = this.res.locals.errorMessage;
const errorDetails = this.res.locals.errorDetails;
if (errorCode && this.req) {
return fail(this.res, this.req, errorCode, errorMessage || 'Error', status, errorDetails);
}
return ok(this.res, data, status, meta);
}
}
/**
* ============================================
* HTTP Status Code Helpers
* ============================================
*/
/**
* Check if status code is a success code (2xx)
*/
export function isSuccess(status: number): boolean {
return status >= 200 && status < 300;
}
/**
* Check if status code is a redirect code (3xx)
*/
export function isRedirect(status: number): boolean {
return status >= 300 && status < 400;
}
/**
* Check if status code is a client error code (4xx)
*/
export function isClientError(status: number): boolean {
return status >= 400 && status < 500;
}
/**
* Check if status code is a server error code (5xx)
*/
export function isServerError(status: number): boolean {
return status >= 500 && status < 600;
}
/**
* Check if status code is an error code (4xx or 5xx)
*/
export function isError(status: number): boolean {
return status >= 400;
}
/**
* Get status code name from number
*/
export function getStatusName(status: number): string {
const statusNames: Record<number, string> = {
200: 'OK',
201: 'Created',
204: 'No Content',
304: 'Not Modified',
400: 'Bad Request',
401: 'Unauthorized',
403: 'Forbidden',
404: 'Not Found',
409: 'Conflict',
422: 'Unprocessable Entity',
429: 'Too Many Requests',
500: 'Internal Server Error',
503: 'Service Unavailable',
};
return statusNames[status] || 'Unknown';
}