@avleon/core 0.0.26 → 0.0.28
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 +601 -561
- package/package.json +38 -6
- package/src/application.ts +104 -125
- package/src/authentication.ts +16 -16
- package/src/cache.ts +91 -91
- package/src/collection.test.ts +71 -0
- package/src/collection.ts +344 -254
- package/src/config.test.ts +35 -0
- package/src/config.ts +85 -42
- package/src/constants.ts +1 -1
- package/src/container.ts +54 -54
- package/src/controller.ts +125 -127
- package/src/decorators.ts +27 -27
- package/src/environment-variables.ts +53 -46
- package/src/exceptions/http-exceptions.ts +86 -86
- package/src/exceptions/index.ts +1 -1
- package/src/exceptions/system-exception.ts +35 -34
- package/src/file-storage.ts +206 -206
- package/src/helpers.ts +324 -328
- package/src/icore.ts +66 -90
- package/src/index.ts +30 -30
- package/src/interfaces/avleon-application.ts +32 -40
- package/src/logger.ts +72 -72
- package/src/map-types.ts +159 -159
- package/src/middleware.ts +119 -98
- package/src/multipart.ts +116 -116
- package/src/openapi.ts +372 -372
- package/src/params.ts +111 -111
- package/src/queue.ts +126 -126
- package/src/response.ts +74 -74
- package/src/results.ts +30 -30
- package/src/route-methods.ts +186 -186
- package/src/swagger-schema.ts +213 -213
- package/src/testing.ts +220 -220
- package/src/types/app-builder.interface.ts +18 -19
- package/src/types/application.interface.ts +7 -9
- package/src/utils/hash.ts +8 -5
- package/src/utils/index.ts +2 -2
- package/src/utils/optional-require.ts +50 -50
- package/src/validation.ts +160 -156
- package/src/validator-extend.ts +25 -25
package/src/route-methods.ts
CHANGED
|
@@ -1,186 +1,186 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @copyright 2024
|
|
3
|
-
* @author Tareq Hossain
|
|
4
|
-
* @email xtrinsic96@gmail.com
|
|
5
|
-
* @url https://github.com/xtareq
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import { CONTROLLER_META_KEY, ROUTE_META_KEY } from "./container";
|
|
9
|
-
import { OpenApiOptions } from "./openapi";
|
|
10
|
-
import { HttpResponse } from "./response";
|
|
11
|
-
import { Results } from "./results";
|
|
12
|
-
export type RouteMethods =
|
|
13
|
-
| "GET"
|
|
14
|
-
| "POST"
|
|
15
|
-
| "PUT"
|
|
16
|
-
| "PATCH"
|
|
17
|
-
| "DELETE"
|
|
18
|
-
| "OPTIONS"
|
|
19
|
-
| "ALL";
|
|
20
|
-
|
|
21
|
-
const schema: OpenApiOptions = {
|
|
22
|
-
tags: ["hello"],
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Options for defining a route's method and metadata.
|
|
27
|
-
*/
|
|
28
|
-
export type RouteMethodOptions = {
|
|
29
|
-
/**
|
|
30
|
-
* HTTP method for the route (e.g., GET, POST, PUT, DELETE).
|
|
31
|
-
*/
|
|
32
|
-
method?: RouteMethods;
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* The path or endpoint for the route (e.g., "/users/:id").
|
|
36
|
-
*/
|
|
37
|
-
path?: string;
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* OpenAPI metadata for the route, including summary and description.
|
|
41
|
-
*/
|
|
42
|
-
openapi?: OpenApiOptions;
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Name of the route.
|
|
46
|
-
*
|
|
47
|
-
* @description If Swagger is enabled in the project, this will appear as a tag in the generated documentation.
|
|
48
|
-
*/
|
|
49
|
-
name?: string;
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
// Overloads
|
|
53
|
-
|
|
54
|
-
// Implementation
|
|
55
|
-
export function createRouteDecorator(
|
|
56
|
-
method: RouteMethods = "GET",
|
|
57
|
-
): (
|
|
58
|
-
pathOrOptions: string | RouteMethodOptions,
|
|
59
|
-
maybeOptions?: RouteMethodOptions,
|
|
60
|
-
) => MethodDecorator {
|
|
61
|
-
return function (
|
|
62
|
-
pathOrOptions: string | RouteMethodOptions,
|
|
63
|
-
maybeOptions?: RouteMethodOptions,
|
|
64
|
-
): MethodDecorator {
|
|
65
|
-
return function (
|
|
66
|
-
target,
|
|
67
|
-
propertyKey: string | symbol,
|
|
68
|
-
descriptor: PropertyDescriptor,
|
|
69
|
-
) {
|
|
70
|
-
let path = "/";
|
|
71
|
-
let options: RouteMethodOptions = {};
|
|
72
|
-
|
|
73
|
-
if (typeof pathOrOptions === "string") {
|
|
74
|
-
path = pathOrOptions;
|
|
75
|
-
options = maybeOptions || {};
|
|
76
|
-
} else if (typeof pathOrOptions === "object") {
|
|
77
|
-
options = pathOrOptions;
|
|
78
|
-
path = options.name || "/";
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
// Define metadata
|
|
82
|
-
Reflect.defineMetadata("route:path", path, target, propertyKey);
|
|
83
|
-
Reflect.defineMetadata(
|
|
84
|
-
"route:method",
|
|
85
|
-
method || "GET",
|
|
86
|
-
target,
|
|
87
|
-
propertyKey,
|
|
88
|
-
);
|
|
89
|
-
Reflect.getMetadata(CONTROLLER_META_KEY, target.constructor);
|
|
90
|
-
Reflect.defineMetadata(
|
|
91
|
-
ROUTE_META_KEY,
|
|
92
|
-
{ ...options, method, path, controller: target.constructor.name },
|
|
93
|
-
target,
|
|
94
|
-
propertyKey,
|
|
95
|
-
);
|
|
96
|
-
|
|
97
|
-
if (options) {
|
|
98
|
-
Reflect.defineMetadata("route:options", options, target, propertyKey);
|
|
99
|
-
}
|
|
100
|
-
};
|
|
101
|
-
};
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
// Usage Example
|
|
105
|
-
/**
|
|
106
|
-
* @description HTTP Get method
|
|
107
|
-
* @param {string} path
|
|
108
|
-
*/
|
|
109
|
-
export function Get(path?: string): MethodDecorator;
|
|
110
|
-
export function Get(path: string | RouteMethodOptions): MethodDecorator;
|
|
111
|
-
/**
|
|
112
|
-
* @description HTTP Get method
|
|
113
|
-
* @param {string} path
|
|
114
|
-
* @param {RouteMethodOptions} options
|
|
115
|
-
*/
|
|
116
|
-
export function Get(path: string, options: RouteMethodOptions): MethodDecorator;
|
|
117
|
-
export function Get(
|
|
118
|
-
path?: string | RouteMethodOptions,
|
|
119
|
-
options?: RouteMethodOptions,
|
|
120
|
-
) {
|
|
121
|
-
const parsedPath =
|
|
122
|
-
!path && !options ? "/" : (path as string | RouteMethodOptions);
|
|
123
|
-
if (options) {
|
|
124
|
-
return createRouteDecorator("GET")(parsedPath, options);
|
|
125
|
-
} else {
|
|
126
|
-
return createRouteDecorator("GET")(parsedPath);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
export function Post(path?: string): MethodDecorator;
|
|
131
|
-
export function Post(path: string | RouteMethodOptions): MethodDecorator;
|
|
132
|
-
export function Post(
|
|
133
|
-
path: string,
|
|
134
|
-
options: RouteMethodOptions,
|
|
135
|
-
): MethodDecorator;
|
|
136
|
-
export function Post(
|
|
137
|
-
path?: string | RouteMethodOptions,
|
|
138
|
-
options?: RouteMethodOptions,
|
|
139
|
-
) {
|
|
140
|
-
const parsedPath =
|
|
141
|
-
!path && !options ? "/" : (path as string | RouteMethodOptions);
|
|
142
|
-
if (options) {
|
|
143
|
-
return createRouteDecorator("POST")(parsedPath, options);
|
|
144
|
-
} else {
|
|
145
|
-
return createRouteDecorator("POST")(parsedPath);
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
export function Put(path?: string): MethodDecorator;
|
|
150
|
-
export function Put(path: string | RouteMethodOptions): MethodDecorator;
|
|
151
|
-
export function Put(path: string, options: RouteMethodOptions): MethodDecorator;
|
|
152
|
-
export function Put(
|
|
153
|
-
path?: string | RouteMethodOptions,
|
|
154
|
-
options?: RouteMethodOptions,
|
|
155
|
-
) {
|
|
156
|
-
const parsedPath =
|
|
157
|
-
!path && !options ? "/" : (path as string | RouteMethodOptions);
|
|
158
|
-
if (options) {
|
|
159
|
-
return createRouteDecorator("PUT")(parsedPath, options);
|
|
160
|
-
} else {
|
|
161
|
-
return createRouteDecorator("PUT")(parsedPath);
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
export function Delete(path?: string): MethodDecorator;
|
|
166
|
-
export function Delete(path: string | RouteMethodOptions): MethodDecorator;
|
|
167
|
-
export function Delete(
|
|
168
|
-
path: string,
|
|
169
|
-
options: RouteMethodOptions,
|
|
170
|
-
): MethodDecorator;
|
|
171
|
-
export function Delete(
|
|
172
|
-
path?: string | RouteMethodOptions,
|
|
173
|
-
options?: RouteMethodOptions,
|
|
174
|
-
) {
|
|
175
|
-
const parsedPath =
|
|
176
|
-
!path && !options ? "/" : (path as string | RouteMethodOptions);
|
|
177
|
-
if (options) {
|
|
178
|
-
return createRouteDecorator("DELETE")(parsedPath, options);
|
|
179
|
-
} else {
|
|
180
|
-
return createRouteDecorator("DELETE")(parsedPath);
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
export const Patch = createRouteDecorator("PATCH");
|
|
185
|
-
export const Options = createRouteDecorator("OPTIONS");
|
|
186
|
-
export const All = createRouteDecorator("ALL");
|
|
1
|
+
/**
|
|
2
|
+
* @copyright 2024
|
|
3
|
+
* @author Tareq Hossain
|
|
4
|
+
* @email xtrinsic96@gmail.com
|
|
5
|
+
* @url https://github.com/xtareq
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { CONTROLLER_META_KEY, ROUTE_META_KEY } from "./container";
|
|
9
|
+
import { OpenApiOptions } from "./openapi";
|
|
10
|
+
import { HttpResponse } from "./response";
|
|
11
|
+
import { Results } from "./results";
|
|
12
|
+
export type RouteMethods =
|
|
13
|
+
| "GET"
|
|
14
|
+
| "POST"
|
|
15
|
+
| "PUT"
|
|
16
|
+
| "PATCH"
|
|
17
|
+
| "DELETE"
|
|
18
|
+
| "OPTIONS"
|
|
19
|
+
| "ALL";
|
|
20
|
+
|
|
21
|
+
const schema: OpenApiOptions = {
|
|
22
|
+
tags: ["hello"],
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Options for defining a route's method and metadata.
|
|
27
|
+
*/
|
|
28
|
+
export type RouteMethodOptions = {
|
|
29
|
+
/**
|
|
30
|
+
* HTTP method for the route (e.g., GET, POST, PUT, DELETE).
|
|
31
|
+
*/
|
|
32
|
+
method?: RouteMethods;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* The path or endpoint for the route (e.g., "/users/:id").
|
|
36
|
+
*/
|
|
37
|
+
path?: string;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* OpenAPI metadata for the route, including summary and description.
|
|
41
|
+
*/
|
|
42
|
+
openapi?: OpenApiOptions;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Name of the route.
|
|
46
|
+
*
|
|
47
|
+
* @description If Swagger is enabled in the project, this will appear as a tag in the generated documentation.
|
|
48
|
+
*/
|
|
49
|
+
name?: string;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
// Overloads
|
|
53
|
+
|
|
54
|
+
// Implementation
|
|
55
|
+
export function createRouteDecorator(
|
|
56
|
+
method: RouteMethods = "GET",
|
|
57
|
+
): (
|
|
58
|
+
pathOrOptions: string | RouteMethodOptions,
|
|
59
|
+
maybeOptions?: RouteMethodOptions,
|
|
60
|
+
) => MethodDecorator {
|
|
61
|
+
return function (
|
|
62
|
+
pathOrOptions: string | RouteMethodOptions,
|
|
63
|
+
maybeOptions?: RouteMethodOptions,
|
|
64
|
+
): MethodDecorator {
|
|
65
|
+
return function (
|
|
66
|
+
target,
|
|
67
|
+
propertyKey: string | symbol,
|
|
68
|
+
descriptor: PropertyDescriptor,
|
|
69
|
+
) {
|
|
70
|
+
let path = "/";
|
|
71
|
+
let options: RouteMethodOptions = {};
|
|
72
|
+
|
|
73
|
+
if (typeof pathOrOptions === "string") {
|
|
74
|
+
path = pathOrOptions;
|
|
75
|
+
options = maybeOptions || {};
|
|
76
|
+
} else if (typeof pathOrOptions === "object") {
|
|
77
|
+
options = pathOrOptions;
|
|
78
|
+
path = options.name || "/";
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Define metadata
|
|
82
|
+
Reflect.defineMetadata("route:path", path, target, propertyKey);
|
|
83
|
+
Reflect.defineMetadata(
|
|
84
|
+
"route:method",
|
|
85
|
+
method || "GET",
|
|
86
|
+
target,
|
|
87
|
+
propertyKey,
|
|
88
|
+
);
|
|
89
|
+
Reflect.getMetadata(CONTROLLER_META_KEY, target.constructor);
|
|
90
|
+
Reflect.defineMetadata(
|
|
91
|
+
ROUTE_META_KEY,
|
|
92
|
+
{ ...options, method, path, controller: target.constructor.name },
|
|
93
|
+
target,
|
|
94
|
+
propertyKey,
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
if (options) {
|
|
98
|
+
Reflect.defineMetadata("route:options", options, target, propertyKey);
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// Usage Example
|
|
105
|
+
/**
|
|
106
|
+
* @description HTTP Get method
|
|
107
|
+
* @param {string} path
|
|
108
|
+
*/
|
|
109
|
+
export function Get(path?: string): MethodDecorator;
|
|
110
|
+
export function Get(path: string | RouteMethodOptions): MethodDecorator;
|
|
111
|
+
/**
|
|
112
|
+
* @description HTTP Get method
|
|
113
|
+
* @param {string} path
|
|
114
|
+
* @param {RouteMethodOptions} options
|
|
115
|
+
*/
|
|
116
|
+
export function Get(path: string, options: RouteMethodOptions): MethodDecorator;
|
|
117
|
+
export function Get(
|
|
118
|
+
path?: string | RouteMethodOptions,
|
|
119
|
+
options?: RouteMethodOptions,
|
|
120
|
+
) {
|
|
121
|
+
const parsedPath =
|
|
122
|
+
!path && !options ? "/" : (path as string | RouteMethodOptions);
|
|
123
|
+
if (options) {
|
|
124
|
+
return createRouteDecorator("GET")(parsedPath, options);
|
|
125
|
+
} else {
|
|
126
|
+
return createRouteDecorator("GET")(parsedPath);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
export function Post(path?: string): MethodDecorator;
|
|
131
|
+
export function Post(path: string | RouteMethodOptions): MethodDecorator;
|
|
132
|
+
export function Post(
|
|
133
|
+
path: string,
|
|
134
|
+
options: RouteMethodOptions,
|
|
135
|
+
): MethodDecorator;
|
|
136
|
+
export function Post(
|
|
137
|
+
path?: string | RouteMethodOptions,
|
|
138
|
+
options?: RouteMethodOptions,
|
|
139
|
+
) {
|
|
140
|
+
const parsedPath =
|
|
141
|
+
!path && !options ? "/" : (path as string | RouteMethodOptions);
|
|
142
|
+
if (options) {
|
|
143
|
+
return createRouteDecorator("POST")(parsedPath, options);
|
|
144
|
+
} else {
|
|
145
|
+
return createRouteDecorator("POST")(parsedPath);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export function Put(path?: string): MethodDecorator;
|
|
150
|
+
export function Put(path: string | RouteMethodOptions): MethodDecorator;
|
|
151
|
+
export function Put(path: string, options: RouteMethodOptions): MethodDecorator;
|
|
152
|
+
export function Put(
|
|
153
|
+
path?: string | RouteMethodOptions,
|
|
154
|
+
options?: RouteMethodOptions,
|
|
155
|
+
) {
|
|
156
|
+
const parsedPath =
|
|
157
|
+
!path && !options ? "/" : (path as string | RouteMethodOptions);
|
|
158
|
+
if (options) {
|
|
159
|
+
return createRouteDecorator("PUT")(parsedPath, options);
|
|
160
|
+
} else {
|
|
161
|
+
return createRouteDecorator("PUT")(parsedPath);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export function Delete(path?: string): MethodDecorator;
|
|
166
|
+
export function Delete(path: string | RouteMethodOptions): MethodDecorator;
|
|
167
|
+
export function Delete(
|
|
168
|
+
path: string,
|
|
169
|
+
options: RouteMethodOptions,
|
|
170
|
+
): MethodDecorator;
|
|
171
|
+
export function Delete(
|
|
172
|
+
path?: string | RouteMethodOptions,
|
|
173
|
+
options?: RouteMethodOptions,
|
|
174
|
+
) {
|
|
175
|
+
const parsedPath =
|
|
176
|
+
!path && !options ? "/" : (path as string | RouteMethodOptions);
|
|
177
|
+
if (options) {
|
|
178
|
+
return createRouteDecorator("DELETE")(parsedPath, options);
|
|
179
|
+
} else {
|
|
180
|
+
return createRouteDecorator("DELETE")(parsedPath);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export const Patch = createRouteDecorator("PATCH");
|
|
185
|
+
export const Options = createRouteDecorator("OPTIONS");
|
|
186
|
+
export const All = createRouteDecorator("ALL");
|