@xrystal/core 3.22.7 → 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.7",
4
+ "version": "3.22.9",
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>;
@@ -26,31 +26,22 @@ export class BaseController {
26
26
  get res() {
27
27
  const store = this.currentStore;
28
28
  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
- };
29
+ const fallbackRes = { status: function () { return this; }, send: (d) => d, json: function (d) { return this.send(d); }, locals: {} };
35
30
  if (!store)
36
31
  return fallbackRes;
37
32
  if (!store.metadata)
38
33
  store.metadata = { locals: {} };
39
34
  return {
40
35
  get locals() { return store.metadata.locals; },
41
- status(code) {
42
- store.metadata._code = code;
43
- return this;
44
- },
36
+ status(code) { store.metadata._code = code; return this; },
45
37
  send(data) {
46
38
  if (self.protocol === ProtocolEnum.WEBSOCKET)
47
39
  return data;
48
40
  if (data instanceof Response)
49
41
  return data;
50
- const isRaw = store.metadata?._isRaw || (data instanceof Blob || data instanceof Uint8Array || data instanceof ArrayBuffer);
51
42
  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' } });
43
+ const body = (store.metadata?._isRaw) ? data : JSON.stringify(data?.getResponse ? data.getResponse : data);
44
+ return new Response(body, { status, headers: { 'content-type': 'application/json' } });
54
45
  },
55
46
  json(data) { return this.send(data); }
56
47
  };
@@ -62,6 +53,14 @@ export default class Controller extends BaseController {
62
53
  const protocols = this.system?.tmp?.configs?.loaders?.controller?.protocols;
63
54
  this.supportedProtocols = Array.isArray(protocols) ? protocols : [protocols || ProtocolEnum.HTTP];
64
55
  }
56
+ smartTranslate(word, t) {
57
+ const keyPath = `keywords.${word}`;
58
+ const translated = t(keyPath);
59
+ if (translated !== keyPath)
60
+ return translated;
61
+ const direct = t(word);
62
+ return direct !== word ? direct : word;
63
+ }
65
64
  parseMessage(input, t) {
66
65
  if (!input || typeof input !== 'string' || !input.startsWith('@'))
67
66
  return input;
@@ -70,10 +69,7 @@ export default class Controller extends BaseController {
70
69
  const hasMethod = typeof responseMessageHelper[maybeMethod] === 'function';
71
70
  const methodName = hasMethod ? maybeMethod : 'successfully';
72
71
  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
- });
72
+ const translatedArgs = rawArgs.map(arg => this.smartTranslate(arg, t));
77
73
  return responseMessageHelper[methodName](translatedArgs[0] || '', translatedArgs.slice(1).join(' '), t);
78
74
  }
79
75
  async schema({ checks, logic, response }) {
@@ -90,11 +86,16 @@ 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 finalCoreData = data !== undefined ? data : payload;
90
+ const finalPayload = {};
91
+ if (finalCoreData !== undefined)
92
+ finalPayload.data = finalCoreData;
93
+ if (Object.keys(extras).length > 0)
94
+ finalPayload.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(finalPayload).length > 0 ? finalPayload : undefined,
98
99
  code
99
100
  }).getResponse);
100
101
  }
@@ -107,25 +108,38 @@ export default class Controller extends BaseController {
107
108
  if (typeof finalRes === 'function')
108
109
  finalRes = await finalRes(p);
109
110
  if (Array.isArray(finalRes)) {
110
- finalRes = { message: `@successfully ${finalRes.join(' ')}` };
111
+ finalRes = { message: `@successfully ${finalRes.join(' ')}`, data: logicRes };
111
112
  }
112
113
  else if (typeof finalRes === 'string' && !finalRes.startsWith('@')) {
113
- finalRes = { message: `@successfully ${finalRes}` };
114
+ finalRes = { message: `@successfully ${finalRes}`, data: logicRes };
114
115
  }
115
116
  if (finalRes instanceof Response || finalRes?.constructor?.name === 'Response')
116
117
  return finalRes;
117
118
  const isSuccess = finalRes?.status !== false;
118
119
  const finalCode = finalRes?.code || store?.metadata?._code || 200;
119
120
  let msgSource = finalRes?.message || (typeof finalRes === 'string' ? finalRes : '@successfully');
120
- const rawOutput = (finalRes && typeof finalRes === 'object' && !Array.isArray(finalRes)) ? finalRes : { data: finalRes };
121
+ const rawOutput = (finalRes && typeof finalRes === 'object' && !Array.isArray(finalRes))
122
+ ? finalRes
123
+ : { data: finalRes || logicRes };
121
124
  const { message, status, code, data, payload, ...extraData } = rawOutput;
122
- 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
+ }
123
129
  const responseData = {
124
130
  status: isSuccess,
125
131
  message: this.parseMessage(msgSource, t)
126
132
  };
127
- if (coreData !== undefined || Object.keys(extraData).length > 0) {
128
- responseData.payload = { data: coreData, extraData: Object.keys(extraData).length > 0 ? extraData : undefined };
133
+ const finalPayload = {};
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
+ 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);