@xrystal/core 3.23.6 → 3.23.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
|
@@ -20,20 +20,24 @@ export class BaseController {
|
|
|
20
20
|
const ctx = store?.ctx || {};
|
|
21
21
|
const req = store?.req || {};
|
|
22
22
|
const identityT = (k) => k;
|
|
23
|
-
const body = ctx.body || req.body || {};
|
|
24
|
-
|
|
23
|
+
const body = ctx.body || req.body || ctx.request?.body || {};
|
|
24
|
+
let query = ctx.query || req.query || {};
|
|
25
|
+
const urlStr = ctx.url || req.url || ctx.request?.url || '';
|
|
26
|
+
if (Object.keys(query).length === 0 && typeof urlStr === 'string' && urlStr.includes('?')) {
|
|
27
|
+
query = qs.parse(urlStr.split('?')[1]);
|
|
28
|
+
}
|
|
25
29
|
const params = ctx.params || req.params || {};
|
|
26
|
-
const headers = ctx.headers || req.headers || {};
|
|
30
|
+
const headers = ctx.headers || req.headers || ctx.request?.headers || {};
|
|
27
31
|
return {
|
|
28
|
-
url:
|
|
29
|
-
method: ctx.method || req.method || '',
|
|
32
|
+
url: urlStr,
|
|
33
|
+
method: ctx.method || req.method || ctx.request?.method || '',
|
|
30
34
|
headers,
|
|
31
35
|
body,
|
|
32
36
|
query,
|
|
33
37
|
params,
|
|
34
38
|
lang: ctx.lang || req.lang || 'en',
|
|
35
39
|
t: ctx.t || req.t || identityT,
|
|
36
|
-
accounts: ctx.accounts || req.accounts
|
|
40
|
+
accounts: ctx.accounts || req.accounts || ctx.user
|
|
37
41
|
};
|
|
38
42
|
}
|
|
39
43
|
get res() {
|
|
@@ -122,10 +126,18 @@ export default class Controller extends BaseController {
|
|
|
122
126
|
if (checkResult !== true) {
|
|
123
127
|
const code = checkResult?.code || 400;
|
|
124
128
|
const msg = checkResult?.message || (typeof checkResult === 'string' ? checkResult : 'error_validation');
|
|
129
|
+
let errorPayload = undefined;
|
|
130
|
+
if (checkResult?.payload) {
|
|
131
|
+
errorPayload = {
|
|
132
|
+
data: checkResult.payload.data ?? checkResult.payload,
|
|
133
|
+
extraData: checkResult.payload.extraData ?? {}
|
|
134
|
+
};
|
|
135
|
+
}
|
|
125
136
|
return currentRes.status(code).send(new ResponseSchema({
|
|
126
137
|
status: false,
|
|
127
138
|
message: this.parseMessage(msg, currentReq.t, true),
|
|
128
|
-
code
|
|
139
|
+
code,
|
|
140
|
+
payload: errorPayload
|
|
129
141
|
}).getResponse);
|
|
130
142
|
}
|
|
131
143
|
}
|
|
@@ -152,29 +164,33 @@ export default class Controller extends BaseController {
|
|
|
152
164
|
let finalExtraData = undefined;
|
|
153
165
|
let messageSource = 'success_process';
|
|
154
166
|
let successCode = this.currentStore?.metadata?._code || 200;
|
|
155
|
-
const
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
167
|
+
const extract = (obj) => {
|
|
168
|
+
if (!obj || typeof obj !== 'object')
|
|
169
|
+
return;
|
|
170
|
+
if (obj.payload && typeof obj.payload === 'object') {
|
|
171
|
+
finalData = obj.payload.data !== undefined ? obj.payload.data : obj.payload;
|
|
172
|
+
finalExtraData = obj.payload.extraData;
|
|
160
173
|
}
|
|
161
|
-
else if (
|
|
162
|
-
finalData =
|
|
163
|
-
finalExtraData =
|
|
174
|
+
else if (obj.data !== undefined || obj.extraData !== undefined) {
|
|
175
|
+
finalData = obj.data;
|
|
176
|
+
finalExtraData = obj.extraData;
|
|
164
177
|
}
|
|
165
178
|
else {
|
|
166
|
-
const
|
|
167
|
-
if (!
|
|
168
|
-
finalData =
|
|
169
|
-
}
|
|
179
|
+
const hasMeta = obj.status !== undefined || obj.message !== undefined || obj.code !== undefined;
|
|
180
|
+
if (!hasMeta && Object.keys(obj).length > 0)
|
|
181
|
+
finalData = obj;
|
|
170
182
|
}
|
|
171
|
-
if (
|
|
172
|
-
messageSource =
|
|
173
|
-
if (
|
|
174
|
-
successCode =
|
|
183
|
+
if (obj.message)
|
|
184
|
+
messageSource = obj.message;
|
|
185
|
+
if (obj.code !== undefined)
|
|
186
|
+
successCode = obj.code;
|
|
187
|
+
};
|
|
188
|
+
if (typeof resResult === 'string') {
|
|
189
|
+
messageSource = resResult;
|
|
190
|
+
extract(logicResult);
|
|
175
191
|
}
|
|
176
|
-
else
|
|
177
|
-
|
|
192
|
+
else {
|
|
193
|
+
extract(resResult !== undefined ? resResult : logicResult);
|
|
178
194
|
}
|
|
179
195
|
let payload = undefined;
|
|
180
196
|
if (finalData !== undefined || finalExtraData !== undefined) {
|