@aifabrix/miso-client 2.0.0 → 2.1.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/README.md +231 -2
- package/dist/express/async-handler.d.ts +41 -0
- package/dist/express/async-handler.d.ts.map +1 -0
- package/dist/express/async-handler.js +58 -0
- package/dist/express/async-handler.js.map +1 -0
- package/dist/express/encryption.d.ts +29 -0
- package/dist/express/encryption.d.ts.map +1 -0
- package/dist/express/encryption.js +95 -0
- package/dist/express/encryption.js.map +1 -0
- package/dist/express/error-handler.d.ts +23 -0
- package/dist/express/error-handler.d.ts.map +1 -0
- package/dist/express/error-handler.js +140 -0
- package/dist/express/error-handler.js.map +1 -0
- package/dist/express/error-response.d.ts +55 -0
- package/dist/express/error-response.d.ts.map +1 -0
- package/dist/express/error-response.js +95 -0
- package/dist/express/error-response.js.map +1 -0
- package/dist/express/error-types.d.ts +46 -0
- package/dist/express/error-types.d.ts.map +1 -0
- package/dist/express/error-types.js +93 -0
- package/dist/express/error-types.js.map +1 -0
- package/dist/express/index.d.ts +14 -0
- package/dist/express/index.d.ts.map +1 -0
- package/dist/express/index.js +39 -0
- package/dist/express/index.js.map +1 -0
- package/dist/express/response-helper.d.ts +67 -0
- package/dist/express/response-helper.d.ts.map +1 -0
- package/dist/express/response-helper.js +83 -0
- package/dist/express/response-helper.js.map +1 -0
- package/dist/express/response-middleware.d.ts +18 -0
- package/dist/express/response-middleware.d.ts.map +1 -0
- package/dist/express/response-middleware.js +29 -0
- package/dist/express/response-middleware.js.map +1 -0
- package/dist/express/validation-helper.d.ts +66 -0
- package/dist/express/validation-helper.d.ts.map +1 -0
- package/dist/express/validation-helper.js +102 -0
- package/dist/express/validation-helper.js.map +1 -0
- package/dist/index.d.ts +23 -29
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +22 -23
- package/dist/index.js.map +1 -1
- package/dist/services/auth.service.d.ts +3 -3
- package/dist/services/auth.service.d.ts.map +1 -1
- package/dist/services/auth.service.js +24 -18
- package/dist/services/auth.service.js.map +1 -1
- package/dist/services/cache.service.d.ts +1 -1
- package/dist/services/cache.service.d.ts.map +1 -1
- package/dist/services/cache.service.js +1 -1
- package/dist/services/cache.service.js.map +1 -1
- package/dist/services/logger.service.d.ts +3 -3
- package/dist/services/logger.service.d.ts.map +1 -1
- package/dist/services/logger.service.js +32 -25
- package/dist/services/logger.service.js.map +1 -1
- package/dist/services/permission.service.d.ts +3 -3
- package/dist/services/permission.service.d.ts.map +1 -1
- package/dist/services/permission.service.js +9 -6
- package/dist/services/permission.service.js.map +1 -1
- package/dist/services/redis.service.d.ts +1 -1
- package/dist/services/redis.service.js +10 -10
- package/dist/services/role.service.d.ts +3 -3
- package/dist/services/role.service.d.ts.map +1 -1
- package/dist/services/role.service.js +8 -5
- package/dist/services/role.service.js.map +1 -1
- package/dist/types/config.types.d.ts +4 -4
- package/dist/types/config.types.d.ts.map +1 -1
- package/dist/types/config.types.js +6 -5
- package/dist/types/config.types.js.map +1 -1
- package/dist/types/filter.types.d.ts +1 -1
- package/dist/types/filter.types.d.ts.map +1 -1
- package/dist/types/filter.types.js +2 -2
- package/dist/types/filter.types.js.map +1 -1
- package/dist/types/sort.types.d.ts +1 -1
- package/dist/utils/audit-log-queue.d.ts +4 -4
- package/dist/utils/audit-log-queue.d.ts.map +1 -1
- package/dist/utils/audit-log-queue.js +11 -11
- package/dist/utils/audit-log-queue.js.map +1 -1
- package/dist/utils/auth-strategy.d.ts +1 -1
- package/dist/utils/auth-strategy.d.ts.map +1 -1
- package/dist/utils/auth-strategy.js +19 -17
- package/dist/utils/auth-strategy.js.map +1 -1
- package/dist/utils/config-loader.d.ts +2 -2
- package/dist/utils/config-loader.d.ts.map +1 -1
- package/dist/utils/config-loader.js +11 -10
- package/dist/utils/config-loader.js.map +1 -1
- package/dist/utils/data-masker.d.ts.map +1 -1
- package/dist/utils/data-masker.js +7 -7
- package/dist/utils/data-masker.js.map +1 -1
- package/dist/utils/errors.d.ts +2 -2
- package/dist/utils/errors.d.ts.map +1 -1
- package/dist/utils/errors.js +37 -30
- package/dist/utils/errors.js.map +1 -1
- package/dist/utils/filter.utils.d.ts +2 -2
- package/dist/utils/filter.utils.d.ts.map +1 -1
- package/dist/utils/filter.utils.js +24 -18
- package/dist/utils/filter.utils.js.map +1 -1
- package/dist/utils/http-client-audit.d.ts +4 -4
- package/dist/utils/http-client-audit.d.ts.map +1 -1
- package/dist/utils/http-client-audit.js +30 -22
- package/dist/utils/http-client-audit.js.map +1 -1
- package/dist/utils/http-client-masking.d.ts +1 -1
- package/dist/utils/http-client-masking.d.ts.map +1 -1
- package/dist/utils/http-client-masking.js +31 -21
- package/dist/utils/http-client-masking.js.map +1 -1
- package/dist/utils/http-client-metadata.d.ts +2 -2
- package/dist/utils/http-client-metadata.d.ts.map +1 -1
- package/dist/utils/http-client-metadata.js +7 -7
- package/dist/utils/http-client-metadata.js.map +1 -1
- package/dist/utils/http-client.d.ts +6 -6
- package/dist/utils/http-client.d.ts.map +1 -1
- package/dist/utils/http-client.js +8 -9
- package/dist/utils/http-client.js.map +1 -1
- package/dist/utils/internal-http-client.d.ts +5 -5
- package/dist/utils/internal-http-client.d.ts.map +1 -1
- package/dist/utils/internal-http-client.js +48 -42
- package/dist/utils/internal-http-client.js.map +1 -1
- package/dist/utils/pagination.utils.d.ts +1 -1
- package/dist/utils/pagination.utils.d.ts.map +1 -1
- package/dist/utils/pagination.utils.js +3 -3
- package/dist/utils/pagination.utils.js.map +1 -1
- package/dist/utils/sensitive-fields.loader.d.ts.map +1 -1
- package/dist/utils/sensitive-fields.loader.js +77 -60
- package/dist/utils/sensitive-fields.loader.js.map +1 -1
- package/dist/utils/sort.utils.d.ts +14 -3
- package/dist/utils/sort.utils.d.ts.map +1 -1
- package/dist/utils/sort.utils.js +73 -9
- package/dist/utils/sort.utils.js.map +1 -1
- package/package.json +11 -2
- package/dist/services/encryption.service.d.ts +0 -32
- package/dist/services/encryption.service.d.ts.map +0 -1
- package/dist/services/encryption.service.js +0 -135
- package/dist/services/encryption.service.js.map +0 -1
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Generic Error Handler Utility
|
|
4
|
+
* Provides standardized error handling for all route handlers
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.setErrorLogger = setErrorLogger;
|
|
8
|
+
exports.handleRouteError = handleRouteError;
|
|
9
|
+
const error_types_1 = require("./error-types");
|
|
10
|
+
const error_response_1 = require("./error-response");
|
|
11
|
+
// Allow dependency injection for custom logger
|
|
12
|
+
let customErrorLogger = null;
|
|
13
|
+
/**
|
|
14
|
+
* Set custom error logger for the application
|
|
15
|
+
* @param logger - Custom logger implementing ErrorLogger interface, or null to use stderr
|
|
16
|
+
*/
|
|
17
|
+
function setErrorLogger(logger) {
|
|
18
|
+
customErrorLogger = logger;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Map error to appropriate HTTP status code
|
|
22
|
+
*/
|
|
23
|
+
function mapErrorToStatusCode(error) {
|
|
24
|
+
// AppError uses its statusCode
|
|
25
|
+
if (error instanceof error_types_1.AppError) {
|
|
26
|
+
return error.statusCode;
|
|
27
|
+
}
|
|
28
|
+
// Prisma errors (duck typing - no import needed)
|
|
29
|
+
if (error && typeof error === "object" && "code" in error) {
|
|
30
|
+
const code = error.code;
|
|
31
|
+
// Support Prisma error codes without importing Prisma
|
|
32
|
+
if (code === "P2002") {
|
|
33
|
+
// Unique constraint violation
|
|
34
|
+
return 409;
|
|
35
|
+
}
|
|
36
|
+
if (code === "P2025") {
|
|
37
|
+
// Record not found
|
|
38
|
+
return 404;
|
|
39
|
+
}
|
|
40
|
+
if (code === "P2003") {
|
|
41
|
+
// Foreign key constraint violation
|
|
42
|
+
return 400;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
// Error message patterns
|
|
46
|
+
const errorMessage = error instanceof Error ? error.message : String(error || "Unknown error");
|
|
47
|
+
if (errorMessage.toLowerCase().includes("not found")) {
|
|
48
|
+
return 404;
|
|
49
|
+
}
|
|
50
|
+
if (errorMessage.toLowerCase().includes("already exists") ||
|
|
51
|
+
errorMessage.toLowerCase().includes("duplicate")) {
|
|
52
|
+
return 409;
|
|
53
|
+
}
|
|
54
|
+
if (errorMessage.toLowerCase().includes("validation") ||
|
|
55
|
+
errorMessage.toLowerCase().includes("invalid")) {
|
|
56
|
+
return 400;
|
|
57
|
+
}
|
|
58
|
+
if (errorMessage.toLowerCase().includes("unauthorized") ||
|
|
59
|
+
errorMessage.toLowerCase().includes("authentication")) {
|
|
60
|
+
return 401;
|
|
61
|
+
}
|
|
62
|
+
if (errorMessage.toLowerCase().includes("forbidden") ||
|
|
63
|
+
errorMessage.toLowerCase().includes("permission")) {
|
|
64
|
+
return 403;
|
|
65
|
+
}
|
|
66
|
+
// Default to 500
|
|
67
|
+
return 500;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Extract error message from error
|
|
71
|
+
*/
|
|
72
|
+
function extractErrorMessage(error) {
|
|
73
|
+
if (error instanceof Error) {
|
|
74
|
+
return error.message;
|
|
75
|
+
}
|
|
76
|
+
if (typeof error === "string") {
|
|
77
|
+
return error;
|
|
78
|
+
}
|
|
79
|
+
return "An unexpected error occurred";
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Create error response from error
|
|
83
|
+
*/
|
|
84
|
+
function createErrorResponseFromError(error, statusCode, request, correlationId) {
|
|
85
|
+
let validationErrors;
|
|
86
|
+
if (error instanceof error_types_1.AppError && error.validationErrors) {
|
|
87
|
+
validationErrors = error.validationErrors;
|
|
88
|
+
}
|
|
89
|
+
return (0, error_response_1.createErrorResponse)(error, statusCode, request, correlationId, validationErrors);
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Generic route error handler
|
|
93
|
+
* Logs error and sends RFC 7807 compliant error response
|
|
94
|
+
*/
|
|
95
|
+
async function handleRouteError(error, req, res, operation) {
|
|
96
|
+
// Get correlation ID from request property (set by requestLogger) or header
|
|
97
|
+
const correlationId = req.correlationId ||
|
|
98
|
+
req.headers["x-correlation-id"] ||
|
|
99
|
+
undefined;
|
|
100
|
+
const statusCode = mapErrorToStatusCode(error);
|
|
101
|
+
const errorMessage = extractErrorMessage(error);
|
|
102
|
+
// Log error using custom logger or stderr
|
|
103
|
+
const operationName = operation || "unknown operation";
|
|
104
|
+
const logMessage = `${operationName} failed: ${errorMessage}`;
|
|
105
|
+
if (customErrorLogger) {
|
|
106
|
+
try {
|
|
107
|
+
await customErrorLogger.logError(logMessage, {
|
|
108
|
+
correlationId,
|
|
109
|
+
operation: operationName,
|
|
110
|
+
statusCode,
|
|
111
|
+
url: req.originalUrl,
|
|
112
|
+
method: req.method,
|
|
113
|
+
errorMessage: error instanceof Error ? error.message : String(error),
|
|
114
|
+
errorName: error instanceof Error ? error.name : "UnknownError",
|
|
115
|
+
stack: error instanceof Error ? error.stack : undefined,
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
catch (logError) {
|
|
119
|
+
// If logging itself fails, fall back to stderr
|
|
120
|
+
const logErr = logError instanceof Error ? logError : new Error(String(logError));
|
|
121
|
+
process.stderr.write(`[ERROR] Failed to log error: ${logErr.message}\n`);
|
|
122
|
+
process.stderr.write(`[ERROR] Original error: ${errorMessage}\n`);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
// Fallback to stderr if no logger configured
|
|
127
|
+
process.stderr.write(`[ERROR] ${logMessage}\n`);
|
|
128
|
+
if (error instanceof Error && error.stack) {
|
|
129
|
+
process.stderr.write(`[ERROR] Stack: ${error.stack}\n`);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
// Create and send error response
|
|
133
|
+
const errorResponse = createErrorResponseFromError(error, statusCode, req, correlationId);
|
|
134
|
+
// If error is AppError with correlationId, use it
|
|
135
|
+
if (error instanceof error_types_1.AppError && error.correlationId) {
|
|
136
|
+
errorResponse.correlationId = error.correlationId;
|
|
137
|
+
}
|
|
138
|
+
(0, error_response_1.sendErrorResponse)(res, errorResponse);
|
|
139
|
+
}
|
|
140
|
+
//# sourceMappingURL=error-handler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error-handler.js","sourceRoot":"","sources":["../../src/express/error-handler.ts"],"names":[],"mappings":";AAAA;;;GAGG;;AAyBH,wCAEC;AAyGD,4CA2DC;AA5LD,+CAAyC;AACzC,qDAI0B;AAU1B,+CAA+C;AAC/C,IAAI,iBAAiB,GAAuB,IAAI,CAAC;AAEjD;;;GAGG;AACH,SAAgB,cAAc,CAAC,MAA0B;IACvD,iBAAiB,GAAG,MAAM,CAAC;AAC7B,CAAC;AAED;;GAEG;AACH,SAAS,oBAAoB,CAAC,KAAc;IAC1C,+BAA+B;IAC/B,IAAI,KAAK,YAAY,sBAAQ,EAAE,CAAC;QAC9B,OAAO,KAAK,CAAC,UAAU,CAAC;IAC1B,CAAC;IAED,iDAAiD;IACjD,IAAI,KAAK,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,MAAM,IAAI,KAAK,EAAE,CAAC;QAC1D,MAAM,IAAI,GAAI,KAA0B,CAAC,IAAI,CAAC;QAC9C,sDAAsD;QACtD,IAAI,IAAI,KAAK,OAAO,EAAE,CAAC;YACrB,8BAA8B;YAC9B,OAAO,GAAG,CAAC;QACb,CAAC;QACD,IAAI,IAAI,KAAK,OAAO,EAAE,CAAC;YACrB,mBAAmB;YACnB,OAAO,GAAG,CAAC;QACb,CAAC;QACD,IAAI,IAAI,KAAK,OAAO,EAAE,CAAC;YACrB,mCAAmC;YACnC,OAAO,GAAG,CAAC;QACb,CAAC;IACH,CAAC;IAED,yBAAyB;IACzB,MAAM,YAAY,GAChB,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,IAAI,eAAe,CAAC,CAAC;IAE5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;QACL,CAAC;QAAC,OAAO,QAAQ,EAAE,CAAC;YAClB,+CAA+C;YAC/C,MAAM,MAAM,GACV,QAAQ,YAAY,KAAK,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC;YACrE,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,gCAAgC,MAAM,CAAC,OAAO,IAAI,CAAC,CAAC;YACzE,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,2BAA2B,YAAY,IAAI,CAAC,CAAC;QACpE,CAAC;IACH,CAAC;SAAM,CAAC;QACN,6CAA6C;QAC7C,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,WAAW,UAAU,IAAI,CAAC,CAAC;QAChD,IAAI,KAAK,YAAY,KAAK,IAAI,KAAK,CAAC,KAAK,EAAE,CAAC;YAC1C,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,kBAAkB,KAAK,CAAC,KAAK,IAAI,CAAC,CAAC;QAC1D,CAAC;IACH,CAAC;IAED,iCAAiC;IACjC,MAAM,aAAa,GAAG,4BAA4B,CAChD,KAAK,EACL,UAAU,EACV,GAAG,EACH,aAAa,CACd,CAAC;IAEF,kDAAkD;IAClD,IAAI,KAAK,YAAY,sBAAQ,IAAI,KAAK,CAAC,aAAa,EAAE,CAAC;QACrD,aAAa,CAAC,aAAa,GAAG,KAAK,CAAC,aAAa,CAAC;IACpD,CAAC;IAED,IAAA,kCAAiB,EAAC,GAAG,EAAE,aAAa,CAAC,CAAC;AACxC,CAAC"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error Response Utilities
|
|
3
|
+
* RFC 7807 Problem Details for HTTP APIs compliance
|
|
4
|
+
*/
|
|
5
|
+
import { Request, Response } from "express";
|
|
6
|
+
import { ValidationError } from "./error-types";
|
|
7
|
+
/**
|
|
8
|
+
* RBAC-specific extensions for RFC 7807 error responses
|
|
9
|
+
*/
|
|
10
|
+
export interface RBACErrorExtensions {
|
|
11
|
+
required?: {
|
|
12
|
+
permissions?: string[];
|
|
13
|
+
roles?: string[];
|
|
14
|
+
requireAll?: boolean;
|
|
15
|
+
};
|
|
16
|
+
missing?: {
|
|
17
|
+
permissions?: string[];
|
|
18
|
+
roles?: string[];
|
|
19
|
+
};
|
|
20
|
+
userPermissions?: string[];
|
|
21
|
+
userRoles?: string[];
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* RFC 7807 compliant error response interface
|
|
25
|
+
*/
|
|
26
|
+
export interface ErrorResponse {
|
|
27
|
+
type: string;
|
|
28
|
+
title: string;
|
|
29
|
+
status: number;
|
|
30
|
+
detail: string;
|
|
31
|
+
instance?: string;
|
|
32
|
+
correlationId?: string;
|
|
33
|
+
errors?: ValidationError[];
|
|
34
|
+
required?: RBACErrorExtensions["required"];
|
|
35
|
+
missing?: RBACErrorExtensions["missing"];
|
|
36
|
+
userPermissions?: string[];
|
|
37
|
+
userRoles?: string[];
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Get error type URI for a given status code
|
|
41
|
+
*/
|
|
42
|
+
export declare function getErrorTypeUri(statusCode: number): string;
|
|
43
|
+
/**
|
|
44
|
+
* Get error title for a given status code
|
|
45
|
+
*/
|
|
46
|
+
export declare function getErrorTitle(statusCode: number): string;
|
|
47
|
+
/**
|
|
48
|
+
* Create RFC 7807 compliant error response
|
|
49
|
+
*/
|
|
50
|
+
export declare function createErrorResponse(error: unknown, statusCode: number, request?: Request, correlationId?: string, validationErrors?: ValidationError[]): ErrorResponse;
|
|
51
|
+
/**
|
|
52
|
+
* Send RFC 7807 compliant error response
|
|
53
|
+
*/
|
|
54
|
+
export declare function sendErrorResponse(res: Response, errorResponse: ErrorResponse): void;
|
|
55
|
+
//# sourceMappingURL=error-response.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error-response.d.ts","sourceRoot":"","sources":["../../src/express/error-response.ts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d,UAAU,EAAE,MAAM,EAClB,OAAO,CAAC,EAAE,OAAO,EACjB,aAAa,CAAC,EAAE,MAAM,EACtB,gBAAgB,CAAC,EAAE,eAAe,EAAE,GACnC,aAAa,CAqCf;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAC/B,GAAG,EAAE,QAAQ,EACb,aAAa,EAAE,aAAa,GAC3B,IAAI,CAGN"}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Error Response Utilities
|
|
4
|
+
* RFC 7807 Problem Details for HTTP APIs compliance
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.getErrorTypeUri = getErrorTypeUri;
|
|
8
|
+
exports.getErrorTitle = getErrorTitle;
|
|
9
|
+
exports.createErrorResponse = createErrorResponse;
|
|
10
|
+
exports.sendErrorResponse = sendErrorResponse;
|
|
11
|
+
/**
|
|
12
|
+
* Error type URI mapping based on HTTP status codes
|
|
13
|
+
*/
|
|
14
|
+
const ERROR_TYPE_URI_MAP = {
|
|
15
|
+
400: "/Errors/BadRequest",
|
|
16
|
+
401: "/Errors/Unauthorized",
|
|
17
|
+
403: "/Errors/Forbidden",
|
|
18
|
+
404: "/Errors/NotFound",
|
|
19
|
+
405: "/Errors/MethodNotAllowed",
|
|
20
|
+
409: "/Errors/Conflict",
|
|
21
|
+
422: "/Errors/UnprocessableEntity",
|
|
22
|
+
429: "/Errors/TooManyRequests",
|
|
23
|
+
500: "/Errors/InternalServerError",
|
|
24
|
+
503: "/Errors/ServiceUnavailable",
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* Get error type URI for a given status code
|
|
28
|
+
*/
|
|
29
|
+
function getErrorTypeUri(statusCode) {
|
|
30
|
+
return ERROR_TYPE_URI_MAP[statusCode] || "/Errors/InternalServerError";
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Get error title for a given status code
|
|
34
|
+
*/
|
|
35
|
+
function getErrorTitle(statusCode) {
|
|
36
|
+
const titles = {
|
|
37
|
+
400: "Bad Request",
|
|
38
|
+
401: "Unauthorized",
|
|
39
|
+
403: "Forbidden",
|
|
40
|
+
404: "Not Found",
|
|
41
|
+
405: "Method Not Allowed",
|
|
42
|
+
409: "Conflict",
|
|
43
|
+
422: "Unprocessable Entity",
|
|
44
|
+
429: "Too Many Requests",
|
|
45
|
+
500: "Internal Server Error",
|
|
46
|
+
503: "Service Unavailable",
|
|
47
|
+
};
|
|
48
|
+
return titles[statusCode] || "Internal Server Error";
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Create RFC 7807 compliant error response
|
|
52
|
+
*/
|
|
53
|
+
function createErrorResponse(error, statusCode, request, correlationId, validationErrors) {
|
|
54
|
+
let errorMessage;
|
|
55
|
+
if (error instanceof Error) {
|
|
56
|
+
errorMessage = error.message;
|
|
57
|
+
}
|
|
58
|
+
else if (typeof error === "string") {
|
|
59
|
+
errorMessage = error;
|
|
60
|
+
}
|
|
61
|
+
else if (error === null || error === undefined) {
|
|
62
|
+
errorMessage = "An error occurred";
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
errorMessage = String(error) || "An error occurred";
|
|
66
|
+
}
|
|
67
|
+
// Build correlation ID - prefer parameter, fallback to request property or header
|
|
68
|
+
// requestLogger middleware sets req.correlationId
|
|
69
|
+
const finalCorrelationId = correlationId ||
|
|
70
|
+
request?.correlationId ||
|
|
71
|
+
request?.headers["x-correlation-id"];
|
|
72
|
+
const errorResponse = {
|
|
73
|
+
type: getErrorTypeUri(statusCode),
|
|
74
|
+
title: getErrorTitle(statusCode),
|
|
75
|
+
status: statusCode,
|
|
76
|
+
detail: errorMessage,
|
|
77
|
+
instance: request?.originalUrl,
|
|
78
|
+
};
|
|
79
|
+
// Only include correlationId if it exists (avoid undefined in JSON)
|
|
80
|
+
if (finalCorrelationId) {
|
|
81
|
+
errorResponse.correlationId = finalCorrelationId;
|
|
82
|
+
}
|
|
83
|
+
if (validationErrors && validationErrors.length > 0) {
|
|
84
|
+
errorResponse.errors = validationErrors;
|
|
85
|
+
}
|
|
86
|
+
return errorResponse;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Send RFC 7807 compliant error response
|
|
90
|
+
*/
|
|
91
|
+
function sendErrorResponse(res, errorResponse) {
|
|
92
|
+
res.setHeader("Content-Type", "application/problem+json");
|
|
93
|
+
res.status(errorResponse.status).json(errorResponse);
|
|
94
|
+
}
|
|
95
|
+
//# sourceMappingURL=error-response.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error-response.js","sourceRoot":"","sources":["../../src/express/error-response.ts"],"names":[],"mappings":";AAAA;;;GAGG;;AA2DH,0CAEC;AAKD,sCAcC;AAKD,kDA2CC;AAKD,8CAMC;AAnGD;;GAEG;AACH,MAAM,kBAAkB,GAA2B;IACjD,GAAG,EAAE,oBAAoB;IACzB,GAAG,EAAE,sBAAsB;IAC3B,GAAG,EAAE,mBAAmB;IACxB,GAAG,EAAE,kBAAkB;IACvB,GAAG,EAAE,0BAA0B;IAC/B,GAAG,EAAE,kBAAkB;IACvB,GAAG,EAAE,6BAA6B;IAClC,GAAG,EAAE,yBAAyB;IAC9B,GAAG,EAAE,6BAA6B;IAClC,GAAG,EAAE,4BAA4B;CAClC,CAAC;AAEF;;GAEG;AACH,SAAgB,eAAe,CAAC,UAAkB;IAChD,OAAO,kBAAkB,CAAC,UAAU,CAAC,IAAI,6BAA6B,CAAC;AACzE,CAAC;AAED;;GAEG;AACH,SAAgB,aAAa,CAAC,UAAkB;IAC9C,MAAM,MAAM,GAA2B;QACrC,GAAG,EAAE,aAAa;QAClB,GAAG,EAAE,cAAc;QACnB,GAAG,EAAE,WAAW;QAChB,GAAG,EAAE,WAAW;QAChB,GAAG,EAAE,oBAAoB;QACzB,GAAG,EAAE,UAAU;QACf,GAAG,EAAE,sBAAsB;QAC3B,GAAG,EAAE,mBAAmB;QACxB,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,CAAC;AACvB,CAAC;AAED;;GAEG;AACH,SAAgB,iBAAiB,CAC/B,GAAa,EACb,aAA4B;IAE5B,GAAG,CAAC,SAAS,CAAC,cAAc,EAAE,0BAA0B,CAAC,CAAC;IAC1D,GAAG,CAAC,MAAM,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC;AACvD,CAAC"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Express Error Types
|
|
3
|
+
* Application error classes and interfaces for Express.js applications
|
|
4
|
+
*/
|
|
5
|
+
export interface ApiResponse<T = Record<string, unknown>> {
|
|
6
|
+
success: boolean;
|
|
7
|
+
data?: T;
|
|
8
|
+
message?: string;
|
|
9
|
+
error?: string;
|
|
10
|
+
timestamp: string;
|
|
11
|
+
correlationId?: string;
|
|
12
|
+
}
|
|
13
|
+
export interface ValidationError {
|
|
14
|
+
field: string;
|
|
15
|
+
message: string;
|
|
16
|
+
value?: string | number | boolean | null;
|
|
17
|
+
}
|
|
18
|
+
export interface ApiError extends Error {
|
|
19
|
+
statusCode: number;
|
|
20
|
+
isOperational: boolean;
|
|
21
|
+
validationErrors?: ValidationError[];
|
|
22
|
+
}
|
|
23
|
+
export declare class AppError extends Error implements ApiError {
|
|
24
|
+
readonly statusCode: number;
|
|
25
|
+
readonly isOperational: boolean;
|
|
26
|
+
readonly validationErrors?: ValidationError[];
|
|
27
|
+
readonly errorType?: string;
|
|
28
|
+
readonly instance?: string;
|
|
29
|
+
readonly correlationId?: string;
|
|
30
|
+
constructor(message: string, statusCode?: number, isOperational?: boolean, validationErrors?: ValidationError[], errorType?: string, instance?: string, correlationId?: string);
|
|
31
|
+
/**
|
|
32
|
+
* Convert AppError to RFC 7807 ErrorResponse format
|
|
33
|
+
*/
|
|
34
|
+
toErrorResponse(requestUrl?: string): {
|
|
35
|
+
type: string;
|
|
36
|
+
title: string;
|
|
37
|
+
status: number;
|
|
38
|
+
detail: string;
|
|
39
|
+
instance?: string;
|
|
40
|
+
correlationId?: string;
|
|
41
|
+
errors?: ValidationError[];
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
export declare const createSuccessResponse: <T>(data: T, message?: string, correlationId?: string) => ApiResponse<T>;
|
|
45
|
+
export declare const createErrorResponse: (error: string, correlationId?: string) => ApiResponse;
|
|
46
|
+
//# sourceMappingURL=error-types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error-types.d.ts","sourceRoot":"","sources":["../../src/express/error-types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,MAAM,WAAW,WAAW,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IACtD,OAAO,EAAE,OAAO,CAAC;IACjB,IAAI,CAAC,EAAE,CAAC,CAAC;IACT,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;IAClB,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB;AAED,MAAM,WAAW,eAAe;IAC9B,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,CAAC;CAC1C;AAED,MAAM,WAAW,QAAS,SAAQ,KAAK;IACrC,UAAU,EAAE,MAAM,CAAC;IACnB,aAAa,EAAE,OAAO,CAAC;IACvB,gBAAgB,CAAC,EAAE,eAAe,EAAE,CAAC;CACtC;AAED,qBAAa,QAAS,SAAQ,KAAM,YAAW,QAAQ;IACrD,SAAgB,UAAU,EAAE,MAAM,CAAC;IACnC,SAAgB,aAAa,EAAE,OAAO,CAAC;IACvC,SAAgB,gBAAgB,CAAC,EAAE,eAAe,EAAE,CAAC;IACrD,SAAgB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnC,SAAgB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClC,SAAgB,aAAa,CAAC,EAAE,MAAM,CAAC;gBAGrC,OAAO,EAAE,MAAM,EACf,UAAU,GAAE,MAAY,EACxB,aAAa,GAAE,OAAc,EAC7B,gBAAgB,CAAC,EAAE,eAAe,EAAE,EACpC,SAAS,CAAC,EAAE,MAAM,EAClB,QAAQ,CAAC,EAAE,MAAM,EACjB,aAAa,CAAC,EAAE,MAAM;IAaxB;;OAEG;IACH,eAAe,CAAC,UAAU,CAAC,EAAE,MAAM,GAAG;QACpC,IAAI,EAAE,MAAM,CAAC;QACb,KAAK,EAAE,MAAM,CAAC;QACd,MAAM,EAAE,MAAM,CAAC;QACf,MAAM,EAAE,MAAM,CAAC;QACf,QAAQ,CAAC,EAAE,MAAM,CAAC;QAClB,aAAa,CAAC,EAAE,MAAM,CAAC;QACvB,MAAM,CAAC,EAAE,eAAe,EAAE,CAAC;KAC5B;CA+CF;AAED,eAAO,MAAM,qBAAqB,GAAI,CAAC,EACrC,MAAM,CAAC,EACP,UAAU,MAAM,EAChB,gBAAgB,MAAM,KACrB,WAAW,CAAC,CAAC,CAgBf,CAAC;AAEF,eAAO,MAAM,mBAAmB,GAC9B,OAAO,MAAM,EACb,gBAAgB,MAAM,KACrB,WAYF,CAAC"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Express Error Types
|
|
4
|
+
* Application error classes and interfaces for Express.js applications
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.createErrorResponse = exports.createSuccessResponse = exports.AppError = void 0;
|
|
8
|
+
class AppError extends Error {
|
|
9
|
+
constructor(message, statusCode = 500, isOperational = true, validationErrors, errorType, instance, correlationId) {
|
|
10
|
+
super(message);
|
|
11
|
+
this.statusCode = statusCode;
|
|
12
|
+
this.isOperational = isOperational;
|
|
13
|
+
this.validationErrors = validationErrors || [];
|
|
14
|
+
this.errorType = errorType;
|
|
15
|
+
this.instance = instance;
|
|
16
|
+
this.correlationId = correlationId;
|
|
17
|
+
Error.captureStackTrace(this, this.constructor);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Convert AppError to RFC 7807 ErrorResponse format
|
|
21
|
+
*/
|
|
22
|
+
toErrorResponse(requestUrl) {
|
|
23
|
+
// Use error type from error if available, otherwise generate from status code
|
|
24
|
+
const typeMap = {
|
|
25
|
+
400: "/Errors/BadRequest",
|
|
26
|
+
401: "/Errors/Unauthorized",
|
|
27
|
+
403: "/Errors/Forbidden",
|
|
28
|
+
404: "/Errors/NotFound",
|
|
29
|
+
405: "/Errors/MethodNotAllowed",
|
|
30
|
+
409: "/Errors/Conflict",
|
|
31
|
+
422: "/Errors/UnprocessableEntity",
|
|
32
|
+
429: "/Errors/TooManyRequests",
|
|
33
|
+
500: "/Errors/InternalServerError",
|
|
34
|
+
503: "/Errors/ServiceUnavailable",
|
|
35
|
+
};
|
|
36
|
+
const titleMap = {
|
|
37
|
+
400: "Bad Request",
|
|
38
|
+
401: "Unauthorized",
|
|
39
|
+
403: "Forbidden",
|
|
40
|
+
404: "Not Found",
|
|
41
|
+
405: "Method Not Allowed",
|
|
42
|
+
409: "Conflict",
|
|
43
|
+
422: "Unprocessable Entity",
|
|
44
|
+
429: "Too Many Requests",
|
|
45
|
+
500: "Internal Server Error",
|
|
46
|
+
503: "Service Unavailable",
|
|
47
|
+
};
|
|
48
|
+
const type = this.errorType ||
|
|
49
|
+
typeMap[this.statusCode] ||
|
|
50
|
+
"/Errors/InternalServerError";
|
|
51
|
+
const title = titleMap[this.statusCode] || "Internal Server Error";
|
|
52
|
+
return {
|
|
53
|
+
type,
|
|
54
|
+
title,
|
|
55
|
+
status: this.statusCode,
|
|
56
|
+
detail: this.message,
|
|
57
|
+
instance: this.instance || requestUrl,
|
|
58
|
+
correlationId: this.correlationId,
|
|
59
|
+
errors: this.validationErrors && this.validationErrors.length > 0
|
|
60
|
+
? this.validationErrors
|
|
61
|
+
: undefined,
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
exports.AppError = AppError;
|
|
66
|
+
const createSuccessResponse = (data, message, correlationId) => {
|
|
67
|
+
const response = {
|
|
68
|
+
success: true,
|
|
69
|
+
data,
|
|
70
|
+
timestamp: new Date().toISOString(),
|
|
71
|
+
};
|
|
72
|
+
if (message) {
|
|
73
|
+
response.message = message;
|
|
74
|
+
}
|
|
75
|
+
if (correlationId) {
|
|
76
|
+
response.correlationId = correlationId;
|
|
77
|
+
}
|
|
78
|
+
return response;
|
|
79
|
+
};
|
|
80
|
+
exports.createSuccessResponse = createSuccessResponse;
|
|
81
|
+
const createErrorResponse = (error, correlationId) => {
|
|
82
|
+
const response = {
|
|
83
|
+
success: false,
|
|
84
|
+
error,
|
|
85
|
+
timestamp: new Date().toISOString(),
|
|
86
|
+
};
|
|
87
|
+
if (correlationId) {
|
|
88
|
+
response.correlationId = correlationId;
|
|
89
|
+
}
|
|
90
|
+
return response;
|
|
91
|
+
};
|
|
92
|
+
exports.createErrorResponse = createErrorResponse;
|
|
93
|
+
//# sourceMappingURL=error-types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"error-types.js","sourceRoot":"","sources":["../../src/express/error-types.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAuBH,MAAa,QAAS,SAAQ,KAAK;IAQjC,YACE,OAAe,EACf,aAAqB,GAAG,EACxB,gBAAyB,IAAI,EAC7B,gBAAoC,EACpC,SAAkB,EAClB,QAAiB,EACjB,aAAsB;QAEtB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;QAC7B,IAAI,CAAC,aAAa,GAAG,aAAa,CAAC;QACnC,IAAI,CAAC,gBAAgB,GAAG,gBAAgB,IAAI,EAAE,CAAC;QAC/C,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;QAC3B,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CA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;gBACvD,CAAC,CAAC,IAAI,CAAC,gBAAgB;gBACvB,CAAC,CAAC,SAAS;SAChB,CAAC;IACJ,CAAC;CACF;AAtFD,4BAsFC;AAEM,MAAM,qBAAqB,GAAG,CACnC,IAAO,EACP,OAAgB,EAChB,aAAsB,EACN,EAAE;IAClB,MAAM,QAAQ,GAAmB;QAC/B,OAAO,EAAE,IAAI;QACb,IAAI;QACJ,SAAS,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;KACpC,CAAC;IAEF,IAAI,OAAO,EAAE,CAAC;QACZ,QAAQ,CAAC,OAAO,GAAG,OAAO,CAAC;IAC7B,CAAC;IAED,IAAI,aAAa,EAAE,CAAC;QAClB,QAAQ,CAAC,aAAa,GAAG,aAAa,CAAC;IACzC,CAAC;IAED,OAAO,QAAQ,CAAC;AAClB,CAAC,CAAC;AApBW,QAAA,qBAAqB,yBAoBhC;AAEK,MAAM,mBAAmB,GAAG,CACjC,KAAa,EACb,aAAsB,EACT,EAAE;IACf,MAAM,QAAQ,GAAgB;QAC5B,OAAO,EAAE,KAAK;QACd,KAAK;QACL,SAAS,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;KACpC,CAAC;IAEF,IAAI,aAAa,EAAE,CAAC;QAClB,QAAQ,CAAC,aAAa,GAAG,aAAa,CAAC;IACzC,CAAC;IAED,OAAO,QAAQ,CAAC;AAClB,CAAC,CAAC;AAfW,QAAA,mBAAmB,uBAe9B"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Express.js Utilities
|
|
3
|
+
* Generic helpers for building Express.js REST APIs
|
|
4
|
+
*/
|
|
5
|
+
export { ResponseHelper, PaginationMeta } from "./response-helper";
|
|
6
|
+
export { injectResponseHelpers } from "./response-middleware";
|
|
7
|
+
export { asyncHandler, asyncHandlerNamed } from "./async-handler";
|
|
8
|
+
export { ValidationHelper } from "./validation-helper";
|
|
9
|
+
export { AppError, ApiError, ValidationError, ApiResponse, createSuccessResponse, createErrorResponse, } from "./error-types";
|
|
10
|
+
export { ErrorLogger, setErrorLogger, handleRouteError } from "./error-handler";
|
|
11
|
+
export { ErrorResponse, RBACErrorExtensions, getErrorTypeUri, getErrorTitle, sendErrorResponse, } from "./error-response";
|
|
12
|
+
export { EncryptionUtil } from "./encryption";
|
|
13
|
+
import "./express.d.ts";
|
|
14
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/express/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACnE,OAAO,EAAE,qBAAqB,EAAE,MAAM,uBAAuB,CAAC;AAG9D,OAAO,EAAE,YAAY,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AAGlE,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAGvD,OAAO,EACL,QAAQ,EACR,QAAQ,EACR,eAAe,EACf,WAAW,EACX,qBAAqB,EACrB,mBAAmB,GACpB,MAAM,eAAe,CAAC;AAGvB,OAAO,EAAE,WAAW,EAAE,cAAc,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAGhF,OAAO,EACL,aAAa,EACb,mBAAmB,EACnB,eAAe,EACf,aAAa,EACb,iBAAiB,GAClB,MAAM,kBAAkB,CAAC;AAG1B,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAG9C,OAAO,gBAAgB,CAAC"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Express.js Utilities
|
|
4
|
+
* Generic helpers for building Express.js REST APIs
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.EncryptionUtil = exports.sendErrorResponse = exports.getErrorTitle = exports.getErrorTypeUri = exports.handleRouteError = exports.setErrorLogger = exports.createErrorResponse = exports.createSuccessResponse = exports.AppError = exports.ValidationHelper = exports.asyncHandlerNamed = exports.asyncHandler = exports.injectResponseHelpers = exports.ResponseHelper = void 0;
|
|
8
|
+
// Response helpers
|
|
9
|
+
var response_helper_1 = require("./response-helper");
|
|
10
|
+
Object.defineProperty(exports, "ResponseHelper", { enumerable: true, get: function () { return response_helper_1.ResponseHelper; } });
|
|
11
|
+
var response_middleware_1 = require("./response-middleware");
|
|
12
|
+
Object.defineProperty(exports, "injectResponseHelpers", { enumerable: true, get: function () { return response_middleware_1.injectResponseHelpers; } });
|
|
13
|
+
// Async handler
|
|
14
|
+
var async_handler_1 = require("./async-handler");
|
|
15
|
+
Object.defineProperty(exports, "asyncHandler", { enumerable: true, get: function () { return async_handler_1.asyncHandler; } });
|
|
16
|
+
Object.defineProperty(exports, "asyncHandlerNamed", { enumerable: true, get: function () { return async_handler_1.asyncHandlerNamed; } });
|
|
17
|
+
// Validation
|
|
18
|
+
var validation_helper_1 = require("./validation-helper");
|
|
19
|
+
Object.defineProperty(exports, "ValidationHelper", { enumerable: true, get: function () { return validation_helper_1.ValidationHelper; } });
|
|
20
|
+
// Error handling - Types
|
|
21
|
+
var error_types_1 = require("./error-types");
|
|
22
|
+
Object.defineProperty(exports, "AppError", { enumerable: true, get: function () { return error_types_1.AppError; } });
|
|
23
|
+
Object.defineProperty(exports, "createSuccessResponse", { enumerable: true, get: function () { return error_types_1.createSuccessResponse; } });
|
|
24
|
+
Object.defineProperty(exports, "createErrorResponse", { enumerable: true, get: function () { return error_types_1.createErrorResponse; } });
|
|
25
|
+
// Error handling - Utilities
|
|
26
|
+
var error_handler_1 = require("./error-handler");
|
|
27
|
+
Object.defineProperty(exports, "setErrorLogger", { enumerable: true, get: function () { return error_handler_1.setErrorLogger; } });
|
|
28
|
+
Object.defineProperty(exports, "handleRouteError", { enumerable: true, get: function () { return error_handler_1.handleRouteError; } });
|
|
29
|
+
// Error handling - Response
|
|
30
|
+
var error_response_1 = require("./error-response");
|
|
31
|
+
Object.defineProperty(exports, "getErrorTypeUri", { enumerable: true, get: function () { return error_response_1.getErrorTypeUri; } });
|
|
32
|
+
Object.defineProperty(exports, "getErrorTitle", { enumerable: true, get: function () { return error_response_1.getErrorTitle; } });
|
|
33
|
+
Object.defineProperty(exports, "sendErrorResponse", { enumerable: true, get: function () { return error_response_1.sendErrorResponse; } });
|
|
34
|
+
// Encryption
|
|
35
|
+
var encryption_1 = require("./encryption");
|
|
36
|
+
Object.defineProperty(exports, "EncryptionUtil", { enumerable: true, get: function () { return encryption_1.EncryptionUtil; } });
|
|
37
|
+
// Type augmentation (imported for side effects)
|
|
38
|
+
require("./express.d.ts");
|
|
39
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/express/index.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,mBAAmB;AACnB,qDAAmE;AAA1D,iHAAA,cAAc,OAAA;AACvB,6DAA8D;AAArD,4HAAA,qBAAqB,OAAA;AAE9B,gBAAgB;AAChB,iDAAkE;AAAzD,6GAAA,YAAY,OAAA;AAAE,kHAAA,iBAAiB,OAAA;AAExC,aAAa;AACb,yDAAuD;AAA9C,qHAAA,gBAAgB,OAAA;AAEzB,yBAAyB;AACzB,6CAOuB;AANrB,uGAAA,QAAQ,OAAA;AAIR,oHAAA,qBAAqB,OAAA;AACrB,kHAAA,mBAAmB,OAAA;AAGrB,6BAA6B;AAC7B,iDAAgF;AAA1D,+GAAA,cAAc,OAAA;AAAE,iHAAA,gBAAgB,OAAA;AAEtD,4BAA4B;AAC5B,mDAM0B;AAHxB,iHAAA,eAAe,OAAA;AACf,+GAAA,aAAa,OAAA;AACb,mHAAA,iBAAiB,OAAA;AAGnB,aAAa;AACb,2CAA8C;AAArC,4GAAA,cAAc,OAAA;AAEvB,gDAAgD;AAChD,0BAAwB"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Response Helper Utilities
|
|
3
|
+
* Standardizes API response formats across the application
|
|
4
|
+
*/
|
|
5
|
+
import { Response } from "express";
|
|
6
|
+
/**
|
|
7
|
+
* Pagination metadata for paginated responses
|
|
8
|
+
*/
|
|
9
|
+
export interface PaginationMeta {
|
|
10
|
+
currentPage: number;
|
|
11
|
+
pageSize: number;
|
|
12
|
+
totalItems: number;
|
|
13
|
+
totalPages?: number;
|
|
14
|
+
type: string;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Response Helper for standardizing API responses
|
|
18
|
+
*/
|
|
19
|
+
export declare class ResponseHelper {
|
|
20
|
+
/**
|
|
21
|
+
* Success response with data (200)
|
|
22
|
+
* @param res - Express response object
|
|
23
|
+
* @param data - Response data
|
|
24
|
+
* @param message - Optional success message
|
|
25
|
+
* @param statusCode - HTTP status code (default: 200)
|
|
26
|
+
*/
|
|
27
|
+
static success<T>(res: Response, data: T, message?: string, statusCode?: number): Response;
|
|
28
|
+
/**
|
|
29
|
+
* Created response (201)
|
|
30
|
+
* @param res - Express response object
|
|
31
|
+
* @param data - Created resource data
|
|
32
|
+
* @param message - Optional success message (default: 'Resource created')
|
|
33
|
+
*/
|
|
34
|
+
static created<T>(res: Response, data: T, message?: string): Response;
|
|
35
|
+
/**
|
|
36
|
+
* Paginated list response (200)
|
|
37
|
+
* Uses miso-client SDK standard format: { data, meta }
|
|
38
|
+
* @param res - Express response object
|
|
39
|
+
* @param items - Array of items
|
|
40
|
+
* @param meta - Pagination metadata
|
|
41
|
+
*/
|
|
42
|
+
static paginated<T>(res: Response, items: T[], meta: PaginationMeta): Response;
|
|
43
|
+
/**
|
|
44
|
+
* No content response (204)
|
|
45
|
+
* @param res - Express response object
|
|
46
|
+
*/
|
|
47
|
+
static noContent(res: Response): Response;
|
|
48
|
+
/**
|
|
49
|
+
* Accepted response (202)
|
|
50
|
+
* Used for async operations that have been accepted but not completed
|
|
51
|
+
* @param res - Express response object
|
|
52
|
+
* @param data - Optional data about the accepted request
|
|
53
|
+
* @param message - Optional message (default: 'Request accepted')
|
|
54
|
+
*/
|
|
55
|
+
static accepted<T>(res: Response, data?: T, message?: string): Response;
|
|
56
|
+
/**
|
|
57
|
+
* Error response helper
|
|
58
|
+
* Throws an AppError which will be caught by error middleware
|
|
59
|
+
* @param message - Error message
|
|
60
|
+
* @param statusCode - HTTP status code (default: 400)
|
|
61
|
+
* @param isOperational - Whether error is operational (default: true)
|
|
62
|
+
* @param validationErrors - Optional validation errors
|
|
63
|
+
* @param errorType - Optional error type/code
|
|
64
|
+
*/
|
|
65
|
+
static error(message: string, statusCode?: number, isOperational?: boolean, validationErrors?: unknown, errorType?: string): never;
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=response-helper.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"response-helper.d.ts","sourceRoot":"","sources":["../../src/express/response-helper.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAInC;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,WAAW,EAAE,MAAM,CAAC;IACpB,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,EAAE,MAAM,CAAC;IACnB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,IAAI,EAAE,MAAM,CAAC;CACd;AAED;;GAEG;AACH,qBAAa,cAAc;IACzB;;;;;;OAMG;IACH,MAAM,CAAC,OAAO,CAAC,CAAC,EACd,GAAG,EAAE,QAAQ,EACb,IAAI,EAAE,CAAC,EACP,OAAO,CAAC,EAAE,MAAM,EAChB,UAAU,SAAM,GACf,QAAQ;IAmBX;;;;;OAKG;IACH,MAAM,CAAC,OAAO,CAAC,CAAC,EACd,GAAG,EAAE,QAAQ,EACb,IAAI,EAAE,CAAC,EACP,OAAO,SAAqB,GAC3B,QAAQ;IAIX;;;;;;OAMG;IACH,MAAM,CAAC,SAAS,CAAC,C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