-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.ts
More file actions
545 lines (486 loc) · 12.1 KB
/
index.ts
File metadata and controls
545 lines (486 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
543
544
545
/**
* Type Definitions
*
* This file contains all TypeScript types, interfaces, and enums used
* throughout the application. Having types centralized promotes consistency
* and makes the codebase easier to understand and maintain.
*/
/**
* ============================================
* Domain Entity Types
* ============================================
*/
/**
* Item Entity
* Represents an inventory item that can be reserved
*/
export interface Item {
/** Unique identifier (e.g., "item_1") */
id: string;
/** Human-readable name */
name: string;
/** Current available quantity for reservation */
availableQty: number;
}
/**
* Reservation Status
* All possible states for a reservation
*/
export type ReservationStatus =
| 'reserved' // Item is reserved (initial state)
| 'confirmed' // Reservation was confirmed (stock permanently allocated)
| 'cancelled' // Reservation was cancelled (stock returned)
| 'expired'; // Reservation expired (stock returned, 10 min timeout)
/**
* Reservation Entity
* Represents a reservation of an item by a user
*/
export interface Reservation {
/** Unique reservation identifier (e.g., "res_12345") */
id: string;
/** User who made the reservation */
userId: string;
/** Item being reserved */
itemId: string;
/** Quantity reserved */
qty: number;
/** Current status of the reservation */
status: ReservationStatus;
/** Unix timestamp when reservation expires (ms) */
expiresAt: number;
/** Unix timestamp when reservation was created (ms) */
createdAt: number;
/** Unix timestamp when reservation was last updated (ms) - optional */
updatedAt?: number;
}
/**
* Idempotency Key Entity
* Ensures that duplicate requests don't create duplicates
*/
export interface IdempotencyKey {
/** The idempotency key from request header */
key: string;
/** The route/method this key is for (e.g., "/reserve") */
route: string;
/** User who made the request */
userId: string;
/** Stored JSON response (cached for replay) */
responseJson: string;
/** Unix timestamp when this key was created (ms) */
createdAt: number;
}
/**
* ============================================
* API Request/Response Types
* ============================================
*/
/**
* Standard Success Response
* All successful API responses follow this structure
*/
export interface SuccessResponse<T> {
/** Indicates success */
ok: true;
/** Response payload */
data: T;
/** Optional metadata (pagination, etc.) */
meta?: ResponseMeta;
}
/**
* Standard Error Response
* All error API responses follow this structure
*/
export interface ErrorResponse {
/** Indicates failure */
ok: false;
/** Error details */
error: {
/** Machine-readable error code */
code: ErrorCode;
/** Human-readable error message */
message: string;
/** Optional additional error details */
details?: Record<string, unknown>;
/** Request ID for tracing */
requestId?: string;
};
}
/**
* Combined API Response Type
* Used for type-safe response handling
*/
export type ApiResponse<T> = SuccessResponse<T> | ErrorResponse;
/**
* Response Metadata
* Used for pagination and additional response information
*/
export interface ResponseMeta {
/** Current page number (1-indexed) */
page?: number;
/** Number of items per page */
pageSize?: number;
/** Total number of items */
total?: number;
/** Total number of pages */
totalPages?: number;
/** Has next page? */
hasNext?: boolean;
/** Has previous page? */
hasPrev?: boolean;
}
/**
* ============================================
* HTTP Error Codes
* ============================================
*/
/**
* Application-specific error codes
* These provide machine-readable error classifications
*/
export type ErrorCode =
// Validation Errors (400)
| 'VALIDATION_ERROR' // Request validation failed
| 'INVALID_BODY' // Request body is malformed
| 'INVALID_PARAMS' // URL/query parameters are invalid
| 'MISSING_FIELD' // Required field is missing
| 'INVALID_FORMAT' // Field format is incorrect (e.g., email)
// Authentication Errors (401)
| 'UNAUTHORIZED' // Authentication required
// Authorization Errors (403)
| 'FORBIDDEN' // Insufficient permissions
// Not Found Errors (404)
| 'NOT_FOUND' // Resource not found
| 'ITEM_NOT_FOUND' // Item doesn't exist
| 'RESERVATION_NOT_FOUND' // Reservation doesn't exist
| 'USER_NOT_FOUND' // User doesn't exist
| 'ROUTE_NOT_FOUND' // API endpoint doesn't exist
// Conflict Errors (409)
| 'CONFLICT' // Request conflicts with current state
| 'OUT_OF_STOCK' // Not enough inventory
| 'RESERVATION_EXPIRED' // Reservation has expired
| 'EXPIRED' // Resource has expired
| 'CANCELLED' // Resource has been cancelled
| 'ALREADY_CONFIRMED' // Reservation already confirmed
| 'ALREADY_CANCELLED' // Reservation already cancelled
| 'IDEMPOTENCY_KEY_CONFLICT' // Idempotency key already used with different params
// Validation Errors (422)
| 'UNPROCESSABLE_ENTITY' // Valid request but semantically incorrect
// Rate Limiting (429)
| 'RATE_LIMITED' // Too many requests
| 'SLOW_DOWN' // Gradual rate limiting
// Server Errors (500)
| 'INTERNAL_ERROR' // Unexpected server error
| 'DATABASE_ERROR' // Database operation failed
| 'CACHE_ERROR' // Cache operation failed
| 'IDEMPOTENCY_ERROR' // Idempotency check failed
| 'SERVICE_UNAVAILABLE'; // Service is down
/**
* ============================================
* HTTP Status Codes
* ============================================
*/
/**
* HTTP Status Code Enumeration
* Provides type-safe status codes
*/
export enum HttpStatus {
// Success
OK = 200,
CREATED = 201,
NO_CONTENT = 204,
// Redirection
NOT_MODIFIED = 304,
// Client Errors
BAD_REQUEST = 400,
UNAUTHORIZED = 401,
FORBIDDEN = 403,
NOT_FOUND = 404,
CONFLICT = 409,
UNPROCESSABLE_ENTITY = 422,
TOO_MANY_REQUESTS = 429,
// Server Errors
INTERNAL_SERVER_ERROR = 500,
NOT_IMPLEMENTED = 501,
BAD_GATEWAY = 502,
SERVICE_UNAVAILABLE = 503,
GATEWAY_TIMEOUT = 504,
}
/**
* ============================================
* API Request DTOs (Data Transfer Objects)
* ============================================
*/
/**
* Reserve Item Request
*/
export interface ReserveRequest {
userId: string;
itemId: string;
qty: number;
}
/**
* Confirm Reservation Request
*/
export interface ConfirmRequest {
userId: string;
reservationId: string;
}
/**
* Cancel Reservation Request
*/
export interface CancelRequest {
userId: string;
reservationId: string;
}
/**
* ============================================
* Pagination & Filtering Types
* ============================================
*/
/**
* Pagination Parameters
*/
export interface PaginationParams {
page?: number;
pageSize?: number;
}
/**
* Sorting Parameters
*/
export interface SortParams {
sortBy?: string;
sortOrder?: 'asc' | 'desc';
}
/**
* Filter Parameters
* Generic filter structure
*/
export interface FilterParams {
status?: ReservationStatus;
itemId?: string;
userId?: string;
}
/**
* ============================================
* Result Types for Domain Logic
* ============================================
*/
/**
* Result type for reserve operation
*/
export type ReserveResult =
| { kind: 'OK'; reservation: Reservation }
| { kind: 'NOT_FOUND' }
| { kind: 'OUT_OF_STOCK'; available: number }
| { kind: 'INVALID_QUANTITY'; min: number; max: number };
/**
* Result type for confirm operation
*/
export type ConfirmResult =
| { kind: 'OK' }
| { kind: 'NOT_FOUND' }
| { kind: 'EXPIRED' }
| { kind: 'ALREADY_CONFIRMED' }
| { kind: 'CANCELLED' };
/**
* Result type for cancel operation
*/
export type CancelResult =
| { kind: 'OK' }
| { kind: 'NOT_FOUND' }
| { kind: 'ALREADY_CANCELLED' }
| { kind: 'ALREADY_CONFIRMED' };
/**
* Result type for expire operation
*/
export type ExpireResult =
| { kind: 'OK'; expired: number }
| { kind: 'ERROR'; message: string };
/**
* ============================================
* Express Extension Types
* ============================================
*/
/**
* Extended Request with custom properties
* These are added by middleware
*/
export interface ExtendedRequest {
/** Unique request ID for tracing */
requestId: string;
/** Request start time for duration tracking */
startTime?: number;
/** User ID extracted from auth (future) */
userId?: string;
}
/**
* ============================================
* Database Types
* ============================================
*/
/**
* Database row types
* These match the SQLite schema exactly
*/
export type ItemRow = {
id: string;
name: string;
availableQty: number;
};
export type ReservationRow = {
id: string;
userId: string;
itemId: string;
qty: number;
status: string;
expiresAt: number;
createdAt: number;
updatedAt?: number;
};
export type IdempotencyKeyRow = {
key: string;
route: string;
userId: string;
responseJson: string;
createdAt: number;
};
/**
* ============================================
* Cache Types
* ============================================
*/
/**
* Cache entry structure
*/
export interface CacheEntry<T> {
/** Cached value */
value: T;
/** Expiration timestamp (Unix ms) */
expiresAt: number;
/** When this entry was created */
createdAt: number;
/** Number of times this entry was accessed */
hits: number;
}
/**
* Cache statistics
*/
export interface CacheStats {
/** Total number of entries */
size: number;
/** Total cache hits */
hits: number;
/** Total cache misses */
misses: number;
/** Hit rate (0-1) */
hitRate: number;
}
/**
* ============================================
* Logging Types
* ============================================
*/
/**
* Log levels
*/
export type LogLevel = 'trace' | 'debug' | 'info' | 'warn' | 'error' | 'fatal';
/**
* Structured log entry
*/
export interface LogEntry {
/** ISO timestamp */
timestamp: string;
/** Log level */
level: LogLevel;
/** Request ID for tracing */
requestId?: string;
/** Log message */
msg: string;
/** Additional context */
[key: string]: unknown;
}
/**
* HTTP-specific log entry
*/
export interface HttpLogEntry extends LogEntry {
/** HTTP method */
method: string;
/** Request path */
path: string;
/** HTTP status code */
status: number;
/** Request duration in ms */
durationMs: number;
/** User ID if authenticated */
userId?: string;
/** IP address */
ip?: string;
/** User agent */
userAgent?: string;
}
/**
* ============================================
* Health Check Types
* ============================================
*/
/**
* Health status
*/
export type HealthStatus = 'healthy' | 'degraded' | 'unhealthy';
/**
* Health check response
*/
export interface HealthCheckResponse {
/** Overall health status */
status: HealthStatus;
/** Current timestamp (ISO) */
timestamp: string;
/** Service uptime in seconds */
uptime: number;
/** Individual component health */
checks: {
database: { status: HealthStatus; latency?: number };
cache: { status: HealthStatus };
};
/** Version info */
version: string;
}
/**
* ============================================
* Metrics Types
* ============================================
*/
/**
* API metrics
*/
export interface ApiMetrics {
/** Total requests */
totalRequests: number;
/** Requests by status code */
requestsByStatus: Record<number, number>;
/** Requests by route */
requestsByRoute: Record<string, number>;
/** Average response time (ms) */
avgResponseTime: number;
/** P95 response time (ms) */
p95ResponseTime: number;
/** Error rate (0-1) */
errorRate: number;
}
/**
* ============================================
* Utility Types
* ============================================
/**
* Make specific properties optional
*/
export type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
/**
* Make specific properties required
*/
export type RequiredBy<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>;
/**
* Extract promise return type
*/
export type AsyncReturnType<T extends (...args: unknown[]) => Promise<unknown>> =
Awaited<ReturnType<T>>;