@xrystal/core 3.24.8 → 3.25.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
|
@@ -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('@')) {
|
|
@@ -91,8 +90,10 @@ export default class Controller extends BaseController {
|
|
|
91
90
|
payloadString = parts.slice(1).join(' ');
|
|
92
91
|
}
|
|
93
92
|
const params = payloadString.split('-').map(p => p.trim()).filter(p => p !== '');
|
|
94
|
-
|
|
95
|
-
|
|
93
|
+
// Kritik Düzeltme: Çiftlemeyi önlemek için parametreleri helper'a t() ile sarmadan gönderiyoruz
|
|
94
|
+
// Çünkü helper zaten kendi içinde t() kullanıyor
|
|
95
|
+
const p1 = params[0] || '';
|
|
96
|
+
const p2 = params.slice(1).join(' ');
|
|
96
97
|
if (responseMessageHelper[method]) {
|
|
97
98
|
return responseMessageHelper[method](p1, p2, t);
|
|
98
99
|
}
|
|
@@ -124,12 +125,7 @@ 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 }).getResponse);
|
|
133
129
|
}
|
|
134
130
|
}
|
|
135
131
|
let logicResult = logic ? await logic(p) : {};
|
|
@@ -142,17 +138,12 @@ export default class Controller extends BaseController {
|
|
|
142
138
|
}
|
|
143
139
|
if (logicResult?.status === false) {
|
|
144
140
|
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);
|
|
141
|
+
return currentRes.status(bizCode).send(new ResponseSchema({ status: false, message: this.parseMessage(logicResult.message, currentReq.t, true), code: bizCode }).getResponse);
|
|
150
142
|
}
|
|
151
143
|
const resResult = response ? await response({ ...p, logicResult }) : logicResult;
|
|
152
144
|
extractMeta(resResult);
|
|
153
|
-
if (resResult instanceof Response || resResult instanceof Blob || resResult instanceof Uint8Array || resResult instanceof ArrayBuffer)
|
|
145
|
+
if (resResult instanceof Response || resResult instanceof Blob || resResult instanceof Uint8Array || resResult instanceof ArrayBuffer)
|
|
154
146
|
return currentRes.status(200).send(resResult);
|
|
155
|
-
}
|
|
156
147
|
let finalData = undefined;
|
|
157
148
|
let finalExtraData = undefined;
|
|
158
149
|
let messageSource = 'success_process';
|
|
@@ -191,21 +182,12 @@ export default class Controller extends BaseController {
|
|
|
191
182
|
if (finalExtraData !== undefined)
|
|
192
183
|
payload.extraData = finalExtraData;
|
|
193
184
|
}
|
|
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);
|
|
185
|
+
return currentRes.status(bizSuccessCode).send(new ResponseSchema({ status: true, message: this.parseMessage(messageSource, currentReq.t, false), payload, code: bizSuccessCode }).getResponse);
|
|
200
186
|
}
|
|
201
187
|
catch (error) {
|
|
202
188
|
this.logger?.log(LoggerLayerEnum.ERROR, `Controller Error: ${error.message}`);
|
|
203
189
|
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);
|
|
190
|
+
return this.res.status(500).send(new ResponseSchema({ status: false, message: this.parseMessage(error.message, t, true), code: 500 }).getResponse);
|
|
209
191
|
}
|
|
210
192
|
}
|
|
211
193
|
}
|
|
@@ -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 = {
|