@xrystal/core 3.14.2 → 3.14.4
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/package.json +1 -1
- package/source/loader/controller/index.d.ts +5 -9
- package/source/loader/controller/index.js +29 -69
- package/source/project/index.js +5 -8
- package/source/utils/models/classes/class.x.d.ts +3 -5
- package/source/utils/models/classes/class.x.js +32 -33
- package/source/utils/models/index.d.ts +0 -1
- package/source/utils/models/index.js +0 -1
package/package.json
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
2
|
import { ProtocolEnum } from '../../utils/index';
|
|
3
3
|
export declare const controllerContextStorage: AsyncLocalStorage<{
|
|
4
|
-
protocol: ProtocolEnum;
|
|
5
4
|
ctx?: any;
|
|
6
5
|
req?: any;
|
|
7
6
|
res?: any;
|
|
8
7
|
}>;
|
|
9
8
|
export declare const getControllerCtx: () => {
|
|
10
|
-
protocol: ProtocolEnum;
|
|
11
9
|
ctx?: any;
|
|
12
10
|
req?: any;
|
|
13
11
|
res?: any;
|
|
@@ -32,26 +30,24 @@ declare abstract class Controller {
|
|
|
32
30
|
protected loggerService: any;
|
|
33
31
|
constructor({ loggerService }: any);
|
|
34
32
|
protected get currentStore(): {
|
|
35
|
-
protocol: ProtocolEnum;
|
|
36
33
|
ctx?: any;
|
|
37
34
|
req?: any;
|
|
38
35
|
res?: any;
|
|
39
36
|
};
|
|
40
|
-
protected get protocol(): ProtocolEnum;
|
|
41
37
|
protected get req(): CustomRequest;
|
|
42
38
|
protected get res(): CustomResponse;
|
|
43
39
|
protected responseProtocolSwitch: ({ res, resStatus, context, req }: any) => Promise<any>;
|
|
44
40
|
protected parsedQuerys: (url: string) => Record<string, any>;
|
|
45
41
|
}
|
|
46
42
|
export declare abstract class ControllerService extends Controller {
|
|
47
|
-
protected controllerType:
|
|
48
|
-
load(
|
|
49
|
-
type?:
|
|
43
|
+
protected controllerType: ProtocolEnum;
|
|
44
|
+
load(props?: {
|
|
45
|
+
type?: ProtocolEnum;
|
|
50
46
|
}): Promise<void>;
|
|
51
|
-
schema({ checks, logic, response
|
|
47
|
+
schema({ checks, logic, response }: {
|
|
52
48
|
checks?: (args: any) => Promise<any>;
|
|
53
49
|
logic: (args: any) => Promise<any>;
|
|
54
50
|
response?: (args: any) => Promise<any>;
|
|
55
51
|
}): Promise<any>;
|
|
56
52
|
}
|
|
57
|
-
export {
|
|
53
|
+
export {};
|
|
@@ -11,13 +11,10 @@ class Controller {
|
|
|
11
11
|
get currentStore() {
|
|
12
12
|
return getControllerCtx();
|
|
13
13
|
}
|
|
14
|
-
get protocol() {
|
|
15
|
-
return this.currentStore?.protocol || ProtocolEnum.HTTP;
|
|
16
|
-
}
|
|
17
14
|
get req() {
|
|
18
15
|
const store = this.currentStore;
|
|
19
16
|
if (!store)
|
|
20
|
-
return {
|
|
17
|
+
return {};
|
|
21
18
|
if (store.ctx) {
|
|
22
19
|
const { ctx } = store;
|
|
23
20
|
return {
|
|
@@ -31,24 +28,23 @@ class Controller {
|
|
|
31
28
|
t: ctx.t
|
|
32
29
|
};
|
|
33
30
|
}
|
|
34
|
-
const { req } = store;
|
|
35
31
|
return {
|
|
36
|
-
url: req?.originalUrl || req?.url || '',
|
|
37
|
-
method: req?.method || 'GET',
|
|
38
|
-
headers: req?.headers || {},
|
|
39
|
-
body: req?.body,
|
|
40
|
-
params: req?.params || {},
|
|
41
|
-
query: req?.query || {},
|
|
42
|
-
accounts: req?.accounts,
|
|
43
|
-
t: req?.t
|
|
32
|
+
url: store.req?.originalUrl || store.req?.url || '',
|
|
33
|
+
method: store.req?.method || 'GET',
|
|
34
|
+
headers: store.req?.headers || {},
|
|
35
|
+
body: store.req?.body,
|
|
36
|
+
params: store.req?.params || {},
|
|
37
|
+
query: store.req?.query || {},
|
|
38
|
+
accounts: store.req?.accounts,
|
|
39
|
+
t: store.req?.t
|
|
44
40
|
};
|
|
45
41
|
}
|
|
46
42
|
get res() {
|
|
47
43
|
const store = this.currentStore;
|
|
48
|
-
const protocol = this.protocol;
|
|
49
44
|
if (!store)
|
|
50
|
-
return {
|
|
45
|
+
return {};
|
|
51
46
|
if (store.ctx) {
|
|
47
|
+
const protocol = this.controllerType || ProtocolEnum.HTTP;
|
|
52
48
|
return {
|
|
53
49
|
locals: {},
|
|
54
50
|
status(code) {
|
|
@@ -63,27 +59,24 @@ class Controller {
|
|
|
63
59
|
headers: { 'content-type': 'application/json' }
|
|
64
60
|
});
|
|
65
61
|
},
|
|
66
|
-
json(data) {
|
|
67
|
-
return this.send(data);
|
|
68
|
-
}
|
|
62
|
+
json(data) { return this.send(data); }
|
|
69
63
|
};
|
|
70
64
|
}
|
|
71
|
-
const { res } = store;
|
|
72
65
|
return {
|
|
73
|
-
locals: res?.locals || {},
|
|
66
|
+
locals: store.res?.locals || {},
|
|
74
67
|
status(code) {
|
|
75
|
-
if (res?.status)
|
|
76
|
-
res.status(code);
|
|
68
|
+
if (store.res?.status)
|
|
69
|
+
store.res.status(code);
|
|
77
70
|
return this;
|
|
78
71
|
},
|
|
79
72
|
send(data) {
|
|
80
|
-
if (res?.send)
|
|
81
|
-
return res.send(data);
|
|
73
|
+
if (store.res?.send)
|
|
74
|
+
return store.res.send(data);
|
|
82
75
|
return data;
|
|
83
76
|
},
|
|
84
77
|
json(data) {
|
|
85
|
-
if (res?.json)
|
|
86
|
-
return res.json(data);
|
|
78
|
+
if (store.res?.json)
|
|
79
|
+
return store.res.json(data);
|
|
87
80
|
return data;
|
|
88
81
|
}
|
|
89
82
|
};
|
|
@@ -98,74 +91,42 @@ class Controller {
|
|
|
98
91
|
};
|
|
99
92
|
}
|
|
100
93
|
export class ControllerService extends Controller {
|
|
101
|
-
controllerType =
|
|
102
|
-
async load(
|
|
103
|
-
if (type)
|
|
104
|
-
this.controllerType = type;
|
|
94
|
+
controllerType = ProtocolEnum.HTTP;
|
|
95
|
+
async load(props = {}) {
|
|
96
|
+
if (props.type)
|
|
97
|
+
this.controllerType = props.type;
|
|
105
98
|
}
|
|
106
|
-
async schema({ checks, logic, response
|
|
99
|
+
async schema({ checks, logic, response }) {
|
|
107
100
|
const currentReq = this.req;
|
|
108
101
|
const currentRes = this.res;
|
|
109
102
|
if (!currentReq || !currentRes)
|
|
110
103
|
return;
|
|
111
104
|
const payload = { req: currentReq, res: currentRes };
|
|
112
|
-
const convertedPayload = {
|
|
113
|
-
...payload,
|
|
114
|
-
parsedQuerys: this.parsedQuerys(currentReq.url)
|
|
115
|
-
};
|
|
105
|
+
const convertedPayload = { ...payload, parsedQuerys: this.parsedQuerys(currentReq.url) };
|
|
116
106
|
try {
|
|
117
107
|
if (checks) {
|
|
118
108
|
const checkResult = await checks({ payload, convertedPayload });
|
|
119
|
-
|
|
120
|
-
if (Array.isArray(checkResult)) {
|
|
121
|
-
isInvalid = !checkResult.every(c => c !== undefined && c !== null && c !== "");
|
|
122
|
-
}
|
|
123
|
-
else if (typeof checkResult === 'object' && checkResult.message) {
|
|
124
|
-
return await this.responseProtocolSwitch({
|
|
125
|
-
req: currentReq,
|
|
126
|
-
res: currentRes,
|
|
127
|
-
context: () => new ResponseSchema({
|
|
128
|
-
status: checkResult.status || false,
|
|
129
|
-
message: checkResult.message,
|
|
130
|
-
payload: checkResult.payload,
|
|
131
|
-
code: checkResult.code
|
|
132
|
-
}).getResponse
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
|
-
if (isInvalid) {
|
|
109
|
+
if (checkResult?.message) {
|
|
136
110
|
return await this.responseProtocolSwitch({
|
|
137
111
|
req: currentReq,
|
|
138
112
|
res: currentRes,
|
|
139
|
-
|
|
140
|
-
context: ({ localeLanguageConverter }) => new ResponseSchema({
|
|
141
|
-
status: false,
|
|
142
|
-
message: responseMessageHelper.schemaMissingDataChecks(localeLanguageConverter)
|
|
143
|
-
}).getResponse
|
|
113
|
+
context: () => new ResponseSchema(checkResult).getResponse
|
|
144
114
|
});
|
|
145
115
|
}
|
|
146
116
|
}
|
|
147
117
|
const logicResult = await logic({ payload, convertedPayload });
|
|
148
|
-
if (logicResult.response instanceof Function)
|
|
118
|
+
if (logicResult.response instanceof Function)
|
|
149
119
|
return logicResult.response(logicResult.payload);
|
|
150
|
-
}
|
|
151
120
|
if (logicResult.message) {
|
|
152
121
|
return await this.responseProtocolSwitch({
|
|
153
122
|
req: currentReq,
|
|
154
123
|
res: currentRes,
|
|
155
124
|
resStatus: 400,
|
|
156
|
-
context: () => new ResponseSchema(
|
|
157
|
-
status: logicResult.status || false,
|
|
158
|
-
message: logicResult.message,
|
|
159
|
-
payload: logicResult.payload,
|
|
160
|
-
code: logicResult.code
|
|
161
|
-
}).getResponse
|
|
125
|
+
context: () => new ResponseSchema(logicResult).getResponse
|
|
162
126
|
});
|
|
163
127
|
}
|
|
164
128
|
if (response) {
|
|
165
129
|
const resResult = await response({ payload, convertedPayload });
|
|
166
|
-
if (resResult.response instanceof Function) {
|
|
167
|
-
return resResult.response(resResult);
|
|
168
|
-
}
|
|
169
130
|
return await this.responseProtocolSwitch({
|
|
170
131
|
req: currentReq,
|
|
171
132
|
res: currentRes,
|
|
@@ -183,4 +144,3 @@ export class ControllerService extends Controller {
|
|
|
183
144
|
}
|
|
184
145
|
}
|
|
185
146
|
}
|
|
186
|
-
export { Controller };
|
package/source/project/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// => import dependencies
|
|
2
2
|
import path from 'path';
|
|
3
|
-
import { SystemService, ConfigsService, LoggerService, EventsService, LocalizationsService, ClientsService } from '../loader/index';
|
|
4
|
-
import { packageName, x, kafkaBrokers, systemLoggerLayer, getTmp,
|
|
3
|
+
import { SystemService, ConfigsService, LoggerService, EventsService, LocalizationsService, ClientsService, ControllerService } from '../loader/index';
|
|
4
|
+
import { packageName, x, kafkaBrokers, systemLoggerLayer, getTmp, ProtocolEnum, } from '../utils/index';
|
|
5
5
|
//
|
|
6
6
|
let coreHasRun = false;
|
|
7
7
|
export const core = getTmp();
|
|
@@ -16,14 +16,11 @@ const coreLoader = async ({}) => {
|
|
|
16
16
|
try {
|
|
17
17
|
const { configs } = core._;
|
|
18
18
|
const rootFolderPath = configs.rootFolderPath;
|
|
19
|
-
x
|
|
20
|
-
.load([
|
|
19
|
+
await (await x.load([
|
|
21
20
|
path.join(__dirname, '..', 'loader', '**/*.{ts,js}'),
|
|
22
21
|
], {
|
|
23
|
-
exclude: [
|
|
24
|
-
|
|
25
|
-
]
|
|
26
|
-
})
|
|
22
|
+
exclude: [path.join(__dirname, '..', 'utils', '**/class.x.{ts,js}')]
|
|
23
|
+
}))
|
|
27
24
|
.initialize([
|
|
28
25
|
{
|
|
29
26
|
service: SystemService,
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { LifetimeType } from 'awilix';
|
|
2
2
|
export declare class X {
|
|
3
3
|
private container;
|
|
4
4
|
private initializedNames;
|
|
@@ -6,9 +6,9 @@ export declare class X {
|
|
|
6
6
|
private getSource;
|
|
7
7
|
load(patterns: string | string[], options?: {
|
|
8
8
|
verbose?: boolean;
|
|
9
|
-
exclude?:
|
|
9
|
+
exclude?: (string | Function)[];
|
|
10
10
|
lifetime?: LifetimeType;
|
|
11
|
-
}): this
|
|
11
|
+
}): Promise<this>;
|
|
12
12
|
register(Dependency: any, lifetime?: LifetimeType): this;
|
|
13
13
|
registerAll(dependencies: any[], lifetime?: LifetimeType): this;
|
|
14
14
|
registerInstance(name: string, instance: any): this;
|
|
@@ -20,10 +20,8 @@ export declare class X {
|
|
|
20
20
|
props?: any;
|
|
21
21
|
}[], verbose?: boolean): Promise<this>;
|
|
22
22
|
get<T>(target: string | any): T;
|
|
23
|
-
createScope(): AwilixContainer<any>;
|
|
24
23
|
get cradle(): any;
|
|
25
24
|
private isRegistered;
|
|
26
|
-
private getSourceByInstance;
|
|
27
25
|
}
|
|
28
26
|
declare const _default: X;
|
|
29
27
|
export default _default;
|
|
@@ -14,7 +14,8 @@ export class X {
|
|
|
14
14
|
const normalizedPath = filePath.replace(/\\/g, '/');
|
|
15
15
|
return normalizedPath.includes('node_modules') || !normalizedPath.startsWith(projectRoot) ? 'LIB' : 'APP';
|
|
16
16
|
}
|
|
17
|
-
|
|
17
|
+
// 1. ASYNC YAPTIK (await import desteği için)
|
|
18
|
+
async load(patterns, options = {}) {
|
|
18
19
|
const { verbose = false, exclude = [], lifetime = Lifetime.SINGLETON } = options;
|
|
19
20
|
const cwd = process.cwd();
|
|
20
21
|
const excludeList = Array.isArray(exclude) ? exclude : [exclude];
|
|
@@ -32,26 +33,33 @@ export class X {
|
|
|
32
33
|
}
|
|
33
34
|
for (const m of modules) {
|
|
34
35
|
const source = this.getSource(m.path);
|
|
35
|
-
|
|
36
|
+
const normalizedMPath = m.path.replace(/\\/g, '/');
|
|
37
|
+
if (normalizedMPath === __filename.replace(/\\/g, '/') || normalizedMPath.endsWith('.d.ts'))
|
|
36
38
|
continue;
|
|
39
|
+
// --- KRİTİK DÜZELTME 1: DOSYAYI YÜKLEMEDEN ÖNCE EXCLUDE KONTROLÜ ---
|
|
40
|
+
const isPathExcluded = excludeList.some(ex => {
|
|
41
|
+
if (typeof ex === 'string') {
|
|
42
|
+
const normalizedEx = ex.replace(/\\/g, '/');
|
|
43
|
+
return normalizedMPath.includes(normalizedEx) || m.name === ex;
|
|
44
|
+
}
|
|
45
|
+
return false;
|
|
46
|
+
});
|
|
47
|
+
if (isPathExcluded) {
|
|
48
|
+
if (verbose)
|
|
49
|
+
console.log(`[DI][${source}] Pre-load Excluded: ${m.name}`);
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
37
52
|
try {
|
|
38
|
-
|
|
53
|
+
// --- KRİTİK DÜZELTME 2: REQUIRE YERİNE IMPORT (Async Module Hatası İçin) ---
|
|
54
|
+
const loaded = await import(m.path);
|
|
39
55
|
let dependency = loaded.default;
|
|
40
56
|
if (!dependency) {
|
|
41
57
|
dependency = Object.values(loaded).find(val => typeof val === 'function' && !!val.prototype && !!val.name);
|
|
42
58
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
if (typeof ex === 'function')
|
|
47
|
-
return dependency === ex;
|
|
48
|
-
return false;
|
|
49
|
-
});
|
|
50
|
-
if (isExcluded) {
|
|
51
|
-
if (verbose)
|
|
52
|
-
console.log(`[DI][${source}] Excluded: ${m.name}`);
|
|
59
|
+
// Class referansı üzerinden exclude (Eğer class yüklendiyse)
|
|
60
|
+
const isClassExcluded = excludeList.some(ex => typeof ex === 'function' && dependency === ex);
|
|
61
|
+
if (isClassExcluded)
|
|
53
62
|
continue;
|
|
54
|
-
}
|
|
55
63
|
const isClass = typeof dependency === 'function' && !!dependency.prototype && !!dependency.name;
|
|
56
64
|
if (isClass) {
|
|
57
65
|
const className = dependency.name;
|
|
@@ -61,25 +69,26 @@ export class X {
|
|
|
61
69
|
[name]: asClass(dependency).setLifetime(lifetime)
|
|
62
70
|
});
|
|
63
71
|
if (verbose)
|
|
64
|
-
console.log(`[DI][${source}] Registered
|
|
72
|
+
console.log(`[DI][${source}] Registered: ${name}`);
|
|
65
73
|
}
|
|
66
74
|
}
|
|
67
75
|
}
|
|
68
76
|
catch (err) {
|
|
69
|
-
|
|
77
|
+
// Burada hata alıyorsan dosya bozuktur veya import edilemiyordur
|
|
78
|
+
if (verbose)
|
|
79
|
+
console.error(`[DI][${source}] Load Error in ${m.name}:`, err.message);
|
|
70
80
|
}
|
|
71
81
|
}
|
|
72
82
|
return this;
|
|
73
83
|
}
|
|
84
|
+
// ... (register, initialize, get metodları aynı kalıyor)
|
|
74
85
|
register(Dependency, lifetime = Lifetime.SINGLETON) {
|
|
75
86
|
if (!Dependency?.name)
|
|
76
87
|
return this;
|
|
77
88
|
const name = Dependency.name.charAt(0).toLowerCase() + Dependency.name.slice(1);
|
|
78
89
|
if (this.isRegistered(name))
|
|
79
90
|
return this;
|
|
80
|
-
this.container.register({
|
|
81
|
-
[name]: asClass(Dependency).setLifetime(lifetime)
|
|
82
|
-
});
|
|
91
|
+
this.container.register({ [name]: asClass(Dependency).setLifetime(lifetime) });
|
|
83
92
|
return this;
|
|
84
93
|
}
|
|
85
94
|
registerAll(dependencies, lifetime = Lifetime.SINGLETON) {
|
|
@@ -117,16 +126,15 @@ export class X {
|
|
|
117
126
|
continue;
|
|
118
127
|
const instance = cradle[key];
|
|
119
128
|
if (instance && typeof instance.load === 'function') {
|
|
120
|
-
const source = this.getSourceByInstance(instance);
|
|
121
129
|
try {
|
|
122
130
|
const props = propsMap.get(key) || {};
|
|
123
131
|
await instance.load(props);
|
|
124
132
|
this.initializedNames.add(key);
|
|
125
133
|
if (verbose)
|
|
126
|
-
console.log(`[DI]
|
|
134
|
+
console.log(`[DI] Initialized: ${key}`);
|
|
127
135
|
}
|
|
128
136
|
catch (err) {
|
|
129
|
-
console.error(`[DI]
|
|
137
|
+
console.error(`[DI] Initialization Failed: ${key} ->`, err.message);
|
|
130
138
|
}
|
|
131
139
|
}
|
|
132
140
|
}
|
|
@@ -134,28 +142,19 @@ export class X {
|
|
|
134
142
|
}
|
|
135
143
|
get(target) {
|
|
136
144
|
try {
|
|
137
|
-
const resolveName = typeof target === 'function'
|
|
138
|
-
? target.name.charAt(0).toLowerCase() + target.name.slice(1)
|
|
139
|
-
: target;
|
|
145
|
+
const resolveName = typeof target === 'function' ? target.name.charAt(0).toLowerCase() + target.name.slice(1) : target;
|
|
140
146
|
return this.container.resolve(resolveName);
|
|
141
147
|
}
|
|
142
148
|
catch (err) {
|
|
143
149
|
if (err.message.includes('Cyclic dependencies')) {
|
|
144
|
-
console.error(
|
|
145
|
-
console.error(`🔍 Resolution Path: ${err.resolutionStack}`);
|
|
150
|
+
console.error(`\n❌ [DI][CRITICAL] Cyclic dependency detected!\n🔍 Path: ${err.resolutionStack}`);
|
|
146
151
|
}
|
|
147
152
|
throw err;
|
|
148
153
|
}
|
|
149
154
|
}
|
|
150
|
-
createScope() {
|
|
151
|
-
return this.container.createScope();
|
|
152
|
-
}
|
|
153
155
|
get cradle() { return this.container.cradle; }
|
|
154
156
|
isRegistered(name) {
|
|
155
157
|
return !!this.container.registrations[name] && this.container.registrations[name].resolve !== undefined;
|
|
156
158
|
}
|
|
157
|
-
getSourceByInstance(instance) {
|
|
158
|
-
return instance.constructor?.name?.includes('Service') || instance.constructor?.name?.includes('Controller') ? 'APP' : 'LIB';
|
|
159
|
-
}
|
|
160
159
|
}
|
|
161
160
|
export default new X();
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import x, { X } from './classes/class.x';
|
|
2
2
|
import locator, { Locator } from './classes/class.service-locator';
|
|
3
3
|
export * from './classes/class.tmp-file-loader';
|
|
4
|
-
export * from '../../loader/controller';
|
|
5
4
|
export * from './classes/class.response';
|
|
6
5
|
export * from './classes/class.services';
|
|
7
6
|
export * from './classes/class.interfaces';
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import x, { X } from './classes/class.x';
|
|
2
2
|
import locator, { Locator } from './classes/class.service-locator';
|
|
3
3
|
export * from './classes/class.tmp-file-loader';
|
|
4
|
-
export * from '../../loader/controller';
|
|
5
4
|
export * from './classes/class.response';
|
|
6
5
|
export * from './classes/class.services';
|
|
7
6
|
export * from './classes/class.interfaces';
|