@xrystal/core 3.19.5 → 3.19.8
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,5 +1,5 @@
|
|
|
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();
|
|
@@ -15,18 +15,17 @@ class Controller {
|
|
|
15
15
|
get req() {
|
|
16
16
|
const store = this.currentStore;
|
|
17
17
|
const identityT = (k) => k;
|
|
18
|
-
|
|
19
|
-
return { url: '', method: '', headers: {}, params: {}, query: {}, lang: 'en', t: identityT };
|
|
18
|
+
const ctx = store?.ctx || {};
|
|
20
19
|
return {
|
|
21
|
-
url:
|
|
22
|
-
method:
|
|
23
|
-
headers:
|
|
24
|
-
body:
|
|
25
|
-
query:
|
|
26
|
-
params:
|
|
27
|
-
lang:
|
|
28
|
-
t:
|
|
29
|
-
accounts:
|
|
20
|
+
url: ctx.url || '',
|
|
21
|
+
method: ctx.method || '',
|
|
22
|
+
headers: ctx.headers || {},
|
|
23
|
+
body: ctx.body || {},
|
|
24
|
+
query: ctx.query || {},
|
|
25
|
+
params: ctx.params || {},
|
|
26
|
+
lang: ctx.lang || 'en',
|
|
27
|
+
t: ctx.t || identityT,
|
|
28
|
+
accounts: ctx.accounts || store?.req?.accounts
|
|
30
29
|
};
|
|
31
30
|
}
|
|
32
31
|
get res() {
|
|
@@ -81,10 +80,16 @@ export class ControllerService extends Controller {
|
|
|
81
80
|
try {
|
|
82
81
|
const currentReq = this.req;
|
|
83
82
|
const currentRes = this.res;
|
|
84
|
-
const
|
|
85
|
-
|
|
83
|
+
const p = {
|
|
84
|
+
req: currentReq,
|
|
85
|
+
res: currentRes,
|
|
86
|
+
body: currentReq.body,
|
|
87
|
+
query: currentReq.query,
|
|
88
|
+
params: currentReq.params,
|
|
89
|
+
t: currentReq.t
|
|
90
|
+
};
|
|
86
91
|
if (checks) {
|
|
87
|
-
const checkResult = await checks(
|
|
92
|
+
const checkResult = await checks(p);
|
|
88
93
|
if (checkResult === false || (checkResult && typeof checkResult === 'object' && !Array.isArray(checkResult))) {
|
|
89
94
|
return currentRes.status(checkResult?.code || 400).send(new ResponseSchema({
|
|
90
95
|
status: false,
|
|
@@ -93,7 +98,7 @@ export class ControllerService extends Controller {
|
|
|
93
98
|
}).getResponse);
|
|
94
99
|
}
|
|
95
100
|
}
|
|
96
|
-
const logicResult = logic ? await logic(
|
|
101
|
+
const logicResult = logic ? await logic(p) : null;
|
|
97
102
|
if (logicResult?.status === false) {
|
|
98
103
|
return currentRes.status(logicResult.code || 400).send(new ResponseSchema({
|
|
99
104
|
status: false,
|
|
@@ -103,24 +108,29 @@ export class ControllerService extends Controller {
|
|
|
103
108
|
}
|
|
104
109
|
if (logicResult instanceof Response)
|
|
105
110
|
return logicResult;
|
|
106
|
-
const resResult = response ? await response({
|
|
111
|
+
const resResult = response ? await response({ ...p, logicResult }) : logicResult;
|
|
107
112
|
let finalMessage = '';
|
|
113
|
+
let rawMessageValue = '';
|
|
108
114
|
const messageSource = resResult?.message || (typeof resResult === 'string' || Array.isArray(resResult) ? resResult : '');
|
|
109
115
|
if (Array.isArray(messageSource)) {
|
|
110
116
|
finalMessage = responseMessageHelper.successFully(messageSource[0], messageSource[1], currentReq.t);
|
|
117
|
+
rawMessageValue = messageSource[0];
|
|
111
118
|
}
|
|
112
119
|
else if (typeof messageSource === 'string' && messageSource !== '') {
|
|
113
120
|
if (messageSource.startsWith('@')) {
|
|
114
|
-
|
|
121
|
+
const cleanMsg = messageSource.substring(1);
|
|
122
|
+
finalMessage = currentReq.t(cleanMsg, {});
|
|
123
|
+
rawMessageValue = cleanMsg;
|
|
115
124
|
}
|
|
116
125
|
else {
|
|
117
|
-
finalMessage = currentReq.t
|
|
126
|
+
finalMessage = responseMessageHelper.successFully(messageSource, '', currentReq.t);
|
|
127
|
+
rawMessageValue = messageSource;
|
|
118
128
|
}
|
|
119
129
|
}
|
|
120
130
|
let finalPayload = undefined;
|
|
121
131
|
if (logic) {
|
|
122
132
|
finalPayload = logicResult?.payload !== undefined ? logicResult.payload : logicResult;
|
|
123
|
-
if (finalPayload === messageSource || (typeof finalPayload === 'object' && finalPayload
|
|
133
|
+
if (finalPayload === messageSource || (finalPayload && typeof finalPayload === 'object' && (finalPayload.message === rawMessageValue || finalPayload.message === finalMessage))) {
|
|
124
134
|
finalPayload = undefined;
|
|
125
135
|
}
|
|
126
136
|
}
|
|
@@ -132,8 +142,8 @@ export class ControllerService extends Controller {
|
|
|
132
142
|
}
|
|
133
143
|
catch (error) {
|
|
134
144
|
this.logger.winston.log({
|
|
135
|
-
level:
|
|
136
|
-
message: `Controller Error: ${error}`
|
|
145
|
+
level: 'error',
|
|
146
|
+
message: `Controller Error: ${error.message}`
|
|
137
147
|
});
|
|
138
148
|
return this.res.status(500).send(new ResponseSchema({
|
|
139
149
|
status: false,
|