@xrystal/core 3.23.2 → 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
|
@@ -50,9 +50,9 @@ export default abstract class Controller extends BaseController implements IProv
|
|
|
50
50
|
constructor();
|
|
51
51
|
onInit(): Promise<void>;
|
|
52
52
|
private parseMessage;
|
|
53
|
-
schema
|
|
53
|
+
schema({ checks, logic, response }: {
|
|
54
54
|
checks?: (args: any) => Promise<any>;
|
|
55
|
-
logic?: (args: any) => Promise<
|
|
55
|
+
logic?: (args: any) => Promise<any>;
|
|
56
56
|
response?: (args: any) => Promise<any>;
|
|
57
57
|
}): Promise<any>;
|
|
58
58
|
}
|
|
@@ -9,17 +9,31 @@ export class BaseController {
|
|
|
9
9
|
system = x.get(System);
|
|
10
10
|
logger = x.get(Logger);
|
|
11
11
|
supportedProtocols = [ProtocolEnum.HTTP, ProtocolEnum.WEBSOCKET];
|
|
12
|
-
get protocol() {
|
|
13
|
-
|
|
12
|
+
get protocol() {
|
|
13
|
+
return this.currentStore?.protocol || ProtocolEnum.HTTP;
|
|
14
|
+
}
|
|
15
|
+
get currentStore() {
|
|
16
|
+
return getControllerCtx();
|
|
17
|
+
}
|
|
14
18
|
get req() {
|
|
15
19
|
const store = this.currentStore;
|
|
16
20
|
const ctx = store?.ctx || {};
|
|
17
|
-
const
|
|
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 || {};
|
|
18
27
|
return {
|
|
19
|
-
url: ctx.url ||
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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
|
|
23
37
|
};
|
|
24
38
|
}
|
|
25
39
|
get res() {
|
|
@@ -44,90 +58,152 @@ export class BaseController {
|
|
|
44
58
|
send(data) {
|
|
45
59
|
if (self.protocol === ProtocolEnum.WEBSOCKET)
|
|
46
60
|
return data;
|
|
61
|
+
if (data instanceof Response)
|
|
62
|
+
return data;
|
|
63
|
+
const isBinary = data instanceof Blob || data instanceof Uint8Array || data instanceof ArrayBuffer || (typeof data?.pipe === 'function');
|
|
64
|
+
const isRaw = store.metadata?._isRaw || isBinary;
|
|
65
|
+
let contentType = store.metadata?._contentType;
|
|
66
|
+
if (!contentType) {
|
|
67
|
+
contentType = isRaw ? 'application/octet-stream' : 'application/json';
|
|
68
|
+
}
|
|
47
69
|
const status = store.metadata?._code || 200;
|
|
48
|
-
const body =
|
|
49
|
-
|
|
50
|
-
|
|
70
|
+
const body = isRaw || typeof data === 'string'
|
|
71
|
+
? data
|
|
72
|
+
: JSON.stringify(data?.getResponse ? data.getResponse : data);
|
|
73
|
+
return new Response(body, {
|
|
74
|
+
status,
|
|
75
|
+
headers: { 'content-type': contentType }
|
|
76
|
+
});
|
|
77
|
+
},
|
|
78
|
+
json(data) { return this.send(data); }
|
|
51
79
|
};
|
|
52
80
|
}
|
|
53
81
|
}
|
|
54
82
|
export default class Controller extends BaseController {
|
|
55
|
-
constructor() {
|
|
83
|
+
constructor() {
|
|
84
|
+
super();
|
|
85
|
+
}
|
|
56
86
|
async onInit() {
|
|
57
87
|
const protocols = this.system?.tmp?.configs?.loaders?.controller?.protocols;
|
|
58
88
|
this.supportedProtocols = Array.isArray(protocols) ? protocols : [protocols || ProtocolEnum.HTTP];
|
|
59
89
|
}
|
|
60
|
-
parseMessage(
|
|
61
|
-
if (!
|
|
62
|
-
return
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
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
|
+
}
|
|
73
111
|
}
|
|
74
112
|
async schema({ checks, logic, response }) {
|
|
75
113
|
try {
|
|
76
114
|
const currentReq = this.req;
|
|
77
115
|
const currentRes = this.res;
|
|
78
|
-
const
|
|
79
|
-
|
|
80
|
-
|
|
116
|
+
const p = {
|
|
117
|
+
req: currentReq,
|
|
118
|
+
res: currentRes,
|
|
119
|
+
body: currentReq.body,
|
|
120
|
+
query: currentReq.query,
|
|
121
|
+
params: currentReq.params,
|
|
122
|
+
t: currentReq.t,
|
|
123
|
+
accounts: currentReq.accounts
|
|
124
|
+
};
|
|
81
125
|
if (checks) {
|
|
82
|
-
const
|
|
83
|
-
if (
|
|
84
|
-
const
|
|
85
|
-
const
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
rawMsg = '@' + rawMsg;
|
|
89
|
-
const { message, status, code: _c, data, payload, ...extras } = isObj ? checkRes : {};
|
|
90
|
-
const coreData = data || payload;
|
|
91
|
-
return currentRes.status(code).send(new ResponseSchema({
|
|
126
|
+
const checkResult = await checks(p);
|
|
127
|
+
if (checkResult !== true) {
|
|
128
|
+
const checkCode = checkResult?.code || 400;
|
|
129
|
+
const rawMsg = checkResult?.message || checkResult || 'error_validation';
|
|
130
|
+
const finalMsg = this.parseMessage(rawMsg, currentReq.t, true);
|
|
131
|
+
return currentRes.status(checkCode).send(new ResponseSchema({
|
|
92
132
|
status: false,
|
|
93
|
-
message:
|
|
94
|
-
|
|
95
|
-
code
|
|
133
|
+
message: finalMsg,
|
|
134
|
+
code: checkCode
|
|
96
135
|
}).getResponse);
|
|
97
136
|
}
|
|
98
137
|
}
|
|
99
|
-
let
|
|
100
|
-
if (
|
|
101
|
-
return
|
|
138
|
+
let logicResult = logic ? await logic(p) : {};
|
|
139
|
+
if (logicResult instanceof Response || logicResult?.constructor?.name === 'Response') {
|
|
140
|
+
return logicResult;
|
|
102
141
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
if (
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
return
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
const
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
142
|
+
if (logicResult && typeof logicResult === 'object' && typeof logicResult.response === 'function') {
|
|
143
|
+
logicResult = await logicResult.response();
|
|
144
|
+
}
|
|
145
|
+
if (logicResult?.status === false) {
|
|
146
|
+
const errorCode = logicResult.code || 400;
|
|
147
|
+
const finalMsg = this.parseMessage(logicResult.message, currentReq.t, true);
|
|
148
|
+
return currentRes.status(errorCode).send(new ResponseSchema({
|
|
149
|
+
status: false,
|
|
150
|
+
message: finalMsg,
|
|
151
|
+
code: errorCode
|
|
152
|
+
}).getResponse);
|
|
153
|
+
}
|
|
154
|
+
const resResult = response ? await response({ ...p, logicResult }) : logicResult;
|
|
155
|
+
if (resResult instanceof Response || resResult instanceof Blob || resResult instanceof Uint8Array || resResult instanceof ArrayBuffer) {
|
|
156
|
+
return currentRes.status(200).send(resResult);
|
|
157
|
+
}
|
|
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';
|
|
169
|
+
}
|
|
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;
|
|
182
|
+
}
|
|
183
|
+
else {
|
|
184
|
+
if (dataVal === undefined && Object.keys(source).length > 0 && !source.status && !source.message && !source.code) {
|
|
185
|
+
dataVal = source;
|
|
186
|
+
}
|
|
124
187
|
}
|
|
125
|
-
|
|
126
|
-
|
|
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;
|
|
193
|
+
return currentRes.status(successCode).send(new ResponseSchema({
|
|
194
|
+
status: true,
|
|
195
|
+
message: finalMessage,
|
|
196
|
+
...(finalPayload && { payload: finalPayload }),
|
|
197
|
+
code: successCode
|
|
198
|
+
}).getResponse);
|
|
127
199
|
}
|
|
128
200
|
catch (error) {
|
|
129
201
|
this.logger?.log(LoggerLayerEnum.ERROR, `Controller Error: ${error.message}`);
|
|
130
|
-
return this.res.status(500).send(new ResponseSchema({
|
|
202
|
+
return this.res.status(500).send(new ResponseSchema({
|
|
203
|
+
status: false,
|
|
204
|
+
message: error.message || 'error_internal',
|
|
205
|
+
code: 500
|
|
206
|
+
}).getResponse);
|
|
131
207
|
}
|
|
132
208
|
}
|
|
133
209
|
}
|