@xrystal/core 3.23.3 → 3.23.5
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,23 @@ 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
|
+
let content = String(msg);
|
|
94
|
+
let useDirect = isError;
|
|
95
|
+
if (content.startsWith('@')) {
|
|
96
|
+
content = content.substring(1);
|
|
97
|
+
useDirect = true;
|
|
98
|
+
}
|
|
99
|
+
const parts = content.split(' ');
|
|
100
|
+
const key = parts[0];
|
|
101
|
+
const params = parts.slice(1).map(p => t(p));
|
|
102
|
+
if (useDirect) {
|
|
103
|
+
return t(key, params);
|
|
104
|
+
}
|
|
105
|
+
return responseMessageHelper.successfully(t(key), params.join(' '), t);
|
|
106
|
+
}
|
|
84
107
|
async schema({ checks, logic, response }) {
|
|
85
108
|
try {
|
|
86
109
|
const currentReq = this.req;
|
|
@@ -91,68 +114,72 @@ export default class Controller extends BaseController {
|
|
|
91
114
|
body: currentReq.body,
|
|
92
115
|
query: currentReq.query,
|
|
93
116
|
params: currentReq.params,
|
|
94
|
-
t: currentReq.t
|
|
117
|
+
t: currentReq.t,
|
|
118
|
+
accounts: currentReq.accounts
|
|
95
119
|
};
|
|
96
120
|
if (checks) {
|
|
97
121
|
const checkResult = await checks(p);
|
|
98
|
-
if (checkResult
|
|
99
|
-
const
|
|
100
|
-
|
|
122
|
+
if (checkResult !== true) {
|
|
123
|
+
const code = checkResult?.code || 400;
|
|
124
|
+
const msg = checkResult?.message || (typeof checkResult === 'string' ? checkResult : 'error_validation');
|
|
125
|
+
return currentRes.status(code).send(new ResponseSchema({
|
|
101
126
|
status: false,
|
|
102
|
-
message:
|
|
103
|
-
code
|
|
127
|
+
message: this.parseMessage(msg, currentReq.t, true),
|
|
128
|
+
code
|
|
104
129
|
}).getResponse);
|
|
105
130
|
}
|
|
106
131
|
}
|
|
107
|
-
let logicResult = logic ? await logic(p) :
|
|
132
|
+
let logicResult = logic ? await logic(p) : {};
|
|
133
|
+
if (logicResult instanceof Response || logicResult?.constructor?.name === 'Response') {
|
|
134
|
+
return logicResult;
|
|
135
|
+
}
|
|
108
136
|
if (logicResult && typeof logicResult === 'object' && typeof logicResult.response === 'function') {
|
|
109
137
|
logicResult = await logicResult.response();
|
|
110
138
|
}
|
|
111
|
-
if (logicResult instanceof Response || (logicResult?.constructor?.name === 'Response')) {
|
|
112
|
-
return logicResult;
|
|
113
|
-
}
|
|
114
139
|
if (logicResult?.status === false) {
|
|
115
|
-
const
|
|
116
|
-
return currentRes.status(
|
|
140
|
+
const code = logicResult.code || 400;
|
|
141
|
+
return currentRes.status(code).send(new ResponseSchema({
|
|
117
142
|
status: false,
|
|
118
|
-
message: logicResult.message || '',
|
|
119
|
-
code
|
|
143
|
+
message: this.parseMessage(logicResult.message || 'error_process', currentReq.t, true),
|
|
144
|
+
code
|
|
120
145
|
}).getResponse);
|
|
121
146
|
}
|
|
122
147
|
const resResult = response ? await response({ ...p, logicResult }) : logicResult;
|
|
123
|
-
if (resResult instanceof Response ||
|
|
124
|
-
return resResult;
|
|
148
|
+
if (resResult instanceof Response || resResult instanceof Blob || resResult instanceof Uint8Array || resResult instanceof ArrayBuffer) {
|
|
149
|
+
return currentRes.status(200).send(resResult);
|
|
125
150
|
}
|
|
126
|
-
let
|
|
127
|
-
let
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
const cleanMsg = messageSource.substring(1);
|
|
136
|
-
finalMessage = currentReq.t(cleanMsg, {});
|
|
137
|
-
rawMessageValue = cleanMsg;
|
|
151
|
+
let finalData = undefined;
|
|
152
|
+
let finalExtraData = undefined;
|
|
153
|
+
let messageSource = 'success_process';
|
|
154
|
+
let successCode = this.currentStore?.metadata?._code || 200;
|
|
155
|
+
const source = resResult !== undefined ? resResult : logicResult;
|
|
156
|
+
if (source !== null && typeof source === 'object') {
|
|
157
|
+
if (source.payload) {
|
|
158
|
+
finalData = source.payload.data;
|
|
159
|
+
finalExtraData = source.payload.extraData;
|
|
138
160
|
}
|
|
139
|
-
else {
|
|
140
|
-
|
|
141
|
-
|
|
161
|
+
else if (source.data !== undefined || source.extraData !== undefined) {
|
|
162
|
+
finalData = source.data;
|
|
163
|
+
finalExtraData = source.extraData;
|
|
142
164
|
}
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
if (logic) {
|
|
146
|
-
finalPayload = logicResult?.payload !== undefined ? logicResult.payload : logicResult;
|
|
147
|
-
if (finalPayload === messageSource || (finalPayload && typeof finalPayload === 'object' && (finalPayload.message === rawMessageValue || finalPayload.message === finalMessage))) {
|
|
148
|
-
finalPayload = undefined;
|
|
165
|
+
else if (!source.status && !source.message && !source.code) {
|
|
166
|
+
finalData = source;
|
|
149
167
|
}
|
|
168
|
+
if (source.message)
|
|
169
|
+
messageSource = source.message;
|
|
170
|
+
if (source.code)
|
|
171
|
+
successCode = source.code;
|
|
172
|
+
}
|
|
173
|
+
else if (source !== undefined) {
|
|
174
|
+
messageSource = source;
|
|
150
175
|
}
|
|
151
|
-
const
|
|
176
|
+
const payload = (finalData !== undefined || finalExtraData !== undefined)
|
|
177
|
+
? { data: finalData, extraData: finalExtraData || {} }
|
|
178
|
+
: undefined;
|
|
152
179
|
return currentRes.status(successCode).send(new ResponseSchema({
|
|
153
180
|
status: true,
|
|
154
|
-
message:
|
|
155
|
-
|
|
181
|
+
message: this.parseMessage(messageSource, currentReq.t, false),
|
|
182
|
+
payload,
|
|
156
183
|
code: successCode
|
|
157
184
|
}).getResponse);
|
|
158
185
|
}
|
|
@@ -160,7 +187,7 @@ export default class Controller extends BaseController {
|
|
|
160
187
|
this.logger?.log(LoggerLayerEnum.ERROR, `Controller Error: ${error.message}`);
|
|
161
188
|
return this.res.status(500).send(new ResponseSchema({
|
|
162
189
|
status: false,
|
|
163
|
-
message: error.message,
|
|
190
|
+
message: error.message || 'error_internal',
|
|
164
191
|
code: 500
|
|
165
192
|
}).getResponse);
|
|
166
193
|
}
|