wynkjs 1.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 +522 -0
- package/dist/database.d.ts +36 -0
- package/dist/database.d.ts.map +1 -0
- package/dist/database.js +162 -0
- package/dist/decorators/database.decorators.d.ts +55 -0
- package/dist/decorators/database.decorators.d.ts.map +1 -0
- package/dist/decorators/database.decorators.js +131 -0
- package/dist/decorators/exception.advanced.d.ts +160 -0
- package/dist/decorators/exception.advanced.d.ts.map +1 -0
- package/dist/decorators/exception.advanced.js +232 -0
- package/dist/decorators/exception.decorators.d.ts +121 -0
- package/dist/decorators/exception.decorators.d.ts.map +1 -0
- package/dist/decorators/exception.decorators.js +242 -0
- package/dist/decorators/guard.decorators.d.ts +43 -0
- package/dist/decorators/guard.decorators.d.ts.map +1 -0
- package/dist/decorators/guard.decorators.js +67 -0
- package/dist/decorators/http.decorators.d.ts +130 -0
- package/dist/decorators/http.decorators.d.ts.map +1 -0
- package/dist/decorators/http.decorators.js +209 -0
- package/dist/decorators/interceptor.advanced.d.ts +93 -0
- package/dist/decorators/interceptor.advanced.d.ts.map +1 -0
- package/dist/decorators/interceptor.advanced.js +228 -0
- package/dist/decorators/interceptor.decorators.d.ts +91 -0
- package/dist/decorators/interceptor.decorators.d.ts.map +1 -0
- package/dist/decorators/interceptor.decorators.js +163 -0
- package/dist/decorators/param.decorators.d.ts +144 -0
- package/dist/decorators/param.decorators.d.ts.map +1 -0
- package/dist/decorators/param.decorators.js +205 -0
- package/dist/decorators/pipe.advanced.d.ts +125 -0
- package/dist/decorators/pipe.advanced.d.ts.map +1 -0
- package/dist/decorators/pipe.advanced.js +263 -0
- package/dist/decorators/pipe.decorators.d.ts +226 -0
- package/dist/decorators/pipe.decorators.d.ts.map +1 -0
- package/dist/decorators/pipe.decorators.js +420 -0
- package/dist/dto.d.ts +83 -0
- package/dist/dto.d.ts.map +1 -0
- package/dist/dto.js +88 -0
- package/dist/factory.d.ts +76 -0
- package/dist/factory.d.ts.map +1 -0
- package/dist/factory.js +410 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +43 -0
- package/dist/pipes/validation.pipe.d.ts +91 -0
- package/dist/pipes/validation.pipe.d.ts.map +1 -0
- package/dist/pipes/validation.pipe.js +163 -0
- package/package.json +68 -0
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
/**
|
|
3
|
+
* Advanced Exception Filters for WynkJS Framework
|
|
4
|
+
* Specialized filters for different error scenarios
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Validation Exception Filter - Handles validation errors
|
|
8
|
+
* @example
|
|
9
|
+
* @UseFilters(ValidationExceptionFilter)
|
|
10
|
+
* @Post()
|
|
11
|
+
* async create(@Body() data: any) {}
|
|
12
|
+
*/
|
|
13
|
+
export class ValidationExceptionFilter {
|
|
14
|
+
catch(exception, context) {
|
|
15
|
+
const response = context.getResponse();
|
|
16
|
+
const request = context.getRequest();
|
|
17
|
+
return {
|
|
18
|
+
statusCode: exception.statusCode,
|
|
19
|
+
error: "Validation Error",
|
|
20
|
+
message: exception.message,
|
|
21
|
+
errors: exception.errors || [],
|
|
22
|
+
timestamp: new Date().toISOString(),
|
|
23
|
+
path: request.url,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Database Exception Filter - Handles database errors
|
|
29
|
+
* @example
|
|
30
|
+
* @UseFilters(DatabaseExceptionFilter)
|
|
31
|
+
* @Controller('/users')
|
|
32
|
+
* export class UserController {}
|
|
33
|
+
*/
|
|
34
|
+
export class DatabaseExceptionFilter {
|
|
35
|
+
catch(exception, context) {
|
|
36
|
+
const response = context.getResponse();
|
|
37
|
+
const request = context.getRequest();
|
|
38
|
+
// Check for common database errors
|
|
39
|
+
let message = "Database error occurred";
|
|
40
|
+
let statusCode = 500;
|
|
41
|
+
if (exception.code === "23505" || exception.message?.includes("unique")) {
|
|
42
|
+
message = "Resource already exists";
|
|
43
|
+
statusCode = 409; // Conflict
|
|
44
|
+
}
|
|
45
|
+
else if (exception.code === "23503" ||
|
|
46
|
+
exception.message?.includes("foreign key")) {
|
|
47
|
+
message = "Referenced resource does not exist";
|
|
48
|
+
statusCode = 400;
|
|
49
|
+
}
|
|
50
|
+
else if (exception.code === "23502" ||
|
|
51
|
+
exception.message?.includes("not null")) {
|
|
52
|
+
message = "Required field is missing";
|
|
53
|
+
statusCode = 400;
|
|
54
|
+
}
|
|
55
|
+
return {
|
|
56
|
+
statusCode,
|
|
57
|
+
error: "Database Error",
|
|
58
|
+
message,
|
|
59
|
+
timestamp: new Date().toISOString(),
|
|
60
|
+
path: request.url,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Authentication Exception Filter - Handles auth errors
|
|
66
|
+
* @example
|
|
67
|
+
* @UseFilters(AuthenticationExceptionFilter)
|
|
68
|
+
* @Controller('/auth')
|
|
69
|
+
* export class AuthController {}
|
|
70
|
+
*/
|
|
71
|
+
export class AuthenticationExceptionFilter {
|
|
72
|
+
catch(exception, context) {
|
|
73
|
+
const response = context.getResponse();
|
|
74
|
+
const request = context.getRequest();
|
|
75
|
+
return {
|
|
76
|
+
statusCode: exception.statusCode,
|
|
77
|
+
error: "Authentication Failed",
|
|
78
|
+
message: exception.message || "Invalid credentials",
|
|
79
|
+
timestamp: new Date().toISOString(),
|
|
80
|
+
path: request.url,
|
|
81
|
+
hint: "Please check your authentication token or credentials",
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Authorization Exception Filter - Handles permission errors
|
|
87
|
+
* @example
|
|
88
|
+
* @UseFilters(AuthorizationExceptionFilter)
|
|
89
|
+
* @Controller('/admin')
|
|
90
|
+
* export class AdminController {}
|
|
91
|
+
*/
|
|
92
|
+
export class AuthorizationExceptionFilter {
|
|
93
|
+
catch(exception, context) {
|
|
94
|
+
const response = context.getResponse();
|
|
95
|
+
const request = context.getRequest();
|
|
96
|
+
return {
|
|
97
|
+
statusCode: exception.statusCode,
|
|
98
|
+
error: "Authorization Failed",
|
|
99
|
+
message: exception.message ||
|
|
100
|
+
"You don't have permission to access this resource",
|
|
101
|
+
timestamp: new Date().toISOString(),
|
|
102
|
+
path: request.url,
|
|
103
|
+
requiredPermissions: exception.requiredPermissions || [],
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Not Found Exception Filter - Handles 404 errors
|
|
109
|
+
* @example
|
|
110
|
+
* @UseFilters(NotFoundExceptionFilter)
|
|
111
|
+
* @Get('/:id')
|
|
112
|
+
* async findOne(@Param('id') id: string) {}
|
|
113
|
+
*/
|
|
114
|
+
export class NotFoundExceptionFilter {
|
|
115
|
+
catch(exception, context) {
|
|
116
|
+
const response = context.getResponse();
|
|
117
|
+
const request = context.getRequest();
|
|
118
|
+
return {
|
|
119
|
+
statusCode: exception.statusCode,
|
|
120
|
+
error: "Not Found",
|
|
121
|
+
message: exception.message || "Resource not found",
|
|
122
|
+
timestamp: new Date().toISOString(),
|
|
123
|
+
path: request.url,
|
|
124
|
+
suggestion: "Please check the resource ID or URL",
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Rate Limit Exception Filter - Handles rate limit errors
|
|
130
|
+
* @example
|
|
131
|
+
* @UseFilters(RateLimitExceptionFilter)
|
|
132
|
+
* @Post()
|
|
133
|
+
* async create() {}
|
|
134
|
+
*/
|
|
135
|
+
export class RateLimitExceptionFilter {
|
|
136
|
+
catch(exception, context) {
|
|
137
|
+
const response = context.getResponse();
|
|
138
|
+
const request = context.getRequest();
|
|
139
|
+
return {
|
|
140
|
+
statusCode: 429,
|
|
141
|
+
error: "Too Many Requests",
|
|
142
|
+
message: exception.message || "Rate limit exceeded",
|
|
143
|
+
timestamp: new Date().toISOString(),
|
|
144
|
+
path: request.url,
|
|
145
|
+
retryAfter: exception.retryAfter || 60,
|
|
146
|
+
hint: "Please wait before making another request",
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Business Logic Exception Filter - Handles business rule violations
|
|
152
|
+
* @example
|
|
153
|
+
* @UseFilters(BusinessLogicExceptionFilter)
|
|
154
|
+
* @Post('/transfer')
|
|
155
|
+
* async transfer(@Body() data: any) {}
|
|
156
|
+
*/
|
|
157
|
+
export class BusinessLogicExceptionFilter {
|
|
158
|
+
catch(exception, context) {
|
|
159
|
+
const response = context.getResponse();
|
|
160
|
+
const request = context.getRequest();
|
|
161
|
+
return {
|
|
162
|
+
statusCode: exception.statusCode || 422,
|
|
163
|
+
error: "Business Rule Violation",
|
|
164
|
+
message: exception.message || "Business logic constraint violated",
|
|
165
|
+
timestamp: new Date().toISOString(),
|
|
166
|
+
path: request.url,
|
|
167
|
+
rule: exception.rule || "unknown",
|
|
168
|
+
details: exception.details || {},
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* File Upload Exception Filter - Handles file upload errors
|
|
174
|
+
* @example
|
|
175
|
+
* @UseFilters(FileUploadExceptionFilter)
|
|
176
|
+
* @Post('/upload')
|
|
177
|
+
* async upload(@UploadedFile() file: any) {}
|
|
178
|
+
*/
|
|
179
|
+
export class FileUploadExceptionFilter {
|
|
180
|
+
catch(exception, context) {
|
|
181
|
+
const response = context.getResponse();
|
|
182
|
+
const request = context.getRequest();
|
|
183
|
+
let message = "File upload failed";
|
|
184
|
+
if (exception.message?.includes("size")) {
|
|
185
|
+
message = "File size exceeds limit";
|
|
186
|
+
}
|
|
187
|
+
else if (exception.message?.includes("type")) {
|
|
188
|
+
message = "Invalid file type";
|
|
189
|
+
}
|
|
190
|
+
else if (exception.message?.includes("required")) {
|
|
191
|
+
message = "File is required";
|
|
192
|
+
}
|
|
193
|
+
return {
|
|
194
|
+
statusCode: 400,
|
|
195
|
+
error: "File Upload Error",
|
|
196
|
+
message,
|
|
197
|
+
timestamp: new Date().toISOString(),
|
|
198
|
+
path: request.url,
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Global Exception Filter - Catches all unhandled exceptions
|
|
204
|
+
* @example
|
|
205
|
+
* app.useGlobalFilters(new GlobalExceptionFilter());
|
|
206
|
+
*/
|
|
207
|
+
export class GlobalExceptionFilter {
|
|
208
|
+
catch(exception, context) {
|
|
209
|
+
const response = context.getResponse();
|
|
210
|
+
const request = context.getRequest();
|
|
211
|
+
const statusCode = exception.statusCode || 500;
|
|
212
|
+
const message = exception.message || "Internal server error";
|
|
213
|
+
// Log the error for debugging
|
|
214
|
+
console.error("❌ Unhandled exception:", {
|
|
215
|
+
statusCode,
|
|
216
|
+
message,
|
|
217
|
+
path: request.url,
|
|
218
|
+
method: request.method,
|
|
219
|
+
stack: exception.stack,
|
|
220
|
+
});
|
|
221
|
+
return {
|
|
222
|
+
statusCode,
|
|
223
|
+
error: exception.name || "Error",
|
|
224
|
+
message,
|
|
225
|
+
timestamp: new Date().toISOString(),
|
|
226
|
+
path: request.url,
|
|
227
|
+
...(process.env.NODE_ENV === "development" && {
|
|
228
|
+
stack: exception.stack,
|
|
229
|
+
}),
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
import { ExecutionContext } from "./guard.decorators";
|
|
3
|
+
/**
|
|
4
|
+
* Exception Filter Decorators and Interfaces for WynkJS Framework
|
|
5
|
+
* Exception filters for error handling
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Wynk exception filter interface
|
|
9
|
+
*/
|
|
10
|
+
export interface WynkExceptionFilter<T = any> {
|
|
11
|
+
catch(exception: T, context: ExecutionContext): any;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* @Catch decorator - Define which exceptions the filter catches
|
|
15
|
+
* @param exceptions Exception types to catch
|
|
16
|
+
* @example
|
|
17
|
+
* @Catch(HttpException)
|
|
18
|
+
* export class HttpWynkExceptionFilter implements WynkExceptionFilter {}
|
|
19
|
+
*
|
|
20
|
+
* @Catch() // Catches all exceptions
|
|
21
|
+
* export class AllExceptionsFilter implements WynkExceptionFilter {}
|
|
22
|
+
*/
|
|
23
|
+
export declare function Catch(...exceptions: any[]): ClassDecorator;
|
|
24
|
+
/**
|
|
25
|
+
* @UseFilters decorator - Apply exception filters
|
|
26
|
+
* @param filters Filter classes to apply
|
|
27
|
+
* @example
|
|
28
|
+
* @UseFilters(HttpWynkExceptionFilter)
|
|
29
|
+
* @Controller('/users')
|
|
30
|
+
* export class UserController {}
|
|
31
|
+
*
|
|
32
|
+
* @Post()
|
|
33
|
+
* @UseFilters(ValidationWynkExceptionFilter)
|
|
34
|
+
* create(@Body() dto: CreateDto) {}
|
|
35
|
+
*/
|
|
36
|
+
export declare function UseFilters(...filters: (Function | WynkExceptionFilter)[]): MethodDecorator & ClassDecorator;
|
|
37
|
+
/**
|
|
38
|
+
* Built-in HTTP Exceptions
|
|
39
|
+
*/
|
|
40
|
+
export declare class HttpException extends Error {
|
|
41
|
+
readonly message: string;
|
|
42
|
+
readonly statusCode: number;
|
|
43
|
+
readonly error?: string | undefined;
|
|
44
|
+
constructor(message: string, statusCode: number, error?: string | undefined);
|
|
45
|
+
getStatus(): number;
|
|
46
|
+
getResponse(): any;
|
|
47
|
+
}
|
|
48
|
+
export declare class BadRequestException extends HttpException {
|
|
49
|
+
constructor(message?: string);
|
|
50
|
+
}
|
|
51
|
+
export declare class UnauthorizedException extends HttpException {
|
|
52
|
+
constructor(message?: string);
|
|
53
|
+
}
|
|
54
|
+
export declare class ForbiddenException extends HttpException {
|
|
55
|
+
constructor(message?: string);
|
|
56
|
+
}
|
|
57
|
+
export declare class NotFoundException extends HttpException {
|
|
58
|
+
constructor(message?: string);
|
|
59
|
+
}
|
|
60
|
+
export declare class MethodNotAllowedException extends HttpException {
|
|
61
|
+
constructor(message?: string);
|
|
62
|
+
}
|
|
63
|
+
export declare class NotAcceptableException extends HttpException {
|
|
64
|
+
constructor(message?: string);
|
|
65
|
+
}
|
|
66
|
+
export declare class RequestTimeoutException extends HttpException {
|
|
67
|
+
constructor(message?: string);
|
|
68
|
+
}
|
|
69
|
+
export declare class ConflictException extends HttpException {
|
|
70
|
+
constructor(message?: string);
|
|
71
|
+
}
|
|
72
|
+
export declare class AlreadyExistsException extends HttpException {
|
|
73
|
+
constructor(message?: string);
|
|
74
|
+
}
|
|
75
|
+
export declare class GoneException extends HttpException {
|
|
76
|
+
constructor(message?: string);
|
|
77
|
+
}
|
|
78
|
+
export declare class PayloadTooLargeException extends HttpException {
|
|
79
|
+
constructor(message?: string);
|
|
80
|
+
}
|
|
81
|
+
export declare class UnsupportedMediaTypeException extends HttpException {
|
|
82
|
+
constructor(message?: string);
|
|
83
|
+
}
|
|
84
|
+
export declare class UnprocessableEntityException extends HttpException {
|
|
85
|
+
constructor(message?: string);
|
|
86
|
+
}
|
|
87
|
+
export declare class InternalServerErrorException extends HttpException {
|
|
88
|
+
constructor(message?: string);
|
|
89
|
+
}
|
|
90
|
+
export declare class NotImplementedException extends HttpException {
|
|
91
|
+
constructor(message?: string);
|
|
92
|
+
}
|
|
93
|
+
export declare class BadGatewayException extends HttpException {
|
|
94
|
+
constructor(message?: string);
|
|
95
|
+
}
|
|
96
|
+
export declare class ServiceUnavailableException extends HttpException {
|
|
97
|
+
constructor(message?: string);
|
|
98
|
+
}
|
|
99
|
+
export declare class GatewayTimeoutException extends HttpException {
|
|
100
|
+
constructor(message?: string);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Helper function to execute exception filters
|
|
104
|
+
*/
|
|
105
|
+
export declare function executeExceptionFilters(filters: (Function | WynkExceptionFilter)[], exception: any, context: ExecutionContext): Promise<any>;
|
|
106
|
+
/**
|
|
107
|
+
* Built-in Exception Filters
|
|
108
|
+
*/
|
|
109
|
+
/**
|
|
110
|
+
* Default HTTP exception filter
|
|
111
|
+
*/
|
|
112
|
+
export declare class HttpWynkExceptionFilter implements WynkExceptionFilter<HttpException> {
|
|
113
|
+
catch(exception: HttpException, context: ExecutionContext): any;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* All exceptions filter - catches everything
|
|
117
|
+
*/
|
|
118
|
+
export declare class AllExceptionsFilter implements WynkExceptionFilter {
|
|
119
|
+
catch(exception: any, context: ExecutionContext): any;
|
|
120
|
+
}
|
|
121
|
+
//# sourceMappingURL=exception.decorators.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"exception.decorators.d.ts","sourceRoot":"","sources":["../../core/decorators/exception.decorators.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
import "reflect-metadata";
|
|
8
|
+
/**
|
|
9
|
+
* @Catch decorator - Define which exceptions the filter catches
|
|
10
|
+
* @param exceptions Exception types to catch
|
|
11
|
+
* @example
|
|
12
|
+
* @Catch(HttpException)
|
|
13
|
+
* export class HttpWynkExceptionFilter implements WynkExceptionFilter {}
|
|
14
|
+
*
|
|
15
|
+
* @Catch() // Catches all exceptions
|
|
16
|
+
* export class AllExceptionsFilter implements WynkExceptionFilter {}
|
|
17
|
+
*/
|
|
18
|
+
export function Catch(...exceptions) {
|
|
19
|
+
return (target) => {
|
|
20
|
+
Reflect.defineMetadata("catch:exceptions", exceptions, target);
|
|
21
|
+
return target;
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* @UseFilters decorator - Apply exception filters
|
|
26
|
+
* @param filters Filter classes to apply
|
|
27
|
+
* @example
|
|
28
|
+
* @UseFilters(HttpWynkExceptionFilter)
|
|
29
|
+
* @Controller('/users')
|
|
30
|
+
* export class UserController {}
|
|
31
|
+
*
|
|
32
|
+
* @Post()
|
|
33
|
+
* @UseFilters(ValidationWynkExceptionFilter)
|
|
34
|
+
* create(@Body() dto: CreateDto) {}
|
|
35
|
+
*/
|
|
36
|
+
export function UseFilters(...filters) {
|
|
37
|
+
return (target, propertyKey, descriptor) => {
|
|
38
|
+
if (propertyKey && descriptor) {
|
|
39
|
+
// Method decorator
|
|
40
|
+
const existing = Reflect.getMetadata("filters", target, propertyKey) || [];
|
|
41
|
+
Reflect.defineMetadata("filters", [...existing, ...filters], target, propertyKey);
|
|
42
|
+
return descriptor;
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
// Class decorator
|
|
46
|
+
const existing = Reflect.getMetadata("filters", target) || [];
|
|
47
|
+
Reflect.defineMetadata("filters", [...existing, ...filters], target);
|
|
48
|
+
return target;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Built-in HTTP Exceptions
|
|
54
|
+
*/
|
|
55
|
+
export class HttpException extends Error {
|
|
56
|
+
message;
|
|
57
|
+
statusCode;
|
|
58
|
+
error;
|
|
59
|
+
constructor(message, statusCode, error) {
|
|
60
|
+
super(message);
|
|
61
|
+
this.message = message;
|
|
62
|
+
this.statusCode = statusCode;
|
|
63
|
+
this.error = error;
|
|
64
|
+
this.name = "HttpException";
|
|
65
|
+
}
|
|
66
|
+
getStatus() {
|
|
67
|
+
return this.statusCode;
|
|
68
|
+
}
|
|
69
|
+
getResponse() {
|
|
70
|
+
return {
|
|
71
|
+
statusCode: this.statusCode,
|
|
72
|
+
message: this.message,
|
|
73
|
+
error: this.error,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
export class BadRequestException extends HttpException {
|
|
78
|
+
constructor(message = "Bad Request") {
|
|
79
|
+
super(message, 400, "Bad Request");
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
export class UnauthorizedException extends HttpException {
|
|
83
|
+
constructor(message = "Unauthorized") {
|
|
84
|
+
super(message, 401, "Unauthorized");
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
export class ForbiddenException extends HttpException {
|
|
88
|
+
constructor(message = "Forbidden") {
|
|
89
|
+
super(message, 403, "Forbidden");
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
export class NotFoundException extends HttpException {
|
|
93
|
+
constructor(message = "Not Found") {
|
|
94
|
+
super(message, 404, "Not Found");
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
export class MethodNotAllowedException extends HttpException {
|
|
98
|
+
constructor(message = "Method Not Allowed") {
|
|
99
|
+
super(message, 405, "Method Not Allowed");
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
export class NotAcceptableException extends HttpException {
|
|
103
|
+
constructor(message = "Not Acceptable") {
|
|
104
|
+
super(message, 406, "Not Acceptable");
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
export class RequestTimeoutException extends HttpException {
|
|
108
|
+
constructor(message = "Request Timeout") {
|
|
109
|
+
super(message, 408, "Request Timeout");
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
export class ConflictException extends HttpException {
|
|
113
|
+
constructor(message = "Conflict") {
|
|
114
|
+
super(message, 409, "Conflict");
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
export class AlreadyExistsException extends HttpException {
|
|
118
|
+
constructor(message = "Resource Already Exists") {
|
|
119
|
+
super(message, 409, "Conflict");
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
export class GoneException extends HttpException {
|
|
123
|
+
constructor(message = "Gone") {
|
|
124
|
+
super(message, 410, "Gone");
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
export class PayloadTooLargeException extends HttpException {
|
|
128
|
+
constructor(message = "Payload Too Large") {
|
|
129
|
+
super(message, 413, "Payload Too Large");
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
export class UnsupportedMediaTypeException extends HttpException {
|
|
133
|
+
constructor(message = "Unsupported Media Type") {
|
|
134
|
+
super(message, 415, "Unsupported Media Type");
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
export class UnprocessableEntityException extends HttpException {
|
|
138
|
+
constructor(message = "Unprocessable Entity") {
|
|
139
|
+
super(message, 422, "Unprocessable Entity");
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
export class InternalServerErrorException extends HttpException {
|
|
143
|
+
constructor(message = "Internal Server Error") {
|
|
144
|
+
super(message, 500, "Internal Server Error");
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
export class NotImplementedException extends HttpException {
|
|
148
|
+
constructor(message = "Not Implemented") {
|
|
149
|
+
super(message, 501, "Not Implemented");
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
export class BadGatewayException extends HttpException {
|
|
153
|
+
constructor(message = "Bad Gateway") {
|
|
154
|
+
super(message, 502, "Bad Gateway");
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
export class ServiceUnavailableException extends HttpException {
|
|
158
|
+
constructor(message = "Service Unavailable") {
|
|
159
|
+
super(message, 503, "Service Unavailable");
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
export class GatewayTimeoutException extends HttpException {
|
|
163
|
+
constructor(message = "Gateway Timeout") {
|
|
164
|
+
super(message, 504, "Gateway Timeout");
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Helper function to execute exception filters
|
|
169
|
+
*/
|
|
170
|
+
export async function executeExceptionFilters(filters, exception, context) {
|
|
171
|
+
for (const filter of filters) {
|
|
172
|
+
let filterInstance;
|
|
173
|
+
if (typeof filter === "function") {
|
|
174
|
+
filterInstance = new filter();
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
filterInstance = filter;
|
|
178
|
+
}
|
|
179
|
+
// Check if filter handles this exception type
|
|
180
|
+
const catchTypes = Reflect.getMetadata("catch:exceptions", filterInstance.constructor);
|
|
181
|
+
if (!catchTypes || catchTypes.length === 0) {
|
|
182
|
+
// Catches all exceptions
|
|
183
|
+
return filterInstance.catch(exception, context);
|
|
184
|
+
}
|
|
185
|
+
// Check if exception matches any of the catch types
|
|
186
|
+
for (const catchType of catchTypes) {
|
|
187
|
+
if (exception instanceof catchType) {
|
|
188
|
+
return filterInstance.catch(exception, context);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
// If no filter handles the exception, rethrow it
|
|
193
|
+
throw exception;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Built-in Exception Filters
|
|
197
|
+
*/
|
|
198
|
+
/**
|
|
199
|
+
* Default HTTP exception filter
|
|
200
|
+
*/
|
|
201
|
+
let HttpWynkExceptionFilter = class HttpWynkExceptionFilter {
|
|
202
|
+
catch(exception, context) {
|
|
203
|
+
const response = context.getResponse();
|
|
204
|
+
const status = exception.getStatus();
|
|
205
|
+
return {
|
|
206
|
+
statusCode: status,
|
|
207
|
+
timestamp: new Date().toISOString(),
|
|
208
|
+
...exception.getResponse(),
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
HttpWynkExceptionFilter = __decorate([
|
|
213
|
+
Catch(HttpException)
|
|
214
|
+
], HttpWynkExceptionFilter);
|
|
215
|
+
export { HttpWynkExceptionFilter };
|
|
216
|
+
/**
|
|
217
|
+
* All exceptions filter - catches everything
|
|
218
|
+
*/
|
|
219
|
+
let AllExceptionsFilter = class AllExceptionsFilter {
|
|
220
|
+
catch(exception, context) {
|
|
221
|
+
const response = context.getResponse();
|
|
222
|
+
if (exception instanceof HttpException) {
|
|
223
|
+
const status = exception.getStatus();
|
|
224
|
+
return {
|
|
225
|
+
statusCode: status,
|
|
226
|
+
timestamp: new Date().toISOString(),
|
|
227
|
+
...exception.getResponse(),
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
// Unknown exception
|
|
231
|
+
return {
|
|
232
|
+
statusCode: 500,
|
|
233
|
+
timestamp: new Date().toISOString(),
|
|
234
|
+
message: exception.message || "Internal server error",
|
|
235
|
+
error: "Internal Server Error",
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
};
|
|
239
|
+
AllExceptionsFilter = __decorate([
|
|
240
|
+
Catch()
|
|
241
|
+
], AllExceptionsFilter);
|
|
242
|
+
export { AllExceptionsFilter };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
/**
|
|
3
|
+
* Guard Decorators and Interfaces for ElysiaJS Framework
|
|
4
|
+
* Similar to NestJS guards for route protection
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Execution context interface - provides access to request details
|
|
8
|
+
*/
|
|
9
|
+
export interface ExecutionContext {
|
|
10
|
+
getRequest<T = any>(): T;
|
|
11
|
+
getResponse<T = any>(): T;
|
|
12
|
+
getContext<T = any>(): T;
|
|
13
|
+
getHandler(): Function;
|
|
14
|
+
getClass(): any;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* CanActivate interface - All guards must implement this
|
|
18
|
+
*/
|
|
19
|
+
export interface CanActivate {
|
|
20
|
+
canActivate(context: ExecutionContext): boolean | Promise<boolean>;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* @UseGuards decorator - Apply guards to routes or controllers
|
|
24
|
+
* @param guards Guard classes to apply
|
|
25
|
+
* @example
|
|
26
|
+
* @Controller('/admin')
|
|
27
|
+
* @UseGuards(AuthGuard, RolesGuard)
|
|
28
|
+
* export class AdminController {}
|
|
29
|
+
*
|
|
30
|
+
* @Get('/profile')
|
|
31
|
+
* @UseGuards(AuthGuard)
|
|
32
|
+
* getProfile() {}
|
|
33
|
+
*/
|
|
34
|
+
export declare function UseGuards(...guards: (Function | CanActivate)[]): MethodDecorator & ClassDecorator;
|
|
35
|
+
/**
|
|
36
|
+
* Helper function to create execution context
|
|
37
|
+
*/
|
|
38
|
+
export declare function createExecutionContext(ctx: any, handler: Function, controllerClass: any): ExecutionContext;
|
|
39
|
+
/**
|
|
40
|
+
* Helper function to execute guards
|
|
41
|
+
*/
|
|
42
|
+
export declare function executeGuards(guards: (Function | CanActivate)[], context: ExecutionContext): Promise<boolean>;
|
|
43
|
+
//# sourceMappingURL=guard.decorators.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"guard.decorators.d.ts","sourceRoot":"","sources":["../../core/decorators/guard.decorators.ts"],"names":[],"mappings":"AAAA,OAAO,kBAAkB,CAAC;AAE1B;;;GAGG;AAEH;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B,UAAU,CAAC,CAAC,GAAG,GAAG,KAAK,CAAC,CAAC;IACzB,WAAW,CAAC,CAAC,GAAG,GAAG,KAAK,CAAC,CAAC;IAC1B,UAAU,CAAC,CAAC,GAAG,GAAG,KAAK,CAAC,CAAC;IACzB,UAAU,IAAI,QAAQ,CAAC;IACvB,QAAQ,IAAI,GAAG,CAAC;CACjB;AAED;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,WAAW,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;CACpE;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,SAAS,CACvB,GAAG,MAAM,EAAE,CAAC,QAAQ,GAAG,WAAW,CAAC,EAAE,GACpC,eAAe,GAAG,cAAc,CAwBlC;AAED;;GAEG;AACH,wBAAgB,sBAAsB,CACpC,GAAG,EAAE,GAAG,EACR,OAAO,EAAE,QAAQ,EACjB,eAAe,EAAE,GAAG,GACnB,gBAAgB,CAQlB;AAED;;GAEG;AACH,wBAAsB,aAAa,CACjC,MAAM,EAAE,CAAC,QAAQ,GAAG,WAAW,CAAC,EAAE,EAClC,OAAO,EAAE,gBAAgB,GACxB,OAAO,CAAC,OAAO,CAAC,CAyBlB"}
|