-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathapi-response.utils.ts
More file actions
209 lines (191 loc) · 5.59 KB
/
Copy pathapi-response.utils.ts
File metadata and controls
209 lines (191 loc) · 5.59 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
// src/utils/api-response.utils.ts
// Shared API response formatters for consistent client-facing responses.
import { Response } from 'express';
import { ZodIssue } from 'zod';
import { ErrorCode, ErrorCodeType } from '../constants/error.constants';
import { requestContextStorage } from './als.utils';
import { serializeBigInt } from './bigint-serializer.utils';
/**
* Standard API error response shape.
*
* Every error returned by the API follows this structure so frontend
* clients can parse errors predictably.
*
* @example
* {
* success: false,
* error: {
* code: "VALIDATION_ERROR",
* message: "Email is required",
* details: [{ field: "email", message: "Required" }]
* }
* }
*/
interface ApiErrorResponse {
success: false;
requestId?: string;
error: {
code: string;
message: string;
details?: Array<{ field?: string; message: string }>;
};
}
/**
* Builds a structured error response body, embedding the request ID from the
* current async-local-storage context when available. The `requestId` field is
* omitted entirely when no context is active, keeping the shape clean for
* callers that run outside a request lifecycle (e.g. tests, scripts).
*
* Use this instead of constructing `ApiErrorResponse` literals directly so
* that request IDs are consistently included and can be correlated with server
* log entries.
*
* @param code - Machine-readable error code
* @param message - Human-readable error message
* @param details - Optional per-field validation details
* @returns Structured error response body ready to pass to `res.json()`
*
* @example
* res.status(400).json(buildErrorResponse(ErrorCode.VALIDATION_ERROR, 'Bad input'));
*/
export function buildErrorResponse(
code: ErrorCodeType,
message: string,
details?: Array<{ field?: string; message: string }>
): ApiErrorResponse {
const requestId = requestContextStorage.getStore()?.requestId;
const body: ApiErrorResponse = {
success: false,
...(requestId ? { requestId } : {}),
error: {
code,
message,
...(details && details.length > 0 ? { details } : {}),
},
};
return body;
}
/**
* Standard API success response shape.
*/
interface ApiSuccessResponse<T = unknown> {
success: true;
data: T;
message?: string;
}
/**
* Standard API pagination metadata.
*/
export interface PaginationMetadata {
page: number;
limit: number;
totalCount: number;
totalPages: number;
hasNextPage: boolean;
hasPrevPage: boolean;
}
/**
* Standard paginated API response shape.
*/
interface PaginatedResponse<T = unknown> {
success: true;
data: T[];
meta: PaginationMetadata;
message?: string;
}
export { ErrorCode, ErrorCodeType };
// ── Formatters ───────────────────────────────────────────────
/**
* Send a formatted error response.
*/
export function sendError(
res: Response,
statusCode: number,
code: ErrorCodeType,
message: string,
details?: Array<{ field?: string; message: string }>
): void {
res.setHeader('Content-Type', 'application/json');
res.status(statusCode).json(buildErrorResponse(code, message, details));
}
/**
* Send a formatted success response.
*/
export function sendSuccess<T>(
res: Response,
data: T,
statusCode = 200,
message?: string
): void {
const body: ApiSuccessResponse<T> = {
success: true,
data: serializeBigInt(data) as T,
...(message ? { message } : {}),
};
res.setHeader('Content-Type', 'application/json');
res.status(statusCode).json(body);
}
/**
* Send a formatted paginated success response.
*/
export function sendPaginatedSuccess<T>(
res: Response,
data: T[],
meta: PaginationMetadata,
statusCode = 200,
message?: string
): void {
const body: PaginatedResponse<T> = {
success: true,
data: serializeBigInt(data) as T[],
meta,
...(message ? { message } : {}),
};
res.setHeader('Content-Type', 'application/json');
res.status(statusCode).json(body);
}
// ── Convenience helpers ──────────────────────────────────────
/**
* Maps Zod issues to the standard `details` array used in error responses.
*
* @example
* const result = schema.safeParse(input);
* if (!result.success) {
* return sendValidationError(res, 'Invalid input', zodIssuesToDetails(result.error.issues));
* }
*/
export function zodIssuesToDetails(
issues: ZodIssue[]
): Array<{ field: string; message: string }> {
return issues.map(issue => ({
field: issue.path.join('.'),
message: issue.message,
}));
}
export function sendValidationError(
res: Response,
message: string,
details?: Array<{ field?: string; message: string }>
): void {
sendError(res, 400, ErrorCode.VALIDATION_ERROR, message, details);
}
export function sendNotFound(res: Response, resource: string): void {
sendError(res, 404, ErrorCode.NOT_FOUND, `${resource} not found`);
}
export function sendUnauthorized(
res: Response,
message = 'Unauthorized access',
details?: Array<{ field?: string; message: string }>
): void {
sendError(res, 401, ErrorCode.UNAUTHORIZED, message, details);
}
export function sendForbidden(
res: Response,
message = 'Access forbidden',
details?: Array<{ field?: string; message: string }>
): void {
sendError(res, 403, ErrorCode.FORBIDDEN, message, details);
}
export function sendConflict(res: Response, message: string): void {
sendError(res, 409, ErrorCode.CONFLICT, message);
}