@xrystal/core 3.23.4 → 3.23.6

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.23.4",
4
+ "version": "3.23.6",
5
5
  "description": "Project core for xrystal",
6
6
  "publishConfig": {
7
7
  "access": "public",
@@ -90,24 +90,19 @@ export default class Controller extends BaseController {
90
90
  parseMessage(msg, t, isError = false) {
91
91
  if (!msg)
92
92
  return '';
93
- if (typeof msg !== 'string' && !Array.isArray(msg))
94
- return '';
95
- let cleanMsg = msg;
93
+ let content = String(msg);
96
94
  let useDirect = isError;
97
- if (typeof cleanMsg === 'string' && cleanMsg.startsWith('@')) {
98
- cleanMsg = cleanMsg.substring(1);
95
+ if (content.startsWith('@')) {
96
+ content = content.substring(1);
99
97
  useDirect = true;
100
98
  }
101
- const parts = Array.isArray(cleanMsg) ? cleanMsg : cleanMsg.split(' ');
99
+ const parts = content.split(' ');
102
100
  const key = parts[0];
103
- const rawParams = parts.slice(1);
104
- const translatedParams = rawParams.map((p) => t(p));
101
+ const params = parts.slice(1).map(p => t(p));
105
102
  if (useDirect) {
106
- return t(key, translatedParams);
107
- }
108
- else {
109
- return responseMessageHelper.successFully(key, translatedParams.join(' '), t);
103
+ return t(key, params);
110
104
  }
105
+ return responseMessageHelper.successfully(t(key), params.join(' '), t);
111
106
  }
112
107
  async schema({ checks, logic, response }) {
113
108
  try {
@@ -125,13 +120,12 @@ export default class Controller extends BaseController {
125
120
  if (checks) {
126
121
  const checkResult = await checks(p);
127
122
  if (checkResult !== true) {
128
- const checkCode = checkResult?.code || 400;
129
- const rawMsg = checkResult?.message || checkResult || 'error_validation';
130
- const finalMsg = this.parseMessage(rawMsg, currentReq.t, true);
131
- return currentRes.status(checkCode).send(new ResponseSchema({
123
+ const code = checkResult?.code || 400;
124
+ const msg = checkResult?.message || (typeof checkResult === 'string' ? checkResult : 'error_validation');
125
+ return currentRes.status(code).send(new ResponseSchema({
132
126
  status: false,
133
- message: finalMsg,
134
- code: checkCode
127
+ message: this.parseMessage(msg, currentReq.t, true),
128
+ code
135
129
  }).getResponse);
136
130
  }
137
131
  }
@@ -143,57 +137,56 @@ export default class Controller extends BaseController {
143
137
  logicResult = await logicResult.response();
144
138
  }
145
139
  if (logicResult?.status === false) {
146
- const errorCode = logicResult.code || 400;
147
- const finalMsg = this.parseMessage(logicResult.message, currentReq.t, true);
148
- return currentRes.status(errorCode).send(new ResponseSchema({
140
+ const code = logicResult.code || 400;
141
+ return currentRes.status(code).send(new ResponseSchema({
149
142
  status: false,
150
- message: finalMsg,
151
- code: errorCode
143
+ message: this.parseMessage(logicResult.message || 'error_process', currentReq.t, true),
144
+ code
152
145
  }).getResponse);
153
146
  }
154
147
  const resResult = response ? await response({ ...p, logicResult }) : logicResult;
155
148
  if (resResult instanceof Response || resResult instanceof Blob || resResult instanceof Uint8Array || resResult instanceof ArrayBuffer) {
156
149
  return currentRes.status(200).send(resResult);
157
150
  }
158
- let source = resResult;
159
- if (source === undefined || source === null)
160
- source = logicResult || {};
161
- let messageSource = source?.message;
162
- if (!messageSource) {
163
- if (typeof source === 'string')
164
- messageSource = source;
165
- else if (Array.isArray(source))
166
- messageSource = source;
167
- else
168
- messageSource = 'success_process';
169
- }
170
- if (messageSource === source) {
171
- source = {};
151
+ let finalData = undefined;
152
+ let finalExtraData = undefined;
153
+ let messageSource = 'success_process';
154
+ let successCode = this.currentStore?.metadata?._code || 200;
155
+ const source = resResult !== undefined ? resResult : logicResult;
156
+ if (source !== null && typeof source === 'object') {
157
+ if (source.payload && typeof source.payload === 'object') {
158
+ finalData = source.payload.data !== undefined ? source.payload.data : source.payload;
159
+ finalExtraData = source.payload.extraData;
160
+ }
161
+ else if (source.data !== undefined || source.extraData !== undefined) {
162
+ finalData = source.data;
163
+ finalExtraData = source.extraData;
164
+ }
165
+ else {
166
+ const hasMetadata = source.status !== undefined || source.message !== undefined || source.code !== undefined;
167
+ if (!hasMetadata && Object.keys(source).length > 0) {
168
+ finalData = source;
169
+ }
170
+ }
171
+ if (source.message)
172
+ messageSource = source.message;
173
+ if (source.code !== undefined)
174
+ successCode = source.code;
172
175
  }
173
- let dataVal = source.data;
174
- let extraDataVal = source.extraData;
175
- if (source.payload) {
176
- if (source.payload.data !== undefined)
177
- dataVal = source.payload.data;
178
- else if (!source.payload.extraData && !source.data)
179
- dataVal = source.payload;
180
- if (source.payload.extraData !== undefined)
181
- extraDataVal = source.payload.extraData;
176
+ else if (source !== undefined) {
177
+ messageSource = source;
182
178
  }
183
- else {
184
- if (dataVal === undefined && Object.keys(source).length > 0 && !source.status && !source.message && !source.code) {
185
- dataVal = source;
186
- }
179
+ let payload = undefined;
180
+ if (finalData !== undefined || finalExtraData !== undefined) {
181
+ payload = {
182
+ data: finalData ?? {},
183
+ extraData: finalExtraData ?? {}
184
+ };
187
185
  }
188
- const finalPayload = (dataVal !== undefined || extraDataVal !== undefined)
189
- ? { data: dataVal, extraData: extraDataVal }
190
- : undefined;
191
- const finalMessage = this.parseMessage(messageSource, currentReq.t, false);
192
- const successCode = this.currentStore?.metadata?._code || source?.code || 200;
193
186
  return currentRes.status(successCode).send(new ResponseSchema({
194
187
  status: true,
195
- message: finalMessage,
196
- ...(finalPayload && { payload: finalPayload }),
188
+ message: this.parseMessage(messageSource, currentReq.t, false),
189
+ payload,
197
190
  code: successCode
198
191
  }).getResponse);
199
192
  }