@xrystal/core 3.14.2 → 3.14.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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();
|
|
@@ -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';
|