@riktajs/core 0.10.2 → 0.10.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.
Files changed (111) hide show
  1. package/dist/index.cjs +3591 -0
  2. package/dist/index.cjs.map +1 -0
  3. package/dist/index.d.cts +770 -0
  4. package/dist/index.d.ts +770 -2
  5. package/dist/index.js +3415 -2
  6. package/dist/index.js.map +1 -0
  7. package/package.json +12 -5
  8. package/dist/core/application.d.ts +0 -6
  9. package/dist/core/application.js +0 -248
  10. package/dist/core/config/abstract-config-provider.d.ts +0 -14
  11. package/dist/core/config/abstract-config-provider.js +0 -53
  12. package/dist/core/config/env-loader.d.ts +0 -3
  13. package/dist/core/config/env-loader.js +0 -26
  14. package/dist/core/config/index.d.ts +0 -2
  15. package/dist/core/config/index.js +0 -2
  16. package/dist/core/constants.d.ts +0 -30
  17. package/dist/core/constants.js +0 -31
  18. package/dist/core/container/abstract-class.utils.d.ts +0 -10
  19. package/dist/core/container/abstract-class.utils.js +0 -44
  20. package/dist/core/container/container.d.ts +0 -32
  21. package/dist/core/container/container.js +0 -292
  22. package/dist/core/container/implements.decorator.d.ts +0 -6
  23. package/dist/core/container/implements.decorator.js +0 -39
  24. package/dist/core/container/index.d.ts +0 -5
  25. package/dist/core/container/index.js +0 -5
  26. package/dist/core/container/injection-token.d.ts +0 -29
  27. package/dist/core/container/injection-token.js +0 -11
  28. package/dist/core/container/request-scope.d.ts +0 -16
  29. package/dist/core/container/request-scope.js +0 -49
  30. package/dist/core/decorators/apply-decorators.d.ts +0 -8
  31. package/dist/core/decorators/apply-decorators.js +0 -36
  32. package/dist/core/decorators/autowired.decorator.d.ts +0 -11
  33. package/dist/core/decorators/autowired.decorator.js +0 -78
  34. package/dist/core/decorators/config-property.decorator.d.ts +0 -9
  35. package/dist/core/decorators/config-property.decorator.js +0 -59
  36. package/dist/core/decorators/controller.decorator.d.ts +0 -5
  37. package/dist/core/decorators/controller.decorator.js +0 -18
  38. package/dist/core/decorators/create-param-decorator.d.ts +0 -11
  39. package/dist/core/decorators/create-param-decorator.js +0 -21
  40. package/dist/core/decorators/index.d.ts +0 -9
  41. package/dist/core/decorators/index.js +0 -9
  42. package/dist/core/decorators/injectable.decorator.d.ts +0 -3
  43. package/dist/core/decorators/injectable.decorator.js +0 -11
  44. package/dist/core/decorators/param.decorator.d.ts +0 -59
  45. package/dist/core/decorators/param.decorator.js +0 -38
  46. package/dist/core/decorators/provider.decorator.d.ts +0 -12
  47. package/dist/core/decorators/provider.decorator.js +0 -56
  48. package/dist/core/decorators/route.decorator.d.ts +0 -9
  49. package/dist/core/decorators/route.decorator.js +0 -30
  50. package/dist/core/discovery.d.ts +0 -3
  51. package/dist/core/discovery.js +0 -111
  52. package/dist/core/exceptions/catch.decorator.d.ts +0 -8
  53. package/dist/core/exceptions/catch.decorator.js +0 -13
  54. package/dist/core/exceptions/config.exceptions.d.ts +0 -9
  55. package/dist/core/exceptions/config.exceptions.js +0 -34
  56. package/dist/core/exceptions/discovery.exception.d.ts +0 -18
  57. package/dist/core/exceptions/discovery.exception.js +0 -39
  58. package/dist/core/exceptions/exception-filter.d.ts +0 -38
  59. package/dist/core/exceptions/exception-filter.js +0 -103
  60. package/dist/core/exceptions/exceptions.d.ts +0 -55
  61. package/dist/core/exceptions/exceptions.js +0 -91
  62. package/dist/core/exceptions/http-exception.d.ts +0 -24
  63. package/dist/core/exceptions/http-exception.js +0 -62
  64. package/dist/core/exceptions/index.d.ts +0 -12
  65. package/dist/core/exceptions/index.js +0 -7
  66. package/dist/core/exceptions/validation.exception.d.ts +0 -17
  67. package/dist/core/exceptions/validation.exception.js +0 -33
  68. package/dist/core/guards/can-activate.interface.d.ts +0 -4
  69. package/dist/core/guards/can-activate.interface.js +0 -1
  70. package/dist/core/guards/execution-context.d.ts +0 -28
  71. package/dist/core/guards/execution-context.js +0 -35
  72. package/dist/core/guards/index.d.ts +0 -4
  73. package/dist/core/guards/index.js +0 -2
  74. package/dist/core/guards/use-guards.decorator.d.ts +0 -6
  75. package/dist/core/guards/use-guards.decorator.js +0 -22
  76. package/dist/core/index.d.ts +0 -19
  77. package/dist/core/index.js +0 -17
  78. package/dist/core/interceptors/index.d.ts +0 -3
  79. package/dist/core/interceptors/index.js +0 -1
  80. package/dist/core/interceptors/interceptor.interface.d.ts +0 -7
  81. package/dist/core/interceptors/interceptor.interface.js +0 -1
  82. package/dist/core/interceptors/use-interceptors.decorator.d.ts +0 -6
  83. package/dist/core/interceptors/use-interceptors.decorator.js +0 -19
  84. package/dist/core/lifecycle/event-bus.d.ts +0 -57
  85. package/dist/core/lifecycle/event-bus.js +0 -108
  86. package/dist/core/lifecycle/index.d.ts +0 -3
  87. package/dist/core/lifecycle/index.js +0 -3
  88. package/dist/core/lifecycle/interfaces.d.ts +0 -15
  89. package/dist/core/lifecycle/interfaces.js +0 -1
  90. package/dist/core/lifecycle/on.decorator.d.ts +0 -11
  91. package/dist/core/lifecycle/on.decorator.js +0 -13
  92. package/dist/core/metadata.d.ts +0 -20
  93. package/dist/core/metadata.js +0 -64
  94. package/dist/core/middleware/index.d.ts +0 -4
  95. package/dist/core/middleware/index.js +0 -2
  96. package/dist/core/middleware/middleware.decorator.d.ts +0 -2
  97. package/dist/core/middleware/middleware.decorator.js +0 -7
  98. package/dist/core/middleware/rikta-middleware.interface.d.ts +0 -5
  99. package/dist/core/middleware/rikta-middleware.interface.js +0 -1
  100. package/dist/core/middleware/use-middleware.decorator.d.ts +0 -6
  101. package/dist/core/middleware/use-middleware.decorator.js +0 -22
  102. package/dist/core/profiler/index.d.ts +0 -2
  103. package/dist/core/profiler/index.js +0 -1
  104. package/dist/core/profiler/performance-profiler.d.ts +0 -42
  105. package/dist/core/profiler/performance-profiler.js +0 -95
  106. package/dist/core/registry.d.ts +0 -41
  107. package/dist/core/registry.js +0 -109
  108. package/dist/core/router/router.d.ts +0 -32
  109. package/dist/core/router/router.js +0 -300
  110. package/dist/core/types.d.ts +0 -62
  111. package/dist/core/types.js +0 -1
@@ -1,292 +0,0 @@
1
- import 'reflect-metadata';
2
- import { INJECT_METADATA, AUTOWIRED_METADATA } from '../constants.js';
3
- import { InjectionToken } from './injection-token.js';
4
- import { registry } from '../registry.js';
5
- import { isAbstractClass } from './abstract-class.utils.js';
6
- import { requestScopeStorage } from './request-scope.js';
7
- export class Container {
8
- static instance;
9
- singletons = new Map();
10
- providers = new Map();
11
- resolutionStack = new Set();
12
- constructor() { }
13
- static getInstance() {
14
- if (!Container.instance) {
15
- Container.instance = new Container();
16
- }
17
- return Container.instance;
18
- }
19
- static reset() {
20
- Container.instance = new Container();
21
- }
22
- register(target, options = {}) {
23
- const scope = options.scope ?? 'singleton';
24
- this.providers.set(target, { scope });
25
- }
26
- registerProvider(provider) {
27
- if (typeof provider === 'function') {
28
- this.register(provider);
29
- return;
30
- }
31
- const token = provider.provide;
32
- if ('useValue' in provider) {
33
- this.providers.set(token, { scope: 'singleton', provider });
34
- this.singletons.set(token, provider.useValue);
35
- }
36
- else if ('useFactory' in provider) {
37
- this.providers.set(token, { scope: 'singleton', provider });
38
- }
39
- else if ('useClass' in provider) {
40
- this.providers.set(token, { scope: 'singleton', provider });
41
- }
42
- else if ('useExisting' in provider) {
43
- this.providers.set(token, { scope: 'singleton', provider });
44
- }
45
- }
46
- registerValue(token, value) {
47
- this.registerProvider({ provide: token, useValue: value });
48
- }
49
- registerFactory(token, factory, inject) {
50
- this.registerProvider({ provide: token, useFactory: factory, inject });
51
- }
52
- has(token) {
53
- return this.providers.has(token);
54
- }
55
- resolve(token) {
56
- return this.resolveToken(token, false);
57
- }
58
- resolveOptional(token) {
59
- return this.resolveToken(token, true);
60
- }
61
- resolveToken(token, optional) {
62
- if (this.resolutionStack.has(token)) {
63
- const chain = [...this.resolutionStack]
64
- .map(t => this.getTokenName(t))
65
- .join(' -> ');
66
- throw new Error(`Circular dependency detected: ${chain} -> ${this.getTokenName(token)}`);
67
- }
68
- if (this.singletons.has(token)) {
69
- return this.singletons.get(token);
70
- }
71
- const config = this.providers.get(token);
72
- if (!config) {
73
- if (typeof token === 'function') {
74
- const implementation = this.resolveAbstractClass(token);
75
- if (implementation) {
76
- const instance = this.resolveClass(implementation);
77
- this.singletons.set(token, instance);
78
- return instance;
79
- }
80
- if (!isAbstractClass(token)) {
81
- return this.resolveClass(token);
82
- }
83
- }
84
- if (optional) {
85
- return undefined;
86
- }
87
- throw new Error(`No provider found for token: ${this.getTokenName(token)}`);
88
- }
89
- if (config.provider && typeof config.provider !== 'function') {
90
- return this.resolveProvider(config.provider, config.scope);
91
- }
92
- if (typeof token === 'function') {
93
- return this.resolveClass(token);
94
- }
95
- throw new Error(`Cannot resolve token: ${this.getTokenName(token)}`);
96
- }
97
- resolveAbstractClass(abstractClass, name) {
98
- const config = this.providers.get(abstractClass);
99
- if (config?.provider && 'useClass' in config.provider) {
100
- return config.provider.useClass;
101
- }
102
- const implementations = registry.getImplementations(abstractClass);
103
- if (!implementations || implementations.length === 0) {
104
- return undefined;
105
- }
106
- if (name) {
107
- const named = implementations.find(i => i.name === name);
108
- if (named) {
109
- return named.implementation;
110
- }
111
- throw new Error(`No implementation named '${name}' found for abstract class ${abstractClass.name}. ` +
112
- `Available names: ${implementations.filter(i => i.name).map(i => i.name).join(', ') || 'none'}`);
113
- }
114
- if (implementations.length === 1) {
115
- return implementations[0].implementation;
116
- }
117
- const primary = implementations.find(i => i.isPrimary);
118
- if (primary) {
119
- return primary.implementation;
120
- }
121
- const implNames = implementations.map(i => i.implementation.name).join(', ');
122
- throw new Error(`Multiple implementations found for abstract class ${abstractClass.name}: ${implNames}. ` +
123
- `Use @Primary() to mark one as the default, or @Named() for qualified injection.`);
124
- }
125
- resolveProvider(provider, scope) {
126
- if (typeof provider === 'function') {
127
- return this.resolveClass(provider);
128
- }
129
- const token = provider.provide;
130
- if ('useValue' in provider) {
131
- return provider.useValue;
132
- }
133
- if ('useFactory' in provider) {
134
- const factoryProvider = provider;
135
- const deps = (factoryProvider.inject ?? []).map(dep => this.resolve(dep));
136
- const result = factoryProvider.useFactory(...deps);
137
- if (scope === 'singleton') {
138
- this.singletons.set(token, result);
139
- }
140
- return result;
141
- }
142
- if ('useClass' in provider) {
143
- const classProvider = provider;
144
- const instance = this.resolveClass(classProvider.useClass);
145
- if (scope === 'singleton') {
146
- this.singletons.set(token, instance);
147
- }
148
- return instance;
149
- }
150
- if ('useExisting' in provider) {
151
- return this.resolve(provider.useExisting);
152
- }
153
- throw new Error('Invalid provider configuration');
154
- }
155
- resolveClass(target) {
156
- if (this.resolutionStack.has(target)) {
157
- const chain = [...this.resolutionStack, target]
158
- .map(t => this.getTokenName(t))
159
- .join(' -> ');
160
- throw new Error(`Circular dependency detected: ${chain}`);
161
- }
162
- const providerConfig = this.providers.get(target);
163
- const scope = providerConfig?.scope ?? 'singleton';
164
- if (scope === 'singleton' && this.singletons.has(target)) {
165
- return this.singletons.get(target);
166
- }
167
- if (scope === 'request') {
168
- if (!requestScopeStorage.isInRequestScope()) {
169
- throw new Error(`Cannot resolve request-scoped provider '${target.name}' outside of a request context. ` +
170
- `Request-scoped providers can only be resolved during HTTP request handling.`);
171
- }
172
- const existingInstance = requestScopeStorage.get(target);
173
- if (existingInstance !== undefined) {
174
- return existingInstance;
175
- }
176
- }
177
- this.resolutionStack.add(target);
178
- try {
179
- const injectMeta = Reflect.getMetadata(INJECT_METADATA, target) ?? [];
180
- const paramTypes = Reflect.getMetadata('design:paramtypes', target) ?? [];
181
- const maxIndex = injectMeta.length > 0
182
- ? Math.max(...injectMeta.map(m => m.index ?? -1))
183
- : -1;
184
- const paramCount = Math.max(paramTypes.length, maxIndex + 1);
185
- const dependencies = [];
186
- for (let index = 0; index < paramCount; index++) {
187
- const paramType = paramTypes[index];
188
- const injectOverride = injectMeta.find(m => m.index === index);
189
- const token = injectOverride?.token ?? paramType;
190
- const isOptional = injectOverride?.optional ?? false;
191
- const name = injectOverride?.name;
192
- if (!token) {
193
- if (isOptional) {
194
- dependencies.push(undefined);
195
- continue;
196
- }
197
- throw new Error(`Cannot resolve constructor parameter ${index} of ${target.name}. ` +
198
- `Use @Autowired(token) decorator.`);
199
- }
200
- if (!injectOverride && this.isPrimitive(paramType)) {
201
- if (isOptional) {
202
- dependencies.push(undefined);
203
- continue;
204
- }
205
- throw new Error(`Cannot inject primitive type '${paramType?.name ?? 'unknown'}' into ${target.name}. ` +
206
- `Use @Autowired(token) decorator.`);
207
- }
208
- dependencies.push(isOptional
209
- ? this.resolveWithNameOptional(token, name)
210
- : this.resolveWithName(token, name));
211
- }
212
- const instance = new target(...dependencies);
213
- this.injectProperties(target, instance);
214
- if (scope === 'singleton') {
215
- this.singletons.set(target, instance);
216
- }
217
- else if (scope === 'request') {
218
- requestScopeStorage.set(target, instance);
219
- }
220
- return instance;
221
- }
222
- finally {
223
- this.resolutionStack.delete(target);
224
- }
225
- }
226
- injectProperties(target, instance) {
227
- const autowireMeta = Reflect.getMetadata(AUTOWIRED_METADATA, target) ?? [];
228
- for (const meta of autowireMeta) {
229
- if (!meta.propertyKey)
230
- continue;
231
- const isOptional = meta.optional ?? false;
232
- try {
233
- const value = isOptional
234
- ? this.resolveWithNameOptional(meta.token, meta.name)
235
- : this.resolveWithName(meta.token, meta.name);
236
- instance[meta.propertyKey] = value;
237
- }
238
- catch (error) {
239
- if (!isOptional) {
240
- throw error;
241
- }
242
- }
243
- }
244
- }
245
- resolveWithName(token, name) {
246
- if (name && typeof token === 'function') {
247
- const implementation = this.resolveAbstractClass(token, name);
248
- if (implementation) {
249
- return this.resolveClass(implementation);
250
- }
251
- throw new Error(`No implementation named '${name}' found for ${this.getTokenName(token)}`);
252
- }
253
- return this.resolve(token);
254
- }
255
- resolveWithNameOptional(token, name) {
256
- try {
257
- return this.resolveWithName(token, name);
258
- }
259
- catch {
260
- return undefined;
261
- }
262
- }
263
- registerInstance(target, instance) {
264
- this.providers.set(target, { scope: 'singleton' });
265
- this.singletons.set(target, instance);
266
- }
267
- getProviders() {
268
- return [...this.providers.keys()];
269
- }
270
- clearSingletons() {
271
- this.singletons.clear();
272
- }
273
- isPrimitive(type) {
274
- if (!type)
275
- return true;
276
- const primitives = [String, Number, Boolean, Object, Array, Function];
277
- return primitives.includes(type);
278
- }
279
- getTokenName(token) {
280
- if (typeof token === 'function') {
281
- return token.name;
282
- }
283
- if (token instanceof InjectionToken) {
284
- return token.toString();
285
- }
286
- if (typeof token === 'symbol') {
287
- return token.toString();
288
- }
289
- return String(token);
290
- }
291
- }
292
- export const container = Container.getInstance();
@@ -1,6 +0,0 @@
1
- import 'reflect-metadata';
2
- import { AnyConstructor } from '../types.js';
3
- export declare function Implements<T extends AnyConstructor>(abstractClass: T): ClassDecorator;
4
- export declare function Primary(): ClassDecorator;
5
- export declare function Named(name: string): ClassDecorator;
6
- export declare function AbstractClass(): ClassDecorator;
@@ -1,39 +0,0 @@
1
- import 'reflect-metadata';
2
- import { setImplementsMetadata, setPrimaryMetadata, markAsAbstract, IMPLEMENTS_METADATA } from './abstract-class.utils.js';
3
- import { registry } from '../registry.js';
4
- export function Implements(abstractClass) {
5
- return (target) => {
6
- setImplementsMetadata(target, abstractClass);
7
- markAsAbstract(abstractClass);
8
- const name = Reflect.getMetadata('rikta:named', target);
9
- registry.registerAbstractImplementation(abstractClass, target, name);
10
- const isPrimary = Reflect.getMetadata('rikta:is-primary', target) === true;
11
- if (isPrimary) {
12
- registry.setPrimaryImplementation(abstractClass, target);
13
- }
14
- };
15
- }
16
- export function Primary() {
17
- return (target) => {
18
- setPrimaryMetadata(target);
19
- const abstractClass = Reflect.getMetadata(IMPLEMENTS_METADATA, target);
20
- if (abstractClass) {
21
- registry.setPrimaryImplementation(abstractClass, target);
22
- }
23
- Reflect.defineMetadata('rikta:is-primary', true, target);
24
- };
25
- }
26
- export function Named(name) {
27
- return (target) => {
28
- Reflect.defineMetadata('rikta:named', name, target);
29
- const abstractClass = Reflect.getMetadata(IMPLEMENTS_METADATA, target);
30
- if (abstractClass) {
31
- registry.setImplementationName(abstractClass, target, name);
32
- }
33
- };
34
- }
35
- export function AbstractClass() {
36
- return (target) => {
37
- markAsAbstract(target);
38
- };
39
- }
@@ -1,5 +0,0 @@
1
- export * from './container.js';
2
- export * from './injection-token.js';
3
- export * from './abstract-class.utils.js';
4
- export * from './implements.decorator.js';
5
- export * from './request-scope.js';
@@ -1,5 +0,0 @@
1
- export * from './container.js';
2
- export * from './injection-token.js';
3
- export * from './abstract-class.utils.js';
4
- export * from './implements.decorator.js';
5
- export * from './request-scope.js';
@@ -1,29 +0,0 @@
1
- export declare class InjectionToken<T = unknown> {
2
- readonly description: string;
3
- readonly options?: {
4
- factory?: () => T;
5
- } | undefined;
6
- constructor(description: string, options?: {
7
- factory?: () => T;
8
- } | undefined);
9
- toString(): string;
10
- }
11
- export type Token<T = unknown> = (new (...args: unknown[]) => T) | InjectionToken<T> | string | symbol;
12
- export interface ClassProvider<T = unknown> {
13
- provide: Token<T>;
14
- useClass: new (...args: unknown[]) => T;
15
- }
16
- export interface ValueProvider<T = unknown> {
17
- provide: Token<T>;
18
- useValue: T;
19
- }
20
- export interface FactoryProvider<T = unknown> {
21
- provide: Token<T>;
22
- useFactory: (...args: unknown[]) => T | Promise<T>;
23
- inject?: Token[];
24
- }
25
- export interface ExistingProvider<T = unknown> {
26
- provide: Token<T>;
27
- useExisting: Token<T>;
28
- }
29
- export type ProviderDefinition<T = unknown> = ClassProvider<T> | ValueProvider<T> | FactoryProvider<T> | ExistingProvider<T> | (new (...args: unknown[]) => T);
@@ -1,11 +0,0 @@
1
- export class InjectionToken {
2
- description;
3
- options;
4
- constructor(description, options) {
5
- this.description = description;
6
- this.options = options;
7
- }
8
- toString() {
9
- return `InjectionToken(${this.description})`;
10
- }
11
- }
@@ -1,16 +0,0 @@
1
- import { Token } from './injection-token.js';
2
- export declare class RequestScopeStorage {
3
- private static instance;
4
- private readonly asyncLocalStorage;
5
- private constructor();
6
- static getInstance(): RequestScopeStorage;
7
- static reset(): void;
8
- run<T>(fn: () => T): T;
9
- runAsync<T>(fn: () => Promise<T>): Promise<T>;
10
- isInRequestScope(): boolean;
11
- get<T>(token: Token<T>): T | undefined;
12
- set<T>(token: Token<T>, instance: T): void;
13
- has(token: Token): boolean;
14
- getStore(): Map<Token, unknown> | undefined;
15
- }
16
- export declare const requestScopeStorage: RequestScopeStorage;
@@ -1,49 +0,0 @@
1
- import { AsyncLocalStorage } from 'node:async_hooks';
2
- export class RequestScopeStorage {
3
- static instance;
4
- asyncLocalStorage = new AsyncLocalStorage();
5
- constructor() { }
6
- static getInstance() {
7
- if (!RequestScopeStorage.instance) {
8
- RequestScopeStorage.instance = new RequestScopeStorage();
9
- }
10
- return RequestScopeStorage.instance;
11
- }
12
- static reset() {
13
- RequestScopeStorage.instance = new RequestScopeStorage();
14
- }
15
- run(fn) {
16
- const store = new Map();
17
- return this.asyncLocalStorage.run(store, fn);
18
- }
19
- async runAsync(fn) {
20
- const store = new Map();
21
- return this.asyncLocalStorage.run(store, fn);
22
- }
23
- isInRequestScope() {
24
- return this.asyncLocalStorage.getStore() !== undefined;
25
- }
26
- get(token) {
27
- const store = this.asyncLocalStorage.getStore();
28
- if (!store) {
29
- return undefined;
30
- }
31
- return store.get(token);
32
- }
33
- set(token, instance) {
34
- const store = this.asyncLocalStorage.getStore();
35
- if (!store) {
36
- throw new Error('Cannot set request-scoped instance outside of a request context. ' +
37
- 'Make sure RequestScopeStorage.run() or runAsync() is being used.');
38
- }
39
- store.set(token, instance);
40
- }
41
- has(token) {
42
- const store = this.asyncLocalStorage.getStore();
43
- return store?.has(token) ?? false;
44
- }
45
- getStore() {
46
- return this.asyncLocalStorage.getStore();
47
- }
48
- }
49
- export const requestScopeStorage = RequestScopeStorage.getInstance();
@@ -1,8 +0,0 @@
1
- import 'reflect-metadata';
2
- type ClassDecorator = <TFunction extends Function>(target: TFunction) => TFunction | void;
3
- type MethodDecorator = <T>(target: Object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T> | void;
4
- type PropertyDecorator = (target: Object, propertyKey: string | symbol) => void;
5
- type AnyDecorator = ClassDecorator | MethodDecorator | PropertyDecorator;
6
- export declare function applyDecorators(...decorators: AnyDecorator[]): MethodDecorator & ClassDecorator & PropertyDecorator;
7
- export declare function SetMetadata<T = unknown>(key: string | symbol, value: T): MethodDecorator & ClassDecorator;
8
- export {};
@@ -1,36 +0,0 @@
1
- import 'reflect-metadata';
2
- export function applyDecorators(...decorators) {
3
- return ((target, propertyKey, descriptor) => {
4
- for (const decorator of decorators.reverse()) {
5
- if (descriptor) {
6
- const result = decorator(target, propertyKey, descriptor);
7
- if (result) {
8
- descriptor = result;
9
- }
10
- }
11
- else if (propertyKey !== undefined) {
12
- decorator(target, propertyKey);
13
- }
14
- else {
15
- const result = decorator(target);
16
- if (result) {
17
- return result;
18
- }
19
- }
20
- }
21
- if (descriptor) {
22
- return descriptor;
23
- }
24
- });
25
- }
26
- export function SetMetadata(key, value) {
27
- const decoratorFactory = (target, propertyKey, descriptor) => {
28
- if (descriptor) {
29
- Reflect.defineMetadata(key, value, target.constructor, propertyKey);
30
- }
31
- else {
32
- Reflect.defineMetadata(key, value, target);
33
- }
34
- };
35
- return decoratorFactory;
36
- }
@@ -1,11 +0,0 @@
1
- import 'reflect-metadata';
2
- import { Token } from '../container/injection-token.js';
3
- export interface AutowiredMetadata {
4
- token: Token;
5
- index?: number;
6
- propertyKey?: string;
7
- optional?: boolean;
8
- name?: string;
9
- }
10
- export declare function Autowired(token?: Token, name?: string): ParameterDecorator & PropertyDecorator;
11
- export declare function Optional(): ParameterDecorator & PropertyDecorator;
@@ -1,78 +0,0 @@
1
- import 'reflect-metadata';
2
- import { INJECT_METADATA, AUTOWIRED_METADATA } from '../constants.js';
3
- export function Autowired(token, name) {
4
- return (target, propertyKey, parameterIndex) => {
5
- if (typeof parameterIndex === 'number') {
6
- const paramTypes = Reflect.getMetadata('design:paramtypes', target) ?? [];
7
- const inferredType = paramTypes[parameterIndex];
8
- const resolvedToken = token ?? inferredType;
9
- if (!resolvedToken) {
10
- throw new Error(`@Autowired() on constructor parameter ${parameterIndex} of ${target.name} ` +
11
- `could not infer type. Please provide a token explicitly.`);
12
- }
13
- const existingInjects = Reflect.getMetadata(INJECT_METADATA, target) ?? [];
14
- existingInjects.push({
15
- token: resolvedToken,
16
- index: parameterIndex,
17
- name,
18
- });
19
- Reflect.defineMetadata(INJECT_METADATA, existingInjects, target);
20
- }
21
- else if (propertyKey !== undefined) {
22
- const inferredType = Reflect.getMetadata('design:type', target, propertyKey);
23
- const resolvedToken = token ?? inferredType;
24
- if (!resolvedToken) {
25
- throw new Error(`@Autowired() on ${target.constructor.name}.${String(propertyKey)} ` +
26
- `could not infer type. Please provide a token explicitly.`);
27
- }
28
- const existingAutowires = Reflect.getMetadata(AUTOWIRED_METADATA, target.constructor) ?? [];
29
- existingAutowires.push({
30
- token: resolvedToken,
31
- propertyKey: String(propertyKey),
32
- name,
33
- });
34
- Reflect.defineMetadata(AUTOWIRED_METADATA, existingAutowires, target.constructor);
35
- }
36
- };
37
- }
38
- export function Optional() {
39
- return (target, propertyKey, parameterIndex) => {
40
- if (typeof parameterIndex === 'number') {
41
- const existingInjects = Reflect.getMetadata(INJECT_METADATA, target) ?? [];
42
- let meta = existingInjects.find(m => m.index === parameterIndex);
43
- if (meta) {
44
- meta.optional = true;
45
- }
46
- else {
47
- const paramTypes = Reflect.getMetadata('design:paramtypes', target) ?? [];
48
- const paramType = paramTypes[parameterIndex];
49
- if (paramType) {
50
- existingInjects.push({
51
- token: paramType,
52
- index: parameterIndex,
53
- optional: true,
54
- });
55
- }
56
- }
57
- Reflect.defineMetadata(INJECT_METADATA, existingInjects, target);
58
- }
59
- else if (propertyKey !== undefined) {
60
- const existingAutowires = Reflect.getMetadata(AUTOWIRED_METADATA, target.constructor) ?? [];
61
- let meta = existingAutowires.find(m => m.propertyKey === String(propertyKey));
62
- if (meta) {
63
- meta.optional = true;
64
- }
65
- else {
66
- const propType = Reflect.getMetadata('design:type', target, propertyKey);
67
- if (propType) {
68
- existingAutowires.push({
69
- token: propType,
70
- propertyKey: String(propertyKey),
71
- optional: true,
72
- });
73
- }
74
- }
75
- Reflect.defineMetadata(AUTOWIRED_METADATA, existingAutowires, target.constructor);
76
- }
77
- };
78
- }
@@ -1,9 +0,0 @@
1
- import 'reflect-metadata';
2
- export interface ConfigPropertyMapping {
3
- propertyKey: string;
4
- envKey: string;
5
- }
6
- export declare function ConfigProperty(envKey?: string): PropertyDecorator;
7
- export declare function getConfigPropertyMappings(target: Function): ConfigPropertyMapping[];
8
- export declare function hasConfigProperties(target: Function): boolean;
9
- export declare function clearPropertyNameCache(): void;
@@ -1,59 +0,0 @@
1
- import 'reflect-metadata';
2
- import { CONFIG_PROPERTY_METADATA } from '../constants.js';
3
- const propertyNameCache = new Map();
4
- function toUpperSnakeCase(propertyName) {
5
- if (propertyNameCache.has(propertyName)) {
6
- return propertyNameCache.get(propertyName);
7
- }
8
- const envKey = propertyName
9
- .replace(/([A-Z])/g, '_$1')
10
- .toUpperCase()
11
- .replace(/^_/, '');
12
- propertyNameCache.set(propertyName, envKey);
13
- return envKey;
14
- }
15
- export function ConfigProperty(envKey) {
16
- return (target, propertyKey) => {
17
- if (typeof propertyKey === 'symbol') {
18
- throw new Error(`@ConfigProperty: Symbol properties are not supported. ` +
19
- `Property "${String(propertyKey)}" must be a string.`);
20
- }
21
- const finalEnvKey = envKey ?? toUpperSnakeCase(propertyKey);
22
- if (!finalEnvKey || typeof finalEnvKey !== 'string') {
23
- throw new Error(`@ConfigProperty: Invalid env key for property "${propertyKey}". ` +
24
- `Env key must be a non-empty string.`);
25
- }
26
- if (finalEnvKey !== finalEnvKey.toUpperCase()) {
27
- throw new Error(`@ConfigProperty: Env key "${finalEnvKey}" for property "${propertyKey}" must be UPPERCASE. ` +
28
- `Use "${finalEnvKey.toUpperCase()}" instead.`);
29
- }
30
- const constructor = target.constructor;
31
- const existingMappings = Reflect.getMetadata(CONFIG_PROPERTY_METADATA, constructor) || [];
32
- const duplicate = existingMappings.find(m => m.propertyKey === propertyKey);
33
- if (duplicate) {
34
- throw new Error(`@ConfigProperty: Property "${propertyKey}" on class "${constructor.name}" ` +
35
- `is already decorated. Remove the duplicate @ConfigProperty decorator.`);
36
- }
37
- const duplicateEnvKey = existingMappings.find(m => m.envKey === finalEnvKey);
38
- if (duplicateEnvKey) {
39
- throw new Error(`@ConfigProperty: Env key "${finalEnvKey}" is already mapped to property "${duplicateEnvKey.propertyKey}" ` +
40
- `on class "${constructor.name}". Each env key can only be mapped to one property.`);
41
- }
42
- const newMapping = {
43
- propertyKey,
44
- envKey: finalEnvKey,
45
- };
46
- const updatedMappings = [...existingMappings, newMapping];
47
- Reflect.defineMetadata(CONFIG_PROPERTY_METADATA, updatedMappings, constructor);
48
- };
49
- }
50
- export function getConfigPropertyMappings(target) {
51
- return Reflect.getMetadata(CONFIG_PROPERTY_METADATA, target) || [];
52
- }
53
- export function hasConfigProperties(target) {
54
- const mappings = getConfigPropertyMappings(target);
55
- return mappings.length > 0;
56
- }
57
- export function clearPropertyNameCache() {
58
- propertyNameCache.clear();
59
- }
@@ -1,5 +0,0 @@
1
- import 'reflect-metadata';
2
- export interface ControllerOptions {
3
- prefix?: string;
4
- }
5
- export declare function Controller(prefixOrOptions?: string | ControllerOptions): ClassDecorator;