@hemia/common 0.0.14 → 0.0.15

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.
@@ -21,6 +21,16 @@ const METADATA_KEYS = {
21
21
  MODULE: "hemia:module",
22
22
  INJECTION_ID: "hemia:injection_id",
23
23
  MANUAL_REGISTER: 'hemia:controller:manual_register',
24
+ IS_PUBLIC: 'isPublic',
25
+ ALLOW_ANY: 'allowAny',
26
+ OWNER_PARAM: 'ownerParam',
27
+ RATE_LIMIT: 'rateLimit',
28
+ SCOPES: 'scopes',
29
+ THROTTLE: 'hemia:throttle',
30
+ API_KEY: 'hemia:apiKey',
31
+ IP_WHITELIST: 'hemia:ipWhitelist',
32
+ POLICIES: 'hemia:policies',
33
+ FEATURE_FLAGS: 'hemia:featureFlags'
24
34
  };
25
35
 
26
36
  var HttpMethod;
@@ -919,6 +929,105 @@ class ParseDatePipe {
919
929
  }
920
930
  }
921
931
 
932
+ /**
933
+ * Define los roles requeridos para acceder a un recurso.
934
+ * @param roles Lista de roles (ej: 'admin', 'user')
935
+ */
936
+ const Roles = (...roles) => {
937
+ return SetMetadata(METADATA_KEYS.ROLES, roles);
938
+ };
939
+
940
+ /**
941
+ * Define los permisos específicos requeridos para acceder a un recurso.
942
+ * @param permissions Lista de permisos (ej: 'read:users', 'write:posts')
943
+ */
944
+ const Permissions = (...permissions) => {
945
+ return SetMetadata(METADATA_KEYS.PERMISSIONS, permissions);
946
+ };
947
+
948
+ /**
949
+ * Marca una ruta como pública, sin requerir autenticación.
950
+ */
951
+ const Public = () => {
952
+ return SetMetadata(METADATA_KEYS.IS_PUBLIC, true);
953
+ };
954
+
955
+ /**
956
+ * Permite el acceso sin restricciones de roles o permisos.
957
+ * El usuario debe estar autenticado pero no se validan roles/permisos.
958
+ */
959
+ const AllowAny = () => {
960
+ return SetMetadata(METADATA_KEYS.ALLOW_ANY, true);
961
+ };
962
+
963
+ /**
964
+ * Valida que el usuario autenticado sea el propietario del recurso.
965
+ * @param paramName Nombre del parámetro que contiene el ID del propietario (default: 'id')
966
+ */
967
+ const Owner = (paramName = 'id') => {
968
+ return SetMetadata(METADATA_KEYS.OWNER_PARAM, paramName);
969
+ };
970
+
971
+ /**
972
+ * Limita el número de peticiones por ventana de tiempo.
973
+ * @param options Configuración de rate limiting
974
+ */
975
+ const RateLimit = (options) => {
976
+ return SetMetadata(METADATA_KEYS.RATE_LIMIT, options);
977
+ };
978
+
979
+ /**
980
+ * Define los scopes OAuth requeridos para acceder a un recurso.
981
+ * @param scopes Lista de scopes (ej: 'email', 'profile', 'openid')
982
+ */
983
+ const Scopes = (...scopes) => {
984
+ return SetMetadata(METADATA_KEYS.SCOPES, scopes);
985
+ };
986
+
987
+ /**
988
+ * Requiere una API Key válida para acceder al recurso.
989
+ * @param options Configuración de la API Key
990
+ */
991
+ const ApiKey = (options = {}) => {
992
+ return SetMetadata(METADATA_KEYS.API_KEY, {
993
+ headerName: options.headerName || 'X-API-Key',
994
+ queryParam: options.queryParam || 'apiKey',
995
+ required: options.required !== false,
996
+ });
997
+ };
998
+
999
+ /**
1000
+ * Restringe el acceso solo a las IPs especificadas.
1001
+ * @param ips Lista de direcciones IP permitidas
1002
+ */
1003
+ const IpWhitelist = (...ips) => {
1004
+ return SetMetadata(METADATA_KEYS.IP_WHITELIST, ips);
1005
+ };
1006
+
1007
+ /**
1008
+ * Define políticas de autorización complejas.
1009
+ * @param policies Lista de nombres de políticas a evaluar
1010
+ */
1011
+ const PolicyBased = (...policies) => {
1012
+ return SetMetadata(METADATA_KEYS.POLICIES, policies);
1013
+ };
1014
+
1015
+ /**
1016
+ * Requiere que ciertas feature flags estén habilitadas.
1017
+ * @param flags Lista de feature flags requeridas
1018
+ */
1019
+ const FeatureFlag = (...flags) => {
1020
+ return SetMetadata(METADATA_KEYS.FEATURE_FLAGS, flags);
1021
+ };
1022
+
1023
+ /**
1024
+ * Limita el número de peticiones por usuario en un periodo de tiempo.
1025
+ * @param options Configuración de throttling
1026
+ */
1027
+ const Throttle = (options) => {
1028
+ return SetMetadata(METADATA_KEYS.THROTTLE, options);
1029
+ };
1030
+
922
1031
  class ApiResponse {
923
1032
  static success(data, message = 'OK', status = 200) {
924
1033
  return {
@@ -938,4 +1047,4 @@ class ApiResponse {
938
1047
  }
939
1048
  }
940
1049
 
941
- export { ApiResponse, BackupError, BadRequestError, Body, BusinessRuleViolationError, ConfigurationError, ConflictError, ConnectionError, Controller, ControllerRegistry, Cookies, Custom, CustomHttpError, DataConflictError, DataIntegrityError, DataMigrationError, DataNotFoundError, DataValidationError, DefaultValuePipe, Delete, DependencyError, DomainError, DuplicateEntityError, EntityNotFoundError, File, Files, ForbiddenError, GatewayTimeoutError, Get, Head, Header, Headers, Host, HttpError, HttpErrorWithDetails, HttpMethod, IndexingError, InfraAuthenticationError, InfraAuthorizationError, InfraCacheConnectionError, InfraConfigurationError, InfraDataDeserializationError, InfraDataSerializationError, InfraDatabaseConnectionError, InfraExternalServiceError, InfraMessageQueueError, InfraNetworkError, InfraServiceUnavailableError, InfraTimeoutError, InfrastructureError, InternalServerError, Ip, Locale, METADATA_KEYS, ManualRegister, Module, Next, NotFoundError, OperationNotAllowedError, Options, Param, ParamType, ParseArrayPipe, ParseBoolPipe, ParseDatePipe, ParseEnumPipe, ParseFilePipe, ParseFloatPipe, ParseIntPipe, ParseUUIDPipe, Patch, PersistenceError, Post, Put, Query, QueryExecutionError, Redirect, Repository, Req, ReqAuth, ReqContext, ReqPermissions, ReqUser, Request, Res, ResourceLimitError, ResourceLimitExceededError, Response, RestoreError, SchemaMismatchError, Serialize, Service, ServiceUnavailableError, Session, SetMetadata, TimeoutError, TransactionError, Transform, UnauthorizedError, UnprocessableEntityError, UseGuards, UseInterceptors, UsePipes, Validate, ValidationError, ValidationPipe, isRedirectResponse };
1050
+ export { AllowAny, ApiKey, ApiResponse, BackupError, BadRequestError, Body, BusinessRuleViolationError, ConfigurationError, ConflictError, ConnectionError, Controller, ControllerRegistry, Cookies, Custom, CustomHttpError, DataConflictError, DataIntegrityError, DataMigrationError, DataNotFoundError, DataValidationError, DefaultValuePipe, Delete, DependencyError, DomainError, DuplicateEntityError, EntityNotFoundError, FeatureFlag, File, Files, ForbiddenError, GatewayTimeoutError, Get, Head, Header, Headers, Host, HttpError, HttpErrorWithDetails, HttpMethod, IndexingError, InfraAuthenticationError, InfraAuthorizationError, InfraCacheConnectionError, InfraConfigurationError, InfraDataDeserializationError, InfraDataSerializationError, InfraDatabaseConnectionError, InfraExternalServiceError, InfraMessageQueueError, InfraNetworkError, InfraServiceUnavailableError, InfraTimeoutError, InfrastructureError, InternalServerError, Ip, IpWhitelist, Locale, METADATA_KEYS, ManualRegister, Module, Next, NotFoundError, OperationNotAllowedError, Options, Owner, Param, ParamType, ParseArrayPipe, ParseBoolPipe, ParseDatePipe, ParseEnumPipe, ParseFilePipe, ParseFloatPipe, ParseIntPipe, ParseUUIDPipe, Patch, Permissions, PersistenceError, PolicyBased, Post, Public, Put, Query, QueryExecutionError, RateLimit, Redirect, Repository, Req, ReqAuth, ReqContext, ReqPermissions, ReqUser, Request, Res, ResourceLimitError, ResourceLimitExceededError, Response, RestoreError, Roles, SchemaMismatchError, Scopes, Serialize, Service, ServiceUnavailableError, Session, SetMetadata, Throttle, TimeoutError, TransactionError, Transform, UnauthorizedError, UnprocessableEntityError, UseGuards, UseInterceptors, UsePipes, Validate, ValidationError, ValidationPipe, isRedirectResponse };
@@ -23,6 +23,16 @@ const METADATA_KEYS = {
23
23
  MODULE: "hemia:module",
24
24
  INJECTION_ID: "hemia:injection_id",
25
25
  MANUAL_REGISTER: 'hemia:controller:manual_register',
26
+ IS_PUBLIC: 'isPublic',
27
+ ALLOW_ANY: 'allowAny',
28
+ OWNER_PARAM: 'ownerParam',
29
+ RATE_LIMIT: 'rateLimit',
30
+ SCOPES: 'scopes',
31
+ THROTTLE: 'hemia:throttle',
32
+ API_KEY: 'hemia:apiKey',
33
+ IP_WHITELIST: 'hemia:ipWhitelist',
34
+ POLICIES: 'hemia:policies',
35
+ FEATURE_FLAGS: 'hemia:featureFlags'
26
36
  };
27
37
 
28
38
  exports.HttpMethod = void 0;
@@ -921,6 +931,105 @@ class ParseDatePipe {
921
931
  }
922
932
  }
923
933
 
934
+ /**
935
+ * Define los roles requeridos para acceder a un recurso.
936
+ * @param roles Lista de roles (ej: 'admin', 'user')
937
+ */
938
+ const Roles = (...roles) => {
939
+ return SetMetadata(METADATA_KEYS.ROLES, roles);
940
+ };
941
+
942
+ /**
943
+ * Define los permisos específicos requeridos para acceder a un recurso.
944
+ * @param permissions Lista de permisos (ej: 'read:users', 'write:posts')
945
+ */
946
+ const Permissions = (...permissions) => {
947
+ return SetMetadata(METADATA_KEYS.PERMISSIONS, permissions);
948
+ };
949
+
950
+ /**
951
+ * Marca una ruta como pública, sin requerir autenticación.
952
+ */
953
+ const Public = () => {
954
+ return SetMetadata(METADATA_KEYS.IS_PUBLIC, true);
955
+ };
956
+
957
+ /**
958
+ * Permite el acceso sin restricciones de roles o permisos.
959
+ * El usuario debe estar autenticado pero no se validan roles/permisos.
960
+ */
961
+ const AllowAny = () => {
962
+ return SetMetadata(METADATA_KEYS.ALLOW_ANY, true);
963
+ };
964
+
965
+ /**
966
+ * Valida que el usuario autenticado sea el propietario del recurso.
967
+ * @param paramName Nombre del parámetro que contiene el ID del propietario (default: 'id')
968
+ */
969
+ const Owner = (paramName = 'id') => {
970
+ return SetMetadata(METADATA_KEYS.OWNER_PARAM, paramName);
971
+ };
972
+
973
+ /**
974
+ * Limita el número de peticiones por ventana de tiempo.
975
+ * @param options Configuración de rate limiting
976
+ */
977
+ const RateLimit = (options) => {
978
+ return SetMetadata(METADATA_KEYS.RATE_LIMIT, options);
979
+ };
980
+
981
+ /**
982
+ * Define los scopes OAuth requeridos para acceder a un recurso.
983
+ * @param scopes Lista de scopes (ej: 'email', 'profile', 'openid')
984
+ */
985
+ const Scopes = (...scopes) => {
986
+ return SetMetadata(METADATA_KEYS.SCOPES, scopes);
987
+ };
988
+
989
+ /**
990
+ * Requiere una API Key válida para acceder al recurso.
991
+ * @param options Configuración de la API Key
992
+ */
993
+ const ApiKey = (options = {}) => {
994
+ return SetMetadata(METADATA_KEYS.API_KEY, {
995
+ headerName: options.headerName || 'X-API-Key',
996
+ queryParam: options.queryParam || 'apiKey',
997
+ required: options.required !== false,
998
+ });
999
+ };
1000
+
1001
+ /**
1002
+ * Restringe el acceso solo a las IPs especificadas.
1003
+ * @param ips Lista de direcciones IP permitidas
1004
+ */
1005
+ const IpWhitelist = (...ips) => {
1006
+ return SetMetadata(METADATA_KEYS.IP_WHITELIST, ips);
1007
+ };
1008
+
1009
+ /**
1010
+ * Define políticas de autorización complejas.
1011
+ * @param policies Lista de nombres de políticas a evaluar
1012
+ */
1013
+ const PolicyBased = (...policies) => {
1014
+ return SetMetadata(METADATA_KEYS.POLICIES, policies);
1015
+ };
1016
+
1017
+ /**
1018
+ * Requiere que ciertas feature flags estén habilitadas.
1019
+ * @param flags Lista de feature flags requeridas
1020
+ */
1021
+ const FeatureFlag = (...flags) => {
1022
+ return SetMetadata(METADATA_KEYS.FEATURE_FLAGS, flags);
1023
+ };
1024
+
1025
+ /**
1026
+ * Limita el número de peticiones por usuario en un periodo de tiempo.
1027
+ * @param options Configuración de throttling
1028
+ */
1029
+ const Throttle = (options) => {
1030
+ return SetMetadata(METADATA_KEYS.THROTTLE, options);
1031
+ };
1032
+
924
1033
  class ApiResponse {
925
1034
  static success(data, message = 'OK', status = 200) {
926
1035
  return {
@@ -940,6 +1049,8 @@ class ApiResponse {
940
1049
  }
941
1050
  }
942
1051
 
1052
+ exports.AllowAny = AllowAny;
1053
+ exports.ApiKey = ApiKey;
943
1054
  exports.ApiResponse = ApiResponse;
944
1055
  exports.BackupError = BackupError;
945
1056
  exports.BadRequestError = BadRequestError;
@@ -964,6 +1075,7 @@ exports.DependencyError = DependencyError;
964
1075
  exports.DomainError = DomainError;
965
1076
  exports.DuplicateEntityError = DuplicateEntityError;
966
1077
  exports.EntityNotFoundError = EntityNotFoundError;
1078
+ exports.FeatureFlag = FeatureFlag;
967
1079
  exports.File = File;
968
1080
  exports.Files = Files;
969
1081
  exports.ForbiddenError = ForbiddenError;
@@ -991,6 +1103,7 @@ exports.InfraTimeoutError = InfraTimeoutError;
991
1103
  exports.InfrastructureError = InfrastructureError;
992
1104
  exports.InternalServerError = InternalServerError;
993
1105
  exports.Ip = Ip;
1106
+ exports.IpWhitelist = IpWhitelist;
994
1107
  exports.Locale = Locale;
995
1108
  exports.METADATA_KEYS = METADATA_KEYS;
996
1109
  exports.ManualRegister = ManualRegister;
@@ -999,6 +1112,7 @@ exports.Next = Next;
999
1112
  exports.NotFoundError = NotFoundError;
1000
1113
  exports.OperationNotAllowedError = OperationNotAllowedError;
1001
1114
  exports.Options = Options;
1115
+ exports.Owner = Owner;
1002
1116
  exports.Param = Param;
1003
1117
  exports.ParseArrayPipe = ParseArrayPipe;
1004
1118
  exports.ParseBoolPipe = ParseBoolPipe;
@@ -1009,11 +1123,15 @@ exports.ParseFloatPipe = ParseFloatPipe;
1009
1123
  exports.ParseIntPipe = ParseIntPipe;
1010
1124
  exports.ParseUUIDPipe = ParseUUIDPipe;
1011
1125
  exports.Patch = Patch;
1126
+ exports.Permissions = Permissions;
1012
1127
  exports.PersistenceError = PersistenceError;
1128
+ exports.PolicyBased = PolicyBased;
1013
1129
  exports.Post = Post;
1130
+ exports.Public = Public;
1014
1131
  exports.Put = Put;
1015
1132
  exports.Query = Query;
1016
1133
  exports.QueryExecutionError = QueryExecutionError;
1134
+ exports.RateLimit = RateLimit;
1017
1135
  exports.Redirect = Redirect;
1018
1136
  exports.Repository = Repository;
1019
1137
  exports.Req = Req;
@@ -1027,12 +1145,15 @@ exports.ResourceLimitError = ResourceLimitError;
1027
1145
  exports.ResourceLimitExceededError = ResourceLimitExceededError;
1028
1146
  exports.Response = Response;
1029
1147
  exports.RestoreError = RestoreError;
1148
+ exports.Roles = Roles;
1030
1149
  exports.SchemaMismatchError = SchemaMismatchError;
1150
+ exports.Scopes = Scopes;
1031
1151
  exports.Serialize = Serialize;
1032
1152
  exports.Service = Service;
1033
1153
  exports.ServiceUnavailableError = ServiceUnavailableError;
1034
1154
  exports.Session = Session;
1035
1155
  exports.SetMetadata = SetMetadata;
1156
+ exports.Throttle = Throttle;
1036
1157
  exports.TimeoutError = TimeoutError;
1037
1158
  exports.TransactionError = TransactionError;
1038
1159
  exports.Transform = Transform;
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Permite el acceso sin restricciones de roles o permisos.
3
+ * El usuario debe estar autenticado pero no se validan roles/permisos.
4
+ */
5
+ export declare const AllowAny: () => (target: object | Function, key?: string | symbol, descriptor?: PropertyDescriptor) => object;
@@ -0,0 +1,10 @@
1
+ export interface ApiKeyOptions {
2
+ headerName?: string;
3
+ queryParam?: string;
4
+ required?: boolean;
5
+ }
6
+ /**
7
+ * Requiere una API Key válida para acceder al recurso.
8
+ * @param options Configuración de la API Key
9
+ */
10
+ export declare const ApiKey: (options?: ApiKeyOptions) => (target: object | Function, key?: string | symbol, descriptor?: PropertyDescriptor) => object;
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Requiere que ciertas feature flags estén habilitadas.
3
+ * @param flags Lista de feature flags requeridas
4
+ */
5
+ export declare const FeatureFlag: (...flags: string[]) => (target: object | Function, key?: string | symbol, descriptor?: PropertyDescriptor) => object;
@@ -1,2 +1,12 @@
1
1
  export * from './roles.decorator';
2
2
  export * from './permissions.decorator';
3
+ export * from './public.decorator';
4
+ export * from './allow-any.decorator';
5
+ export * from './owner.decorator';
6
+ export * from './rate-limit.decorator';
7
+ export * from './scopes.decorator';
8
+ export * from './api-key.decorator';
9
+ export * from './ip-whitelist.decorator';
10
+ export * from './policy-based.decorator';
11
+ export * from './feature-flag.decorator';
12
+ export * from './throttle.decorator';
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Restringe el acceso solo a las IPs especificadas.
3
+ * @param ips Lista de direcciones IP permitidas
4
+ */
5
+ export declare const IpWhitelist: (...ips: string[]) => (target: object | Function, key?: string | symbol, descriptor?: PropertyDescriptor) => object;
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Valida que el usuario autenticado sea el propietario del recurso.
3
+ * @param paramName Nombre del parámetro que contiene el ID del propietario (default: 'id')
4
+ */
5
+ export declare const Owner: (paramName?: string) => (target: object | Function, key?: string | symbol, descriptor?: PropertyDescriptor) => object;
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Define políticas de autorización complejas.
3
+ * @param policies Lista de nombres de políticas a evaluar
4
+ */
5
+ export declare const PolicyBased: (...policies: string[]) => (target: object | Function, key?: string | symbol, descriptor?: PropertyDescriptor) => object;
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Marca una ruta como pública, sin requerir autenticación.
3
+ */
4
+ export declare const Public: () => (target: object | Function, key?: string | symbol, descriptor?: PropertyDescriptor) => object;
@@ -0,0 +1,10 @@
1
+ export interface RateLimitOptions {
2
+ maxRequests: number;
3
+ windowMs: number;
4
+ message?: string;
5
+ }
6
+ /**
7
+ * Limita el número de peticiones por ventana de tiempo.
8
+ * @param options Configuración de rate limiting
9
+ */
10
+ export declare const RateLimit: (options: RateLimitOptions) => (target: object | Function, key?: string | symbol, descriptor?: PropertyDescriptor) => object;
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Define los scopes OAuth requeridos para acceder a un recurso.
3
+ * @param scopes Lista de scopes (ej: 'email', 'profile', 'openid')
4
+ */
5
+ export declare const Scopes: (...scopes: string[]) => (target: object | Function, key?: string | symbol, descriptor?: PropertyDescriptor) => object;
@@ -0,0 +1,10 @@
1
+ export interface ThrottleOptions {
2
+ limit: number;
3
+ ttl: number;
4
+ skipIf?: (context: any) => boolean;
5
+ }
6
+ /**
7
+ * Limita el número de peticiones por usuario en un periodo de tiempo.
8
+ * @param options Configuración de throttling
9
+ */
10
+ export declare const Throttle: (options: ThrottleOptions) => (target: object | Function, key?: string | symbol, descriptor?: PropertyDescriptor) => object;
@@ -5,3 +5,4 @@ export * from "./custom";
5
5
  export * from "./validation";
6
6
  export * from "./transform";
7
7
  export * from "./pipes";
8
+ export * from "./auth";
@@ -18,4 +18,14 @@ export declare const METADATA_KEYS: {
18
18
  readonly MODULE: "hemia:module";
19
19
  readonly INJECTION_ID: "hemia:injection_id";
20
20
  readonly MANUAL_REGISTER: "hemia:controller:manual_register";
21
+ readonly IS_PUBLIC: "isPublic";
22
+ readonly ALLOW_ANY: "allowAny";
23
+ readonly OWNER_PARAM: "ownerParam";
24
+ readonly RATE_LIMIT: "rateLimit";
25
+ readonly SCOPES: "scopes";
26
+ readonly THROTTLE: "hemia:throttle";
27
+ readonly API_KEY: "hemia:apiKey";
28
+ readonly IP_WHITELIST: "hemia:ipWhitelist";
29
+ readonly POLICIES: "hemia:policies";
30
+ readonly FEATURE_FLAGS: "hemia:featureFlags";
21
31
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hemia/common",
3
- "version": "0.0.14",
3
+ "version": "0.0.15",
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",