@xrystal/core 3.23.5 → 3.23.7

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.5",
4
+ "version": "3.23.7",
5
5
  "description": "Project core for xrystal",
6
6
  "publishConfig": {
7
7
  "access": "public",
@@ -21,19 +21,25 @@ export class BaseController {
21
21
  const req = store?.req || {};
22
22
  const identityT = (k) => k;
23
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]) : {});
24
+ let query = ctx.query || req.query || {};
25
+ if (Object.keys(query).length === 0) {
26
+ const urlStr = ctx.url || req.url || ctx.request?.url || '';
27
+ if (typeof urlStr === 'string' && urlStr.includes('?')) {
28
+ query = qs.parse(urlStr.split('?')[1]);
29
+ }
30
+ }
25
31
  const params = ctx.params || req.params || {};
26
- const headers = ctx.headers || req.headers || {};
32
+ const headers = ctx.headers || req.headers || ctx.request?.headers || {};
27
33
  return {
28
- url: ctx.url || req.url || '',
29
- method: ctx.method || req.method || '',
34
+ url: ctx.url || req.url || ctx.request?.url || '',
35
+ method: ctx.method || req.method || ctx.request?.method || '',
30
36
  headers,
31
37
  body,
32
38
  query,
33
39
  params,
34
40
  lang: ctx.lang || req.lang || 'en',
35
41
  t: ctx.t || req.t || identityT,
36
- accounts: ctx.accounts || req.accounts
42
+ accounts: ctx.accounts || req.accounts || ctx.user
37
43
  };
38
44
  }
39
45
  get res() {
@@ -152,30 +158,42 @@ export default class Controller extends BaseController {
152
158
  let finalExtraData = undefined;
153
159
  let messageSource = 'success_process';
154
160
  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) {
158
- finalData = source.payload.data;
159
- finalExtraData = source.payload.extraData;
161
+ const extract = (obj) => {
162
+ if (!obj || typeof obj !== 'object')
163
+ return;
164
+ if (obj.payload && typeof obj.payload === 'object') {
165
+ finalData = obj.payload.data !== undefined ? obj.payload.data : obj.payload;
166
+ finalExtraData = obj.payload.extraData;
160
167
  }
161
- else if (source.data !== undefined || source.extraData !== undefined) {
162
- finalData = source.data;
163
- finalExtraData = source.extraData;
168
+ else if (obj.data !== undefined || obj.extraData !== undefined) {
169
+ finalData = obj.data;
170
+ finalExtraData = obj.extraData;
164
171
  }
165
- else if (!source.status && !source.message && !source.code) {
166
- finalData = source;
172
+ else {
173
+ const hasMetadata = obj.status !== undefined || obj.message !== undefined || obj.code !== undefined;
174
+ if (!hasMetadata && Object.keys(obj).length > 0) {
175
+ finalData = obj;
176
+ }
167
177
  }
168
- if (source.message)
169
- messageSource = source.message;
170
- if (source.code)
171
- successCode = source.code;
178
+ if (obj.message)
179
+ messageSource = obj.message;
180
+ if (obj.code !== undefined)
181
+ successCode = obj.code;
182
+ };
183
+ if (typeof resResult === 'string') {
184
+ messageSource = resResult;
185
+ extract(logicResult);
186
+ }
187
+ else {
188
+ extract(resResult !== undefined ? resResult : logicResult);
172
189
  }
173
- else if (source !== undefined) {
174
- messageSource = source;
190
+ let payload = undefined;
191
+ if (finalData !== undefined || finalExtraData !== undefined) {
192
+ payload = {
193
+ data: finalData ?? {},
194
+ extraData: finalExtraData ?? {}
195
+ };
175
196
  }
176
- const payload = (finalData !== undefined || finalExtraData !== undefined)
177
- ? { data: finalData, extraData: finalExtraData || {} }
178
- : undefined;
179
197
  return currentRes.status(successCode).send(new ResponseSchema({
180
198
  status: true,
181
199
  message: this.parseMessage(messageSource, currentReq.t, false),