@xrystal/core 3.22.8 → 3.22.9

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
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "author": "Yusuf Yasir KAYGUSUZ",
3
3
  "name": "@xrystal/core",
4
- "version": "3.22.8",
4
+ "version": "3.22.9",
5
5
  "description": "Project core for xrystal",
6
6
  "publishConfig": {
7
7
  "access": "public",
@@ -13,12 +13,13 @@ 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
20
  url: ctx.url || '', method: ctx.method || '', headers: ctx.headers || {},
20
21
  body: ctx.body || {}, query: query, params: ctx.params || {},
21
- lang: ctx.lang || 'en', t: ctx.t || ((k) => k),
22
+ lang: ctx.lang || 'en', t: ctx.t || identityT,
22
23
  accounts: ctx.accounts || store?.req?.accounts
23
24
  };
24
25
  }
@@ -78,7 +79,6 @@ export default class Controller extends BaseController {
78
79
  const store = this.currentStore;
79
80
  const t = currentReq.t;
80
81
  const p = { req: currentReq, res: currentRes, body: currentReq.body, query: currentReq.query, params: currentReq.params, locals: store?.metadata?.locals || {}, t };
81
- // 1. CHECKS
82
82
  if (checks) {
83
83
  const checkRes = await checks(p);
84
84
  if (checkRes !== true) {
@@ -86,30 +86,27 @@ export default class Controller extends BaseController {
86
86
  const code = isObj ? checkRes.code || 400 : 400;
87
87
  const rawMsg = isObj ? checkRes.message : (typeof checkRes === 'string' ? checkRes : '@schemaMissingDataChecks');
88
88
  const { message, status, code: _c, data, payload, ...extras } = isObj ? checkRes : {};
89
- const coreData = data !== undefined ? data : payload;
90
- const errPayload = {};
91
- if (coreData !== undefined)
92
- errPayload.data = coreData;
89
+ const finalCoreData = data !== undefined ? data : payload;
90
+ const finalPayload = {};
91
+ if (finalCoreData !== undefined)
92
+ finalPayload.data = finalCoreData;
93
93
  if (Object.keys(extras).length > 0)
94
- errPayload.extraData = extras;
94
+ finalPayload.extraData = extras;
95
95
  return currentRes.status(code).send(new ResponseSchema({
96
96
  status: false,
97
97
  message: this.parseMessage(rawMsg, t),
98
- payload: Object.keys(errPayload).length > 0 ? errPayload : undefined,
98
+ payload: Object.keys(finalPayload).length > 0 ? finalPayload : undefined,
99
99
  code
100
100
  }).getResponse);
101
101
  }
102
102
  }
103
- // 2. LOGIC
104
103
  let logicRes = logic ? await logic(p) : null;
105
104
  if (logic && logicRes === false) {
106
105
  return currentRes.status(400).send(new ResponseSchema({ status: false, message: this.parseMessage('@unsuccessful logic', t), code: 400 }).getResponse);
107
106
  }
108
- // 3. RESPONSE
109
107
  let finalRes = response ? await response({ ...p, logicResult: logicRes }) : logicRes;
110
108
  if (typeof finalRes === 'function')
111
109
  finalRes = await finalRes(p);
112
- // Auto-success conversion
113
110
  if (Array.isArray(finalRes)) {
114
111
  finalRes = { message: `@successfully ${finalRes.join(' ')}`, data: logicRes };
115
112
  }
@@ -118,24 +115,27 @@ export default class Controller extends BaseController {
118
115
  }
119
116
  if (finalRes instanceof Response || finalRes?.constructor?.name === 'Response')
120
117
  return finalRes;
121
- // 4. FINAL PACKAGING (BU BÖLÜMÜ DÜZELTTİK)
122
118
  const isSuccess = finalRes?.status !== false;
123
119
  const finalCode = finalRes?.code || store?.metadata?._code || 200;
124
120
  let msgSource = finalRes?.message || (typeof finalRes === 'string' ? finalRes : '@successfully');
125
- // Eğer finalRes bir obje değilse onu data olarak sarmala
126
- const rawOutput = (finalRes && typeof finalRes === 'object' && !Array.isArray(finalRes)) ? finalRes : { data: finalRes };
127
- // Destructuring ile alanları ayır
121
+ const rawOutput = (finalRes && typeof finalRes === 'object' && !Array.isArray(finalRes))
122
+ ? finalRes
123
+ : { data: finalRes || logicRes };
128
124
  const { message, status, code, data, payload, ...extraData } = rawOutput;
129
- // Data veya Payload'dan gelen asıl veriyi belirle
130
- const coreData = data !== undefined ? data : payload;
125
+ let coreData = data !== undefined ? data : payload;
126
+ if (coreData === undefined && logicRes !== null && logicRes !== undefined) {
127
+ coreData = logicRes;
128
+ }
131
129
  const responseData = {
132
130
  status: isSuccess,
133
131
  message: this.parseMessage(msgSource, t)
134
132
  };
135
- // Payload oluşturma mantığı
136
133
  const finalPayload = {};
137
- if (coreData !== undefined)
138
- finalPayload.data = coreData;
134
+ if (coreData !== undefined) {
135
+ finalPayload.data = (coreData && typeof coreData === 'object' && 'data' in coreData && Object.keys(coreData).length === 1)
136
+ ? coreData.data
137
+ : coreData;
138
+ }
139
139
  if (Object.keys(extraData).length > 0)
140
140
  finalPayload.extraData = extraData;
141
141
  if (Object.keys(finalPayload).length > 0) {