@riktajs/core 0.9.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 -132
- package/dist/core/application.d.ts +1 -1
- package/dist/core/application.js +36 -43
- 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 +16 -20
- 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 -5
- package/dist/core/container/index.js +5 -21
- package/dist/core/container/injection-token.js +1 -5
- package/dist/core/container/request-scope.d.ts +1 -1
- package/dist/core/container/request-scope.js +4 -8
- 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 +1 -1
- package/dist/core/discovery.js +25 -57
- 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.js +1 -5
- 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 -7
- package/dist/core/exceptions/index.js +7 -39
- 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 +1 -1
- package/dist/core/guards/execution-context.js +1 -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 -16
- package/dist/core/index.js +17 -35
- package/dist/core/interceptors/index.d.ts +2 -2
- package/dist/core/interceptors/index.js +1 -6
- package/dist/core/interceptors/interceptor.interface.d.ts +1 -1
- package/dist/core/interceptors/interceptor.interface.js +1 -2
- package/dist/core/interceptors/use-interceptors.decorator.d.ts +2 -2
- package/dist/core/interceptors/use-interceptors.decorator.js +10 -14
- package/dist/core/lifecycle/event-bus.d.ts +1 -1
- package/dist/core/lifecycle/event-bus.js +4 -7
- 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 -2
- package/dist/core/profiler/index.js +1 -8
- package/dist/core/profiler/performance-profiler.d.ts +1 -1
- package/dist/core/profiler/performance-profiler.js +6 -12
- package/dist/core/registry.d.ts +1 -1
- package/dist/core/registry.js +4 -7
- package/dist/core/router/router.d.ts +2 -2
- package/dist/core/router/router.js +31 -35
- package/dist/core/types.d.ts +3 -3
- 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,132 +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
|
-
## 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
|
|
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,33 +1,27 @@
|
|
|
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({
|
|
31
25
|
patterns,
|
|
32
26
|
cwd: callerDir,
|
|
33
27
|
strict: config.strictDiscovery ?? false,
|
|
@@ -39,8 +33,6 @@ class RiktaFactory {
|
|
|
39
33
|
return app;
|
|
40
34
|
}
|
|
41
35
|
}
|
|
42
|
-
exports.RiktaFactory = RiktaFactory;
|
|
43
|
-
exports.Rikta = RiktaFactory;
|
|
44
36
|
class RiktaApplicationImpl {
|
|
45
37
|
server;
|
|
46
38
|
container;
|
|
@@ -55,32 +47,32 @@ class RiktaApplicationImpl {
|
|
|
55
47
|
constructor(config) {
|
|
56
48
|
const silent = config.silent ?? false;
|
|
57
49
|
this.config = {
|
|
58
|
-
port: config.port ??
|
|
59
|
-
host: config.host ??
|
|
60
|
-
logger: config.logger ?? (silent ? false :
|
|
61
|
-
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,
|
|
62
54
|
silent: silent,
|
|
63
55
|
controllers: config.controllers,
|
|
64
56
|
providers: config.providers,
|
|
65
57
|
exceptionFilter: config.exceptionFilter,
|
|
66
58
|
exceptionFilters: config.exceptionFilters,
|
|
67
59
|
};
|
|
68
|
-
this.server = (
|
|
69
|
-
this.container =
|
|
70
|
-
this.router = new
|
|
71
|
-
this.events = new
|
|
72
|
-
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);
|
|
73
65
|
this.setupExceptionHandler();
|
|
74
66
|
}
|
|
75
67
|
setupExceptionHandler() {
|
|
76
68
|
const silent = this.config.silent ?? false;
|
|
77
|
-
const globalFilter = new
|
|
69
|
+
const globalFilter = new GlobalExceptionFilter({
|
|
78
70
|
includeStack: this.config.exceptionFilter?.includeStack,
|
|
79
71
|
logErrors: this.config.exceptionFilter?.logErrors ?? (silent ? false : true),
|
|
80
72
|
});
|
|
81
73
|
const customFilters = this.config.exceptionFilters ?? [];
|
|
82
74
|
for (const FilterClass of customFilters) {
|
|
83
|
-
const metadata =
|
|
75
|
+
const metadata = getCatchMetadata(FilterClass);
|
|
84
76
|
const filterInstance = this.container.resolve(FilterClass);
|
|
85
77
|
if (metadata && metadata.exceptions.length > 0) {
|
|
86
78
|
for (const ExceptionType of metadata.exceptions) {
|
|
@@ -91,14 +83,14 @@ class RiktaApplicationImpl {
|
|
|
91
83
|
this.customExceptionFilters.set(Error, filterInstance);
|
|
92
84
|
}
|
|
93
85
|
}
|
|
94
|
-
this.server.setErrorHandler(
|
|
86
|
+
this.server.setErrorHandler(createExceptionHandler(globalFilter, this.customExceptionFilters));
|
|
95
87
|
}
|
|
96
88
|
async init(discoveredFiles = []) {
|
|
97
89
|
await this.events.emit('app:discovery', { files: discoveredFiles });
|
|
98
90
|
await this.processConfigProviders();
|
|
99
91
|
await this.processCustomProviders();
|
|
100
92
|
await this.events.emit('app:providers', {
|
|
101
|
-
count:
|
|
93
|
+
count: registry.getCustomProviders().length
|
|
102
94
|
});
|
|
103
95
|
const providers = this.getSortedProviders();
|
|
104
96
|
const additionalProviders = this.config.providers ?? [];
|
|
@@ -108,7 +100,7 @@ class RiktaApplicationImpl {
|
|
|
108
100
|
for (const { target, priority } of providers) {
|
|
109
101
|
await this.initializeProvider(target, priority);
|
|
110
102
|
}
|
|
111
|
-
const controllers = this.config.controllers ??
|
|
103
|
+
const controllers = this.config.controllers ?? registry.getControllers();
|
|
112
104
|
if (!this.config.silent)
|
|
113
105
|
console.log('📡 Registering routes:');
|
|
114
106
|
for (const controller of controllers) {
|
|
@@ -123,7 +115,7 @@ class RiktaApplicationImpl {
|
|
|
123
115
|
console.log('\n✅ Rikta is ready\n');
|
|
124
116
|
}
|
|
125
117
|
async processConfigProviders() {
|
|
126
|
-
const configProviders =
|
|
118
|
+
const configProviders = registry.getConfigProviderRegistrations();
|
|
127
119
|
if (configProviders.length === 0)
|
|
128
120
|
return;
|
|
129
121
|
for (const { token, providerClass } of configProviders) {
|
|
@@ -134,11 +126,11 @@ class RiktaApplicationImpl {
|
|
|
134
126
|
}
|
|
135
127
|
}
|
|
136
128
|
async processCustomProviders() {
|
|
137
|
-
const customProviders =
|
|
129
|
+
const customProviders = registry.getCustomProviders();
|
|
138
130
|
if (customProviders.length === 0)
|
|
139
131
|
return;
|
|
140
132
|
for (const ProviderClass of customProviders) {
|
|
141
|
-
const metadata = Reflect.getMetadata(
|
|
133
|
+
const metadata = Reflect.getMetadata(PROVIDER_METADATA, ProviderClass);
|
|
142
134
|
if (!metadata)
|
|
143
135
|
continue;
|
|
144
136
|
const providerInstance = this.container.resolve(ProviderClass);
|
|
@@ -147,7 +139,7 @@ class RiktaApplicationImpl {
|
|
|
147
139
|
}
|
|
148
140
|
}
|
|
149
141
|
getSortedProviders() {
|
|
150
|
-
const providers =
|
|
142
|
+
const providers = registry.getProviders();
|
|
151
143
|
return providers
|
|
152
144
|
.map(target => ({
|
|
153
145
|
target,
|
|
@@ -156,7 +148,7 @@ class RiktaApplicationImpl {
|
|
|
156
148
|
.sort((a, b) => b.priority - a.priority);
|
|
157
149
|
}
|
|
158
150
|
getProviderPriority(target) {
|
|
159
|
-
const options = Reflect.getMetadata(
|
|
151
|
+
const options = Reflect.getMetadata(INJECTABLE_METADATA, target);
|
|
160
152
|
return options?.priority ?? 0;
|
|
161
153
|
}
|
|
162
154
|
async initializeProvider(target, priority) {
|
|
@@ -170,7 +162,7 @@ class RiktaApplicationImpl {
|
|
|
170
162
|
this.registerEventListeners(target, instance);
|
|
171
163
|
}
|
|
172
164
|
registerEventListeners(target, instance) {
|
|
173
|
-
const metadata = Reflect.getMetadata(
|
|
165
|
+
const metadata = Reflect.getMetadata(ON_EVENT_METADATA, target) ?? [];
|
|
174
166
|
for (const { event, methodName } of metadata) {
|
|
175
167
|
const method = instance[methodName];
|
|
176
168
|
if (typeof method === 'function') {
|
|
@@ -253,3 +245,4 @@ class RiktaApplicationImpl {
|
|
|
253
245
|
this.router.clearAllCaches();
|
|
254
246
|
}
|
|
255
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;
|