@zenofolio/hyper-decor 0.0.3
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/.mocharc.js +5 -0
- package/LICENSE +21 -0
- package/README.md +191 -0
- package/dist/__internals/constants.d.ts +61 -0
- package/dist/__internals/constants.js +72 -0
- package/dist/__internals/creators/request.creator.d.ts +12 -0
- package/dist/__internals/creators/request.creator.js +53 -0
- package/dist/__internals/creators/routes.creator.d.ts +10 -0
- package/dist/__internals/creators/routes.creator.js +37 -0
- package/dist/__internals/decorator-base.d.ts +30 -0
- package/dist/__internals/decorator-base.js +86 -0
- package/dist/__internals/helpers/merge-metadata.d.ts +14 -0
- package/dist/__internals/helpers/merge-metadata.js +44 -0
- package/dist/__internals/helpers/who.helper.d.ts +9 -0
- package/dist/__internals/helpers/who.helper.js +19 -0
- package/dist/__internals/store.d.ts +10 -0
- package/dist/__internals/store.js +17 -0
- package/dist/__internals/stores/index.d.ts +15 -0
- package/dist/__internals/stores/index.js +37 -0
- package/dist/__internals/stores/middleware.store.d.ts +7 -0
- package/dist/__internals/stores/middleware.store.js +19 -0
- package/dist/__internals/stores/params.store.d.ts +21 -0
- package/dist/__internals/stores/params.store.js +65 -0
- package/dist/__internals/stores/routes.store.d.ts +17 -0
- package/dist/__internals/stores/routes.store.js +43 -0
- package/dist/__internals/stores/store.interface.d.ts +8 -0
- package/dist/__internals/stores/store.interface.js +2 -0
- package/dist/__internals/transform/method.transform.d.ts +2 -0
- package/dist/__internals/transform/method.transform.js +20 -0
- package/dist/__internals/transform/pass.transfrom.d.ts +1 -0
- package/dist/__internals/transform/pass.transfrom.js +2 -0
- package/dist/__internals/transform/role.transform.d.ts +7 -0
- package/dist/__internals/transform/role.transform.js +86 -0
- package/dist/__internals/transform/scope.transfrom.d.ts +7 -0
- package/dist/__internals/transform/scope.transfrom.js +72 -0
- package/dist/__internals/types.d.ts +8 -0
- package/dist/__internals/types.js +2 -0
- package/dist/__internals/utils/function.util.d.ts +4 -0
- package/dist/__internals/utils/function.util.js +20 -0
- package/dist/__internals/utils/mixin.utils.d.ts +11 -0
- package/dist/__internals/utils/mixin.utils.js +34 -0
- package/dist/__internals/utils/object.util.d.ts +27 -0
- package/dist/__internals/utils/object.util.js +72 -0
- package/dist/__internals/utils/path.util.d.ts +4 -0
- package/dist/__internals/utils/path.util.js +8 -0
- package/dist/__internals/utils/router.d.ts +1 -0
- package/dist/__internals/utils/router.js +2 -0
- package/dist/common/bootstrap.d.ts +3 -0
- package/dist/common/bootstrap.js +25 -0
- package/dist/common/helpers/index.d.ts +2 -0
- package/dist/common/helpers/index.js +18 -0
- package/dist/common/helpers/role.d.ts +22 -0
- package/dist/common/helpers/role.js +43 -0
- package/dist/common/helpers/scopes.d.ts +23 -0
- package/dist/common/helpers/scopes.js +47 -0
- package/dist/common/openapi/collect-class-data.d.ts +21 -0
- package/dist/common/openapi/collect-class-data.js +45 -0
- package/dist/common/openapi/collect-function-data.d.ts +32 -0
- package/dist/common/openapi/collect-function-data.js +70 -0
- package/dist/common/openapi/index.d.ts +2 -0
- package/dist/common/openapi/index.js +18 -0
- package/dist/constants.d.ts +3 -0
- package/dist/constants.js +6 -0
- package/dist/decorators/File.d.ts +65 -0
- package/dist/decorators/File.js +180 -0
- package/dist/decorators/Http.d.ts +55 -0
- package/dist/decorators/Http.js +93 -0
- package/dist/decorators/HyperApp.d.ts +7 -0
- package/dist/decorators/HyperApp.js +262 -0
- package/dist/decorators/HyperController.d.ts +2 -0
- package/dist/decorators/HyperController.js +19 -0
- package/dist/decorators/HyperModule.d.ts +5 -0
- package/dist/decorators/HyperModule.js +14 -0
- package/dist/decorators/Middleware.d.ts +24 -0
- package/dist/decorators/Middleware.js +51 -0
- package/dist/decorators/Pass.d.ts +12 -0
- package/dist/decorators/Pass.js +29 -0
- package/dist/decorators/Role.d.ts +6 -0
- package/dist/decorators/Role.js +34 -0
- package/dist/decorators/Routes.d.ts +14 -0
- package/dist/decorators/Routes.js +21 -0
- package/dist/decorators/Scope.d.ts +6 -0
- package/dist/decorators/Scope.js +25 -0
- package/dist/decorators/index.d.ts +11 -0
- package/dist/decorators/index.js +27 -0
- package/dist/decorators/types.d.ts +89 -0
- package/dist/decorators/types.js +2 -0
- package/dist/exeptions/DuplicateControllerPathException.d.ts +14 -0
- package/dist/exeptions/DuplicateControllerPathException.js +12 -0
- package/dist/exeptions/DuplicatedException.d.ts +8 -0
- package/dist/exeptions/DuplicatedException.js +12 -0
- package/dist/exeptions/DuplicatedHandlerException.d.ts +4 -0
- package/dist/exeptions/DuplicatedHandlerException.js +12 -0
- package/dist/exeptions/HyperException.d.ts +7 -0
- package/dist/exeptions/HyperException.js +13 -0
- package/dist/exeptions/HyperFileException.d.ts +4 -0
- package/dist/exeptions/HyperFileException.js +12 -0
- package/dist/exeptions/MethodNotFountException.d.ts +4 -0
- package/dist/exeptions/MethodNotFountException.js +12 -0
- package/dist/exeptions/NotPropertyException.d.ts +6 -0
- package/dist/exeptions/NotPropertyException.js +16 -0
- package/dist/exeptions/NotRoleException.d.ts +6 -0
- package/dist/exeptions/NotRoleException.js +17 -0
- package/dist/exeptions/NotScopeException.d.ts +7 -0
- package/dist/exeptions/NotScopeException.js +18 -0
- package/dist/exeptions/WrongPlaceException.d.ts +8 -0
- package/dist/exeptions/WrongPlaceException.js +21 -0
- package/dist/exeptions/index.d.ts +8 -0
- package/dist/exeptions/index.js +18 -0
- package/dist/exeptions/types.d.ts +1 -0
- package/dist/exeptions/types.js +2 -0
- package/dist/extension.d.ts +1 -0
- package/dist/extension.js +41 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +25 -0
- package/dist/type.d.ts +10 -0
- package/dist/type.js +2 -0
- package/hyper-express-decorators.d.ts +16 -0
- package/package.json +61 -0
- package/tsconfig.json +17 -0
package/.mocharc.js
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 @zenofolio
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
# Hyper Express Decorators
|
|
2
|
+
A simple decorators library for Hyper Express
|
|
3
|
+
|
|
4
|
+
### Why use this?
|
|
5
|
+
This is a personal library designed to make it easier for me to create fast APIs. Maybe it can be helpful for someone else too.
|
|
6
|
+
|
|
7
|
+
## Inspiration
|
|
8
|
+
|
|
9
|
+
- [NestJS](https://github.com/nestjs/nest)
|
|
10
|
+
- [HyperExpress](https://github.com/kartikk221/hyper-express)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
## Decorators
|
|
14
|
+
|
|
15
|
+
### Core
|
|
16
|
+
- `@HyperApp` – Defines the application module.
|
|
17
|
+
- `@HyperModule` – Creates a module with controllers.
|
|
18
|
+
- `@HyperController` – Defines a controller within a module.
|
|
19
|
+
|
|
20
|
+
### Request Decorators (`@Req`)
|
|
21
|
+
- `@Body(validator?)` – Retrieves and optionally validates the request body.
|
|
22
|
+
- `@Query(name?: string)` – Extracts query parameters from the request.
|
|
23
|
+
- `@Param(name?: string)` – Extracts route parameters from the request.
|
|
24
|
+
|
|
25
|
+
### Response Decorators
|
|
26
|
+
- `@Res` – Handles the response object.
|
|
27
|
+
|
|
28
|
+
### Middleware & Access Control
|
|
29
|
+
- `@Middleware` – Attaches middleware to a route or controller.
|
|
30
|
+
- `@Role(["ADMIN", "SUPERVISOR"])` – Ensures the request is valid for users with any of the specified roles.
|
|
31
|
+
- `@Scope` – Defines permissions for specific actions.
|
|
32
|
+
|
|
33
|
+
### Route Methods (`@Routes`)
|
|
34
|
+
- `@Get` – Handles GET requests.
|
|
35
|
+
- `@Post` – Handles POST requests.
|
|
36
|
+
- `@Put` – Handles PUT requests.
|
|
37
|
+
- `@Delete` – Handles DELETE requests.
|
|
38
|
+
- `@Patch` – Handles PATCH requests.
|
|
39
|
+
- `@Options` – Handles OPTIONS requests.
|
|
40
|
+
- `@Head` – Handles HEAD requests.
|
|
41
|
+
- `@Trace` – Handles TRACE requests.
|
|
42
|
+
- `@Any` – Handles any type of HTTP request.
|
|
43
|
+
- `@All` – Handles all HTTP requests.
|
|
44
|
+
- `@Connect` – Handles CONNECT requests.
|
|
45
|
+
- `@WS` – Handles WebSocket requests.
|
|
46
|
+
- `@Upgrade` – Handles HTTP upgrade requests.
|
|
47
|
+
|
|
48
|
+
### Custom Parameter Decorators
|
|
49
|
+
Use `createCustomRequestDecorator` if you want to create custom parameter decorators for requests.
|
|
50
|
+
|
|
51
|
+
#### Example: Zod Schema
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
const Parser = <T extends any>(schema: ZodSchema<T> | ZodEffects<any>) =>
|
|
55
|
+
createCustomRequestDecorator(
|
|
56
|
+
'Parser',
|
|
57
|
+
async (request) => schema.parse(await request.json())
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
// Use the decorator
|
|
61
|
+
|
|
62
|
+
const userScheme = z.object({
|
|
63
|
+
name: z.string(),
|
|
64
|
+
email: z.string().email(),
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
type UserScheme = z.infer<typeof userScheme>;
|
|
68
|
+
|
|
69
|
+
@HyperController()
|
|
70
|
+
class ParserController {
|
|
71
|
+
@Post()
|
|
72
|
+
async create(
|
|
73
|
+
@Parser(userScheme) user: UserScheme,
|
|
74
|
+
@Res response: Response
|
|
75
|
+
) {
|
|
76
|
+
response.json(user);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## Add Role | Scope to Request
|
|
82
|
+
|
|
83
|
+
This package extends the `hyper-express/Request` class by adding methods that help manage roles and scopes for requests.
|
|
84
|
+
|
|
85
|
+
### Summary of Available Methods
|
|
86
|
+
- `req.setRole(role)` – Assigns a role to the request.
|
|
87
|
+
- `req.hasRole(role)` – Checks if the request has the specified role.
|
|
88
|
+
- `req.setScopes(scopes)` – Assigns scopes to the request.
|
|
89
|
+
- `req.hasScopes(scopes)` – Checks if the request has the specified scopes.
|
|
90
|
+
- `req.setRoleScopes(role, scopes)` – Sets both the role and the scopes in one method.
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
### examples
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
|
|
97
|
+
@Middleware((req, res, next) => {
|
|
98
|
+
|
|
99
|
+
// Assigning a role to the request
|
|
100
|
+
req.setRole("ADMIN");
|
|
101
|
+
|
|
102
|
+
// Assigning scopes to the request
|
|
103
|
+
req.setScopes(["read", "write"]);
|
|
104
|
+
|
|
105
|
+
// set role and scopes
|
|
106
|
+
// req.setRoleScopes("ADMIN", ["read", "write"]);
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
// Check if the request has the "ADMIN" role
|
|
110
|
+
// if (req.hasRole("ADMIN")) {
|
|
111
|
+
// res.send("Role: ADMIN is assigned");
|
|
112
|
+
// }
|
|
113
|
+
|
|
114
|
+
// Check if the request has the "write" scope
|
|
115
|
+
// if (req.hasScopes(["write"])) {
|
|
116
|
+
// res.send("Scope: write is granted");
|
|
117
|
+
// }
|
|
118
|
+
|
|
119
|
+
next();
|
|
120
|
+
})
|
|
121
|
+
@HyperModule({
|
|
122
|
+
path: 'users'
|
|
123
|
+
})
|
|
124
|
+
class UserModule {}
|
|
125
|
+
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## Usage/Decorators examples
|
|
129
|
+
|
|
130
|
+
`@HyperController` - Simple versioned controller
|
|
131
|
+
```typescript
|
|
132
|
+
@HyperController("v1")
|
|
133
|
+
class TestController extends CRUD<string> {
|
|
134
|
+
|
|
135
|
+
@Get("/list")
|
|
136
|
+
async index(@Query() query: any, @Res() res: Response) {
|
|
137
|
+
res.send("hello");
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
}
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
`@HyperModule` - define module with controllers
|
|
144
|
+
```typescript
|
|
145
|
+
@HyperModule({
|
|
146
|
+
path: "users",
|
|
147
|
+
controllers: [HyperController]
|
|
148
|
+
})
|
|
149
|
+
class UserModule {}
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
`@HyperApp` - define application
|
|
155
|
+
```typescript
|
|
156
|
+
@HyperApp({
|
|
157
|
+
name: "Hyper Express Decorators",
|
|
158
|
+
version: "1.0.0",
|
|
159
|
+
description: "Decorators to make development easier",
|
|
160
|
+
modules: [UserV1Module],
|
|
161
|
+
prefix: "/api",
|
|
162
|
+
})
|
|
163
|
+
export class Application implements IHyperApplication {
|
|
164
|
+
onPrepare() {
|
|
165
|
+
console.log("This method will be called after the app is prepared");
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
## Run Application
|
|
171
|
+
```typescript
|
|
172
|
+
const app = await createApplication(Application)
|
|
173
|
+
await app.listen(3000);
|
|
174
|
+
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
As a result, we get:
|
|
178
|
+
|
|
179
|
+
- `/api/users/v1/list`
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
# Examples
|
|
183
|
+
|
|
184
|
+
- [Add Roles and scopes](./examples/add-roles-and-scopes.ts)
|
|
185
|
+
- [Middleware](./examples/middleware.ts)
|
|
186
|
+
- [File](./examples//upload-file.ts)
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
# All for now
|
|
191
|
+
More documentation will be added here late.
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
export declare const KEY_TYPE_APP = "hyper:type:app";
|
|
2
|
+
export declare const KEY_TYPE_CONTROLLER = "hyper:type:controller";
|
|
3
|
+
export declare const KEY_TYPE_MODULE = "hyper:type:module";
|
|
4
|
+
export declare const KEY_TYPE_ROUTE = "hyper:type:route";
|
|
5
|
+
export declare const KEY_TYPE_SERVICE = "hyper:type:service";
|
|
6
|
+
export type KeyTypes = typeof KEY_TYPE_APP | typeof KEY_TYPE_CONTROLLER | typeof KEY_TYPE_MODULE | typeof KEY_TYPE_SERVICE | typeof KEY_TYPE_ROUTE;
|
|
7
|
+
export declare const KEY_PARAMS_APP = "hyper:type:app";
|
|
8
|
+
export declare const KEY_PARAMS_CONTROLLER = "hyper:type:controller";
|
|
9
|
+
export declare const KEY_PARAMS_MODULE = "hyper:type:module";
|
|
10
|
+
export declare const KEY_PARAMS_ROUTE = "hyper:type:route";
|
|
11
|
+
export declare const KEY_PARAMS_PARAM = "hyper:type:param";
|
|
12
|
+
export declare const KEY_PARAMS_MIDDLEWARES = "hyper:type:middlewares";
|
|
13
|
+
export declare const KEY_PARAMS_SCOPE = "hyper:type:scope";
|
|
14
|
+
export declare const KEY_PARAMS_ROLE = "hyper:type:role";
|
|
15
|
+
export declare const KEY_PARAMS_PASS = "hyper:type:pass";
|
|
16
|
+
export type KeyParams = typeof KEY_PARAMS_APP | typeof KEY_PARAMS_CONTROLLER | typeof KEY_PARAMS_MODULE | typeof KEY_PARAMS_ROUTE | typeof KEY_PARAMS_PARAM | typeof KEY_PARAMS_MIDDLEWARES | typeof KEY_PARAMS_SCOPE | typeof KEY_PARAMS_ROLE | typeof KEY_PARAMS_PASS;
|
|
17
|
+
export declare const KEY_STATE_UPDATED = "hyper:state:updated";
|
|
18
|
+
export declare const KEY_STATE_CREATED = "hyper:state:created";
|
|
19
|
+
export declare const KEY_STATE_PREPARED = "hyper:state:prepared";
|
|
20
|
+
export declare const KEY_STATE_BY_PASS = "hyper:state:bypass";
|
|
21
|
+
export type KeyState = typeof KEY_STATE_UPDATED | typeof KEY_STATE_CREATED | typeof KEY_STATE_PREPARED;
|
|
22
|
+
export declare const DESIGN_PARAMTYPES = "design:paramtypes";
|
|
23
|
+
export declare const DESIGN_RETURNTYPE = "design:returntype";
|
|
24
|
+
export declare const DESIGN_TYPE = "design:type";
|
|
25
|
+
export type DesignKeys = typeof DESIGN_PARAMTYPES | typeof DESIGN_RETURNTYPE | typeof DESIGN_TYPE;
|
|
26
|
+
export declare const METADATA_HYPER_TYPE: {
|
|
27
|
+
APP: string;
|
|
28
|
+
CONTROLLER: string;
|
|
29
|
+
MODULE: string;
|
|
30
|
+
ROUTE: string;
|
|
31
|
+
PARAM: string;
|
|
32
|
+
};
|
|
33
|
+
export declare const METADATA_KEYS: {
|
|
34
|
+
APP_INFO: string;
|
|
35
|
+
MODULES: string;
|
|
36
|
+
PREFIX: string;
|
|
37
|
+
CONTROLLERS: string;
|
|
38
|
+
ROUTES: string;
|
|
39
|
+
ROLES: string;
|
|
40
|
+
SCOPES: string;
|
|
41
|
+
SCOPED: string;
|
|
42
|
+
MIDDLEWARES: string;
|
|
43
|
+
};
|
|
44
|
+
export declare const METADATA_STORE_KEYS: {
|
|
45
|
+
PARAMS: string;
|
|
46
|
+
};
|
|
47
|
+
export declare const METADATA_METHOD_KEYS: {
|
|
48
|
+
ARGUMENTS: string;
|
|
49
|
+
ARGUMENTS_NAMES: string;
|
|
50
|
+
ARGUMENTS_TYPE: string;
|
|
51
|
+
};
|
|
52
|
+
export declare const METADATA_PARAMS_KEYS: {
|
|
53
|
+
DESIGN_PARAM_TYPES: string;
|
|
54
|
+
DESIGN_TYPE: string;
|
|
55
|
+
DESIGN_RETURN_TYPE: string;
|
|
56
|
+
};
|
|
57
|
+
export declare const METADATA_STATE_KEYS: {
|
|
58
|
+
UPDATED: string;
|
|
59
|
+
CREATED: string;
|
|
60
|
+
PREPARED: string;
|
|
61
|
+
};
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.METADATA_STATE_KEYS = exports.METADATA_PARAMS_KEYS = exports.METADATA_METHOD_KEYS = exports.METADATA_STORE_KEYS = exports.METADATA_KEYS = exports.METADATA_HYPER_TYPE = exports.DESIGN_TYPE = exports.DESIGN_RETURNTYPE = exports.DESIGN_PARAMTYPES = exports.KEY_STATE_BY_PASS = exports.KEY_STATE_PREPARED = exports.KEY_STATE_CREATED = exports.KEY_STATE_UPDATED = exports.KEY_PARAMS_PASS = exports.KEY_PARAMS_ROLE = exports.KEY_PARAMS_SCOPE = exports.KEY_PARAMS_MIDDLEWARES = exports.KEY_PARAMS_PARAM = exports.KEY_PARAMS_ROUTE = exports.KEY_PARAMS_MODULE = exports.KEY_PARAMS_CONTROLLER = exports.KEY_PARAMS_APP = exports.KEY_TYPE_SERVICE = exports.KEY_TYPE_ROUTE = exports.KEY_TYPE_MODULE = exports.KEY_TYPE_CONTROLLER = exports.KEY_TYPE_APP = void 0;
|
|
4
|
+
//////////////////////////////
|
|
5
|
+
/// Types constants
|
|
6
|
+
//////////////////////////////
|
|
7
|
+
exports.KEY_TYPE_APP = "hyper:type:app";
|
|
8
|
+
exports.KEY_TYPE_CONTROLLER = "hyper:type:controller";
|
|
9
|
+
exports.KEY_TYPE_MODULE = "hyper:type:module";
|
|
10
|
+
exports.KEY_TYPE_ROUTE = "hyper:type:route";
|
|
11
|
+
exports.KEY_TYPE_SERVICE = "hyper:type:service";
|
|
12
|
+
//////////////////////////////
|
|
13
|
+
/// Params constants
|
|
14
|
+
//////////////////////////////
|
|
15
|
+
exports.KEY_PARAMS_APP = "hyper:type:app";
|
|
16
|
+
exports.KEY_PARAMS_CONTROLLER = "hyper:type:controller";
|
|
17
|
+
exports.KEY_PARAMS_MODULE = "hyper:type:module";
|
|
18
|
+
exports.KEY_PARAMS_ROUTE = "hyper:type:route";
|
|
19
|
+
exports.KEY_PARAMS_PARAM = "hyper:type:param";
|
|
20
|
+
exports.KEY_PARAMS_MIDDLEWARES = "hyper:type:middlewares";
|
|
21
|
+
exports.KEY_PARAMS_SCOPE = "hyper:type:scope";
|
|
22
|
+
exports.KEY_PARAMS_ROLE = "hyper:type:role";
|
|
23
|
+
exports.KEY_PARAMS_PASS = "hyper:type:pass";
|
|
24
|
+
//////////////////////////////
|
|
25
|
+
/// State constants
|
|
26
|
+
//////////////////////////////
|
|
27
|
+
exports.KEY_STATE_UPDATED = "hyper:state:updated";
|
|
28
|
+
exports.KEY_STATE_CREATED = "hyper:state:created";
|
|
29
|
+
exports.KEY_STATE_PREPARED = "hyper:state:prepared";
|
|
30
|
+
exports.KEY_STATE_BY_PASS = "hyper:state:bypass";
|
|
31
|
+
//////////////////////////////
|
|
32
|
+
/// Metadata constants
|
|
33
|
+
//////////////////////////////
|
|
34
|
+
exports.DESIGN_PARAMTYPES = "design:paramtypes";
|
|
35
|
+
exports.DESIGN_RETURNTYPE = "design:returntype";
|
|
36
|
+
exports.DESIGN_TYPE = "design:type";
|
|
37
|
+
exports.METADATA_HYPER_TYPE = {
|
|
38
|
+
APP: "hyper:app",
|
|
39
|
+
CONTROLLER: "hyper:controller",
|
|
40
|
+
MODULE: "hyper:module",
|
|
41
|
+
ROUTE: "hyper:route",
|
|
42
|
+
PARAM: "hyper:param",
|
|
43
|
+
};
|
|
44
|
+
exports.METADATA_KEYS = {
|
|
45
|
+
APP_INFO: "hyper:app:info",
|
|
46
|
+
MODULES: "hyper:modules",
|
|
47
|
+
PREFIX: "hyper:prefix",
|
|
48
|
+
CONTROLLERS: "hyper:controllers",
|
|
49
|
+
ROUTES: "hyper:routes",
|
|
50
|
+
ROLES: "hyper:roles",
|
|
51
|
+
SCOPES: "hyper:scopes",
|
|
52
|
+
SCOPED: "hyper:scoped",
|
|
53
|
+
MIDDLEWARES: "hyper:middleware",
|
|
54
|
+
};
|
|
55
|
+
exports.METADATA_STORE_KEYS = {
|
|
56
|
+
PARAMS: "hyper:store:params",
|
|
57
|
+
};
|
|
58
|
+
exports.METADATA_METHOD_KEYS = {
|
|
59
|
+
ARGUMENTS: "hyper:arguments",
|
|
60
|
+
ARGUMENTS_NAMES: "hyper:arguments:names",
|
|
61
|
+
ARGUMENTS_TYPE: "hyper:arguments:type",
|
|
62
|
+
};
|
|
63
|
+
exports.METADATA_PARAMS_KEYS = {
|
|
64
|
+
DESIGN_PARAM_TYPES: "design:paramtypes",
|
|
65
|
+
DESIGN_TYPE: "design:type",
|
|
66
|
+
DESIGN_RETURN_TYPE: "design:type",
|
|
67
|
+
};
|
|
68
|
+
exports.METADATA_STATE_KEYS = {
|
|
69
|
+
UPDATED: "hyper:updated",
|
|
70
|
+
CREATED: "hyper:created",
|
|
71
|
+
PREPARED: "hyper:prepared",
|
|
72
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
import { Request } from "hyper-express";
|
|
3
|
+
import { ByPassKeys } from "../stores/params.store";
|
|
4
|
+
import { ParameterResolver } from "../../decorators";
|
|
5
|
+
/**
|
|
6
|
+
* Creates a parameter decorator for handling request data.
|
|
7
|
+
*
|
|
8
|
+
* @param {keyof Request | ByPassKeys} key - The key to extract from the request.
|
|
9
|
+
* @param {IParamsResolver} resolver - Resolver function to handle the parameter.
|
|
10
|
+
* @returns {ParameterDecorator} - The parameter decorator function.
|
|
11
|
+
*/
|
|
12
|
+
export default function createParamDecorator(key: keyof Request | ByPassKeys, decoratorName: string, resolver: ParameterResolver): ParameterDecorator;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.default = createParamDecorator;
|
|
7
|
+
require("reflect-metadata");
|
|
8
|
+
const function_util_1 = require("../utils/function.util");
|
|
9
|
+
const constants_1 = require("../constants");
|
|
10
|
+
const decorator_base_1 = require("../decorator-base");
|
|
11
|
+
const who_helper_1 = __importDefault(require("../helpers/who.helper"));
|
|
12
|
+
const WrongPlaceException_1 = __importDefault(require("../../exeptions/WrongPlaceException"));
|
|
13
|
+
/**
|
|
14
|
+
* Creates a parameter decorator for handling request data.
|
|
15
|
+
*
|
|
16
|
+
* @param {keyof Request | ByPassKeys} key - The key to extract from the request.
|
|
17
|
+
* @param {IParamsResolver} resolver - Resolver function to handle the parameter.
|
|
18
|
+
* @returns {ParameterDecorator} - The parameter decorator function.
|
|
19
|
+
*/
|
|
20
|
+
function createParamDecorator(key, decoratorName, resolver) {
|
|
21
|
+
const _key = key;
|
|
22
|
+
return (0, decorator_base_1.DecoratorHelper)({
|
|
23
|
+
type: constants_1.KEY_TYPE_CONTROLLER,
|
|
24
|
+
key: constants_1.KEY_PARAMS_PARAM,
|
|
25
|
+
options: (options, Target, propertyKey, parameterIndex) => {
|
|
26
|
+
const { isProperty } = (0, who_helper_1.default)(Target, propertyKey, parameterIndex);
|
|
27
|
+
if (!isProperty)
|
|
28
|
+
throw new WrongPlaceException_1.default(decoratorName, "parameter", `${Target.constructor.name}.${propertyKey}`, Target);
|
|
29
|
+
const saved = options !== null && options !== void 0 ? options : { params: {} };
|
|
30
|
+
const names = (0, function_util_1.extreactArgsNames)(Target[propertyKey]);
|
|
31
|
+
const types = Reflect.getMetadata(constants_1.DESIGN_PARAMTYPES, Target, propertyKey);
|
|
32
|
+
const name = names === null || names === void 0 ? void 0 : names[parameterIndex];
|
|
33
|
+
const type = types === null || types === void 0 ? void 0 : types[parameterIndex];
|
|
34
|
+
if (name && saved) {
|
|
35
|
+
if (!saved.params[propertyKey]) {
|
|
36
|
+
saved.params[propertyKey] = [];
|
|
37
|
+
}
|
|
38
|
+
saved.params[propertyKey].push({
|
|
39
|
+
name,
|
|
40
|
+
type,
|
|
41
|
+
index: parameterIndex,
|
|
42
|
+
key: _key,
|
|
43
|
+
method: propertyKey.toString(),
|
|
44
|
+
resolver,
|
|
45
|
+
});
|
|
46
|
+
// sort by index
|
|
47
|
+
saved.params[propertyKey].sort((a, b) => a.index - b.index);
|
|
48
|
+
Reflect.defineMetadata(constants_1.KEY_PARAMS_PARAM, saved, Target[propertyKey]);
|
|
49
|
+
}
|
|
50
|
+
return saved;
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { Request, Response } from "hyper-express";
|
|
2
|
+
/**
|
|
3
|
+
* Helper function to create route decorators for HTTP methods.
|
|
4
|
+
*
|
|
5
|
+
* @param {string} method - The HTTP method (e.g., GET, POST).
|
|
6
|
+
* @param {(req: Request, res: Response) => any} [resolver] - Optional resolver for the route.
|
|
7
|
+
* @returns {(path?: string) => MethodDecorator} - A method decorator for defining routes.
|
|
8
|
+
*
|
|
9
|
+
*/
|
|
10
|
+
export default function createRouteDecorator(method: string, resolver?: (req: Request, res: Response) => any): (path?: string) => MethodDecorator & ClassDecorator;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = createRouteDecorator;
|
|
4
|
+
const constants_1 = require("../constants");
|
|
5
|
+
const decorator_base_1 = require("../decorator-base");
|
|
6
|
+
/**
|
|
7
|
+
* Helper function to create route decorators for HTTP methods.
|
|
8
|
+
*
|
|
9
|
+
* @param {string} method - The HTTP method (e.g., GET, POST).
|
|
10
|
+
* @param {(req: Request, res: Response) => any} [resolver] - Optional resolver for the route.
|
|
11
|
+
* @returns {(path?: string) => MethodDecorator} - A method decorator for defining routes.
|
|
12
|
+
*
|
|
13
|
+
*/
|
|
14
|
+
function createRouteDecorator(method, resolver) {
|
|
15
|
+
return (path = "/") => (0, decorator_base_1.DecoratorHelper)({
|
|
16
|
+
type: constants_1.KEY_TYPE_CONTROLLER,
|
|
17
|
+
key: constants_1.KEY_PARAMS_ROUTE,
|
|
18
|
+
targetResolver: (target) => { var _a; return (_a = target.constructor) !== null && _a !== void 0 ? _a : target; },
|
|
19
|
+
options: (data, Target, propertyKey, descriptor) => {
|
|
20
|
+
var _a;
|
|
21
|
+
const handler = descriptor.value;
|
|
22
|
+
if (typeof handler !== "function")
|
|
23
|
+
return data;
|
|
24
|
+
const saved = (_a = Reflect.getMetadata(constants_1.KEY_PARAMS_ROUTE, Target)) !== null && _a !== void 0 ? _a : {
|
|
25
|
+
routes: new Set(),
|
|
26
|
+
};
|
|
27
|
+
saved.routes.add({
|
|
28
|
+
className: Target.name,
|
|
29
|
+
method,
|
|
30
|
+
path,
|
|
31
|
+
propertyKey,
|
|
32
|
+
handler: handler,
|
|
33
|
+
});
|
|
34
|
+
return saved;
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
export type DecoratorBaseTypes = "APP" | "MODULE" | "CONTROLLER" | "ROUTE" | "MIDDLEWARE";
|
|
3
|
+
type DefineResolve<T> = (old: T, target?: any, property?: any, descriptor?: any) => T;
|
|
4
|
+
interface onDefineResolve<T> {
|
|
5
|
+
(data: {
|
|
6
|
+
key: string;
|
|
7
|
+
options: T;
|
|
8
|
+
target: any;
|
|
9
|
+
property?: any;
|
|
10
|
+
descriptor?: any;
|
|
11
|
+
}, defineData: (options: T) => void): void;
|
|
12
|
+
}
|
|
13
|
+
interface BeseDecoratorOptions<T extends any = any> {
|
|
14
|
+
type?: DecoratorBaseTypes | string;
|
|
15
|
+
key: string;
|
|
16
|
+
options: T | DefineResolve<T>;
|
|
17
|
+
targetResolver?: (target: any, propertyKey?: any, descriptorOrIndex?: any) => any;
|
|
18
|
+
onDefineData?: onDefineResolve<T>;
|
|
19
|
+
}
|
|
20
|
+
interface BeseDecorator<T> {
|
|
21
|
+
(target: T): any;
|
|
22
|
+
(target: T, propertyKey?: any, descriptor?: any): any;
|
|
23
|
+
}
|
|
24
|
+
type TransformFunction<TOptions, T> = (options: TOptions, target: T, propertyKey?: any, descriptor?: any) => any;
|
|
25
|
+
export declare function DecoratorHelper<T, Target extends any = any>({ key, type, options, targetResolver, onDefineData }: BeseDecoratorOptions<T>, ...transformers: TransformFunction<T, Target>[]): BeseDecorator<Target>;
|
|
26
|
+
export declare const defineDecorData: <T>(key: string, options: T | DefineResolve<T>, target: any, property?: any, descriptor?: any) => void;
|
|
27
|
+
export declare const getDecorData: <T>(key: string, target: any, property?: any) => T;
|
|
28
|
+
export declare const hasDecorData: (key: string, target: any, property?: any) => boolean;
|
|
29
|
+
export declare const extractDecorData: <T>(target: any) => T | undefined;
|
|
30
|
+
export {};
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.extractDecorData = exports.hasDecorData = exports.getDecorData = exports.defineDecorData = void 0;
|
|
4
|
+
exports.DecoratorHelper = DecoratorHelper;
|
|
5
|
+
require("reflect-metadata");
|
|
6
|
+
const tsyringe_1 = require("tsyringe");
|
|
7
|
+
const object_util_1 = require("./utils/object.util");
|
|
8
|
+
function DecoratorHelper({ key, type, options, targetResolver, onDefineData }, ...transformers) {
|
|
9
|
+
return (target, propertyKey, descriptor) => {
|
|
10
|
+
const isProperty = !!propertyKey;
|
|
11
|
+
const isMethod = !!descriptor;
|
|
12
|
+
let _options = options;
|
|
13
|
+
const Target = targetResolver
|
|
14
|
+
? targetResolver(target, propertyKey, descriptor)
|
|
15
|
+
: target;
|
|
16
|
+
if (options instanceof Function) {
|
|
17
|
+
const data = (0, exports.getDecorData)(key, Target);
|
|
18
|
+
const optionsResolver = options;
|
|
19
|
+
const value = optionsResolver(data, Target, propertyKey, descriptor);
|
|
20
|
+
if (value)
|
|
21
|
+
_options = Object.assign(Object.assign({}, _options), value);
|
|
22
|
+
}
|
|
23
|
+
if (onDefineData) {
|
|
24
|
+
onDefineData({
|
|
25
|
+
key,
|
|
26
|
+
options: Object.assign({ type }, _options),
|
|
27
|
+
target: Target,
|
|
28
|
+
property: propertyKey,
|
|
29
|
+
descriptor,
|
|
30
|
+
}, (data) => {
|
|
31
|
+
(0, exports.defineDecorData)(key, Object.assign({ type }, data), Target);
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
(0, exports.defineDecorData)(key, Object.assign({ type }, _options), Target);
|
|
36
|
+
}
|
|
37
|
+
let value = Target;
|
|
38
|
+
if (transformers.length) {
|
|
39
|
+
const transforms = transformers.map((fn) => fn.bind(Target, _options));
|
|
40
|
+
if (isProperty) {
|
|
41
|
+
(0, exports.defineDecorData)(key, Object.assign({ type }, options), Target, propertyKey);
|
|
42
|
+
value = Reflect.decorate(transforms, Target, propertyKey);
|
|
43
|
+
}
|
|
44
|
+
else if (isMethod) {
|
|
45
|
+
(0, exports.defineDecorData)(key, Object.assign({ type }, options), Target);
|
|
46
|
+
value = Reflect.decorate(transforms, Target, propertyKey, descriptor);
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
(0, exports.defineDecorData)(key, Object.assign({ type }, options), Target);
|
|
50
|
+
value = Reflect.decorate(transforms, Target);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
(0, tsyringe_1.injectable)()((0, object_util_1.$constructor)(target));
|
|
54
|
+
return value;
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
const defineDecorData = (key, options, target, property, descriptor) => {
|
|
58
|
+
let value = options;
|
|
59
|
+
if (typeof options === "function") {
|
|
60
|
+
const old = (0, exports.getDecorData)(key, target, property);
|
|
61
|
+
value = Object.assign(Object.assign({}, old), options(old, target, property, descriptor));
|
|
62
|
+
}
|
|
63
|
+
if (property) {
|
|
64
|
+
Reflect.defineMetadata(key, value, target, property);
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
Reflect.defineMetadata(key, value, target);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
exports.defineDecorData = defineDecorData;
|
|
71
|
+
const getDecorData = (key, target, property) => property
|
|
72
|
+
? Reflect.getMetadata(key, target, property)
|
|
73
|
+
: Reflect.getMetadata(key, target);
|
|
74
|
+
exports.getDecorData = getDecorData;
|
|
75
|
+
const hasDecorData = (key, target, property) => property
|
|
76
|
+
? Reflect.hasMetadata(key, target, property)
|
|
77
|
+
: Reflect.hasMetadata(key, target);
|
|
78
|
+
exports.hasDecorData = hasDecorData;
|
|
79
|
+
const extractDecorData = (target) => {
|
|
80
|
+
const keys = Reflect.getMetadataKeys(target);
|
|
81
|
+
return keys.reduce((acc, key) => {
|
|
82
|
+
acc[key] = Reflect.getMetadata(key, target);
|
|
83
|
+
return acc;
|
|
84
|
+
}, {});
|
|
85
|
+
};
|
|
86
|
+
exports.extractDecorData = extractDecorData;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import "reflect-metadata";
|
|
2
|
+
type MetadataKey = string | symbol;
|
|
3
|
+
type MergeOptions = {
|
|
4
|
+
overwriteArrays?: boolean;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Performs a deep merge of metadata from source to target using reflect-metadata.
|
|
8
|
+
* @param target Object where the metadata will be merged.
|
|
9
|
+
* @param source Object from which the metadata will be copied.
|
|
10
|
+
* @param keys Metadata keys to merge.
|
|
11
|
+
* @param options Merge options (e.g., array handling).
|
|
12
|
+
*/
|
|
13
|
+
export declare function mergeMetadata(target: any, source: any, keys: MetadataKey[], options?: MergeOptions): void;
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.mergeMetadata = mergeMetadata;
|
|
4
|
+
require("reflect-metadata");
|
|
5
|
+
/**
|
|
6
|
+
* Performs a deep merge of metadata from source to target using reflect-metadata.
|
|
7
|
+
* @param target Object where the metadata will be merged.
|
|
8
|
+
* @param source Object from which the metadata will be copied.
|
|
9
|
+
* @param keys Metadata keys to merge.
|
|
10
|
+
* @param options Merge options (e.g., array handling).
|
|
11
|
+
*/
|
|
12
|
+
function mergeMetadata(target, source, keys, options = {}) {
|
|
13
|
+
for (const key of keys) {
|
|
14
|
+
const sourceMeta = Reflect.getMetadata(key, source);
|
|
15
|
+
const targetMeta = Reflect.getMetadata(key, target);
|
|
16
|
+
if (sourceMeta === undefined)
|
|
17
|
+
continue;
|
|
18
|
+
let mergedMeta = sourceMeta;
|
|
19
|
+
if (typeof sourceMeta === "object" && typeof targetMeta === "object") {
|
|
20
|
+
mergedMeta = deepMerge(targetMeta, sourceMeta, options);
|
|
21
|
+
}
|
|
22
|
+
Reflect.defineMetadata(key, mergedMeta, target);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Performs a deep merge of two objects.
|
|
27
|
+
* @param target Target object.
|
|
28
|
+
* @param source Source object.
|
|
29
|
+
* @param options Merge options.
|
|
30
|
+
* @returns Merged object.
|
|
31
|
+
*/
|
|
32
|
+
function deepMerge(target, source, options) {
|
|
33
|
+
if (Array.isArray(target) && Array.isArray(source)) {
|
|
34
|
+
return (options.overwriteArrays ? source : Array.from(new Set([...target, ...source])));
|
|
35
|
+
}
|
|
36
|
+
if (typeof target === "object" && target !== null && typeof source === "object" && source !== null) {
|
|
37
|
+
const merged = Object.assign({}, target);
|
|
38
|
+
for (const key of Object.keys(source)) {
|
|
39
|
+
merged[key] = deepMerge(target[key], source[key], options);
|
|
40
|
+
}
|
|
41
|
+
return merged;
|
|
42
|
+
}
|
|
43
|
+
return source;
|
|
44
|
+
}
|