@hemia/common 0.0.1
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 +291 -0
- package/dist/hemia-common.esm.js +101 -0
- package/dist/hemia-common.js +126 -0
- package/dist/types/decorators/controllers/controller.decorator.d.ts +2 -0
- package/dist/types/decorators/controllers/index.d.ts +4 -0
- package/dist/types/decorators/controllers/method.decorators.d.ts +8 -0
- package/dist/types/decorators/controllers/param.decorators.d.ts +13 -0
- package/dist/types/decorators/controllers/response.decorators.d.ts +3 -0
- package/dist/types/decorators/index.d.ts +2 -0
- package/dist/types/decorators/metadata.d.ts +7 -0
- package/dist/types/enums/index.d.ts +2 -0
- package/dist/types/enums/methods.enum.d.ts +9 -0
- package/dist/types/enums/param-type.enum.d.ts +12 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/interfaces/index.d.ts +2 -0
- package/dist/types/interfaces/param.interface.d.ts +14 -0
- package/dist/types/interfaces/route.interface.d.ts +6 -0
- package/dist/types/types/index.d.ts +1 -0
- package/dist/types/types/redirect.types.d.ts +4 -0
- package/dist/types/utils/index.d.ts +1 -0
- package/dist/types/utils/type-guards.d.ts +2 -0
- package/package.json +41 -0
package/README.md
ADDED
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
|
|
2
|
+
# @hemia/common
|
|
3
|
+
|
|
4
|
+
Sistema de decoradores y utilidades para crear controladores HTTP con TypeScript usando metadata reflection.
|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## 📦 Instalación
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @hemia/common reflect-metadata
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
> **Nota:** Este paquete requiere `reflect-metadata` como dependencia peer.
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
## 🚀 Características
|
|
19
|
+
|
|
20
|
+
- ✨ Decoradores para controladores HTTP estilo Express/NestJS
|
|
21
|
+
- 🎯 Soporte completo para métodos HTTP (GET, POST, PUT, DELETE, PATCH, OPTIONS, HEAD)
|
|
22
|
+
- 📝 Decoradores de parámetros para request, response, body, query, params, headers, etc.
|
|
23
|
+
- 🔄 Manejo de redirecciones y headers personalizados
|
|
24
|
+
- 🛡️ Type-safe con TypeScript
|
|
25
|
+
- 📦 Metadata reflection con `reflect-metadata`
|
|
26
|
+
|
|
27
|
+
---
|
|
28
|
+
|
|
29
|
+
## 📖 Uso
|
|
30
|
+
|
|
31
|
+
### Configuración inicial
|
|
32
|
+
|
|
33
|
+
Asegúrate de importar `reflect-metadata` al inicio de tu aplicación:
|
|
34
|
+
|
|
35
|
+
```typescript
|
|
36
|
+
import 'reflect-metadata';
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
También necesitas habilitar los siguientes flags en tu `tsconfig.json`:
|
|
40
|
+
|
|
41
|
+
```json
|
|
42
|
+
{
|
|
43
|
+
"compilerOptions": {
|
|
44
|
+
"experimentalDecorators": true,
|
|
45
|
+
"emitDecoratorMetadata": true
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Ejemplo básico
|
|
51
|
+
|
|
52
|
+
```typescript
|
|
53
|
+
import { Controller, Get, Post, Body, Param, Query } from '@hemia/common';
|
|
54
|
+
|
|
55
|
+
@Controller('/users')
|
|
56
|
+
class UserController {
|
|
57
|
+
@Get('/')
|
|
58
|
+
getAllUsers(@Query('page') page: string) {
|
|
59
|
+
// Maneja GET /users?page=1
|
|
60
|
+
return { users: [], page };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
@Get('/:id')
|
|
64
|
+
getUserById(@Param('id') id: string) {
|
|
65
|
+
// Maneja GET /users/123
|
|
66
|
+
return { id, name: 'John Doe' };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
@Post('/')
|
|
70
|
+
createUser(@Body() userData: any) {
|
|
71
|
+
// Maneja POST /users
|
|
72
|
+
return { created: true, data: userData };
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
---
|
|
78
|
+
|
|
79
|
+
## 🎨 API Reference
|
|
80
|
+
|
|
81
|
+
### Decoradores de Clase
|
|
82
|
+
|
|
83
|
+
#### `@Controller(basePath?: string)`
|
|
84
|
+
|
|
85
|
+
Define una clase como controlador HTTP con una ruta base opcional.
|
|
86
|
+
|
|
87
|
+
```typescript
|
|
88
|
+
@Controller('/api/v1')
|
|
89
|
+
class ApiController {
|
|
90
|
+
// ...
|
|
91
|
+
}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### Decoradores de Método (HTTP)
|
|
95
|
+
|
|
96
|
+
Los siguientes decoradores definen rutas y métodos HTTP:
|
|
97
|
+
|
|
98
|
+
- `@Get(path?: string)` - Método GET
|
|
99
|
+
- `@Post(path?: string)` - Método POST
|
|
100
|
+
- `@Put(path?: string)` - Método PUT
|
|
101
|
+
- `@Delete(path?: string)` - Método DELETE
|
|
102
|
+
- `@Patch(path?: string)` - Método PATCH
|
|
103
|
+
- `@Options(path?: string)` - Método OPTIONS
|
|
104
|
+
- `@Head(path?: string)` - Método HEAD
|
|
105
|
+
|
|
106
|
+
```typescript
|
|
107
|
+
@Controller('/products')
|
|
108
|
+
class ProductController {
|
|
109
|
+
@Get('/')
|
|
110
|
+
findAll() { /* ... */ }
|
|
111
|
+
|
|
112
|
+
@Post('/create')
|
|
113
|
+
create() { /* ... */ }
|
|
114
|
+
|
|
115
|
+
@Put('/:id')
|
|
116
|
+
update() { /* ... */ }
|
|
117
|
+
|
|
118
|
+
@Delete('/:id')
|
|
119
|
+
remove() { /* ... */ }
|
|
120
|
+
}
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
### Decoradores de Parámetros
|
|
124
|
+
|
|
125
|
+
Extraen información de la petición HTTP:
|
|
126
|
+
|
|
127
|
+
#### `@Request()` / `@Req()`
|
|
128
|
+
Inyecta el objeto request completo.
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
@Get('/')
|
|
132
|
+
handler(@Request() req: any) {
|
|
133
|
+
// Acceso al request completo
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
#### `@Response()` / `@Res()`
|
|
138
|
+
Inyecta el objeto response completo.
|
|
139
|
+
|
|
140
|
+
```typescript
|
|
141
|
+
@Get('/')
|
|
142
|
+
handler(@Response() res: any) {
|
|
143
|
+
// Acceso al response completo
|
|
144
|
+
}
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
#### `@Body(key?: string)`
|
|
148
|
+
Extrae el cuerpo de la petición o una propiedad específica.
|
|
149
|
+
|
|
150
|
+
```typescript
|
|
151
|
+
@Post('/')
|
|
152
|
+
create(@Body() data: any) { /* ... */ }
|
|
153
|
+
|
|
154
|
+
@Post('/')
|
|
155
|
+
create(@Body('email') email: string) { /* ... */ }
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
#### `@Param(key?: string)`
|
|
159
|
+
Extrae parámetros de ruta.
|
|
160
|
+
|
|
161
|
+
```typescript
|
|
162
|
+
@Get('/:id/:name')
|
|
163
|
+
handler(@Param('id') id: string, @Param('name') name: string) { /* ... */ }
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
#### `@Query(key?: string)`
|
|
167
|
+
Extrae parámetros de query string.
|
|
168
|
+
|
|
169
|
+
```typescript
|
|
170
|
+
@Get('/')
|
|
171
|
+
search(@Query('q') searchTerm: string, @Query('limit') limit: string) { /* ... */ }
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
#### `@Headers(key?: string)`
|
|
175
|
+
Extrae headers de la petición.
|
|
176
|
+
|
|
177
|
+
```typescript
|
|
178
|
+
@Get('/')
|
|
179
|
+
handler(@Headers('authorization') auth: string) { /* ... */ }
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
#### `@Session()`
|
|
183
|
+
Inyecta el objeto de sesión.
|
|
184
|
+
|
|
185
|
+
```typescript
|
|
186
|
+
@Get('/profile')
|
|
187
|
+
getProfile(@Session() session: any) { /* ... */ }
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
#### `@Ip()`
|
|
191
|
+
Obtiene la dirección IP del cliente.
|
|
192
|
+
|
|
193
|
+
```typescript
|
|
194
|
+
@Get('/')
|
|
195
|
+
handler(@Ip() ip: string) { /* ... */ }
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
#### `@Host()`
|
|
199
|
+
Obtiene el host de la petición.
|
|
200
|
+
|
|
201
|
+
```typescript
|
|
202
|
+
@Get('/')
|
|
203
|
+
handler(@Host() host: string) { /* ... */ }
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
#### `@Next()`
|
|
207
|
+
Inyecta la función next (para middleware).
|
|
208
|
+
|
|
209
|
+
```typescript
|
|
210
|
+
@Get('/')
|
|
211
|
+
handler(@Next() next: Function) { /* ... */ }
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
### Decoradores de Respuesta
|
|
215
|
+
|
|
216
|
+
#### `@Header(name: string, value: string)`
|
|
217
|
+
Define headers personalizados para la respuesta.
|
|
218
|
+
|
|
219
|
+
```typescript
|
|
220
|
+
@Get('/')
|
|
221
|
+
@Header('X-Custom-Header', 'value')
|
|
222
|
+
@Header('Cache-Control', 'no-cache')
|
|
223
|
+
handler() {
|
|
224
|
+
return { data: 'example' };
|
|
225
|
+
}
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
#### `@Redirect(url: string, statusCode?: number)`
|
|
229
|
+
Redirige a una URL específica (código por defecto: 302).
|
|
230
|
+
|
|
231
|
+
```typescript
|
|
232
|
+
@Get('/old-page')
|
|
233
|
+
@Redirect('/new-page', 301)
|
|
234
|
+
redirectHandler() {
|
|
235
|
+
// Redirige permanentemente
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
@Get('/external')
|
|
239
|
+
@Redirect('https://example.com')
|
|
240
|
+
externalRedirect() {
|
|
241
|
+
// Redirige temporalmente
|
|
242
|
+
}
|
|
243
|
+
```
|
|
244
|
+
---
|
|
245
|
+
|
|
246
|
+
## 🔍 Metadata Keys
|
|
247
|
+
|
|
248
|
+
El paquete utiliza las siguientes claves de metadata:
|
|
249
|
+
|
|
250
|
+
```typescript
|
|
251
|
+
const METADATA_KEYS = {
|
|
252
|
+
BASE_PATH: 'base_path', // Ruta base del controlador
|
|
253
|
+
ROUTES: 'routes', // Rutas definidas
|
|
254
|
+
PARAMS: 'params', // Parámetros del método
|
|
255
|
+
HEADERS: 'headers', // Headers personalizados
|
|
256
|
+
REDIRECT: 'redirect' // Configuración de redirección
|
|
257
|
+
}
|
|
258
|
+
```
|
|
259
|
+
|
|
260
|
+
---
|
|
261
|
+
|
|
262
|
+
## 🧪 Desarrollo
|
|
263
|
+
|
|
264
|
+
### Scripts disponibles
|
|
265
|
+
|
|
266
|
+
| Script | Descripción |
|
|
267
|
+
|-----------------------|---------------------------------------|
|
|
268
|
+
| `npm run build` | Compila el paquete con Rollup |
|
|
269
|
+
| `npm run test` | Ejecuta pruebas con Jest |
|
|
270
|
+
| `npm run test:watch` | Ejecuta pruebas en modo watch |
|
|
271
|
+
| `npm run test:coverage` | Genera reporte de cobertura |
|
|
272
|
+
| `npm run clean` | Limpia la carpeta `dist/` |
|
|
273
|
+
|
|
274
|
+
### Archivos generados
|
|
275
|
+
|
|
276
|
+
- `dist/hemia-common.js` - Bundle CommonJS
|
|
277
|
+
- `dist/hemia-common.esm.js` - Bundle ES Module
|
|
278
|
+
- `dist/types/` - Declaraciones de TypeScript
|
|
279
|
+
|
|
280
|
+
---
|
|
281
|
+
|
|
282
|
+
## 📄 Licencia
|
|
283
|
+
|
|
284
|
+
ISC
|
|
285
|
+
|
|
286
|
+
---
|
|
287
|
+
|
|
288
|
+
## ✨ Generado con Hemia CLI
|
|
289
|
+
|
|
290
|
+
Este paquete fue generado usando [Hemia CLI](https://www.npmjs.com/package/@hemia/cli).
|
|
291
|
+
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import 'reflect-metadata';
|
|
2
|
+
|
|
3
|
+
const METADATA_KEYS = {
|
|
4
|
+
BASE_PATH: 'base_path',
|
|
5
|
+
ROUTES: 'routes',
|
|
6
|
+
PARAMS: 'params',
|
|
7
|
+
HEADERS: 'headers',
|
|
8
|
+
REDIRECT: 'redirect'
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const Controller = (basePath = '/') => {
|
|
12
|
+
return (target) => {
|
|
13
|
+
Reflect.defineMetadata(METADATA_KEYS.BASE_PATH, basePath, target);
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
var HttpMethod;
|
|
18
|
+
(function (HttpMethod) {
|
|
19
|
+
HttpMethod["GET"] = "get";
|
|
20
|
+
HttpMethod["POST"] = "post";
|
|
21
|
+
HttpMethod["PUT"] = "put";
|
|
22
|
+
HttpMethod["DELETE"] = "delete";
|
|
23
|
+
HttpMethod["PATCH"] = "patch";
|
|
24
|
+
HttpMethod["OPTIONS"] = "options";
|
|
25
|
+
HttpMethod["HEAD"] = "head";
|
|
26
|
+
})(HttpMethod || (HttpMethod = {}));
|
|
27
|
+
|
|
28
|
+
var ParamType;
|
|
29
|
+
(function (ParamType) {
|
|
30
|
+
ParamType["REQUEST"] = "REQUEST";
|
|
31
|
+
ParamType["RESPONSE"] = "RESPONSE";
|
|
32
|
+
ParamType["NEXT"] = "NEXT";
|
|
33
|
+
ParamType["BODY"] = "BODY";
|
|
34
|
+
ParamType["QUERY"] = "QUERY";
|
|
35
|
+
ParamType["PARAM"] = "PARAM";
|
|
36
|
+
ParamType["HEADERS"] = "HEADERS";
|
|
37
|
+
ParamType["SESSION"] = "SESSION";
|
|
38
|
+
ParamType["IP"] = "IP";
|
|
39
|
+
ParamType["HOST"] = "HOST";
|
|
40
|
+
})(ParamType || (ParamType = {}));
|
|
41
|
+
|
|
42
|
+
const createMethodDecorator = (method) => {
|
|
43
|
+
return (path = '/') => {
|
|
44
|
+
return (target, propertyKey) => {
|
|
45
|
+
const routes = Reflect.getMetadata(METADATA_KEYS.ROUTES, target.constructor) || [];
|
|
46
|
+
routes.push({ path, method, methodName: propertyKey });
|
|
47
|
+
Reflect.defineMetadata(METADATA_KEYS.ROUTES, routes, target.constructor);
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
const Get = createMethodDecorator(HttpMethod.GET);
|
|
52
|
+
const Post = createMethodDecorator(HttpMethod.POST);
|
|
53
|
+
const Put = createMethodDecorator(HttpMethod.PUT);
|
|
54
|
+
const Delete = createMethodDecorator(HttpMethod.DELETE);
|
|
55
|
+
const Patch = createMethodDecorator(HttpMethod.PATCH);
|
|
56
|
+
const Options = createMethodDecorator(HttpMethod.OPTIONS);
|
|
57
|
+
const Head = createMethodDecorator(HttpMethod.HEAD);
|
|
58
|
+
|
|
59
|
+
const createParamDecorator = (type) => {
|
|
60
|
+
return (data) => {
|
|
61
|
+
return (target, propertyKey, parameterIndex) => {
|
|
62
|
+
const existingParams = Reflect.getMetadata(METADATA_KEYS.PARAMS, target, propertyKey || '') || [];
|
|
63
|
+
existingParams.push({ index: parameterIndex, type, data });
|
|
64
|
+
Reflect.defineMetadata(METADATA_KEYS.PARAMS, existingParams, target, propertyKey || '');
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
const Request = createParamDecorator(ParamType.REQUEST);
|
|
69
|
+
const Req = createParamDecorator(ParamType.REQUEST);
|
|
70
|
+
const Response = createParamDecorator(ParamType.RESPONSE);
|
|
71
|
+
const Res = createParamDecorator(ParamType.RESPONSE);
|
|
72
|
+
const Next = createParamDecorator(ParamType.NEXT);
|
|
73
|
+
const Session = createParamDecorator(ParamType.SESSION);
|
|
74
|
+
const Ip = createParamDecorator(ParamType.IP);
|
|
75
|
+
const Host = createParamDecorator(ParamType.HOST);
|
|
76
|
+
const Body = createParamDecorator(ParamType.BODY);
|
|
77
|
+
const Query = createParamDecorator(ParamType.QUERY);
|
|
78
|
+
const Param = createParamDecorator(ParamType.PARAM);
|
|
79
|
+
const Headers = createParamDecorator(ParamType.HEADERS);
|
|
80
|
+
|
|
81
|
+
const Header = (name, value) => {
|
|
82
|
+
return (target, propertyKey, descriptor) => {
|
|
83
|
+
const existingHeaders = Reflect.getMetadata(METADATA_KEYS.HEADERS, target.constructor, propertyKey) || [];
|
|
84
|
+
existingHeaders.push({ name, value });
|
|
85
|
+
Reflect.defineMetadata(METADATA_KEYS.HEADERS, existingHeaders, target.constructor, propertyKey);
|
|
86
|
+
return descriptor;
|
|
87
|
+
};
|
|
88
|
+
};
|
|
89
|
+
const Redirect = (url, statusCode = 302) => {
|
|
90
|
+
return (target, propertyKey, descriptor) => {
|
|
91
|
+
const redirectMetadata = { url, statusCode };
|
|
92
|
+
Reflect.defineMetadata(METADATA_KEYS.REDIRECT, redirectMetadata, target.constructor, propertyKey);
|
|
93
|
+
return descriptor;
|
|
94
|
+
};
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
function isRedirectResponse(value) {
|
|
98
|
+
return value && typeof value === 'object' && typeof value.url === 'string';
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export { Body, Controller, Delete, Get, Head, Header, Headers, Host, HttpMethod, Ip, METADATA_KEYS, Next, Options, Param, ParamType, Patch, Post, Put, Query, Redirect, Req, Request, Res, Response, Session, isRedirectResponse };
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
require('reflect-metadata');
|
|
4
|
+
|
|
5
|
+
const METADATA_KEYS = {
|
|
6
|
+
BASE_PATH: 'base_path',
|
|
7
|
+
ROUTES: 'routes',
|
|
8
|
+
PARAMS: 'params',
|
|
9
|
+
HEADERS: 'headers',
|
|
10
|
+
REDIRECT: 'redirect'
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const Controller = (basePath = '/') => {
|
|
14
|
+
return (target) => {
|
|
15
|
+
Reflect.defineMetadata(METADATA_KEYS.BASE_PATH, basePath, target);
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
exports.HttpMethod = void 0;
|
|
20
|
+
(function (HttpMethod) {
|
|
21
|
+
HttpMethod["GET"] = "get";
|
|
22
|
+
HttpMethod["POST"] = "post";
|
|
23
|
+
HttpMethod["PUT"] = "put";
|
|
24
|
+
HttpMethod["DELETE"] = "delete";
|
|
25
|
+
HttpMethod["PATCH"] = "patch";
|
|
26
|
+
HttpMethod["OPTIONS"] = "options";
|
|
27
|
+
HttpMethod["HEAD"] = "head";
|
|
28
|
+
})(exports.HttpMethod || (exports.HttpMethod = {}));
|
|
29
|
+
|
|
30
|
+
exports.ParamType = void 0;
|
|
31
|
+
(function (ParamType) {
|
|
32
|
+
ParamType["REQUEST"] = "REQUEST";
|
|
33
|
+
ParamType["RESPONSE"] = "RESPONSE";
|
|
34
|
+
ParamType["NEXT"] = "NEXT";
|
|
35
|
+
ParamType["BODY"] = "BODY";
|
|
36
|
+
ParamType["QUERY"] = "QUERY";
|
|
37
|
+
ParamType["PARAM"] = "PARAM";
|
|
38
|
+
ParamType["HEADERS"] = "HEADERS";
|
|
39
|
+
ParamType["SESSION"] = "SESSION";
|
|
40
|
+
ParamType["IP"] = "IP";
|
|
41
|
+
ParamType["HOST"] = "HOST";
|
|
42
|
+
})(exports.ParamType || (exports.ParamType = {}));
|
|
43
|
+
|
|
44
|
+
const createMethodDecorator = (method) => {
|
|
45
|
+
return (path = '/') => {
|
|
46
|
+
return (target, propertyKey) => {
|
|
47
|
+
const routes = Reflect.getMetadata(METADATA_KEYS.ROUTES, target.constructor) || [];
|
|
48
|
+
routes.push({ path, method, methodName: propertyKey });
|
|
49
|
+
Reflect.defineMetadata(METADATA_KEYS.ROUTES, routes, target.constructor);
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
const Get = createMethodDecorator(exports.HttpMethod.GET);
|
|
54
|
+
const Post = createMethodDecorator(exports.HttpMethod.POST);
|
|
55
|
+
const Put = createMethodDecorator(exports.HttpMethod.PUT);
|
|
56
|
+
const Delete = createMethodDecorator(exports.HttpMethod.DELETE);
|
|
57
|
+
const Patch = createMethodDecorator(exports.HttpMethod.PATCH);
|
|
58
|
+
const Options = createMethodDecorator(exports.HttpMethod.OPTIONS);
|
|
59
|
+
const Head = createMethodDecorator(exports.HttpMethod.HEAD);
|
|
60
|
+
|
|
61
|
+
const createParamDecorator = (type) => {
|
|
62
|
+
return (data) => {
|
|
63
|
+
return (target, propertyKey, parameterIndex) => {
|
|
64
|
+
const existingParams = Reflect.getMetadata(METADATA_KEYS.PARAMS, target, propertyKey || '') || [];
|
|
65
|
+
existingParams.push({ index: parameterIndex, type, data });
|
|
66
|
+
Reflect.defineMetadata(METADATA_KEYS.PARAMS, existingParams, target, propertyKey || '');
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
};
|
|
70
|
+
const Request = createParamDecorator(exports.ParamType.REQUEST);
|
|
71
|
+
const Req = createParamDecorator(exports.ParamType.REQUEST);
|
|
72
|
+
const Response = createParamDecorator(exports.ParamType.RESPONSE);
|
|
73
|
+
const Res = createParamDecorator(exports.ParamType.RESPONSE);
|
|
74
|
+
const Next = createParamDecorator(exports.ParamType.NEXT);
|
|
75
|
+
const Session = createParamDecorator(exports.ParamType.SESSION);
|
|
76
|
+
const Ip = createParamDecorator(exports.ParamType.IP);
|
|
77
|
+
const Host = createParamDecorator(exports.ParamType.HOST);
|
|
78
|
+
const Body = createParamDecorator(exports.ParamType.BODY);
|
|
79
|
+
const Query = createParamDecorator(exports.ParamType.QUERY);
|
|
80
|
+
const Param = createParamDecorator(exports.ParamType.PARAM);
|
|
81
|
+
const Headers = createParamDecorator(exports.ParamType.HEADERS);
|
|
82
|
+
|
|
83
|
+
const Header = (name, value) => {
|
|
84
|
+
return (target, propertyKey, descriptor) => {
|
|
85
|
+
const existingHeaders = Reflect.getMetadata(METADATA_KEYS.HEADERS, target.constructor, propertyKey) || [];
|
|
86
|
+
existingHeaders.push({ name, value });
|
|
87
|
+
Reflect.defineMetadata(METADATA_KEYS.HEADERS, existingHeaders, target.constructor, propertyKey);
|
|
88
|
+
return descriptor;
|
|
89
|
+
};
|
|
90
|
+
};
|
|
91
|
+
const Redirect = (url, statusCode = 302) => {
|
|
92
|
+
return (target, propertyKey, descriptor) => {
|
|
93
|
+
const redirectMetadata = { url, statusCode };
|
|
94
|
+
Reflect.defineMetadata(METADATA_KEYS.REDIRECT, redirectMetadata, target.constructor, propertyKey);
|
|
95
|
+
return descriptor;
|
|
96
|
+
};
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
function isRedirectResponse(value) {
|
|
100
|
+
return value && typeof value === 'object' && typeof value.url === 'string';
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
exports.Body = Body;
|
|
104
|
+
exports.Controller = Controller;
|
|
105
|
+
exports.Delete = Delete;
|
|
106
|
+
exports.Get = Get;
|
|
107
|
+
exports.Head = Head;
|
|
108
|
+
exports.Header = Header;
|
|
109
|
+
exports.Headers = Headers;
|
|
110
|
+
exports.Host = Host;
|
|
111
|
+
exports.Ip = Ip;
|
|
112
|
+
exports.METADATA_KEYS = METADATA_KEYS;
|
|
113
|
+
exports.Next = Next;
|
|
114
|
+
exports.Options = Options;
|
|
115
|
+
exports.Param = Param;
|
|
116
|
+
exports.Patch = Patch;
|
|
117
|
+
exports.Post = Post;
|
|
118
|
+
exports.Put = Put;
|
|
119
|
+
exports.Query = Query;
|
|
120
|
+
exports.Redirect = Redirect;
|
|
121
|
+
exports.Req = Req;
|
|
122
|
+
exports.Request = Request;
|
|
123
|
+
exports.Res = Res;
|
|
124
|
+
exports.Response = Response;
|
|
125
|
+
exports.Session = Session;
|
|
126
|
+
exports.isRedirectResponse = isRedirectResponse;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import 'reflect-metadata';
|
|
2
|
+
export declare const Get: (path?: string) => MethodDecorator;
|
|
3
|
+
export declare const Post: (path?: string) => MethodDecorator;
|
|
4
|
+
export declare const Put: (path?: string) => MethodDecorator;
|
|
5
|
+
export declare const Delete: (path?: string) => MethodDecorator;
|
|
6
|
+
export declare const Patch: (path?: string) => MethodDecorator;
|
|
7
|
+
export declare const Options: (path?: string) => MethodDecorator;
|
|
8
|
+
export declare const Head: (path?: string) => MethodDecorator;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import 'reflect-metadata';
|
|
2
|
+
export declare const Request: (data?: string) => ParameterDecorator;
|
|
3
|
+
export declare const Req: (data?: string) => ParameterDecorator;
|
|
4
|
+
export declare const Response: (data?: string) => ParameterDecorator;
|
|
5
|
+
export declare const Res: (data?: string) => ParameterDecorator;
|
|
6
|
+
export declare const Next: (data?: string) => ParameterDecorator;
|
|
7
|
+
export declare const Session: (data?: string) => ParameterDecorator;
|
|
8
|
+
export declare const Ip: (data?: string) => ParameterDecorator;
|
|
9
|
+
export declare const Host: (data?: string) => ParameterDecorator;
|
|
10
|
+
export declare const Body: (data?: string) => ParameterDecorator;
|
|
11
|
+
export declare const Query: (data?: string) => ParameterDecorator;
|
|
12
|
+
export declare const Param: (data?: string) => ParameterDecorator;
|
|
13
|
+
export declare const Headers: (data?: string) => ParameterDecorator;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { ParamType } from "../enums/param-type.enum";
|
|
2
|
+
export interface ParamMetadata {
|
|
3
|
+
index: number;
|
|
4
|
+
type: ParamType;
|
|
5
|
+
data?: string;
|
|
6
|
+
}
|
|
7
|
+
export interface HeaderMetadata {
|
|
8
|
+
name: string;
|
|
9
|
+
value: string;
|
|
10
|
+
}
|
|
11
|
+
export interface RedirectMetadata {
|
|
12
|
+
url: string;
|
|
13
|
+
statusCode: number;
|
|
14
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./redirect.types";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./type-guards";
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hemia/common",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Paquete común para proyectos de Hemia",
|
|
5
|
+
"main": "dist/hemia-common.js",
|
|
6
|
+
"module": "dist/hemia-common.esm.js",
|
|
7
|
+
"types": "dist/types/index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"clean": "rimraf dist",
|
|
10
|
+
"tscBuild": "rollup -c",
|
|
11
|
+
"build": "npm run clean && npm run tscBuild",
|
|
12
|
+
"test": "jest --detectOpenHandles",
|
|
13
|
+
"test:coverage": "jest --coverage",
|
|
14
|
+
"test:watch": "jest --watch"
|
|
15
|
+
},
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"@rollup/plugin-commonjs": "^26.0.1",
|
|
18
|
+
"@rollup/plugin-json": "^6.1.0",
|
|
19
|
+
"@rollup/plugin-node-resolve": "^15.2.3",
|
|
20
|
+
"@types/jest": "^29.5.14",
|
|
21
|
+
"@types/node": "^22.3.0",
|
|
22
|
+
"@typescript-eslint/eslint-plugin": "^8.5.0",
|
|
23
|
+
"events": "^3.3.0",
|
|
24
|
+
"jest": "^29.7.0",
|
|
25
|
+
"reflect-metadata": "^0.2.2",
|
|
26
|
+
"rimraf": "^6.0.1",
|
|
27
|
+
"rollup": "^4.20.0",
|
|
28
|
+
"rollup-plugin-typescript2": "^0.36.0",
|
|
29
|
+
"ts-jest": "^29.2.5",
|
|
30
|
+
"ts-node": "^8.9.0",
|
|
31
|
+
"typescript": "^5.5.4"
|
|
32
|
+
},
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"reflect-metadata": "^0.2.2"
|
|
35
|
+
},
|
|
36
|
+
"author": "",
|
|
37
|
+
"license": "ISC",
|
|
38
|
+
"files": [
|
|
39
|
+
"dist"
|
|
40
|
+
]
|
|
41
|
+
}
|