@xrystal/core 3.22.8 → 3.23.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
|
@@ -13,13 +13,19 @@ export class BaseController {
|
|
|
13
13
|
get currentStore() { return getControllerCtx(); }
|
|
14
14
|
get req() {
|
|
15
15
|
const store = this.currentStore;
|
|
16
|
+
const identityT = (k) => k;
|
|
16
17
|
const ctx = store?.ctx || {};
|
|
17
18
|
const query = typeof ctx.query === 'string' ? qs.parse(ctx.query) : (ctx.query || {});
|
|
18
19
|
return {
|
|
19
|
-
url: ctx.url || '',
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
url: ctx.url || '',
|
|
21
|
+
method: ctx.method || '',
|
|
22
|
+
headers: ctx.headers || {},
|
|
23
|
+
body: ctx.body || {},
|
|
24
|
+
query: query,
|
|
25
|
+
params: ctx.params || {},
|
|
26
|
+
lang: ctx.lang || 'en',
|
|
27
|
+
t: ctx.t || identityT,
|
|
28
|
+
accounts: ctx.accounts || {}
|
|
23
29
|
};
|
|
24
30
|
}
|
|
25
31
|
get res() {
|
|
@@ -32,7 +38,10 @@ export class BaseController {
|
|
|
32
38
|
store.metadata = { locals: {} };
|
|
33
39
|
return {
|
|
34
40
|
get locals() { return store.metadata.locals; },
|
|
35
|
-
status(code) {
|
|
41
|
+
status(code) {
|
|
42
|
+
store.metadata._code = code;
|
|
43
|
+
return this;
|
|
44
|
+
},
|
|
36
45
|
send(data) {
|
|
37
46
|
if (self.protocol === ProtocolEnum.WEBSOCKET)
|
|
38
47
|
return data;
|
|
@@ -74,42 +83,55 @@ export default class Controller extends BaseController {
|
|
|
74
83
|
async schema({ checks, logic, response }) {
|
|
75
84
|
try {
|
|
76
85
|
const currentReq = this.req;
|
|
77
|
-
const currentRes = this.res;
|
|
78
86
|
const store = this.currentStore;
|
|
79
87
|
const t = currentReq.t;
|
|
80
|
-
const p = {
|
|
81
|
-
|
|
88
|
+
const p = {
|
|
89
|
+
req: currentReq,
|
|
90
|
+
res: this.res,
|
|
91
|
+
body: currentReq.body,
|
|
92
|
+
query: currentReq.query,
|
|
93
|
+
params: currentReq.params,
|
|
94
|
+
locals: store?.metadata?.locals || {},
|
|
95
|
+
t
|
|
96
|
+
};
|
|
82
97
|
if (checks) {
|
|
83
98
|
const checkRes = await checks(p);
|
|
84
99
|
if (checkRes !== true) {
|
|
85
100
|
const isObj = checkRes && typeof checkRes === 'object';
|
|
86
101
|
const code = isObj ? checkRes.code || 400 : 400;
|
|
87
102
|
const rawMsg = isObj ? checkRes.message : (typeof checkRes === 'string' ? checkRes : '@schemaMissingDataChecks');
|
|
88
|
-
const { message, status, code: _c,
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
if (
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
103
|
+
const { message, status, code: _c, ...rest } = isObj ? checkRes : {};
|
|
104
|
+
let errCoreData = rest.data !== undefined ? rest.data : rest.payload;
|
|
105
|
+
let errExtras = { ...rest };
|
|
106
|
+
if (rest.data !== undefined || rest.payload !== undefined) {
|
|
107
|
+
delete errExtras.data;
|
|
108
|
+
delete errExtras.payload;
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
errCoreData = Object.keys(rest).length > 0 ? rest : undefined;
|
|
112
|
+
errExtras = {};
|
|
113
|
+
}
|
|
114
|
+
const errRes = {
|
|
96
115
|
status: false,
|
|
97
116
|
message: this.parseMessage(rawMsg, t),
|
|
98
|
-
payload: Object.keys(errPayload).length > 0 ? errPayload : undefined,
|
|
99
117
|
code
|
|
100
|
-
}
|
|
118
|
+
};
|
|
119
|
+
if (errCoreData !== undefined || Object.keys(errExtras).length > 0) {
|
|
120
|
+
errRes.payload = {
|
|
121
|
+
...(errCoreData !== undefined && { data: errCoreData }),
|
|
122
|
+
...(Object.keys(errExtras).length > 0 && { extraData: errExtras })
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
return this.res.status(code).send(new ResponseSchema(errRes).getResponse);
|
|
101
126
|
}
|
|
102
127
|
}
|
|
103
|
-
// 2. LOGIC
|
|
104
128
|
let logicRes = logic ? await logic(p) : null;
|
|
105
129
|
if (logic && logicRes === false) {
|
|
106
|
-
return
|
|
130
|
+
return this.res.status(400).send(new ResponseSchema({ status: false, message: this.parseMessage('@unsuccessful logic', t), code: 400 }).getResponse);
|
|
107
131
|
}
|
|
108
|
-
// 3. RESPONSE
|
|
109
132
|
let finalRes = response ? await response({ ...p, logicResult: logicRes }) : logicRes;
|
|
110
133
|
if (typeof finalRes === 'function')
|
|
111
134
|
finalRes = await finalRes(p);
|
|
112
|
-
// Auto-success conversion
|
|
113
135
|
if (Array.isArray(finalRes)) {
|
|
114
136
|
finalRes = { message: `@successfully ${finalRes.join(' ')}`, data: logicRes };
|
|
115
137
|
}
|
|
@@ -118,31 +140,36 @@ export default class Controller extends BaseController {
|
|
|
118
140
|
}
|
|
119
141
|
if (finalRes instanceof Response || finalRes?.constructor?.name === 'Response')
|
|
120
142
|
return finalRes;
|
|
121
|
-
// 4. FINAL PACKAGING (BU BÖLÜMÜ DÜZELTTİK)
|
|
122
143
|
const isSuccess = finalRes?.status !== false;
|
|
123
144
|
const finalCode = finalRes?.code || store?.metadata?._code || 200;
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
const { message, status, code, data, payload, ...
|
|
129
|
-
|
|
130
|
-
|
|
145
|
+
const msgSource = finalRes?.message || (typeof finalRes === 'string' ? finalRes : '@successfully');
|
|
146
|
+
const rawOutput = (finalRes && typeof finalRes === 'object' && !Array.isArray(finalRes))
|
|
147
|
+
? finalRes
|
|
148
|
+
: { data: finalRes || logicRes };
|
|
149
|
+
const { message: _m, status: _s, code: _co, data, payload, ...restExtras } = rawOutput;
|
|
150
|
+
let coreData;
|
|
151
|
+
let extraData;
|
|
152
|
+
if (data !== undefined || payload !== undefined) {
|
|
153
|
+
coreData = data !== undefined ? data : payload;
|
|
154
|
+
extraData = restExtras;
|
|
155
|
+
}
|
|
156
|
+
else {
|
|
157
|
+
coreData = Object.keys(restExtras).length > 0 ? restExtras : (finalRes || logicRes);
|
|
158
|
+
extraData = {};
|
|
159
|
+
}
|
|
131
160
|
const responseData = {
|
|
132
161
|
status: isSuccess,
|
|
133
162
|
message: this.parseMessage(msgSource, t)
|
|
134
163
|
};
|
|
135
|
-
// Payload oluşturma mantığı
|
|
136
164
|
const finalPayload = {};
|
|
137
|
-
if (coreData !== undefined)
|
|
165
|
+
if (coreData !== undefined && coreData !== null)
|
|
138
166
|
finalPayload.data = coreData;
|
|
139
167
|
if (Object.keys(extraData).length > 0)
|
|
140
168
|
finalPayload.extraData = extraData;
|
|
141
|
-
if (Object.keys(finalPayload).length > 0)
|
|
169
|
+
if (Object.keys(finalPayload).length > 0)
|
|
142
170
|
responseData.payload = finalPayload;
|
|
143
|
-
}
|
|
144
171
|
responseData.code = finalCode;
|
|
145
|
-
return
|
|
172
|
+
return this.res.status(finalCode).send(new ResponseSchema(responseData).getResponse);
|
|
146
173
|
}
|
|
147
174
|
catch (error) {
|
|
148
175
|
this.logger?.log(LoggerLayerEnum.ERROR, `Controller Error: ${error.message}`);
|