@hemia/common 0.0.3 → 0.0.4

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.
@@ -8,7 +8,10 @@ const METADATA_KEYS = {
8
8
  REDIRECT: 'redirect',
9
9
  GUARDS: 'hemia:guards',
10
10
  INTERCEPTORS: 'hemia:interceptors',
11
- CUSTOM: 'hemia:custom'
11
+ CUSTOM: 'hemia:custom',
12
+ SERIALIZE: 'hemia:serialize',
13
+ ROLES: 'hemia:roles',
14
+ PERMISSIONS: 'hemia:permissions'
12
15
  };
13
16
 
14
17
  const Controller = (basePath = '/') => {
@@ -135,6 +138,30 @@ function UseInterceptors(...interceptors) {
135
138
  };
136
139
  }
137
140
 
141
+ /**
142
+ * Decorador que asigna metadata personalizada a una clase o método.
143
+ * @param metadataKey La clave para guardar la metadata (string, symbol, etc.)
144
+ * @param metadataValue El valor a guardar (objeto, array, primitivo, etc.)
145
+ */
146
+ const SetMetadata = (metadataKey, metadataValue) => {
147
+ return (target, key, descriptor) => {
148
+ if (descriptor) {
149
+ Reflect.defineMetadata(metadataKey, metadataValue, descriptor.value);
150
+ return descriptor;
151
+ }
152
+ Reflect.defineMetadata(metadataKey, metadataValue, target);
153
+ return target;
154
+ };
155
+ };
156
+
157
+ /**
158
+ * Decorador que transforma la respuesta saliente a una instancia del DTO proporcionado.
159
+ * @param dto La clase DTO a la que quieres convertir la respuesta.
160
+ */
161
+ function Serialize(dto) {
162
+ return SetMetadata(METADATA_KEYS.SERIALIZE, dto);
163
+ }
164
+
138
165
  class ApiResponse {
139
166
  static success(data, message = 'OK', status = 200) {
140
167
  return {
@@ -223,4 +250,4 @@ class HttpErrorWithDetails extends HttpError {
223
250
  }
224
251
  }
225
252
 
226
- export { ApiResponse, BadRequestError, Body, ConflictError, Controller, CustomHttpError, Delete, ForbiddenError, GatewayTimeoutError, Get, Head, Header, Headers, Host, HttpError, HttpErrorWithDetails, HttpMethod, InternalServerError, Ip, METADATA_KEYS, Next, NotFoundError, Options, Param, ParamType, Patch, Post, Put, Query, Redirect, Req, Request, Res, Response, ServiceUnavailableError, Session, UnauthorizedError, UnprocessableEntityError, UseGuards, UseInterceptors, isRedirectResponse };
253
+ export { ApiResponse, BadRequestError, Body, ConflictError, Controller, CustomHttpError, Delete, ForbiddenError, GatewayTimeoutError, Get, Head, Header, Headers, Host, HttpError, HttpErrorWithDetails, HttpMethod, InternalServerError, Ip, METADATA_KEYS, Next, NotFoundError, Options, Param, ParamType, Patch, Post, Put, Query, Redirect, Req, Request, Res, Response, Serialize, ServiceUnavailableError, Session, SetMetadata, UnauthorizedError, UnprocessableEntityError, UseGuards, UseInterceptors, isRedirectResponse };
@@ -10,7 +10,10 @@ const METADATA_KEYS = {
10
10
  REDIRECT: 'redirect',
11
11
  GUARDS: 'hemia:guards',
12
12
  INTERCEPTORS: 'hemia:interceptors',
13
- CUSTOM: 'hemia:custom'
13
+ CUSTOM: 'hemia:custom',
14
+ SERIALIZE: 'hemia:serialize',
15
+ ROLES: 'hemia:roles',
16
+ PERMISSIONS: 'hemia:permissions'
14
17
  };
15
18
 
16
19
  const Controller = (basePath = '/') => {
@@ -137,6 +140,30 @@ function UseInterceptors(...interceptors) {
137
140
  };
138
141
  }
139
142
 
143
+ /**
144
+ * Decorador que asigna metadata personalizada a una clase o método.
145
+ * @param metadataKey La clave para guardar la metadata (string, symbol, etc.)
146
+ * @param metadataValue El valor a guardar (objeto, array, primitivo, etc.)
147
+ */
148
+ const SetMetadata = (metadataKey, metadataValue) => {
149
+ return (target, key, descriptor) => {
150
+ if (descriptor) {
151
+ Reflect.defineMetadata(metadataKey, metadataValue, descriptor.value);
152
+ return descriptor;
153
+ }
154
+ Reflect.defineMetadata(metadataKey, metadataValue, target);
155
+ return target;
156
+ };
157
+ };
158
+
159
+ /**
160
+ * Decorador que transforma la respuesta saliente a una instancia del DTO proporcionado.
161
+ * @param dto La clase DTO a la que quieres convertir la respuesta.
162
+ */
163
+ function Serialize(dto) {
164
+ return SetMetadata(METADATA_KEYS.SERIALIZE, dto);
165
+ }
166
+
140
167
  class ApiResponse {
141
168
  static success(data, message = 'OK', status = 200) {
142
169
  return {
@@ -257,8 +284,10 @@ exports.Req = Req;
257
284
  exports.Request = Request;
258
285
  exports.Res = Res;
259
286
  exports.Response = Response;
287
+ exports.Serialize = Serialize;
260
288
  exports.ServiceUnavailableError = ServiceUnavailableError;
261
289
  exports.Session = Session;
290
+ exports.SetMetadata = SetMetadata;
262
291
  exports.UnauthorizedError = UnauthorizedError;
263
292
  exports.UnprocessableEntityError = UnprocessableEntityError;
264
293
  exports.UseGuards = UseGuards;
@@ -0,0 +1,2 @@
1
+ export * from './roles.decorator';
2
+ export * from './permissions.decorator';
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Define los permisos específicos requeridos para acceder a un recurso.
3
+ * @param permissions Lista de permisos (ej: 'read:users', 'write:posts')
4
+ */
5
+ export declare const Permissions: (...permissions: string[]) => (target: object | Function, key?: string | symbol, descriptor?: PropertyDescriptor) => object;
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Define los roles requeridos para acceder a un recurso.
3
+ * @param roles Lista de roles (ej: 'admin', 'user')
4
+ */
5
+ export declare const Roles: (...roles: string[]) => (target: object | Function, key?: string | symbol, descriptor?: PropertyDescriptor) => object;
@@ -1,2 +1,4 @@
1
1
  export * from "./use-guards.decorator";
2
2
  export * from "./use-interceptors.decorator";
3
+ export * from "./set-metadata.decorator";
4
+ export * from "./serialize.decorator";
@@ -0,0 +1,9 @@
1
+ import { Type } from "../../interfaces";
2
+ export interface SerializeOptions {
3
+ excludeExtraneousValues?: boolean;
4
+ }
5
+ /**
6
+ * Decorador que transforma la respuesta saliente a una instancia del DTO proporcionado.
7
+ * @param dto La clase DTO a la que quieres convertir la respuesta.
8
+ */
9
+ export declare function Serialize(dto: Type<any>): (target: object | Function, key?: string | symbol, descriptor?: PropertyDescriptor) => object;
@@ -0,0 +1,7 @@
1
+ import 'reflect-metadata';
2
+ /**
3
+ * Decorador que asigna metadata personalizada a una clase o método.
4
+ * @param metadataKey La clave para guardar la metadata (string, symbol, etc.)
5
+ * @param metadataValue El valor a guardar (objeto, array, primitivo, etc.)
6
+ */
7
+ export declare const SetMetadata: <K = string | symbol, V = any>(metadataKey: K, metadataValue: V) => (target: object | Function, key?: string | symbol, descriptor?: PropertyDescriptor) => object;
@@ -7,4 +7,7 @@ export declare const METADATA_KEYS: {
7
7
  readonly GUARDS: "hemia:guards";
8
8
  readonly INTERCEPTORS: "hemia:interceptors";
9
9
  readonly CUSTOM: "hemia:custom";
10
+ readonly SERIALIZE: "hemia:serialize";
11
+ readonly ROLES: "hemia:roles";
12
+ readonly PERMISSIONS: "hemia:permissions";
10
13
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hemia/common",
3
- "version": "0.0.3",
3
+ "version": "0.0.4",
4
4
  "description": "Paquete común para proyectos de Hemia",
5
5
  "main": "dist/hemia-common.js",
6
6
  "module": "dist/hemia-common.esm.js",