@riktajs/core 0.4.4 → 0.4.6

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 (153) hide show
  1. package/dist/core/application.d.ts +0 -29
  2. package/dist/core/application.js +0 -112
  3. package/dist/core/config/abstract-config-provider.d.ts +0 -134
  4. package/dist/core/config/abstract-config-provider.js +0 -124
  5. package/dist/core/config/env-loader.d.ts +0 -24
  6. package/dist/core/config/env-loader.js +0 -30
  7. package/dist/core/config/index.d.ts +0 -1
  8. package/dist/core/config/index.js +0 -1
  9. package/dist/core/constants.d.ts +0 -78
  10. package/dist/core/constants.js +0 -85
  11. package/dist/core/container/container.d.ts +0 -72
  12. package/dist/core/container/container.js +0 -102
  13. package/dist/core/container/index.d.ts +0 -1
  14. package/dist/core/container/index.js +0 -1
  15. package/dist/core/container/injection-token.d.ts +0 -29
  16. package/dist/core/container/injection-token.js +0 -22
  17. package/dist/core/decorators/autowired.decorator.d.ts +0 -62
  18. package/dist/core/decorators/autowired.decorator.js +0 -63
  19. package/dist/core/decorators/config-property.decorator.d.ts +0 -120
  20. package/dist/core/decorators/config-property.decorator.js +0 -140
  21. package/dist/core/decorators/controller.decorator.d.ts +0 -28
  22. package/dist/core/decorators/controller.decorator.js +0 -29
  23. package/dist/core/decorators/index.d.ts +0 -1
  24. package/dist/core/decorators/index.js +0 -2
  25. package/dist/core/decorators/injectable.decorator.d.ts +0 -23
  26. package/dist/core/decorators/injectable.decorator.js +0 -26
  27. package/dist/core/decorators/param.decorator.d.ts +0 -113
  28. package/dist/core/decorators/param.decorator.js +0 -126
  29. package/dist/core/decorators/provider.decorator.d.ts +0 -96
  30. package/dist/core/decorators/provider.decorator.js +0 -113
  31. package/dist/core/decorators/route.decorator.d.ts +0 -44
  32. package/dist/core/decorators/route.decorator.js +0 -51
  33. package/dist/core/discovery.d.ts +0 -33
  34. package/dist/core/discovery.js +4 -74
  35. package/dist/core/exceptions/catch.decorator.d.ts +0 -63
  36. package/dist/core/exceptions/catch.decorator.js +0 -59
  37. package/dist/core/exceptions/config.exceptions.d.ts +0 -12
  38. package/dist/core/exceptions/config.exceptions.js +0 -12
  39. package/dist/core/exceptions/exception-filter.d.ts +0 -96
  40. package/dist/core/exceptions/exception-filter.js +0 -53
  41. package/dist/core/exceptions/exceptions.d.ts +0 -157
  42. package/dist/core/exceptions/exceptions.js +0 -163
  43. package/dist/core/exceptions/http-exception.d.ts +0 -55
  44. package/dist/core/exceptions/http-exception.js +0 -40
  45. package/dist/core/exceptions/index.d.ts +0 -1
  46. package/dist/core/exceptions/index.js +0 -9
  47. package/dist/core/exceptions/validation.exception.d.ts +0 -41
  48. package/dist/core/exceptions/validation.exception.js +0 -34
  49. package/dist/core/guards/can-activate.interface.d.ts +0 -73
  50. package/dist/core/guards/can-activate.interface.js +0 -1
  51. package/dist/core/guards/execution-context.d.ts +0 -51
  52. package/dist/core/guards/execution-context.js +0 -6
  53. package/dist/core/guards/index.d.ts +0 -1
  54. package/dist/core/guards/index.js +0 -2
  55. package/dist/core/guards/use-guards.decorator.d.ts +0 -76
  56. package/dist/core/guards/use-guards.decorator.js +0 -78
  57. package/dist/core/index.d.ts +0 -1
  58. package/dist/core/index.js +0 -5
  59. package/dist/core/lifecycle/event-bus.d.ts +0 -44
  60. package/dist/core/lifecycle/event-bus.js +0 -30
  61. package/dist/core/lifecycle/index.d.ts +0 -1
  62. package/dist/core/lifecycle/index.js +0 -2
  63. package/dist/core/lifecycle/interfaces.d.ts +0 -91
  64. package/dist/core/lifecycle/interfaces.js +0 -8
  65. package/dist/core/lifecycle/on.decorator.d.ts +0 -51
  66. package/dist/core/lifecycle/on.decorator.js +0 -48
  67. package/dist/core/metadata.d.ts +0 -46
  68. package/dist/core/metadata.js +0 -71
  69. package/dist/core/registry.d.ts +0 -77
  70. package/dist/core/registry.js +0 -77
  71. package/dist/core/router/router.d.ts +0 -41
  72. package/dist/core/router/router.js +0 -76
  73. package/dist/core/types.d.ts +0 -109
  74. package/dist/core/types.js +0 -1
  75. package/dist/index.d.ts +0 -42
  76. package/dist/index.js +0 -44
  77. package/package.json +3 -1
  78. package/dist/core/application.d.ts.map +0 -1
  79. package/dist/core/application.js.map +0 -1
  80. package/dist/core/config/abstract-config-provider.d.ts.map +0 -1
  81. package/dist/core/config/abstract-config-provider.js.map +0 -1
  82. package/dist/core/config/env-loader.d.ts.map +0 -1
  83. package/dist/core/config/env-loader.js.map +0 -1
  84. package/dist/core/config/index.d.ts.map +0 -1
  85. package/dist/core/config/index.js.map +0 -1
  86. package/dist/core/constants.d.ts.map +0 -1
  87. package/dist/core/constants.js.map +0 -1
  88. package/dist/core/container/container.d.ts.map +0 -1
  89. package/dist/core/container/container.js.map +0 -1
  90. package/dist/core/container/index.d.ts.map +0 -1
  91. package/dist/core/container/index.js.map +0 -1
  92. package/dist/core/container/injection-token.d.ts.map +0 -1
  93. package/dist/core/container/injection-token.js.map +0 -1
  94. package/dist/core/decorators/autowired.decorator.d.ts.map +0 -1
  95. package/dist/core/decorators/autowired.decorator.js.map +0 -1
  96. package/dist/core/decorators/config-property.decorator.d.ts.map +0 -1
  97. package/dist/core/decorators/config-property.decorator.js.map +0 -1
  98. package/dist/core/decorators/controller.decorator.d.ts.map +0 -1
  99. package/dist/core/decorators/controller.decorator.js.map +0 -1
  100. package/dist/core/decorators/index.d.ts.map +0 -1
  101. package/dist/core/decorators/index.js.map +0 -1
  102. package/dist/core/decorators/injectable.decorator.d.ts.map +0 -1
  103. package/dist/core/decorators/injectable.decorator.js.map +0 -1
  104. package/dist/core/decorators/param.decorator.d.ts.map +0 -1
  105. package/dist/core/decorators/param.decorator.js.map +0 -1
  106. package/dist/core/decorators/provider.decorator.d.ts.map +0 -1
  107. package/dist/core/decorators/provider.decorator.js.map +0 -1
  108. package/dist/core/decorators/route.decorator.d.ts.map +0 -1
  109. package/dist/core/decorators/route.decorator.js.map +0 -1
  110. package/dist/core/discovery.d.ts.map +0 -1
  111. package/dist/core/discovery.js.map +0 -1
  112. package/dist/core/exceptions/catch.decorator.d.ts.map +0 -1
  113. package/dist/core/exceptions/catch.decorator.js.map +0 -1
  114. package/dist/core/exceptions/config.exceptions.d.ts.map +0 -1
  115. package/dist/core/exceptions/config.exceptions.js.map +0 -1
  116. package/dist/core/exceptions/exception-filter.d.ts.map +0 -1
  117. package/dist/core/exceptions/exception-filter.js.map +0 -1
  118. package/dist/core/exceptions/exceptions.d.ts.map +0 -1
  119. package/dist/core/exceptions/exceptions.js.map +0 -1
  120. package/dist/core/exceptions/http-exception.d.ts.map +0 -1
  121. package/dist/core/exceptions/http-exception.js.map +0 -1
  122. package/dist/core/exceptions/index.d.ts.map +0 -1
  123. package/dist/core/exceptions/index.js.map +0 -1
  124. package/dist/core/exceptions/validation.exception.d.ts.map +0 -1
  125. package/dist/core/exceptions/validation.exception.js.map +0 -1
  126. package/dist/core/guards/can-activate.interface.d.ts.map +0 -1
  127. package/dist/core/guards/can-activate.interface.js.map +0 -1
  128. package/dist/core/guards/execution-context.d.ts.map +0 -1
  129. package/dist/core/guards/execution-context.js.map +0 -1
  130. package/dist/core/guards/index.d.ts.map +0 -1
  131. package/dist/core/guards/index.js.map +0 -1
  132. package/dist/core/guards/use-guards.decorator.d.ts.map +0 -1
  133. package/dist/core/guards/use-guards.decorator.js.map +0 -1
  134. package/dist/core/index.d.ts.map +0 -1
  135. package/dist/core/index.js.map +0 -1
  136. package/dist/core/lifecycle/event-bus.d.ts.map +0 -1
  137. package/dist/core/lifecycle/event-bus.js.map +0 -1
  138. package/dist/core/lifecycle/index.d.ts.map +0 -1
  139. package/dist/core/lifecycle/index.js.map +0 -1
  140. package/dist/core/lifecycle/interfaces.d.ts.map +0 -1
  141. package/dist/core/lifecycle/interfaces.js.map +0 -1
  142. package/dist/core/lifecycle/on.decorator.d.ts.map +0 -1
  143. package/dist/core/lifecycle/on.decorator.js.map +0 -1
  144. package/dist/core/metadata.d.ts.map +0 -1
  145. package/dist/core/metadata.js.map +0 -1
  146. package/dist/core/registry.d.ts.map +0 -1
  147. package/dist/core/registry.js.map +0 -1
  148. package/dist/core/router/router.d.ts.map +0 -1
  149. package/dist/core/router/router.js.map +0 -1
  150. package/dist/core/types.d.ts.map +0 -1
  151. package/dist/core/types.js.map +0 -1
  152. package/dist/index.d.ts.map +0 -1
  153. package/dist/index.js.map +0 -1
@@ -1,35 +1,6 @@
1
1
  import 'reflect-metadata';
2
2
  import { RiktaConfig, RiktaApplication } from './types';
3
- /**
4
- * RiktaFactory - Bootstrap the application
5
- *
6
- * Creates and initializes the Rikta application with auto-discovery.
7
- * All classes decorated with @Controller are automatically registered.
8
- *
9
- * .env files are loaded automatically at the start of create(), so environment
10
- * variables are immediately available in your main script.
11
- */
12
3
  export declare class RiktaFactory {
13
- /**
14
- * Create and bootstrap the application
15
- *
16
- * .env files are loaded automatically before any initialization,
17
- * making environment variables available immediately.
18
- *
19
- * @example
20
- * ```typescript
21
- * // Auto-discovery from current directory (default)
22
- * // process.env variables are available immediately after this line
23
- * const app = await Rikta.create({ port: 3000 });
24
- *
25
- * // Auto-discovery from specific paths
26
- * const app = await Rikta.create({
27
- * autowired: ['./src/controllers', './src/services'],
28
- * port: 3000
29
- * });
30
- * ```
31
- */
32
4
  static create(config?: RiktaConfig): Promise<RiktaApplication>;
33
5
  }
34
6
  export { RiktaFactory as Rikta };
35
- //# sourceMappingURL=application.d.ts.map
@@ -15,45 +15,14 @@ const on_decorator_1 = require("./lifecycle/on.decorator");
15
15
  const constants_1 = require("./constants");
16
16
  const exceptions_1 = require("./exceptions");
17
17
  const env_loader_1 = require("./config/env-loader");
18
- /**
19
- * RiktaFactory - Bootstrap the application
20
- *
21
- * Creates and initializes the Rikta application with auto-discovery.
22
- * All classes decorated with @Controller are automatically registered.
23
- *
24
- * .env files are loaded automatically at the start of create(), so environment
25
- * variables are immediately available in your main script.
26
- */
27
18
  class RiktaFactory {
28
- /**
29
- * Create and bootstrap the application
30
- *
31
- * .env files are loaded automatically before any initialization,
32
- * making environment variables available immediately.
33
- *
34
- * @example
35
- * ```typescript
36
- * // Auto-discovery from current directory (default)
37
- * // process.env variables are available immediately after this line
38
- * const app = await Rikta.create({ port: 3000 });
39
- *
40
- * // Auto-discovery from specific paths
41
- * const app = await Rikta.create({
42
- * autowired: ['./src/controllers', './src/services'],
43
- * port: 3000
44
- * });
45
- * ```
46
- */
47
19
  static async create(config = {}) {
48
- // Load .env files FIRST, before anything else
49
- // This ensures environment variables are available immediately
50
20
  (0, env_loader_1.loadEnvFiles)();
51
21
  const silent = config.silent ?? false;
52
22
  if (!silent)
53
23
  console.log('\n🚀 Rikta Framework Starting...\n');
54
24
  const callerDir = (0, discovery_1.getCallerDirectory)();
55
25
  let discoveredFiles = [];
56
- // Auto-discovery: scan for controllers and services
57
26
  if (!config.controllers && config.autowired !== false) {
58
27
  const patterns = Array.isArray(config.autowired) && config.autowired.length > 0
59
28
  ? config.autowired
@@ -67,9 +36,6 @@ class RiktaFactory {
67
36
  }
68
37
  exports.RiktaFactory = RiktaFactory;
69
38
  exports.Rikta = RiktaFactory;
70
- /**
71
- * Internal application implementation
72
- */
73
39
  class RiktaApplicationImpl {
74
40
  server;
75
41
  container;
@@ -86,7 +52,6 @@ class RiktaApplicationImpl {
86
52
  this.config = {
87
53
  port: config.port ?? constants_1.DEFAULT_CONFIG.port,
88
54
  host: config.host ?? constants_1.DEFAULT_CONFIG.host,
89
- // If silent mode, disable logger unless explicitly set
90
55
  logger: config.logger ?? (silent ? false : constants_1.DEFAULT_CONFIG.logger),
91
56
  prefix: config.prefix ?? constants_1.DEFAULT_CONFIG.prefix,
92
57
  silent: silent,
@@ -98,68 +63,46 @@ class RiktaApplicationImpl {
98
63
  this.server = (0, fastify_1.default)({ logger: this.config.logger });
99
64
  this.container = container_1.Container.getInstance();
100
65
  this.router = new router_1.Router(this.server, this.container, this.config.prefix);
101
- // Create and register EventBus
102
66
  this.events = new event_bus_1.EventBus();
103
67
  this.container.registerInstance(event_bus_1.EventBus, this.events);
104
- // Setup global exception handler
105
68
  this.setupExceptionHandler();
106
69
  }
107
- /**
108
- * Setup global exception handler
109
- */
110
70
  setupExceptionHandler() {
111
- // Create global exception filter with config
112
- // When silent mode is enabled, also disable exception logging unless explicitly enabled
113
71
  const silent = this.config.silent ?? false;
114
72
  const globalFilter = new exceptions_1.GlobalExceptionFilter({
115
73
  includeStack: this.config.exceptionFilter?.includeStack,
116
74
  logErrors: this.config.exceptionFilter?.logErrors ?? (silent ? false : true),
117
75
  });
118
- // Register custom exception filters
119
76
  const customFilters = this.config.exceptionFilters ?? [];
120
77
  for (const FilterClass of customFilters) {
121
78
  const metadata = (0, exceptions_1.getCatchMetadata)(FilterClass);
122
79
  const filterInstance = this.container.resolve(FilterClass);
123
80
  if (metadata && metadata.exceptions.length > 0) {
124
- // Register for specific exception types
125
81
  for (const ExceptionType of metadata.exceptions) {
126
82
  this.customExceptionFilters.set(ExceptionType, filterInstance);
127
83
  }
128
84
  }
129
85
  else {
130
- // Catch-all filter (registered as Error)
131
86
  this.customExceptionFilters.set(Error, filterInstance);
132
87
  }
133
88
  }
134
- // Set Fastify error handler
135
89
  this.server.setErrorHandler((0, exceptions_1.createExceptionHandler)(globalFilter, this.customExceptionFilters));
136
90
  }
137
- /**
138
- * Initialize the application
139
- */
140
91
  async init(discoveredFiles = []) {
141
- // Note: .env files are already loaded in create() before this point
142
- // Emit discovery event
143
92
  await this.events.emit('app:discovery', { files: discoveredFiles });
144
- // 1. Process config providers (register them in the container)
145
93
  await this.processConfigProviders();
146
- // 2. Process @Provider classes
147
94
  await this.processCustomProviders();
148
95
  await this.events.emit('app:providers', {
149
96
  count: registry_1.registry.getCustomProviders().length
150
97
  });
151
- // 3. Get and sort providers by priority
152
98
  const providers = this.getSortedProviders();
153
- // 3. Register additional explicit providers
154
99
  const additionalProviders = this.config.providers ?? [];
155
100
  for (const provider of additionalProviders) {
156
101
  this.registerProvider(provider);
157
102
  }
158
- // 4. Initialize all @Injectable providers
159
103
  for (const { target, priority } of providers) {
160
104
  await this.initializeProvider(target, priority);
161
105
  }
162
- // 5. Register routes
163
106
  const controllers = this.config.controllers ?? registry_1.registry.getControllers();
164
107
  if (!this.config.silent)
165
108
  console.log('📡 Registering routes:');
@@ -167,7 +110,6 @@ class RiktaApplicationImpl {
167
110
  this.router.registerController(controller, this.config.silent);
168
111
  }
169
112
  await this.events.emit('app:routes', { count: controllers.length });
170
- // 6. Call onApplicationBootstrap hooks
171
113
  await this.callHook('onApplicationBootstrap');
172
114
  await this.events.emit('app:bootstrap', {
173
115
  providerCount: this.initializedProviders.length
@@ -175,25 +117,17 @@ class RiktaApplicationImpl {
175
117
  if (!this.config.silent)
176
118
  console.log('\n✅ Rikta is ready\n');
177
119
  }
178
- /**
179
- * Process all config providers and register them in the container
180
- */
181
120
  async processConfigProviders() {
182
121
  const configProviders = registry_1.registry.getConfigProviderRegistrations();
183
122
  if (configProviders.length === 0)
184
123
  return;
185
124
  for (const { token, providerClass } of configProviders) {
186
- // Register the config provider class in the container
187
- // This allows it to be resolved via @Autowired(token)
188
125
  this.container.registerProvider({
189
126
  provide: token,
190
127
  useClass: providerClass,
191
128
  });
192
129
  }
193
130
  }
194
- /**
195
- * Process all @Provider decorated classes
196
- */
197
131
  async processCustomProviders() {
198
132
  const customProviders = registry_1.registry.getCustomProviders();
199
133
  if (customProviders.length === 0)
@@ -207,9 +141,6 @@ class RiktaApplicationImpl {
207
141
  this.container.registerValue(metadata.token, value);
208
142
  }
209
143
  }
210
- /**
211
- * Get providers sorted by priority (higher = first)
212
- */
213
144
  getSortedProviders() {
214
145
  const providers = registry_1.registry.getProviders();
215
146
  return providers
@@ -219,33 +150,20 @@ class RiktaApplicationImpl {
219
150
  }))
220
151
  .sort((a, b) => b.priority - a.priority);
221
152
  }
222
- /**
223
- * Get priority from @Injectable() metadata
224
- */
225
153
  getProviderPriority(target) {
226
154
  const options = Reflect.getMetadata(constants_1.INJECTABLE_METADATA, target);
227
155
  return options?.priority ?? 0;
228
156
  }
229
- /**
230
- * Initialize a single provider
231
- */
232
157
  async initializeProvider(target, priority) {
233
158
  const instance = this.container.resolve(target);
234
159
  const name = target.name;
235
- // Track initialized provider
236
160
  this.initializedProviders.push({ instance, priority, name });
237
- // Call onProviderInit hook
238
161
  if (this.hasHook(instance, 'onProviderInit')) {
239
162
  await instance.onProviderInit();
240
163
  }
241
- // Emit provider:init event
242
164
  await this.events.emit('provider:init', { provider: target, name, priority });
243
- // Register @On() event listeners from this instance
244
165
  this.registerEventListeners(target, instance);
245
166
  }
246
- /**
247
- * Register @On() decorated methods as event listeners
248
- */
249
167
  registerEventListeners(target, instance) {
250
168
  const metadata = Reflect.getMetadata(on_decorator_1.ON_EVENT_METADATA, target) ?? [];
251
169
  for (const { event, methodName } of metadata) {
@@ -255,17 +173,11 @@ class RiktaApplicationImpl {
255
173
  }
256
174
  }
257
175
  }
258
- /**
259
- * Register a provider in the container
260
- */
261
176
  registerProvider(provider) {
262
177
  const instance = this.container.resolve(provider);
263
178
  const priority = this.getProviderPriority(provider);
264
179
  this.initializedProviders.push({ instance, priority, name: provider.name });
265
180
  }
266
- /**
267
- * Call a lifecycle hook on all initialized providers
268
- */
269
181
  async callHook(hookName) {
270
182
  for (const { instance } of this.initializedProviders) {
271
183
  if (this.hasHook(instance, hookName)) {
@@ -273,18 +185,12 @@ class RiktaApplicationImpl {
273
185
  }
274
186
  }
275
187
  }
276
- /**
277
- * Check if an instance has a lifecycle hook
278
- */
279
188
  hasHook(instance, hookName) {
280
189
  return (instance !== null &&
281
190
  typeof instance === 'object' &&
282
191
  hookName in instance &&
283
192
  typeof instance[hookName] === 'function');
284
193
  }
285
- /**
286
- * Start listening for requests
287
- */
288
194
  async listen() {
289
195
  if (this.isListening) {
290
196
  return this.address;
@@ -294,56 +200,39 @@ class RiktaApplicationImpl {
294
200
  host: this.config.host,
295
201
  });
296
202
  this.isListening = true;
297
- // Call onApplicationListen hooks
298
203
  for (const { instance } of this.initializedProviders) {
299
204
  if (this.hasHook(instance, 'onApplicationListen')) {
300
205
  await instance.onApplicationListen(this.address);
301
206
  }
302
207
  }
303
- // Emit app:listen event
304
208
  await this.events.emit('app:listen', {
305
209
  address: this.address,
306
210
  port: this.config.port
307
211
  });
308
212
  return this.address;
309
213
  }
310
- /**
311
- * Close the application
312
- */
313
214
  async close(signal) {
314
- // Emit app:shutdown event
315
215
  await this.events.emit('app:shutdown', { signal });
316
- // Call hooks in REVERSE priority order
317
216
  const reversed = [...this.initializedProviders].reverse();
318
217
  for (const { instance } of reversed) {
319
- // Call onApplicationShutdown
320
218
  if (this.hasHook(instance, 'onApplicationShutdown')) {
321
219
  await instance.onApplicationShutdown(signal);
322
220
  }
323
- // Call onProviderDestroy
324
221
  if (this.hasHook(instance, 'onProviderDestroy')) {
325
222
  await instance.onProviderDestroy();
326
223
  }
327
224
  }
328
- // Close Fastify
329
225
  await this.server.close();
330
226
  this.isListening = false;
331
- // Emit app:destroy event
332
227
  await this.events.emit('app:destroy', {
333
228
  uptime: Date.now() - this.startTime
334
229
  });
335
230
  if (!this.config.silent)
336
231
  console.log('\n👋 Application closed\n');
337
232
  }
338
- /**
339
- * Get the server URL
340
- */
341
233
  getUrl() {
342
234
  return this.address;
343
235
  }
344
- /**
345
- * Get the EventBus instance
346
- */
347
236
  getEventBus() {
348
237
  return this.events;
349
238
  }
@@ -351,4 +240,3 @@ class RiktaApplicationImpl {
351
240
  return this.container;
352
241
  }
353
242
  }
354
- //# sourceMappingURL=application.js.map
@@ -1,148 +1,14 @@
1
1
  import { z, ZodSchema } from 'zod';
2
- /**
3
- * Exception thrown when config validation fails
4
- */
5
2
  export declare class ConfigValidationException extends Error {
6
3
  readonly errors: z.ZodError;
7
4
  constructor(errors: z.ZodError, providerName: string);
8
5
  }
9
- /**
10
- * Abstract base class for configuration providers
11
- *
12
- * This class handles:
13
- * - Validating environment variables against a Zod schema
14
- * - Populating decorated properties with validated values
15
- * - Caching validated configuration
16
- *
17
- * Note: .env files are loaded automatically at the start of Rikta.create(),
18
- * so they are available immediately in your main script and before any
19
- * config provider is instantiated.
20
- *
21
- * Child classes must:
22
- * 1. Extend this class
23
- * 2. Implement the abstract schema() method
24
- * 3. Decorate properties with @ConfigProperty()
25
- * 4. Call populate() in their constructor
26
- *
27
- * @example
28
- * ```typescript
29
- * import { AbstractConfigProvider, Provider, ConfigProperty } from '@riktajs/core';
30
- * import { z } from 'zod';
31
- *
32
- * @Provider('APP_CONFIG')
33
- * export class AppConfigProvider extends AbstractConfigProvider {
34
- * schema() {
35
- * return z.object({
36
- * PORT: z.coerce.number().int().min(1).max(65535),
37
- * HOST: z.string().default('localhost'),
38
- * NODE_ENV: z.enum(['development', 'production', 'test']).default('development'),
39
- * });
40
- * }
41
- *
42
- * @ConfigProperty()
43
- * port!: number;
44
- *
45
- * @ConfigProperty()
46
- * host!: string;
47
- *
48
- * @ConfigProperty('NODE_ENV')
49
- * environment!: 'development' | 'production' | 'test';
50
- *
51
- * constructor() {
52
- * super();
53
- * this.populate();
54
- * }
55
- * }
56
- * ```
57
- */
58
6
  export declare abstract class AbstractConfigProvider {
59
- /**
60
- * Cache for validated configuration
61
- * Frozen to prevent accidental mutations
62
- */
63
7
  private _cache?;
64
- /**
65
- * Define the Zod schema for this configuration
66
- *
67
- * @returns A Zod schema that validates the environment variables
68
- *
69
- * @example
70
- * ```typescript
71
- * schema() {
72
- * return z.object({
73
- * DATABASE_URL: z.string().url(),
74
- * DB_POOL_SIZE: z.coerce.number().int().min(1).max(100).default(10),
75
- * DB_TIMEOUT: z.coerce.number().int().default(30000),
76
- * });
77
- * }
78
- * ```
79
- */
80
8
  protected abstract schema(): ZodSchema;
81
- /**
82
- * Constructor ensures .env files are loaded (for standalone usage)
83
- *
84
- * Note: When using Rikta.create(), .env files are loaded automatically
85
- * during bootstrap, so this is a safety measure for standalone usage.
86
- */
87
9
  constructor();
88
- /**
89
- * Validate and cache the configuration
90
- *
91
- * This method runs the Zod schema validation against process.env
92
- * and caches the result. Subsequent calls return the cached value.
93
- *
94
- * @returns The validated and frozen configuration object
95
- * @throws {ConfigValidationException} If validation fails
96
- *
97
- * @private
98
- */
99
10
  private validateAndCache;
100
- /**
101
- * Populate decorated properties with validated values
102
- *
103
- * This method reads the @ConfigProperty metadata and assigns
104
- * the corresponding validated environment values to class properties.
105
- *
106
- * Must be called in the child class constructor after super().
107
- *
108
- * @throws {ConfigValidationException} If validation fails
109
- *
110
- * @example
111
- * ```typescript
112
- * constructor() {
113
- * super();
114
- * this.populate(); // Must call this!
115
- * }
116
- * ```
117
- */
118
11
  protected populate(): void;
119
- /**
120
- * Get the raw validated configuration object
121
- *
122
- * This is useful for accessing config values that aren't mapped
123
- * to properties, or for passing the entire config to other services.
124
- *
125
- * @returns The validated and frozen configuration object
126
- *
127
- * @example
128
- * ```typescript
129
- * const config = this.getConfig();
130
- * console.log('All env vars:', config);
131
- * ```
132
- */
133
12
  protected getConfig(): Readonly<Record<string, unknown>>;
134
- /**
135
- * Get a specific configuration value by key
136
- *
137
- * @param key - The environment variable name
138
- * @returns The validated value, or undefined if not found
139
- *
140
- * @example
141
- * ```typescript
142
- * const port = this.get<number>('PORT');
143
- * const apiKey = this.get<string>('API_KEY');
144
- * ```
145
- */
146
13
  protected get<T = unknown>(key: string): T | undefined;
147
14
  }
148
- //# sourceMappingURL=abstract-config-provider.d.ts.map
@@ -4,9 +4,6 @@ exports.AbstractConfigProvider = exports.ConfigValidationException = void 0;
4
4
  const zod_1 = require("zod");
5
5
  const config_property_decorator_1 = require("../decorators/config-property.decorator");
6
6
  const env_loader_1 = require("./env-loader");
7
- /**
8
- * Exception thrown when config validation fails
9
- */
10
7
  class ConfigValidationException extends Error {
11
8
  errors;
12
9
  constructor(errors, providerName) {
@@ -20,82 +17,11 @@ class ConfigValidationException extends Error {
20
17
  }
21
18
  }
22
19
  exports.ConfigValidationException = ConfigValidationException;
23
- /**
24
- * Abstract base class for configuration providers
25
- *
26
- * This class handles:
27
- * - Validating environment variables against a Zod schema
28
- * - Populating decorated properties with validated values
29
- * - Caching validated configuration
30
- *
31
- * Note: .env files are loaded automatically at the start of Rikta.create(),
32
- * so they are available immediately in your main script and before any
33
- * config provider is instantiated.
34
- *
35
- * Child classes must:
36
- * 1. Extend this class
37
- * 2. Implement the abstract schema() method
38
- * 3. Decorate properties with @ConfigProperty()
39
- * 4. Call populate() in their constructor
40
- *
41
- * @example
42
- * ```typescript
43
- * import { AbstractConfigProvider, Provider, ConfigProperty } from '@riktajs/core';
44
- * import { z } from 'zod';
45
- *
46
- * @Provider('APP_CONFIG')
47
- * export class AppConfigProvider extends AbstractConfigProvider {
48
- * schema() {
49
- * return z.object({
50
- * PORT: z.coerce.number().int().min(1).max(65535),
51
- * HOST: z.string().default('localhost'),
52
- * NODE_ENV: z.enum(['development', 'production', 'test']).default('development'),
53
- * });
54
- * }
55
- *
56
- * @ConfigProperty()
57
- * port!: number;
58
- *
59
- * @ConfigProperty()
60
- * host!: string;
61
- *
62
- * @ConfigProperty('NODE_ENV')
63
- * environment!: 'development' | 'production' | 'test';
64
- *
65
- * constructor() {
66
- * super();
67
- * this.populate();
68
- * }
69
- * }
70
- * ```
71
- */
72
20
  class AbstractConfigProvider {
73
- /**
74
- * Cache for validated configuration
75
- * Frozen to prevent accidental mutations
76
- */
77
21
  _cache;
78
- /**
79
- * Constructor ensures .env files are loaded (for standalone usage)
80
- *
81
- * Note: When using Rikta.create(), .env files are loaded automatically
82
- * during bootstrap, so this is a safety measure for standalone usage.
83
- */
84
22
  constructor() {
85
- // Ensure .env files are loaded (idempotent operation)
86
23
  (0, env_loader_1.loadEnvFiles)();
87
24
  }
88
- /**
89
- * Validate and cache the configuration
90
- *
91
- * This method runs the Zod schema validation against process.env
92
- * and caches the result. Subsequent calls return the cached value.
93
- *
94
- * @returns The validated and frozen configuration object
95
- * @throws {ConfigValidationException} If validation fails
96
- *
97
- * @private
98
- */
99
25
  validateAndCache() {
100
26
  if (this._cache) {
101
27
  return this._cache;
@@ -103,7 +29,6 @@ class AbstractConfigProvider {
103
29
  try {
104
30
  const schema = this.schema();
105
31
  const validated = schema.parse(process.env);
106
- // Freeze the cache to make it immutable (tip from plan)
107
32
  this._cache = Object.freeze(validated);
108
33
  return this._cache;
109
34
  }
@@ -114,69 +39,20 @@ class AbstractConfigProvider {
114
39
  throw error;
115
40
  }
116
41
  }
117
- /**
118
- * Populate decorated properties with validated values
119
- *
120
- * This method reads the @ConfigProperty metadata and assigns
121
- * the corresponding validated environment values to class properties.
122
- *
123
- * Must be called in the child class constructor after super().
124
- *
125
- * @throws {ConfigValidationException} If validation fails
126
- *
127
- * @example
128
- * ```typescript
129
- * constructor() {
130
- * super();
131
- * this.populate(); // Must call this!
132
- * }
133
- * ```
134
- */
135
42
  populate() {
136
- // Validate and get cached config
137
43
  const config = this.validateAndCache();
138
- // Get property mappings from metadata
139
44
  const mappings = (0, config_property_decorator_1.getConfigPropertyMappings)(this.constructor);
140
- // Assign values to properties
141
45
  for (const mapping of mappings) {
142
46
  const value = config[mapping.envKey];
143
- // Use type assertion since we know the property exists
144
47
  this[mapping.propertyKey] = value;
145
48
  }
146
49
  }
147
- /**
148
- * Get the raw validated configuration object
149
- *
150
- * This is useful for accessing config values that aren't mapped
151
- * to properties, or for passing the entire config to other services.
152
- *
153
- * @returns The validated and frozen configuration object
154
- *
155
- * @example
156
- * ```typescript
157
- * const config = this.getConfig();
158
- * console.log('All env vars:', config);
159
- * ```
160
- */
161
50
  getConfig() {
162
51
  return this.validateAndCache();
163
52
  }
164
- /**
165
- * Get a specific configuration value by key
166
- *
167
- * @param key - The environment variable name
168
- * @returns The validated value, or undefined if not found
169
- *
170
- * @example
171
- * ```typescript
172
- * const port = this.get<number>('PORT');
173
- * const apiKey = this.get<string>('API_KEY');
174
- * ```
175
- */
176
53
  get(key) {
177
54
  const config = this.validateAndCache();
178
55
  return config[key];
179
56
  }
180
57
  }
181
58
  exports.AbstractConfigProvider = AbstractConfigProvider;
182
- //# sourceMappingURL=abstract-config-provider.js.map
@@ -1,27 +1,3 @@
1
- /**
2
- * Load .env files with environment-specific precedence
3
- *
4
- * This function is called automatically at the start of Rikta.create(),
5
- * ensuring environment variables are available immediately in your main
6
- * script and before any config providers are initialized.
7
- *
8
- * You can also call this function manually before Rikta.create() if needed.
9
- *
10
- * Loading order (later files override earlier):
11
- * 1. .env (base configuration)
12
- * 2. .env.{NODE_ENV} (environment-specific)
13
- *
14
- * @internal
15
- */
16
1
  export declare function loadEnvFiles(): void;
17
- /**
18
- * Check if .env files have been loaded
19
- * @internal
20
- */
21
2
  export declare function isEnvLoaded(): boolean;
22
- /**
23
- * Reset the env loaded flag (for testing)
24
- * @internal
25
- */
26
3
  export declare function resetEnvLoaded(): void;
27
- //# sourceMappingURL=env-loader.d.ts.map