@xrystal/core 3.15.1 → 3.15.2

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.15.1",
4
+ "version": "3.15.2",
5
5
  "description": "Project core for xrystal",
6
6
  "publishConfig": {
7
7
  "access": "public",
@@ -21,8 +21,9 @@ class Controller {
21
21
  }
22
22
  get req() {
23
23
  const store = this.currentStore;
24
+ const defaultT = (k) => k;
24
25
  if (!store)
25
- return {};
26
+ return { url: '', method: '', headers: {}, params: {}, query: {}, t: defaultT };
26
27
  if (store.ctx) {
27
28
  const { ctx } = store;
28
29
  return {
@@ -33,24 +34,25 @@ class Controller {
33
34
  params: ctx.params || {},
34
35
  query: ctx.query || {},
35
36
  accounts: ctx.user || ctx.accounts,
36
- t: ctx.t
37
+ t: ctx.t || defaultT
37
38
  };
38
39
  }
40
+ const { req } = store;
39
41
  return {
40
- url: store.req?.originalUrl || store.req?.url || '',
41
- method: store.req?.method || 'GET',
42
- headers: store.req?.headers || {},
43
- body: store.req?.body,
44
- params: store.req?.params || {},
45
- query: store.req?.query || {},
46
- accounts: store.req?.accounts,
47
- t: store.req?.t
42
+ url: req?.originalUrl || req?.url || '',
43
+ method: req?.method || 'GET',
44
+ headers: req?.headers || {},
45
+ body: req?.body,
46
+ params: req?.params || {},
47
+ query: req?.query || {},
48
+ accounts: req?.accounts,
49
+ t: req?.t || defaultT
48
50
  };
49
51
  }
50
52
  get res() {
51
53
  const store = this.currentStore;
52
54
  if (!store)
53
- return {};
55
+ return { status: () => { }, send: () => { }, json: () => { }, locals: {} };
54
56
  const currentProtocol = this.protocol;
55
57
  if (store.ctx) {
56
58
  return {
@@ -60,7 +62,7 @@ class Controller {
60
62
  return this;
61
63
  },
62
64
  send(data) {
63
- const plainData = JSON.parse(JSON.stringify(data));
65
+ const plainData = data && typeof data === 'object' ? JSON.parse(JSON.stringify(data)) : data;
64
66
  if (currentProtocol === ProtocolEnum.WEBSOCKET)
65
67
  return plainData;
66
68
  return new Response(JSON.stringify(plainData), {
@@ -71,27 +73,30 @@ class Controller {
71
73
  json(data) { return this.send(data); }
72
74
  };
73
75
  }
76
+ const { res } = store;
74
77
  return {
75
- locals: store.res?.locals || {},
78
+ locals: res?.locals || {},
76
79
  status(code) {
77
- if (store.res?.status)
78
- store.res.status(code);
80
+ if (res?.status)
81
+ res.status(code);
79
82
  return this;
80
83
  },
81
84
  send(data) {
82
- if (store.res?.send)
83
- return store.res.send(data);
84
- return data;
85
+ const plainData = data && typeof data === 'object' ? JSON.parse(JSON.stringify(data)) : data;
86
+ if (res?.send)
87
+ return res.send(plainData);
88
+ return plainData;
85
89
  },
86
90
  json(data) {
87
- if (store.res?.json)
88
- return store.res.json(data);
89
- return data;
91
+ const plainData = data && typeof data === 'object' ? JSON.parse(JSON.stringify(data)) : data;
92
+ if (res?.json)
93
+ return res.json(plainData);
94
+ return plainData;
90
95
  }
91
96
  };
92
97
  }
93
98
  responseProtocolSwitch = async ({ res, resStatus = 200, context, req }) => {
94
- const responseData = context({ localeLanguageConverter: req.t });
99
+ const responseData = context({ localeLanguageConverter: req?.t });
95
100
  return res.status(resStatus).send(responseData);
96
101
  };
97
102
  parsedQuerys = (url) => {
@@ -138,7 +143,8 @@ export class ControllerService extends Controller {
138
143
  }).getResponse;
139
144
  return await this.responseProtocolSwitch({ req: currentReq, res: currentRes, resStatus: 200, context: () => successObj });
140
145
  }
141
- return JSON.parse(JSON.stringify(logicResult.payload || logicResult));
146
+ const finalPayload = logicResult.payload !== undefined ? logicResult.payload : logicResult;
147
+ return finalPayload && typeof finalPayload === 'object' ? JSON.parse(JSON.stringify(finalPayload)) : finalPayload;
142
148
  }
143
149
  catch (error) {
144
150
  const errorPayload = { status: false, message: error.message };