@xrystal/core 3.24.8 → 3.25.1
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,6 +2,23 @@ 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
|
+
}
|
|
5
22
|
export declare const controllerContextStorage: AsyncLocalStorage<{
|
|
6
23
|
protocol?: ProtocolEnum;
|
|
7
24
|
ctx?: any;
|
|
@@ -59,20 +76,3 @@ export default abstract class Controller extends BaseController implements IProv
|
|
|
59
76
|
response?: (args: any) => Promise<any>;
|
|
60
77
|
}): Promise<any>;
|
|
61
78
|
}
|
|
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
|
-
}
|
|
@@ -80,9 +80,8 @@ export default class Controller extends BaseController {
|
|
|
80
80
|
}
|
|
81
81
|
parseMessage(msg, t, isError = false) {
|
|
82
82
|
const content = String(msg || '');
|
|
83
|
-
if (!content
|
|
83
|
+
if (!content)
|
|
84
84
|
return isError ? responseMessageHelper.unsuccessful('', '', t) : responseMessageHelper.successfully('', '', t);
|
|
85
|
-
}
|
|
86
85
|
let method = isError ? 'unsuccessful' : 'successfully';
|
|
87
86
|
let payloadString = content;
|
|
88
87
|
if (content.startsWith('@')) {
|
|
@@ -90,6 +89,8 @@ export default class Controller extends BaseController {
|
|
|
90
89
|
method = parts[0].substring(1);
|
|
91
90
|
payloadString = parts.slice(1).join(' ');
|
|
92
91
|
}
|
|
92
|
+
// Parametreleri '-' ile ayırıyoruz (Örn: @unsuccessful secret and code verifier)
|
|
93
|
+
// Eğer '-' yoksa tamamı tek parametre (p1) olur
|
|
93
94
|
const params = payloadString.split('-').map(p => p.trim()).filter(p => p !== '');
|
|
94
95
|
const p1 = params[0] ? t(params[0]) : '';
|
|
95
96
|
const p2 = params.slice(1).map(v => t(v)).join(' ');
|
|
@@ -124,35 +125,21 @@ export default class Controller extends BaseController {
|
|
|
124
125
|
if (checkResult.payload.extraData !== undefined)
|
|
125
126
|
errorPayload.extraData = checkResult.payload.extraData;
|
|
126
127
|
}
|
|
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);
|
|
128
|
+
return currentRes.status(bizCode).send(new ResponseSchema({ status: false, message: this.parseMessage(checkResult?.message || checkResult, currentReq.t, true), code: bizCode, payload: errorPayload }));
|
|
133
129
|
}
|
|
134
130
|
}
|
|
135
131
|
let logicResult = logic ? await logic(p) : {};
|
|
136
132
|
extractMeta(logicResult);
|
|
137
133
|
if (logicResult instanceof Response || logicResult?.constructor?.name === 'Response')
|
|
138
134
|
return logicResult;
|
|
139
|
-
if (logicResult && typeof logicResult === 'object' && typeof logicResult.response === 'function') {
|
|
140
|
-
logicResult = await logicResult.response();
|
|
141
|
-
extractMeta(logicResult);
|
|
142
|
-
}
|
|
143
135
|
if (logicResult?.status === false) {
|
|
144
136
|
const bizCode = store?.metadata?._code ?? 400;
|
|
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);
|
|
137
|
+
return currentRes.status(bizCode).send(new ResponseSchema({ status: false, message: this.parseMessage(logicResult.message, currentReq.t, true), code: bizCode }));
|
|
150
138
|
}
|
|
151
139
|
const resResult = response ? await response({ ...p, logicResult }) : logicResult;
|
|
152
140
|
extractMeta(resResult);
|
|
153
|
-
if (resResult instanceof Response || resResult instanceof Blob || resResult instanceof Uint8Array || resResult instanceof ArrayBuffer)
|
|
141
|
+
if (resResult instanceof Response || resResult instanceof Blob || resResult instanceof Uint8Array || resResult instanceof ArrayBuffer)
|
|
154
142
|
return currentRes.status(200).send(resResult);
|
|
155
|
-
}
|
|
156
143
|
let finalData = undefined;
|
|
157
144
|
let finalExtraData = undefined;
|
|
158
145
|
let messageSource = 'success_process';
|
|
@@ -191,21 +178,12 @@ export default class Controller extends BaseController {
|
|
|
191
178
|
if (finalExtraData !== undefined)
|
|
192
179
|
payload.extraData = finalExtraData;
|
|
193
180
|
}
|
|
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);
|
|
181
|
+
return currentRes.status(bizSuccessCode).send(new ResponseSchema({ status: true, message: this.parseMessage(messageSource, currentReq.t, false), payload, code: bizSuccessCode }));
|
|
200
182
|
}
|
|
201
183
|
catch (error) {
|
|
202
184
|
this.logger?.log(LoggerLayerEnum.ERROR, `Controller Error: ${error.message}`);
|
|
203
185
|
const t = currentReq?.t || ((k) => k);
|
|
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);
|
|
186
|
+
return this.res.status(500).send(new ResponseSchema({ status: false, message: this.parseMessage(error.message, t, true), code: 500 }));
|
|
209
187
|
}
|
|
210
188
|
}
|
|
211
189
|
}
|
|
@@ -13,13 +13,13 @@ export class ResponseSchema {
|
|
|
13
13
|
return this.payload;
|
|
14
14
|
}
|
|
15
15
|
statusCodeGenerator() {
|
|
16
|
-
if (this.
|
|
17
|
-
|
|
16
|
+
if (this.status === false) {
|
|
17
|
+
if (this.code !== 0 && this.code !== 1) {
|
|
18
|
+
return Number(this.code);
|
|
19
|
+
}
|
|
20
|
+
return 1;
|
|
18
21
|
}
|
|
19
|
-
|
|
20
|
-
return 0;
|
|
21
|
-
}
|
|
22
|
-
return 1;
|
|
22
|
+
return 0;
|
|
23
23
|
}
|
|
24
24
|
get getResponse() {
|
|
25
25
|
const response = {
|