@xrystal/core 3.23.3 → 3.23.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
CHANGED
|
@@ -49,6 +49,7 @@ export declare abstract class BaseController {
|
|
|
49
49
|
export default abstract class Controller extends BaseController implements IProvide<any> {
|
|
50
50
|
constructor();
|
|
51
51
|
onInit(): Promise<void>;
|
|
52
|
+
private parseMessage;
|
|
52
53
|
schema({ checks, logic, response }: {
|
|
53
54
|
checks?: (args: any) => Promise<any>;
|
|
54
55
|
logic?: (args: any) => Promise<any>;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import qs from 'qs';
|
|
1
2
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
3
|
import Logger from '../logger';
|
|
3
4
|
import System from '../system';
|
|
@@ -16,18 +17,23 @@ export class BaseController {
|
|
|
16
17
|
}
|
|
17
18
|
get req() {
|
|
18
19
|
const store = this.currentStore;
|
|
19
|
-
const identityT = (k) => k;
|
|
20
20
|
const ctx = store?.ctx || {};
|
|
21
|
+
const req = store?.req || {};
|
|
22
|
+
const identityT = (k) => k;
|
|
23
|
+
const body = ctx.body || req.body || {};
|
|
24
|
+
const query = ctx.query || req.query || (typeof req.url === 'string' && req.url.includes('?') ? qs.parse(req.url.split('?')[1]) : {});
|
|
25
|
+
const params = ctx.params || req.params || {};
|
|
26
|
+
const headers = ctx.headers || req.headers || {};
|
|
21
27
|
return {
|
|
22
|
-
url: ctx.url || '',
|
|
23
|
-
method: ctx.method || '',
|
|
24
|
-
headers
|
|
25
|
-
body
|
|
26
|
-
query
|
|
27
|
-
params
|
|
28
|
-
lang: ctx.lang || 'en',
|
|
29
|
-
t: ctx.t || identityT,
|
|
30
|
-
accounts: ctx.accounts ||
|
|
28
|
+
url: ctx.url || req.url || '',
|
|
29
|
+
method: ctx.method || req.method || '',
|
|
30
|
+
headers,
|
|
31
|
+
body,
|
|
32
|
+
query,
|
|
33
|
+
params,
|
|
34
|
+
lang: ctx.lang || req.lang || 'en',
|
|
35
|
+
t: ctx.t || req.t || identityT,
|
|
36
|
+
accounts: ctx.accounts || req.accounts
|
|
31
37
|
};
|
|
32
38
|
}
|
|
33
39
|
get res() {
|
|
@@ -54,7 +60,7 @@ export class BaseController {
|
|
|
54
60
|
return data;
|
|
55
61
|
if (data instanceof Response)
|
|
56
62
|
return data;
|
|
57
|
-
const isBinary = data instanceof Blob || data instanceof Uint8Array || data instanceof ArrayBuffer;
|
|
63
|
+
const isBinary = data instanceof Blob || data instanceof Uint8Array || data instanceof ArrayBuffer || (typeof data?.pipe === 'function');
|
|
58
64
|
const isRaw = store.metadata?._isRaw || isBinary;
|
|
59
65
|
let contentType = store.metadata?._contentType;
|
|
60
66
|
if (!contentType) {
|
|
@@ -81,6 +87,28 @@ export default class Controller extends BaseController {
|
|
|
81
87
|
const protocols = this.system?.tmp?.configs?.loaders?.controller?.protocols;
|
|
82
88
|
this.supportedProtocols = Array.isArray(protocols) ? protocols : [protocols || ProtocolEnum.HTTP];
|
|
83
89
|
}
|
|
90
|
+
parseMessage(msg, t, isError = false) {
|
|
91
|
+
if (!msg)
|
|
92
|
+
return '';
|
|
93
|
+
if (typeof msg !== 'string' && !Array.isArray(msg))
|
|
94
|
+
return '';
|
|
95
|
+
let cleanMsg = msg;
|
|
96
|
+
let useDirect = isError;
|
|
97
|
+
if (typeof cleanMsg === 'string' && cleanMsg.startsWith('@')) {
|
|
98
|
+
cleanMsg = cleanMsg.substring(1);
|
|
99
|
+
useDirect = true;
|
|
100
|
+
}
|
|
101
|
+
const parts = Array.isArray(cleanMsg) ? cleanMsg : cleanMsg.split(' ');
|
|
102
|
+
const key = parts[0];
|
|
103
|
+
const rawParams = parts.slice(1);
|
|
104
|
+
const translatedParams = rawParams.map((p) => t(p));
|
|
105
|
+
if (useDirect) {
|
|
106
|
+
return t(key, translatedParams);
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
return responseMessageHelper.successFully(key, translatedParams.join(' '), t);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
84
112
|
async schema({ checks, logic, response }) {
|
|
85
113
|
try {
|
|
86
114
|
const currentReq = this.req;
|
|
@@ -91,64 +119,77 @@ export default class Controller extends BaseController {
|
|
|
91
119
|
body: currentReq.body,
|
|
92
120
|
query: currentReq.query,
|
|
93
121
|
params: currentReq.params,
|
|
94
|
-
t: currentReq.t
|
|
122
|
+
t: currentReq.t,
|
|
123
|
+
accounts: currentReq.accounts
|
|
95
124
|
};
|
|
96
125
|
if (checks) {
|
|
97
126
|
const checkResult = await checks(p);
|
|
98
|
-
if (checkResult
|
|
127
|
+
if (checkResult !== true) {
|
|
99
128
|
const checkCode = checkResult?.code || 400;
|
|
129
|
+
const rawMsg = checkResult?.message || checkResult || 'error_validation';
|
|
130
|
+
const finalMsg = this.parseMessage(rawMsg, currentReq.t, true);
|
|
100
131
|
return currentRes.status(checkCode).send(new ResponseSchema({
|
|
101
132
|
status: false,
|
|
102
|
-
message:
|
|
133
|
+
message: finalMsg,
|
|
103
134
|
code: checkCode
|
|
104
135
|
}).getResponse);
|
|
105
136
|
}
|
|
106
137
|
}
|
|
107
|
-
let logicResult = logic ? await logic(p) :
|
|
138
|
+
let logicResult = logic ? await logic(p) : {};
|
|
139
|
+
if (logicResult instanceof Response || logicResult?.constructor?.name === 'Response') {
|
|
140
|
+
return logicResult;
|
|
141
|
+
}
|
|
108
142
|
if (logicResult && typeof logicResult === 'object' && typeof logicResult.response === 'function') {
|
|
109
143
|
logicResult = await logicResult.response();
|
|
110
144
|
}
|
|
111
|
-
if (logicResult instanceof Response || (logicResult?.constructor?.name === 'Response')) {
|
|
112
|
-
return logicResult;
|
|
113
|
-
}
|
|
114
145
|
if (logicResult?.status === false) {
|
|
115
146
|
const errorCode = logicResult.code || 400;
|
|
147
|
+
const finalMsg = this.parseMessage(logicResult.message, currentReq.t, true);
|
|
116
148
|
return currentRes.status(errorCode).send(new ResponseSchema({
|
|
117
149
|
status: false,
|
|
118
|
-
message:
|
|
150
|
+
message: finalMsg,
|
|
119
151
|
code: errorCode
|
|
120
152
|
}).getResponse);
|
|
121
153
|
}
|
|
122
154
|
const resResult = response ? await response({ ...p, logicResult }) : logicResult;
|
|
123
|
-
if (resResult instanceof Response ||
|
|
124
|
-
return resResult;
|
|
155
|
+
if (resResult instanceof Response || resResult instanceof Blob || resResult instanceof Uint8Array || resResult instanceof ArrayBuffer) {
|
|
156
|
+
return currentRes.status(200).send(resResult);
|
|
125
157
|
}
|
|
126
|
-
let
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
158
|
+
let source = resResult;
|
|
159
|
+
if (source === undefined || source === null)
|
|
160
|
+
source = logicResult || {};
|
|
161
|
+
let messageSource = source?.message;
|
|
162
|
+
if (!messageSource) {
|
|
163
|
+
if (typeof source === 'string')
|
|
164
|
+
messageSource = source;
|
|
165
|
+
else if (Array.isArray(source))
|
|
166
|
+
messageSource = source;
|
|
167
|
+
else
|
|
168
|
+
messageSource = 'success_process';
|
|
132
169
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
170
|
+
if (messageSource === source) {
|
|
171
|
+
source = {};
|
|
172
|
+
}
|
|
173
|
+
let dataVal = source.data;
|
|
174
|
+
let extraDataVal = source.extraData;
|
|
175
|
+
if (source.payload) {
|
|
176
|
+
if (source.payload.data !== undefined)
|
|
177
|
+
dataVal = source.payload.data;
|
|
178
|
+
else if (!source.payload.extraData && !source.data)
|
|
179
|
+
dataVal = source.payload;
|
|
180
|
+
if (source.payload.extraData !== undefined)
|
|
181
|
+
extraDataVal = source.payload.extraData;
|
|
143
182
|
}
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
if (finalPayload === messageSource || (finalPayload && typeof finalPayload === 'object' && (finalPayload.message === rawMessageValue || finalPayload.message === finalMessage))) {
|
|
148
|
-
finalPayload = undefined;
|
|
183
|
+
else {
|
|
184
|
+
if (dataVal === undefined && Object.keys(source).length > 0 && !source.status && !source.message && !source.code) {
|
|
185
|
+
dataVal = source;
|
|
149
186
|
}
|
|
150
187
|
}
|
|
151
|
-
const
|
|
188
|
+
const finalPayload = (dataVal !== undefined || extraDataVal !== undefined)
|
|
189
|
+
? { data: dataVal, extraData: extraDataVal }
|
|
190
|
+
: undefined;
|
|
191
|
+
const finalMessage = this.parseMessage(messageSource, currentReq.t, false);
|
|
192
|
+
const successCode = this.currentStore?.metadata?._code || source?.code || 200;
|
|
152
193
|
return currentRes.status(successCode).send(new ResponseSchema({
|
|
153
194
|
status: true,
|
|
154
195
|
message: finalMessage,
|
|
@@ -160,7 +201,7 @@ export default class Controller extends BaseController {
|
|
|
160
201
|
this.logger?.log(LoggerLayerEnum.ERROR, `Controller Error: ${error.message}`);
|
|
161
202
|
return this.res.status(500).send(new ResponseSchema({
|
|
162
203
|
status: false,
|
|
163
|
-
message: error.message,
|
|
204
|
+
message: error.message || 'error_internal',
|
|
164
205
|
code: 500
|
|
165
206
|
}).getResponse);
|
|
166
207
|
}
|