@xrystal/core 3.19.7 → 3.20.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/package.json
CHANGED
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
|
-
import {
|
|
2
|
+
import { ProtocolEnum, responseMessageHelper, ResponseSchema, x } from '../../utils/index';
|
|
3
3
|
import LoggerService from '../logger';
|
|
4
4
|
export const controllerContextStorage = new AsyncLocalStorage();
|
|
5
5
|
export const getControllerCtx = () => controllerContextStorage.getStore();
|
|
6
|
+
export const protocol = async (callback, protocol = ProtocolEnum.HTTP) => {
|
|
7
|
+
const store = controllerContextStorage.getStore();
|
|
8
|
+
if (store) {
|
|
9
|
+
store.protocol = protocol;
|
|
10
|
+
}
|
|
11
|
+
return await callback();
|
|
12
|
+
};
|
|
6
13
|
class Controller {
|
|
7
14
|
logger = x.get(LoggerService);
|
|
8
15
|
supportedProtocols = [ProtocolEnum.HTTP, ProtocolEnum.WEBSOCKET];
|
|
@@ -15,18 +22,17 @@ class Controller {
|
|
|
15
22
|
get req() {
|
|
16
23
|
const store = this.currentStore;
|
|
17
24
|
const identityT = (k) => k;
|
|
18
|
-
|
|
19
|
-
return { url: '', method: '', headers: {}, params: {}, query: {}, lang: 'en', t: identityT };
|
|
25
|
+
const ctx = store?.ctx || {};
|
|
20
26
|
return {
|
|
21
|
-
url:
|
|
22
|
-
method:
|
|
23
|
-
headers:
|
|
24
|
-
body:
|
|
25
|
-
query:
|
|
26
|
-
params:
|
|
27
|
-
lang:
|
|
28
|
-
t:
|
|
29
|
-
accounts:
|
|
27
|
+
url: ctx.url || '',
|
|
28
|
+
method: ctx.method || '',
|
|
29
|
+
headers: ctx.headers || {},
|
|
30
|
+
body: ctx.body || {},
|
|
31
|
+
query: ctx.query || {},
|
|
32
|
+
params: ctx.params || {},
|
|
33
|
+
lang: ctx.lang || 'en',
|
|
34
|
+
t: ctx.t || identityT,
|
|
35
|
+
accounts: ctx.accounts || store?.req?.accounts
|
|
30
36
|
};
|
|
31
37
|
}
|
|
32
38
|
get res() {
|
|
@@ -81,10 +87,16 @@ export class ControllerService extends Controller {
|
|
|
81
87
|
try {
|
|
82
88
|
const currentReq = this.req;
|
|
83
89
|
const currentRes = this.res;
|
|
84
|
-
const
|
|
85
|
-
|
|
90
|
+
const p = {
|
|
91
|
+
req: currentReq,
|
|
92
|
+
res: currentRes,
|
|
93
|
+
body: currentReq.body,
|
|
94
|
+
query: currentReq.query,
|
|
95
|
+
params: currentReq.params,
|
|
96
|
+
t: currentReq.t
|
|
97
|
+
};
|
|
86
98
|
if (checks) {
|
|
87
|
-
const checkResult = await checks(
|
|
99
|
+
const checkResult = await checks(p);
|
|
88
100
|
if (checkResult === false || (checkResult && typeof checkResult === 'object' && !Array.isArray(checkResult))) {
|
|
89
101
|
return currentRes.status(checkResult?.code || 400).send(new ResponseSchema({
|
|
90
102
|
status: false,
|
|
@@ -93,7 +105,7 @@ export class ControllerService extends Controller {
|
|
|
93
105
|
}).getResponse);
|
|
94
106
|
}
|
|
95
107
|
}
|
|
96
|
-
const logicResult = logic ? await logic(
|
|
108
|
+
const logicResult = logic ? await logic(p) : null;
|
|
97
109
|
if (logicResult?.status === false) {
|
|
98
110
|
return currentRes.status(logicResult.code || 400).send(new ResponseSchema({
|
|
99
111
|
status: false,
|
|
@@ -103,7 +115,7 @@ export class ControllerService extends Controller {
|
|
|
103
115
|
}
|
|
104
116
|
if (logicResult instanceof Response)
|
|
105
117
|
return logicResult;
|
|
106
|
-
const resResult = response ? await response({
|
|
118
|
+
const resResult = response ? await response({ ...p, logicResult }) : logicResult;
|
|
107
119
|
let finalMessage = '';
|
|
108
120
|
let rawMessageValue = '';
|
|
109
121
|
const messageSource = resResult?.message || (typeof resResult === 'string' || Array.isArray(resResult) ? resResult : '');
|
|
@@ -125,8 +137,7 @@ export class ControllerService extends Controller {
|
|
|
125
137
|
let finalPayload = undefined;
|
|
126
138
|
if (logic) {
|
|
127
139
|
finalPayload = logicResult?.payload !== undefined ? logicResult.payload : logicResult;
|
|
128
|
-
|
|
129
|
-
if (finalPayload === messageSource || (finalPayload && typeof finalPayload === 'object' && finalPayload.message === rawMessageValue)) {
|
|
140
|
+
if (finalPayload === messageSource || (finalPayload && typeof finalPayload === 'object' && (finalPayload.message === rawMessageValue || finalPayload.message === finalMessage))) {
|
|
130
141
|
finalPayload = undefined;
|
|
131
142
|
}
|
|
132
143
|
}
|
|
@@ -138,8 +149,8 @@ export class ControllerService extends Controller {
|
|
|
138
149
|
}
|
|
139
150
|
catch (error) {
|
|
140
151
|
this.logger.winston.log({
|
|
141
|
-
level:
|
|
142
|
-
message: `Controller Error: ${error}`
|
|
152
|
+
level: 'error',
|
|
153
|
+
message: `Controller Error: ${error.message}`
|
|
143
154
|
});
|
|
144
155
|
return this.res.status(500).send(new ResponseSchema({
|
|
145
156
|
status: false,
|