@xrystal/core 3.23.6 → 3.23.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.23.6",
4
+ "version": "3.23.8",
5
5
  "description": "Project core for xrystal",
6
6
  "publishConfig": {
7
7
  "access": "public",
@@ -20,20 +20,24 @@ export class BaseController {
20
20
  const ctx = store?.ctx || {};
21
21
  const req = store?.req || {};
22
22
  const identityT = (k) => k;
23
- const body = ctx.body || req.body || {};
24
- const query = ctx.query || req.query || (typeof req.url === 'string' && req.url.includes('?') ? qs.parse(req.url.split('?')[1]) : {});
23
+ const body = ctx.body || req.body || ctx.request?.body || {};
24
+ let query = ctx.query || req.query || {};
25
+ const urlStr = ctx.url || req.url || ctx.request?.url || '';
26
+ if (Object.keys(query).length === 0 && typeof urlStr === 'string' && urlStr.includes('?')) {
27
+ query = qs.parse(urlStr.split('?')[1]);
28
+ }
25
29
  const params = ctx.params || req.params || {};
26
- const headers = ctx.headers || req.headers || {};
30
+ const headers = ctx.headers || req.headers || ctx.request?.headers || {};
27
31
  return {
28
- url: ctx.url || req.url || '',
29
- method: ctx.method || req.method || '',
32
+ url: urlStr,
33
+ method: ctx.method || req.method || ctx.request?.method || '',
30
34
  headers,
31
35
  body,
32
36
  query,
33
37
  params,
34
38
  lang: ctx.lang || req.lang || 'en',
35
39
  t: ctx.t || req.t || identityT,
36
- accounts: ctx.accounts || req.accounts
40
+ accounts: ctx.accounts || req.accounts || ctx.user
37
41
  };
38
42
  }
39
43
  get res() {
@@ -122,10 +126,18 @@ export default class Controller extends BaseController {
122
126
  if (checkResult !== true) {
123
127
  const code = checkResult?.code || 400;
124
128
  const msg = checkResult?.message || (typeof checkResult === 'string' ? checkResult : 'error_validation');
129
+ let errorPayload = undefined;
130
+ if (checkResult?.payload) {
131
+ errorPayload = {
132
+ data: checkResult.payload.data ?? checkResult.payload,
133
+ extraData: checkResult.payload.extraData ?? {}
134
+ };
135
+ }
125
136
  return currentRes.status(code).send(new ResponseSchema({
126
137
  status: false,
127
138
  message: this.parseMessage(msg, currentReq.t, true),
128
- code
139
+ code,
140
+ payload: errorPayload
129
141
  }).getResponse);
130
142
  }
131
143
  }
@@ -152,29 +164,33 @@ export default class Controller extends BaseController {
152
164
  let finalExtraData = undefined;
153
165
  let messageSource = 'success_process';
154
166
  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;
167
+ const extract = (obj) => {
168
+ if (!obj || typeof obj !== 'object')
169
+ return;
170
+ if (obj.payload && typeof obj.payload === 'object') {
171
+ finalData = obj.payload.data !== undefined ? obj.payload.data : obj.payload;
172
+ finalExtraData = obj.payload.extraData;
160
173
  }
161
- else if (source.data !== undefined || source.extraData !== undefined) {
162
- finalData = source.data;
163
- finalExtraData = source.extraData;
174
+ else if (obj.data !== undefined || obj.extraData !== undefined) {
175
+ finalData = obj.data;
176
+ finalExtraData = obj.extraData;
164
177
  }
165
178
  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
- }
179
+ const hasMeta = obj.status !== undefined || obj.message !== undefined || obj.code !== undefined;
180
+ if (!hasMeta && Object.keys(obj).length > 0)
181
+ finalData = obj;
170
182
  }
171
- if (source.message)
172
- messageSource = source.message;
173
- if (source.code !== undefined)
174
- successCode = source.code;
183
+ if (obj.message)
184
+ messageSource = obj.message;
185
+ if (obj.code !== undefined)
186
+ successCode = obj.code;
187
+ };
188
+ if (typeof resResult === 'string') {
189
+ messageSource = resResult;
190
+ extract(logicResult);
175
191
  }
176
- else if (source !== undefined) {
177
- messageSource = source;
192
+ else {
193
+ extract(resResult !== undefined ? resResult : logicResult);
178
194
  }
179
195
  let payload = undefined;
180
196
  if (finalData !== undefined || finalExtraData !== undefined) {