@xrystal/core 3.23.7 → 3.24.0
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,18 +20,16 @@ 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 || {};
|
|
23
|
+
const body = ctx.body || req.body || ctx.request?.body || {};
|
|
24
24
|
let query = ctx.query || req.query || {};
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
query = qs.parse(urlStr.split('?')[1]);
|
|
29
|
-
}
|
|
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]);
|
|
30
28
|
}
|
|
31
29
|
const params = ctx.params || req.params || {};
|
|
32
30
|
const headers = ctx.headers || req.headers || ctx.request?.headers || {};
|
|
33
31
|
return {
|
|
34
|
-
url:
|
|
32
|
+
url: urlStr,
|
|
35
33
|
method: ctx.method || req.method || ctx.request?.method || '',
|
|
36
34
|
headers,
|
|
37
35
|
body,
|
|
@@ -72,12 +70,13 @@ export class BaseController {
|
|
|
72
70
|
if (!contentType) {
|
|
73
71
|
contentType = isRaw ? 'application/octet-stream' : 'application/json';
|
|
74
72
|
}
|
|
75
|
-
|
|
73
|
+
let rawCode = store.metadata._code || 200;
|
|
74
|
+
let httpStatus = (rawCode >= 100 && rawCode <= 101) || (rawCode >= 200 && rawCode <= 599) ? rawCode : (rawCode === 0 || (rawCode >= 200 && rawCode <= 299) ? 200 : 400);
|
|
76
75
|
const body = isRaw || typeof data === 'string'
|
|
77
76
|
? data
|
|
78
77
|
: JSON.stringify(data?.getResponse ? data.getResponse : data);
|
|
79
78
|
return new Response(body, {
|
|
80
|
-
status,
|
|
79
|
+
status: httpStatus,
|
|
81
80
|
headers: { 'content-type': contentType }
|
|
82
81
|
});
|
|
83
82
|
},
|
|
@@ -126,12 +125,20 @@ export default class Controller extends BaseController {
|
|
|
126
125
|
if (checks) {
|
|
127
126
|
const checkResult = await checks(p);
|
|
128
127
|
if (checkResult !== true) {
|
|
129
|
-
const
|
|
128
|
+
const bizCode = checkResult?.code ?? 400;
|
|
130
129
|
const msg = checkResult?.message || (typeof checkResult === 'string' ? checkResult : 'error_validation');
|
|
131
|
-
|
|
130
|
+
let errorPayload = undefined;
|
|
131
|
+
if (checkResult?.payload) {
|
|
132
|
+
errorPayload = {
|
|
133
|
+
data: checkResult.payload.data ?? checkResult.payload,
|
|
134
|
+
extraData: checkResult.payload.extraData ?? {}
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
return currentRes.status(bizCode).send(new ResponseSchema({
|
|
132
138
|
status: false,
|
|
133
139
|
message: this.parseMessage(msg, currentReq.t, true),
|
|
134
|
-
code
|
|
140
|
+
code: bizCode,
|
|
141
|
+
payload: errorPayload
|
|
135
142
|
}).getResponse);
|
|
136
143
|
}
|
|
137
144
|
}
|
|
@@ -143,11 +150,11 @@ export default class Controller extends BaseController {
|
|
|
143
150
|
logicResult = await logicResult.response();
|
|
144
151
|
}
|
|
145
152
|
if (logicResult?.status === false) {
|
|
146
|
-
const
|
|
147
|
-
return currentRes.status(
|
|
153
|
+
const bizCode = logicResult.code ?? 400;
|
|
154
|
+
return currentRes.status(bizCode).send(new ResponseSchema({
|
|
148
155
|
status: false,
|
|
149
156
|
message: this.parseMessage(logicResult.message || 'error_process', currentReq.t, true),
|
|
150
|
-
code
|
|
157
|
+
code: bizCode
|
|
151
158
|
}).getResponse);
|
|
152
159
|
}
|
|
153
160
|
const resResult = response ? await response({ ...p, logicResult }) : logicResult;
|
|
@@ -157,7 +164,7 @@ export default class Controller extends BaseController {
|
|
|
157
164
|
let finalData = undefined;
|
|
158
165
|
let finalExtraData = undefined;
|
|
159
166
|
let messageSource = 'success_process';
|
|
160
|
-
let
|
|
167
|
+
let bizSuccessCode = this.currentStore?.metadata?._code || 200;
|
|
161
168
|
const extract = (obj) => {
|
|
162
169
|
if (!obj || typeof obj !== 'object')
|
|
163
170
|
return;
|
|
@@ -170,15 +177,14 @@ export default class Controller extends BaseController {
|
|
|
170
177
|
finalExtraData = obj.extraData;
|
|
171
178
|
}
|
|
172
179
|
else {
|
|
173
|
-
const
|
|
174
|
-
if (!
|
|
180
|
+
const hasMeta = obj.status !== undefined || obj.message !== undefined || obj.code !== undefined;
|
|
181
|
+
if (!hasMeta && Object.keys(obj).length > 0)
|
|
175
182
|
finalData = obj;
|
|
176
|
-
}
|
|
177
183
|
}
|
|
178
184
|
if (obj.message)
|
|
179
185
|
messageSource = obj.message;
|
|
180
186
|
if (obj.code !== undefined)
|
|
181
|
-
|
|
187
|
+
bizSuccessCode = obj.code;
|
|
182
188
|
};
|
|
183
189
|
if (typeof resResult === 'string') {
|
|
184
190
|
messageSource = resResult;
|
|
@@ -194,11 +200,11 @@ export default class Controller extends BaseController {
|
|
|
194
200
|
extraData: finalExtraData ?? {}
|
|
195
201
|
};
|
|
196
202
|
}
|
|
197
|
-
return currentRes.status(
|
|
203
|
+
return currentRes.status(bizSuccessCode).send(new ResponseSchema({
|
|
198
204
|
status: true,
|
|
199
205
|
message: this.parseMessage(messageSource, currentReq.t, false),
|
|
200
206
|
payload,
|
|
201
|
-
code:
|
|
207
|
+
code: bizSuccessCode
|
|
202
208
|
}).getResponse);
|
|
203
209
|
}
|
|
204
210
|
catch (error) {
|