@xrystal/core 3.24.7 → 3.24.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
|
@@ -2,23 +2,6 @@ import { AsyncLocalStorage } from 'node:async_hooks';
|
|
|
2
2
|
import Logger from '../logger';
|
|
3
3
|
import System from '../system';
|
|
4
4
|
import { IProvide, ProtocolEnum } from '../../utils/index';
|
|
5
|
-
export interface CustomRequest {
|
|
6
|
-
accounts?: any;
|
|
7
|
-
url: string;
|
|
8
|
-
method: string;
|
|
9
|
-
headers: Record<string, any>;
|
|
10
|
-
body?: any;
|
|
11
|
-
params: Record<string, any>;
|
|
12
|
-
query: Record<string, any>;
|
|
13
|
-
lang: string;
|
|
14
|
-
t: (k: string, args?: any) => string;
|
|
15
|
-
}
|
|
16
|
-
export interface CustomResponse {
|
|
17
|
-
status: (code: number) => CustomResponse;
|
|
18
|
-
send: (data: any) => any;
|
|
19
|
-
json: (data: any) => any;
|
|
20
|
-
locals: Record<string, any>;
|
|
21
|
-
}
|
|
22
5
|
export declare const controllerContextStorage: AsyncLocalStorage<{
|
|
23
6
|
protocol?: ProtocolEnum;
|
|
24
7
|
ctx?: any;
|
|
@@ -76,3 +59,20 @@ export default abstract class Controller extends BaseController implements IProv
|
|
|
76
59
|
response?: (args: any) => Promise<any>;
|
|
77
60
|
}): Promise<any>;
|
|
78
61
|
}
|
|
62
|
+
export interface CustomRequest {
|
|
63
|
+
accounts?: any;
|
|
64
|
+
url: string;
|
|
65
|
+
method: string;
|
|
66
|
+
headers: Record<string, any>;
|
|
67
|
+
body?: any;
|
|
68
|
+
params: Record<string, any>;
|
|
69
|
+
query: Record<string, any>;
|
|
70
|
+
lang: string;
|
|
71
|
+
t: (k: string, args?: any) => string;
|
|
72
|
+
}
|
|
73
|
+
export interface CustomResponse {
|
|
74
|
+
status: (code: number) => CustomResponse;
|
|
75
|
+
send: (data: any) => any;
|
|
76
|
+
json: (data: any) => any;
|
|
77
|
+
locals: Record<string, any>;
|
|
78
|
+
}
|
|
@@ -79,28 +79,23 @@ export default class Controller extends BaseController {
|
|
|
79
79
|
this.supportedProtocols = Array.isArray(protocols) ? protocols : [protocols || ProtocolEnum.HTTP];
|
|
80
80
|
}
|
|
81
81
|
parseMessage(msg, t, isError = false) {
|
|
82
|
-
const content = String(msg ||
|
|
82
|
+
const content = String(msg || '');
|
|
83
|
+
if (!content || content === 'success_process' || content === 'unsuccessful') {
|
|
84
|
+
return isError ? responseMessageHelper.unsuccessful('', '', t) : responseMessageHelper.successfully('', '', t);
|
|
85
|
+
}
|
|
83
86
|
let method = isError ? 'unsuccessful' : 'successfully';
|
|
84
87
|
let payloadString = content;
|
|
85
88
|
if (content.startsWith('@')) {
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
payloadString = content.substring(firstSpace + 1);
|
|
90
|
-
}
|
|
91
|
-
else {
|
|
92
|
-
method = content.substring(1);
|
|
93
|
-
payloadString = '';
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
else if (content === 'unsuccessful' || content === 'success_process') {
|
|
97
|
-
payloadString = '';
|
|
89
|
+
const parts = content.split(' ');
|
|
90
|
+
method = parts[0].substring(1);
|
|
91
|
+
payloadString = parts.slice(1).join(' ');
|
|
98
92
|
}
|
|
99
93
|
const params = payloadString.split('-').map(p => p.trim()).filter(p => p !== '');
|
|
100
94
|
const p1 = params[0] ? t(params[0]) : '';
|
|
101
95
|
const p2 = params.slice(1).map(v => t(v)).join(' ');
|
|
102
|
-
if (responseMessageHelper[method])
|
|
96
|
+
if (responseMessageHelper[method]) {
|
|
103
97
|
return responseMessageHelper[method](p1, p2, t);
|
|
98
|
+
}
|
|
104
99
|
return isError ? responseMessageHelper.unsuccessful(p1, p2, t) : responseMessageHelper.successfully(p1, p2, t);
|
|
105
100
|
}
|
|
106
101
|
async schema({ checks, logic, response }) {
|
|
@@ -129,7 +124,12 @@ export default class Controller extends BaseController {
|
|
|
129
124
|
if (checkResult.payload.extraData !== undefined)
|
|
130
125
|
errorPayload.extraData = checkResult.payload.extraData;
|
|
131
126
|
}
|
|
132
|
-
return currentRes.status(bizCode).send(new ResponseSchema({
|
|
127
|
+
return currentRes.status(bizCode).send(new ResponseSchema({
|
|
128
|
+
status: false,
|
|
129
|
+
message: this.parseMessage(checkResult?.message || checkResult, currentReq.t, true),
|
|
130
|
+
code: bizCode,
|
|
131
|
+
payload: errorPayload
|
|
132
|
+
}).getResponse);
|
|
133
133
|
}
|
|
134
134
|
}
|
|
135
135
|
let logicResult = logic ? await logic(p) : {};
|
|
@@ -142,12 +142,17 @@ export default class Controller extends BaseController {
|
|
|
142
142
|
}
|
|
143
143
|
if (logicResult?.status === false) {
|
|
144
144
|
const bizCode = store?.metadata?._code ?? 400;
|
|
145
|
-
return currentRes.status(bizCode).send(new ResponseSchema({
|
|
145
|
+
return currentRes.status(bizCode).send(new ResponseSchema({
|
|
146
|
+
status: false,
|
|
147
|
+
message: this.parseMessage(logicResult.message, currentReq.t, true),
|
|
148
|
+
code: bizCode
|
|
149
|
+
}).getResponse);
|
|
146
150
|
}
|
|
147
151
|
const resResult = response ? await response({ ...p, logicResult }) : logicResult;
|
|
148
152
|
extractMeta(resResult);
|
|
149
|
-
if (resResult instanceof Response || resResult instanceof Blob || resResult instanceof Uint8Array || resResult instanceof ArrayBuffer)
|
|
153
|
+
if (resResult instanceof Response || resResult instanceof Blob || resResult instanceof Uint8Array || resResult instanceof ArrayBuffer) {
|
|
150
154
|
return currentRes.status(200).send(resResult);
|
|
155
|
+
}
|
|
151
156
|
let finalData = undefined;
|
|
152
157
|
let finalExtraData = undefined;
|
|
153
158
|
let messageSource = 'success_process';
|
|
@@ -177,7 +182,7 @@ export default class Controller extends BaseController {
|
|
|
177
182
|
else {
|
|
178
183
|
extractData(resResult !== undefined ? resResult : logicResult);
|
|
179
184
|
}
|
|
180
|
-
|
|
185
|
+
const bizSuccessCode = store?.metadata?._code || 200;
|
|
181
186
|
let payload = undefined;
|
|
182
187
|
if (finalData !== undefined || finalExtraData !== undefined) {
|
|
183
188
|
payload = {};
|
|
@@ -186,12 +191,21 @@ export default class Controller extends BaseController {
|
|
|
186
191
|
if (finalExtraData !== undefined)
|
|
187
192
|
payload.extraData = finalExtraData;
|
|
188
193
|
}
|
|
189
|
-
return currentRes.status(bizSuccessCode).send(new ResponseSchema({
|
|
194
|
+
return currentRes.status(bizSuccessCode).send(new ResponseSchema({
|
|
195
|
+
status: true,
|
|
196
|
+
message: this.parseMessage(messageSource, currentReq.t, false),
|
|
197
|
+
payload,
|
|
198
|
+
code: bizSuccessCode
|
|
199
|
+
}).getResponse);
|
|
190
200
|
}
|
|
191
201
|
catch (error) {
|
|
192
202
|
this.logger?.log(LoggerLayerEnum.ERROR, `Controller Error: ${error.message}`);
|
|
193
203
|
const t = currentReq?.t || ((k) => k);
|
|
194
|
-
return this.res.status(500).send(new ResponseSchema({
|
|
204
|
+
return this.res.status(500).send(new ResponseSchema({
|
|
205
|
+
status: false,
|
|
206
|
+
message: this.parseMessage(error.message, t, true),
|
|
207
|
+
code: 500
|
|
208
|
+
}).getResponse);
|
|
195
209
|
}
|
|
196
210
|
}
|
|
197
211
|
}
|
|
@@ -13,15 +13,13 @@ export class ResponseSchema {
|
|
|
13
13
|
return this.payload;
|
|
14
14
|
}
|
|
15
15
|
statusCodeGenerator() {
|
|
16
|
-
if (this.
|
|
16
|
+
if (this.code !== 0 && this.code !== 1 && this.code !== undefined) {
|
|
17
17
|
return Number(this.code);
|
|
18
18
|
}
|
|
19
|
-
|
|
19
|
+
if (this.status === true) {
|
|
20
20
|
return 0;
|
|
21
21
|
}
|
|
22
|
-
|
|
23
|
-
return 1;
|
|
24
|
-
}
|
|
22
|
+
return 1;
|
|
25
23
|
}
|
|
26
24
|
get getResponse() {
|
|
27
25
|
const response = {
|