@xrystal/core 3.19.7 → 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,7 +108,7 @@ 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 = '';
|
|
108
113
|
let rawMessageValue = '';
|
|
109
114
|
const messageSource = resResult?.message || (typeof resResult === 'string' || Array.isArray(resResult) ? resResult : '');
|
|
@@ -125,8 +130,7 @@ export class ControllerService extends Controller {
|
|
|
125
130
|
let finalPayload = undefined;
|
|
126
131
|
if (logic) {
|
|
127
132
|
finalPayload = logicResult?.payload !== undefined ? logicResult.payload : logicResult;
|
|
128
|
-
|
|
129
|
-
if (finalPayload === messageSource || (finalPayload && typeof finalPayload === 'object' && finalPayload.message === rawMessageValue)) {
|
|
133
|
+
if (finalPayload === messageSource || (finalPayload && typeof finalPayload === 'object' && (finalPayload.message === rawMessageValue || finalPayload.message === finalMessage))) {
|
|
130
134
|
finalPayload = undefined;
|
|
131
135
|
}
|
|
132
136
|
}
|
|
@@ -138,8 +142,8 @@ export class ControllerService extends Controller {
|
|
|
138
142
|
}
|
|
139
143
|
catch (error) {
|
|
140
144
|
this.logger.winston.log({
|
|
141
|
-
level:
|
|
142
|
-
message: `Controller Error: ${error}`
|
|
145
|
+
level: 'error',
|
|
146
|
+
message: `Controller Error: ${error.message}`
|
|
143
147
|
});
|
|
144
148
|
return this.res.status(500).send(new ResponseSchema({
|
|
145
149
|
status: false,
|