@riktajs/core 0.8.0 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +132 -54
- package/dist/core/application.d.ts +1 -1
- package/dist/core/application.js +51 -45
- package/dist/core/config/abstract-config-provider.js +8 -13
- package/dist/core/config/env-loader.js +12 -17
- package/dist/core/config/index.d.ts +2 -2
- package/dist/core/config/index.js +2 -18
- package/dist/core/constants.js +18 -21
- package/dist/core/container/abstract-class.utils.d.ts +1 -1
- package/dist/core/container/abstract-class.utils.js +14 -24
- package/dist/core/container/container.d.ts +2 -2
- package/dist/core/container/container.js +47 -19
- package/dist/core/container/implements.decorator.d.ts +1 -1
- package/dist/core/container/implements.decorator.js +17 -23
- package/dist/core/container/index.d.ts +5 -4
- package/dist/core/container/index.js +5 -20
- package/dist/core/container/injection-token.js +1 -5
- package/dist/core/container/request-scope.d.ts +16 -0
- package/dist/core/container/request-scope.js +49 -0
- package/dist/core/decorators/apply-decorators.js +3 -7
- package/dist/core/decorators/autowired.decorator.d.ts +1 -1
- package/dist/core/decorators/autowired.decorator.js +12 -16
- package/dist/core/decorators/config-property.decorator.js +9 -15
- package/dist/core/decorators/controller.decorator.js +9 -12
- package/dist/core/decorators/create-param-decorator.d.ts +1 -1
- package/dist/core/decorators/create-param-decorator.js +7 -12
- package/dist/core/decorators/index.d.ts +9 -9
- package/dist/core/decorators/index.js +9 -25
- package/dist/core/decorators/injectable.decorator.d.ts +1 -1
- package/dist/core/decorators/injectable.decorator.js +8 -11
- package/dist/core/decorators/param.decorator.d.ts +1 -1
- package/dist/core/decorators/param.decorator.js +14 -17
- package/dist/core/decorators/provider.decorator.d.ts +2 -2
- package/dist/core/decorators/provider.decorator.js +12 -17
- package/dist/core/decorators/route.decorator.js +13 -17
- package/dist/core/discovery.d.ts +2 -1
- package/dist/core/discovery.js +44 -64
- package/dist/core/exceptions/catch.decorator.d.ts +1 -1
- package/dist/core/exceptions/catch.decorator.js +6 -11
- package/dist/core/exceptions/config.exceptions.js +3 -9
- package/dist/core/exceptions/discovery.exception.d.ts +18 -0
- package/dist/core/exceptions/discovery.exception.js +39 -0
- package/dist/core/exceptions/exception-filter.js +5 -10
- package/dist/core/exceptions/exceptions.d.ts +1 -1
- package/dist/core/exceptions/exceptions.js +19 -40
- package/dist/core/exceptions/http-exception.js +1 -5
- package/dist/core/exceptions/index.d.ts +7 -6
- package/dist/core/exceptions/index.js +7 -37
- package/dist/core/exceptions/validation.exception.d.ts +1 -1
- package/dist/core/exceptions/validation.exception.js +2 -6
- package/dist/core/guards/can-activate.interface.d.ts +1 -1
- package/dist/core/guards/can-activate.interface.js +1 -2
- package/dist/core/guards/execution-context.d.ts +8 -1
- package/dist/core/guards/execution-context.js +9 -5
- package/dist/core/guards/index.d.ts +3 -3
- package/dist/core/guards/index.js +2 -8
- package/dist/core/guards/use-guards.decorator.d.ts +2 -2
- package/dist/core/guards/use-guards.decorator.js +10 -14
- package/dist/core/index.d.ts +16 -14
- package/dist/core/index.js +17 -33
- package/dist/core/interceptors/index.d.ts +2 -0
- package/dist/core/interceptors/index.js +1 -0
- package/dist/core/interceptors/interceptor.interface.d.ts +7 -0
- package/dist/core/interceptors/interceptor.interface.js +1 -0
- package/dist/core/interceptors/use-interceptors.decorator.d.ts +6 -0
- package/dist/core/interceptors/use-interceptors.decorator.js +19 -0
- package/dist/core/lifecycle/event-bus.d.ts +10 -5
- package/dist/core/lifecycle/event-bus.js +61 -14
- package/dist/core/lifecycle/index.d.ts +3 -3
- package/dist/core/lifecycle/index.js +3 -19
- package/dist/core/lifecycle/interfaces.js +1 -2
- package/dist/core/lifecycle/on.decorator.d.ts +1 -1
- package/dist/core/lifecycle/on.decorator.js +5 -9
- package/dist/core/metadata.d.ts +2 -2
- package/dist/core/metadata.js +26 -43
- package/dist/core/middleware/index.d.ts +3 -3
- package/dist/core/middleware/index.js +2 -9
- package/dist/core/middleware/middleware.decorator.js +2 -5
- package/dist/core/middleware/rikta-middleware.interface.js +1 -2
- package/dist/core/middleware/use-middleware.decorator.d.ts +2 -2
- package/dist/core/middleware/use-middleware.decorator.js +10 -14
- package/dist/core/profiler/index.d.ts +2 -0
- package/dist/core/profiler/index.js +1 -0
- package/dist/core/profiler/performance-profiler.d.ts +42 -0
- package/dist/core/profiler/performance-profiler.js +95 -0
- package/dist/core/registry.d.ts +1 -3
- package/dist/core/registry.js +4 -39
- package/dist/core/router/router.d.ts +12 -2
- package/dist/core/router/router.js +112 -39
- package/dist/core/types.d.ts +8 -4
- package/dist/core/types.js +1 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -20
- package/package.json +8 -1
package/README.md
CHANGED
|
@@ -1,54 +1,132 @@
|
|
|
1
|
-
# @riktajs/core
|
|
2
|
-
|
|
3
|
-
The core package of the Rikta framework.
|
|
4
|
-
|
|
5
|
-
See the [main repository README](../../README.md) for full documentation.
|
|
6
|
-
|
|
7
|
-
## Installation
|
|
8
|
-
|
|
9
|
-
```bash
|
|
10
|
-
npm install @riktajs/core
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
## Quick Start
|
|
14
|
-
|
|
15
|
-
```typescript
|
|
16
|
-
import { Rikta, Controller, Injectable, Get, Autowired } from '@riktajs/core';
|
|
17
|
-
|
|
18
|
-
@Injectable()
|
|
19
|
-
class HelloService {
|
|
20
|
-
getMessage() {
|
|
21
|
-
return { message: 'Hello from Rikta!' };
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
@Controller('/api')
|
|
26
|
-
class HelloController {
|
|
27
|
-
@Autowired()
|
|
28
|
-
private helloService!: HelloService;
|
|
29
|
-
|
|
30
|
-
@Get('/hello')
|
|
31
|
-
getHello() {
|
|
32
|
-
return this.helloService.getMessage();
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const app = await Rikta.create({
|
|
37
|
-
port: 3000,
|
|
38
|
-
autowired: ['./src']
|
|
39
|
-
});
|
|
40
|
-
await app.listen();
|
|
41
|
-
```
|
|
42
|
-
|
|
43
|
-
## Features
|
|
44
|
-
|
|
45
|
-
- 🚀 Zero-Config Autowiring
|
|
46
|
-
- ⚡ Fastify Powered
|
|
47
|
-
- 🛡️ Type-Safe by Default
|
|
48
|
-
- 🔄 Hybrid Lifecycle
|
|
49
|
-
- 📦 Built-in Dependency Injection
|
|
50
|
-
- ✅ Zod Validation Integration
|
|
51
|
-
|
|
52
|
-
##
|
|
53
|
-
|
|
54
|
-
|
|
1
|
+
# @riktajs/core
|
|
2
|
+
|
|
3
|
+
The core package of the Rikta framework.
|
|
4
|
+
|
|
5
|
+
See the [main repository README](../../README.md) for full documentation.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @riktajs/core
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quick Start
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { Rikta, Controller, Injectable, Get, Autowired } from '@riktajs/core';
|
|
17
|
+
|
|
18
|
+
@Injectable()
|
|
19
|
+
class HelloService {
|
|
20
|
+
getMessage() {
|
|
21
|
+
return { message: 'Hello from Rikta!' };
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
@Controller('/api')
|
|
26
|
+
class HelloController {
|
|
27
|
+
@Autowired()
|
|
28
|
+
private helloService!: HelloService;
|
|
29
|
+
|
|
30
|
+
@Get('/hello')
|
|
31
|
+
getHello() {
|
|
32
|
+
return this.helloService.getMessage();
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const app = await Rikta.create({
|
|
37
|
+
port: 3000,
|
|
38
|
+
autowired: ['./src']
|
|
39
|
+
});
|
|
40
|
+
await app.listen();
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Features
|
|
44
|
+
|
|
45
|
+
- 🚀 Zero-Config Autowiring
|
|
46
|
+
- ⚡ Fastify Powered
|
|
47
|
+
- 🛡️ Type-Safe by Default
|
|
48
|
+
- 🔄 Hybrid Lifecycle
|
|
49
|
+
- 📦 Built-in Dependency Injection
|
|
50
|
+
- ✅ Zod Validation Integration
|
|
51
|
+
|
|
52
|
+
## Provider Scopes
|
|
53
|
+
|
|
54
|
+
Rikta supports three provider scopes:
|
|
55
|
+
|
|
56
|
+
```typescript
|
|
57
|
+
// Singleton (default) - shared instance
|
|
58
|
+
@Injectable()
|
|
59
|
+
class ConfigService {}
|
|
60
|
+
|
|
61
|
+
// Transient - new instance each time
|
|
62
|
+
@Injectable({ scope: 'transient' })
|
|
63
|
+
class RequestLogger {}
|
|
64
|
+
|
|
65
|
+
// Request - one instance per HTTP request
|
|
66
|
+
@Injectable({ scope: 'request' })
|
|
67
|
+
class RequestContext {}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Strict Discovery Mode
|
|
71
|
+
|
|
72
|
+
Enable strict discovery to catch import errors early:
|
|
73
|
+
|
|
74
|
+
```typescript
|
|
75
|
+
const app = await Rikta.create({
|
|
76
|
+
strictDiscovery: process.env.NODE_ENV !== 'production',
|
|
77
|
+
onDiscoveryError: (file, error) => {
|
|
78
|
+
console.warn(`Failed to import: ${file}`);
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## EventBus
|
|
84
|
+
|
|
85
|
+
The EventBus provides pub/sub for lifecycle events:
|
|
86
|
+
|
|
87
|
+
```typescript
|
|
88
|
+
import { Injectable, Autowired, EventBus } from '@riktajs/core';
|
|
89
|
+
|
|
90
|
+
@Injectable()
|
|
91
|
+
class MonitorService {
|
|
92
|
+
@Autowired()
|
|
93
|
+
private events!: EventBus;
|
|
94
|
+
|
|
95
|
+
onProviderInit() {
|
|
96
|
+
this.events.on('app:listen', ({ address }) => {
|
|
97
|
+
console.log(`Server at ${address}`);
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## Interceptors
|
|
104
|
+
|
|
105
|
+
Interceptors allow you to bind extra logic before/after method execution:
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
import { Injectable, Interceptor, ExecutionContext, CallHandler, UseInterceptors } from '@riktajs/core';
|
|
109
|
+
|
|
110
|
+
@Injectable()
|
|
111
|
+
class LoggingInterceptor implements Interceptor {
|
|
112
|
+
async intercept(context: ExecutionContext, next: CallHandler): Promise<unknown> {
|
|
113
|
+
const start = Date.now();
|
|
114
|
+
const result = await next.handle();
|
|
115
|
+
console.log(`Request took ${Date.now() - start}ms`);
|
|
116
|
+
return result;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
@Controller('/api')
|
|
121
|
+
@UseInterceptors(LoggingInterceptor)
|
|
122
|
+
class ApiController {
|
|
123
|
+
@Get('/data')
|
|
124
|
+
getData() {
|
|
125
|
+
return { data: 'example' };
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
## License
|
|
131
|
+
|
|
132
|
+
MIT
|
package/dist/core/application.js
CHANGED
|
@@ -1,41 +1,38 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
};
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
const event_bus_1 = require("./lifecycle/event-bus");
|
|
14
|
-
const on_decorator_1 = require("./lifecycle/on.decorator");
|
|
15
|
-
const constants_1 = require("./constants");
|
|
16
|
-
const exceptions_1 = require("./exceptions");
|
|
17
|
-
const env_loader_1 = require("./config/env-loader");
|
|
18
|
-
class RiktaFactory {
|
|
1
|
+
import 'reflect-metadata';
|
|
2
|
+
import Fastify from 'fastify';
|
|
3
|
+
import { Container } from './container/container.js';
|
|
4
|
+
import { Router } from './router/router.js';
|
|
5
|
+
import { registry } from './registry.js';
|
|
6
|
+
import { discoverModules, getCallerDirectory } from './discovery.js';
|
|
7
|
+
import { EventBus } from './lifecycle/event-bus.js';
|
|
8
|
+
import { ON_EVENT_METADATA } from './lifecycle/on.decorator.js';
|
|
9
|
+
import { DEFAULT_CONFIG, PROVIDER_METADATA, INJECTABLE_METADATA } from './constants.js';
|
|
10
|
+
import { GlobalExceptionFilter, createExceptionHandler, getCatchMetadata } from './exceptions/index.js';
|
|
11
|
+
import { loadEnvFiles } from './config/env-loader.js';
|
|
12
|
+
export class RiktaFactory {
|
|
19
13
|
static async create(config = {}) {
|
|
20
|
-
|
|
14
|
+
loadEnvFiles();
|
|
21
15
|
const silent = config.silent ?? false;
|
|
22
16
|
if (!silent)
|
|
23
17
|
console.log('\n🚀 Rikta Framework Starting...\n');
|
|
24
|
-
const callerDir =
|
|
18
|
+
const callerDir = getCallerDirectory();
|
|
25
19
|
let discoveredFiles = [];
|
|
26
20
|
if (!config.controllers && config.autowired !== false) {
|
|
27
21
|
const patterns = Array.isArray(config.autowired) && config.autowired.length > 0
|
|
28
22
|
? config.autowired
|
|
29
23
|
: ['./**'];
|
|
30
|
-
discoveredFiles = await
|
|
24
|
+
discoveredFiles = await discoverModules({
|
|
25
|
+
patterns,
|
|
26
|
+
cwd: callerDir,
|
|
27
|
+
strict: config.strictDiscovery ?? false,
|
|
28
|
+
onImportError: config.onDiscoveryError,
|
|
29
|
+
});
|
|
31
30
|
}
|
|
32
31
|
const app = new RiktaApplicationImpl(config);
|
|
33
32
|
await app.init(discoveredFiles);
|
|
34
33
|
return app;
|
|
35
34
|
}
|
|
36
35
|
}
|
|
37
|
-
exports.RiktaFactory = RiktaFactory;
|
|
38
|
-
exports.Rikta = RiktaFactory;
|
|
39
36
|
class RiktaApplicationImpl {
|
|
40
37
|
server;
|
|
41
38
|
container;
|
|
@@ -50,32 +47,32 @@ class RiktaApplicationImpl {
|
|
|
50
47
|
constructor(config) {
|
|
51
48
|
const silent = config.silent ?? false;
|
|
52
49
|
this.config = {
|
|
53
|
-
port: config.port ??
|
|
54
|
-
host: config.host ??
|
|
55
|
-
logger: config.logger ?? (silent ? false :
|
|
56
|
-
prefix: config.prefix ??
|
|
50
|
+
port: config.port ?? DEFAULT_CONFIG.port,
|
|
51
|
+
host: config.host ?? DEFAULT_CONFIG.host,
|
|
52
|
+
logger: config.logger ?? (silent ? false : DEFAULT_CONFIG.logger),
|
|
53
|
+
prefix: config.prefix ?? DEFAULT_CONFIG.prefix,
|
|
57
54
|
silent: silent,
|
|
58
55
|
controllers: config.controllers,
|
|
59
56
|
providers: config.providers,
|
|
60
57
|
exceptionFilter: config.exceptionFilter,
|
|
61
58
|
exceptionFilters: config.exceptionFilters,
|
|
62
59
|
};
|
|
63
|
-
this.server = (
|
|
64
|
-
this.container =
|
|
65
|
-
this.router = new
|
|
66
|
-
this.events = new
|
|
67
|
-
this.container.registerInstance(
|
|
60
|
+
this.server = Fastify({ logger: this.config.logger });
|
|
61
|
+
this.container = Container.getInstance();
|
|
62
|
+
this.router = new Router(this.server, this.container, this.config.prefix);
|
|
63
|
+
this.events = new EventBus();
|
|
64
|
+
this.container.registerInstance(EventBus, this.events);
|
|
68
65
|
this.setupExceptionHandler();
|
|
69
66
|
}
|
|
70
67
|
setupExceptionHandler() {
|
|
71
68
|
const silent = this.config.silent ?? false;
|
|
72
|
-
const globalFilter = new
|
|
69
|
+
const globalFilter = new GlobalExceptionFilter({
|
|
73
70
|
includeStack: this.config.exceptionFilter?.includeStack,
|
|
74
71
|
logErrors: this.config.exceptionFilter?.logErrors ?? (silent ? false : true),
|
|
75
72
|
});
|
|
76
73
|
const customFilters = this.config.exceptionFilters ?? [];
|
|
77
74
|
for (const FilterClass of customFilters) {
|
|
78
|
-
const metadata =
|
|
75
|
+
const metadata = getCatchMetadata(FilterClass);
|
|
79
76
|
const filterInstance = this.container.resolve(FilterClass);
|
|
80
77
|
if (metadata && metadata.exceptions.length > 0) {
|
|
81
78
|
for (const ExceptionType of metadata.exceptions) {
|
|
@@ -86,14 +83,14 @@ class RiktaApplicationImpl {
|
|
|
86
83
|
this.customExceptionFilters.set(Error, filterInstance);
|
|
87
84
|
}
|
|
88
85
|
}
|
|
89
|
-
this.server.setErrorHandler(
|
|
86
|
+
this.server.setErrorHandler(createExceptionHandler(globalFilter, this.customExceptionFilters));
|
|
90
87
|
}
|
|
91
88
|
async init(discoveredFiles = []) {
|
|
92
89
|
await this.events.emit('app:discovery', { files: discoveredFiles });
|
|
93
90
|
await this.processConfigProviders();
|
|
94
91
|
await this.processCustomProviders();
|
|
95
92
|
await this.events.emit('app:providers', {
|
|
96
|
-
count:
|
|
93
|
+
count: registry.getCustomProviders().length
|
|
97
94
|
});
|
|
98
95
|
const providers = this.getSortedProviders();
|
|
99
96
|
const additionalProviders = this.config.providers ?? [];
|
|
@@ -103,7 +100,7 @@ class RiktaApplicationImpl {
|
|
|
103
100
|
for (const { target, priority } of providers) {
|
|
104
101
|
await this.initializeProvider(target, priority);
|
|
105
102
|
}
|
|
106
|
-
const controllers = this.config.controllers ??
|
|
103
|
+
const controllers = this.config.controllers ?? registry.getControllers();
|
|
107
104
|
if (!this.config.silent)
|
|
108
105
|
console.log('📡 Registering routes:');
|
|
109
106
|
for (const controller of controllers) {
|
|
@@ -118,7 +115,7 @@ class RiktaApplicationImpl {
|
|
|
118
115
|
console.log('\n✅ Rikta is ready\n');
|
|
119
116
|
}
|
|
120
117
|
async processConfigProviders() {
|
|
121
|
-
const configProviders =
|
|
118
|
+
const configProviders = registry.getConfigProviderRegistrations();
|
|
122
119
|
if (configProviders.length === 0)
|
|
123
120
|
return;
|
|
124
121
|
for (const { token, providerClass } of configProviders) {
|
|
@@ -129,11 +126,11 @@ class RiktaApplicationImpl {
|
|
|
129
126
|
}
|
|
130
127
|
}
|
|
131
128
|
async processCustomProviders() {
|
|
132
|
-
const customProviders =
|
|
129
|
+
const customProviders = registry.getCustomProviders();
|
|
133
130
|
if (customProviders.length === 0)
|
|
134
131
|
return;
|
|
135
132
|
for (const ProviderClass of customProviders) {
|
|
136
|
-
const metadata = Reflect.getMetadata(
|
|
133
|
+
const metadata = Reflect.getMetadata(PROVIDER_METADATA, ProviderClass);
|
|
137
134
|
if (!metadata)
|
|
138
135
|
continue;
|
|
139
136
|
const providerInstance = this.container.resolve(ProviderClass);
|
|
@@ -142,7 +139,7 @@ class RiktaApplicationImpl {
|
|
|
142
139
|
}
|
|
143
140
|
}
|
|
144
141
|
getSortedProviders() {
|
|
145
|
-
const providers =
|
|
142
|
+
const providers = registry.getProviders();
|
|
146
143
|
return providers
|
|
147
144
|
.map(target => ({
|
|
148
145
|
target,
|
|
@@ -151,7 +148,7 @@ class RiktaApplicationImpl {
|
|
|
151
148
|
.sort((a, b) => b.priority - a.priority);
|
|
152
149
|
}
|
|
153
150
|
getProviderPriority(target) {
|
|
154
|
-
const options = Reflect.getMetadata(
|
|
151
|
+
const options = Reflect.getMetadata(INJECTABLE_METADATA, target);
|
|
155
152
|
return options?.priority ?? 0;
|
|
156
153
|
}
|
|
157
154
|
async initializeProvider(target, priority) {
|
|
@@ -165,11 +162,11 @@ class RiktaApplicationImpl {
|
|
|
165
162
|
this.registerEventListeners(target, instance);
|
|
166
163
|
}
|
|
167
164
|
registerEventListeners(target, instance) {
|
|
168
|
-
const metadata = Reflect.getMetadata(
|
|
165
|
+
const metadata = Reflect.getMetadata(ON_EVENT_METADATA, target) ?? [];
|
|
169
166
|
for (const { event, methodName } of metadata) {
|
|
170
167
|
const method = instance[methodName];
|
|
171
168
|
if (typeof method === 'function') {
|
|
172
|
-
this.events.on(event, method.bind(instance));
|
|
169
|
+
this.events.on(event, method.bind(instance), target.name);
|
|
173
170
|
}
|
|
174
171
|
}
|
|
175
172
|
}
|
|
@@ -214,19 +211,21 @@ class RiktaApplicationImpl {
|
|
|
214
211
|
async close(signal) {
|
|
215
212
|
await this.events.emit('app:shutdown', { signal });
|
|
216
213
|
const reversed = [...this.initializedProviders].reverse();
|
|
217
|
-
for (const { instance } of reversed) {
|
|
214
|
+
for (const { instance, name } of reversed) {
|
|
218
215
|
if (this.hasHook(instance, 'onApplicationShutdown')) {
|
|
219
216
|
await instance.onApplicationShutdown(signal);
|
|
220
217
|
}
|
|
221
218
|
if (this.hasHook(instance, 'onProviderDestroy')) {
|
|
222
219
|
await instance.onProviderDestroy();
|
|
223
220
|
}
|
|
221
|
+
this.events.removeByOwner(name);
|
|
224
222
|
}
|
|
225
223
|
await this.server.close();
|
|
226
224
|
this.isListening = false;
|
|
227
225
|
await this.events.emit('app:destroy', {
|
|
228
226
|
uptime: Date.now() - this.startTime
|
|
229
227
|
});
|
|
228
|
+
this.events.clear();
|
|
230
229
|
if (!this.config.silent)
|
|
231
230
|
console.log('\n👋 Application closed\n');
|
|
232
231
|
}
|
|
@@ -239,4 +238,11 @@ class RiktaApplicationImpl {
|
|
|
239
238
|
getContainer() {
|
|
240
239
|
return this.container;
|
|
241
240
|
}
|
|
241
|
+
getRouter() {
|
|
242
|
+
return this.router;
|
|
243
|
+
}
|
|
244
|
+
clearRouterCaches() {
|
|
245
|
+
this.router.clearAllCaches();
|
|
246
|
+
}
|
|
242
247
|
}
|
|
248
|
+
export { RiktaFactory as Rikta };
|
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const config_property_decorator_1 = require("../decorators/config-property.decorator");
|
|
6
|
-
const env_loader_1 = require("./env-loader");
|
|
7
|
-
class ConfigValidationException extends Error {
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { getConfigPropertyMappings } from '../decorators/config-property.decorator.js';
|
|
3
|
+
import { loadEnvFiles } from './env-loader.js';
|
|
4
|
+
export class ConfigValidationException extends Error {
|
|
8
5
|
errors;
|
|
9
6
|
constructor(errors, providerName) {
|
|
10
7
|
const errorMessages = errors.issues
|
|
@@ -16,11 +13,10 @@ class ConfigValidationException extends Error {
|
|
|
16
13
|
this.name = 'ConfigValidationException';
|
|
17
14
|
}
|
|
18
15
|
}
|
|
19
|
-
|
|
20
|
-
class AbstractConfigProvider {
|
|
16
|
+
export class AbstractConfigProvider {
|
|
21
17
|
_cache;
|
|
22
18
|
constructor() {
|
|
23
|
-
|
|
19
|
+
loadEnvFiles();
|
|
24
20
|
}
|
|
25
21
|
validateAndCache() {
|
|
26
22
|
if (this._cache) {
|
|
@@ -33,7 +29,7 @@ class AbstractConfigProvider {
|
|
|
33
29
|
return this._cache;
|
|
34
30
|
}
|
|
35
31
|
catch (error) {
|
|
36
|
-
if (error instanceof
|
|
32
|
+
if (error instanceof z.ZodError) {
|
|
37
33
|
throw new ConfigValidationException(error, this.constructor.name);
|
|
38
34
|
}
|
|
39
35
|
throw error;
|
|
@@ -41,7 +37,7 @@ class AbstractConfigProvider {
|
|
|
41
37
|
}
|
|
42
38
|
populate() {
|
|
43
39
|
const config = this.validateAndCache();
|
|
44
|
-
const mappings =
|
|
40
|
+
const mappings = getConfigPropertyMappings(this.constructor);
|
|
45
41
|
for (const mapping of mappings) {
|
|
46
42
|
const value = config[mapping.envKey];
|
|
47
43
|
this[mapping.propertyKey] = value;
|
|
@@ -55,4 +51,3 @@ class AbstractConfigProvider {
|
|
|
55
51
|
return config[key];
|
|
56
52
|
}
|
|
57
53
|
}
|
|
58
|
-
exports.AbstractConfigProvider = AbstractConfigProvider;
|
|
@@ -1,31 +1,26 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
exports.isEnvLoaded = isEnvLoaded;
|
|
5
|
-
exports.resetEnvLoaded = resetEnvLoaded;
|
|
6
|
-
const dotenv_1 = require("dotenv");
|
|
7
|
-
const fs_1 = require("fs");
|
|
8
|
-
const path_1 = require("path");
|
|
1
|
+
import { config as loadEnv } from 'dotenv';
|
|
2
|
+
import { existsSync } from 'fs';
|
|
3
|
+
import { resolve } from 'path';
|
|
9
4
|
let envLoaded = false;
|
|
10
|
-
function loadEnvFiles() {
|
|
5
|
+
export function loadEnvFiles() {
|
|
11
6
|
if (envLoaded) {
|
|
12
7
|
return;
|
|
13
8
|
}
|
|
14
9
|
const env = process.env.NODE_ENV || 'development';
|
|
15
10
|
const cwd = process.cwd();
|
|
16
|
-
const baseEnvPath =
|
|
17
|
-
if (
|
|
18
|
-
(
|
|
11
|
+
const baseEnvPath = resolve(cwd, '.env');
|
|
12
|
+
if (existsSync(baseEnvPath)) {
|
|
13
|
+
loadEnv({ path: baseEnvPath, override: false });
|
|
19
14
|
}
|
|
20
|
-
const envSpecificPath =
|
|
21
|
-
if (
|
|
22
|
-
(
|
|
15
|
+
const envSpecificPath = resolve(cwd, `.env.${env}`);
|
|
16
|
+
if (existsSync(envSpecificPath)) {
|
|
17
|
+
loadEnv({ path: envSpecificPath, override: true });
|
|
23
18
|
}
|
|
24
19
|
envLoaded = true;
|
|
25
20
|
}
|
|
26
|
-
function isEnvLoaded() {
|
|
21
|
+
export function isEnvLoaded() {
|
|
27
22
|
return envLoaded;
|
|
28
23
|
}
|
|
29
|
-
function resetEnvLoaded() {
|
|
24
|
+
export function resetEnvLoaded() {
|
|
30
25
|
envLoaded = false;
|
|
31
26
|
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from './abstract-config-provider';
|
|
2
|
-
export * from './env-loader';
|
|
1
|
+
export * from './abstract-config-provider.js';
|
|
2
|
+
export * from './env-loader.js';
|
|
@@ -1,18 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("./abstract-config-provider"), exports);
|
|
18
|
-
__exportStar(require("./env-loader"), exports);
|
|
1
|
+
export * from './abstract-config-provider.js';
|
|
2
|
+
export * from './env-loader.js';
|
package/dist/core/constants.js
CHANGED
|
@@ -1,22 +1,19 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
exports.CONFIG_PROVIDER_METADATA = Symbol('config:provider:metadata');
|
|
18
|
-
exports.CONFIG_PROPERTY_METADATA = Symbol('config:property:metadata');
|
|
19
|
-
var ParamType;
|
|
1
|
+
export const CONTROLLER_METADATA = Symbol.for('rikta:controller:metadata');
|
|
2
|
+
export const ROUTES_METADATA = Symbol.for('rikta:routes:metadata');
|
|
3
|
+
export const INJECTABLE_METADATA = Symbol.for('rikta:injectable:metadata');
|
|
4
|
+
export const PARAM_METADATA = Symbol.for('rikta:param:metadata');
|
|
5
|
+
export const GUARDS_METADATA = Symbol.for('rikta:guards:metadata');
|
|
6
|
+
export const INTERCEPTORS_METADATA = Symbol.for('rikta:interceptors:metadata');
|
|
7
|
+
export const MIDDLEWARE_METADATA = Symbol.for('rikta:middleware:metadata');
|
|
8
|
+
export const HTTP_CODE_METADATA = Symbol.for('rikta:http:code:metadata');
|
|
9
|
+
export const HEADERS_METADATA = Symbol.for('rikta:headers:metadata');
|
|
10
|
+
export const INJECT_METADATA = Symbol.for('rikta:inject:metadata');
|
|
11
|
+
export const ZOD_SCHEMA_METADATA = Symbol.for('rikta:zod:schema:metadata');
|
|
12
|
+
export const AUTOWIRED_METADATA = Symbol.for('rikta:autowired:metadata');
|
|
13
|
+
export const PROVIDER_METADATA = Symbol.for('rikta:provider:metadata');
|
|
14
|
+
export const CONFIG_PROVIDER_METADATA = Symbol('config:provider:metadata');
|
|
15
|
+
export const CONFIG_PROPERTY_METADATA = Symbol('config:property:metadata');
|
|
16
|
+
export var ParamType;
|
|
20
17
|
(function (ParamType) {
|
|
21
18
|
ParamType["BODY"] = "body";
|
|
22
19
|
ParamType["QUERY"] = "query";
|
|
@@ -25,8 +22,8 @@ var ParamType;
|
|
|
25
22
|
ParamType["REQUEST"] = "request";
|
|
26
23
|
ParamType["REPLY"] = "reply";
|
|
27
24
|
ParamType["CONTEXT"] = "context";
|
|
28
|
-
})(ParamType || (
|
|
29
|
-
|
|
25
|
+
})(ParamType || (ParamType = {}));
|
|
26
|
+
export const DEFAULT_CONFIG = {
|
|
30
27
|
port: 3000,
|
|
31
28
|
host: '0.0.0.0',
|
|
32
29
|
logger: true,
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Constructor } from '../types';
|
|
1
|
+
import { Constructor } from '../types.js';
|
|
2
2
|
export declare const IMPLEMENTS_METADATA: unique symbol;
|
|
3
3
|
export declare const PRIMARY_METADATA: unique symbol;
|
|
4
4
|
export declare function isAbstractClass(target: unknown): target is Constructor;
|
|
@@ -1,27 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
exports.isAbstractClass = isAbstractClass;
|
|
5
|
-
exports.extendsFrom = extendsFrom;
|
|
6
|
-
exports.getImplementedAbstract = getImplementedAbstract;
|
|
7
|
-
exports.isPrimaryImplementation = isPrimaryImplementation;
|
|
8
|
-
exports.setImplementsMetadata = setImplementsMetadata;
|
|
9
|
-
exports.setPrimaryMetadata = setPrimaryMetadata;
|
|
10
|
-
exports.markAsAbstract = markAsAbstract;
|
|
11
|
-
exports.IMPLEMENTS_METADATA = Symbol('rikta:implements');
|
|
12
|
-
exports.PRIMARY_METADATA = Symbol('rikta:primary');
|
|
13
|
-
function isAbstractClass(target) {
|
|
1
|
+
export const IMPLEMENTS_METADATA = Symbol('rikta:implements');
|
|
2
|
+
export const PRIMARY_METADATA = Symbol('rikta:primary');
|
|
3
|
+
export function isAbstractClass(target) {
|
|
14
4
|
if (typeof target !== 'function') {
|
|
15
5
|
return false;
|
|
16
6
|
}
|
|
17
|
-
const isMarkedAbstract = Reflect.getMetadata(
|
|
7
|
+
const isMarkedAbstract = Reflect.getMetadata(IMPLEMENTS_METADATA, target) === undefined
|
|
18
8
|
&& Reflect.hasMetadata('abstract:class', target);
|
|
19
9
|
if (isMarkedAbstract) {
|
|
20
10
|
return true;
|
|
21
11
|
}
|
|
22
12
|
return Reflect.getMetadata('abstract:class', target) === true;
|
|
23
13
|
}
|
|
24
|
-
function extendsFrom(derived, base) {
|
|
14
|
+
export function extendsFrom(derived, base) {
|
|
25
15
|
if (!derived || !base) {
|
|
26
16
|
return false;
|
|
27
17
|
}
|
|
@@ -37,18 +27,18 @@ function extendsFrom(derived, base) {
|
|
|
37
27
|
}
|
|
38
28
|
return false;
|
|
39
29
|
}
|
|
40
|
-
function getImplementedAbstract(target) {
|
|
41
|
-
return Reflect.getMetadata(
|
|
30
|
+
export function getImplementedAbstract(target) {
|
|
31
|
+
return Reflect.getMetadata(IMPLEMENTS_METADATA, target);
|
|
42
32
|
}
|
|
43
|
-
function isPrimaryImplementation(target) {
|
|
44
|
-
return Reflect.getMetadata(
|
|
33
|
+
export function isPrimaryImplementation(target) {
|
|
34
|
+
return Reflect.getMetadata(PRIMARY_METADATA, target) === true;
|
|
45
35
|
}
|
|
46
|
-
function setImplementsMetadata(target, abstractClass) {
|
|
47
|
-
Reflect.defineMetadata(
|
|
36
|
+
export function setImplementsMetadata(target, abstractClass) {
|
|
37
|
+
Reflect.defineMetadata(IMPLEMENTS_METADATA, abstractClass, target);
|
|
48
38
|
}
|
|
49
|
-
function setPrimaryMetadata(target) {
|
|
50
|
-
Reflect.defineMetadata(
|
|
39
|
+
export function setPrimaryMetadata(target) {
|
|
40
|
+
Reflect.defineMetadata(PRIMARY_METADATA, true, target);
|
|
51
41
|
}
|
|
52
|
-
function markAsAbstract(target) {
|
|
42
|
+
export function markAsAbstract(target) {
|
|
53
43
|
Reflect.defineMetadata('abstract:class', true, target);
|
|
54
44
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import 'reflect-metadata';
|
|
2
|
-
import { Constructor, InjectableOptions } from '../types';
|
|
3
|
-
import { Token, ProviderDefinition } from './injection-token';
|
|
2
|
+
import { Constructor, InjectableOptions } from '../types.js';
|
|
3
|
+
import { Token, ProviderDefinition } from './injection-token.js';
|
|
4
4
|
export declare class Container {
|
|
5
5
|
private static instance;
|
|
6
6
|
private singletons;
|