@xrystal/core 3.22.7 → 3.22.8

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.7",
4
+ "version": "3.22.8",
5
5
  "description": "Project core for xrystal",
6
6
  "publishConfig": {
7
7
  "access": "public",
@@ -49,6 +49,7 @@ export declare abstract class BaseController {
49
49
  export default abstract class Controller extends BaseController implements IProvide<any> {
50
50
  constructor();
51
51
  onInit(): Promise<void>;
52
+ private smartTranslate;
52
53
  private parseMessage;
53
54
  schema<T = any>({ checks, logic, response }: {
54
55
  checks?: (args: any) => Promise<any>;
@@ -13,44 +13,34 @@ export class BaseController {
13
13
  get currentStore() { return getControllerCtx(); }
14
14
  get req() {
15
15
  const store = this.currentStore;
16
- const identityT = (k) => k;
17
16
  const ctx = store?.ctx || {};
18
17
  const query = typeof ctx.query === 'string' ? qs.parse(ctx.query) : (ctx.query || {});
19
18
  return {
20
19
  url: ctx.url || '', method: ctx.method || '', headers: ctx.headers || {},
21
20
  body: ctx.body || {}, query: query, params: ctx.params || {},
22
- lang: ctx.lang || 'en', t: ctx.t || identityT,
21
+ lang: ctx.lang || 'en', t: ctx.t || ((k) => k),
23
22
  accounts: ctx.accounts || store?.req?.accounts
24
23
  };
25
24
  }
26
25
  get res() {
27
26
  const store = this.currentStore;
28
27
  const self = this;
29
- const fallbackRes = {
30
- status: function () { return this; },
31
- send: (d) => d,
32
- json: function (d) { return this.send(d); },
33
- locals: {}
34
- };
28
+ const fallbackRes = { status: function () { return this; }, send: (d) => d, json: function (d) { return this.send(d); }, locals: {} };
35
29
  if (!store)
36
30
  return fallbackRes;
37
31
  if (!store.metadata)
38
32
  store.metadata = { locals: {} };
39
33
  return {
40
34
  get locals() { return store.metadata.locals; },
41
- status(code) {
42
- store.metadata._code = code;
43
- return this;
44
- },
35
+ status(code) { store.metadata._code = code; return this; },
45
36
  send(data) {
46
37
  if (self.protocol === ProtocolEnum.WEBSOCKET)
47
38
  return data;
48
39
  if (data instanceof Response)
49
40
  return data;
50
- const isRaw = store.metadata?._isRaw || (data instanceof Blob || data instanceof Uint8Array || data instanceof ArrayBuffer);
51
41
  const status = store.metadata?._code || 200;
52
- const body = isRaw || typeof data === 'string' ? data : JSON.stringify(data?.getResponse ? data.getResponse : data);
53
- return new Response(body, { status, headers: { 'content-type': isRaw ? 'application/octet-stream' : 'application/json' } });
42
+ const body = (store.metadata?._isRaw) ? data : JSON.stringify(data?.getResponse ? data.getResponse : data);
43
+ return new Response(body, { status, headers: { 'content-type': 'application/json' } });
54
44
  },
55
45
  json(data) { return this.send(data); }
56
46
  };
@@ -62,6 +52,14 @@ export default class Controller extends BaseController {
62
52
  const protocols = this.system?.tmp?.configs?.loaders?.controller?.protocols;
63
53
  this.supportedProtocols = Array.isArray(protocols) ? protocols : [protocols || ProtocolEnum.HTTP];
64
54
  }
55
+ smartTranslate(word, t) {
56
+ const keyPath = `keywords.${word}`;
57
+ const translated = t(keyPath);
58
+ if (translated !== keyPath)
59
+ return translated;
60
+ const direct = t(word);
61
+ return direct !== word ? direct : word;
62
+ }
65
63
  parseMessage(input, t) {
66
64
  if (!input || typeof input !== 'string' || !input.startsWith('@'))
67
65
  return input;
@@ -70,10 +68,7 @@ export default class Controller extends BaseController {
70
68
  const hasMethod = typeof responseMessageHelper[maybeMethod] === 'function';
71
69
  const methodName = hasMethod ? maybeMethod : 'successfully';
72
70
  const rawArgs = hasMethod ? parts.slice(1) : parts;
73
- const translatedArgs = rawArgs.map(arg => {
74
- const res = t(`keywords.${arg}`);
75
- return res !== `keywords.${arg}` ? res : (t(arg) !== arg ? t(arg) : arg);
76
- });
71
+ const translatedArgs = rawArgs.map(arg => this.smartTranslate(arg, t));
77
72
  return responseMessageHelper[methodName](translatedArgs[0] || '', translatedArgs.slice(1).join(' '), t);
78
73
  }
79
74
  async schema({ checks, logic, response }) {
@@ -83,6 +78,7 @@ export default class Controller extends BaseController {
83
78
  const store = this.currentStore;
84
79
  const t = currentReq.t;
85
80
  const p = { req: currentReq, res: currentRes, body: currentReq.body, query: currentReq.query, params: currentReq.params, locals: store?.metadata?.locals || {}, t };
81
+ // 1. CHECKS
86
82
  if (checks) {
87
83
  const checkRes = await checks(p);
88
84
  if (checkRes !== true) {
@@ -90,42 +86,60 @@ export default class Controller extends BaseController {
90
86
  const code = isObj ? checkRes.code || 400 : 400;
91
87
  const rawMsg = isObj ? checkRes.message : (typeof checkRes === 'string' ? checkRes : '@schemaMissingDataChecks');
92
88
  const { message, status, code: _c, data, payload, ...extras } = isObj ? checkRes : {};
93
- const coreData = data || payload;
89
+ const coreData = data !== undefined ? data : payload;
90
+ const errPayload = {};
91
+ if (coreData !== undefined)
92
+ errPayload.data = coreData;
93
+ if (Object.keys(extras).length > 0)
94
+ errPayload.extraData = extras;
94
95
  return currentRes.status(code).send(new ResponseSchema({
95
96
  status: false,
96
97
  message: this.parseMessage(rawMsg, t),
97
- payload: (coreData !== undefined || Object.keys(extras).length > 0) ? { data: coreData, extraData: extras } : undefined,
98
+ payload: Object.keys(errPayload).length > 0 ? errPayload : undefined,
98
99
  code
99
100
  }).getResponse);
100
101
  }
101
102
  }
103
+ // 2. LOGIC
102
104
  let logicRes = logic ? await logic(p) : null;
103
105
  if (logic && logicRes === false) {
104
106
  return currentRes.status(400).send(new ResponseSchema({ status: false, message: this.parseMessage('@unsuccessful logic', t), code: 400 }).getResponse);
105
107
  }
108
+ // 3. RESPONSE
106
109
  let finalRes = response ? await response({ ...p, logicResult: logicRes }) : logicRes;
107
110
  if (typeof finalRes === 'function')
108
111
  finalRes = await finalRes(p);
112
+ // Auto-success conversion
109
113
  if (Array.isArray(finalRes)) {
110
- finalRes = { message: `@successfully ${finalRes.join(' ')}` };
114
+ finalRes = { message: `@successfully ${finalRes.join(' ')}`, data: logicRes };
111
115
  }
112
116
  else if (typeof finalRes === 'string' && !finalRes.startsWith('@')) {
113
- finalRes = { message: `@successfully ${finalRes}` };
117
+ finalRes = { message: `@successfully ${finalRes}`, data: logicRes };
114
118
  }
115
119
  if (finalRes instanceof Response || finalRes?.constructor?.name === 'Response')
116
120
  return finalRes;
121
+ // 4. FINAL PACKAGING (BU BÖLÜMÜ DÜZELTTİK)
117
122
  const isSuccess = finalRes?.status !== false;
118
123
  const finalCode = finalRes?.code || store?.metadata?._code || 200;
119
124
  let msgSource = finalRes?.message || (typeof finalRes === 'string' ? finalRes : '@successfully');
125
+ // Eğer finalRes bir obje değilse onu data olarak sarmala
120
126
  const rawOutput = (finalRes && typeof finalRes === 'object' && !Array.isArray(finalRes)) ? finalRes : { data: finalRes };
127
+ // Destructuring ile alanları ayır
121
128
  const { message, status, code, data, payload, ...extraData } = rawOutput;
129
+ // Data veya Payload'dan gelen asıl veriyi belirle
122
130
  const coreData = data !== undefined ? data : payload;
123
131
  const responseData = {
124
132
  status: isSuccess,
125
133
  message: this.parseMessage(msgSource, t)
126
134
  };
127
- if (coreData !== undefined || Object.keys(extraData).length > 0) {
128
- responseData.payload = { data: coreData, extraData: Object.keys(extraData).length > 0 ? extraData : undefined };
135
+ // Payload oluşturma mantığı
136
+ const finalPayload = {};
137
+ if (coreData !== undefined)
138
+ finalPayload.data = coreData;
139
+ if (Object.keys(extraData).length > 0)
140
+ finalPayload.extraData = extraData;
141
+ if (Object.keys(finalPayload).length > 0) {
142
+ responseData.payload = finalPayload;
129
143
  }
130
144
  responseData.code = finalCode;
131
145
  return currentRes.status(finalCode).send(new ResponseSchema(responseData).getResponse);