@xrystal/core 3.22.9 → 3.23.0

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.9",
4
+ "version": "3.23.0",
5
5
  "description": "Project core for xrystal",
6
6
  "publishConfig": {
7
7
  "access": "public",
@@ -64,7 +64,7 @@ export interface CustomRequest {
64
64
  headers: Record<string, any>;
65
65
  body?: any;
66
66
  params: Record<string, any>;
67
- query: Record<string, any>;
67
+ query: any;
68
68
  lang: string;
69
69
  t: (k: string, args?: any) => string;
70
70
  }
@@ -17,10 +17,15 @@ export class BaseController {
17
17
  const ctx = store?.ctx || {};
18
18
  const query = typeof ctx.query === 'string' ? qs.parse(ctx.query) : (ctx.query || {});
19
19
  return {
20
- url: ctx.url || '', method: ctx.method || '', headers: ctx.headers || {},
21
- body: ctx.body || {}, query: query, params: ctx.params || {},
22
- lang: ctx.lang || 'en', t: ctx.t || identityT,
23
- accounts: ctx.accounts || store?.req?.accounts
20
+ url: ctx.url || '',
21
+ method: ctx.method || '',
22
+ headers: ctx.headers || {},
23
+ body: ctx.body || {},
24
+ query: query,
25
+ params: ctx.params || {},
26
+ lang: ctx.lang || 'en',
27
+ t: ctx.t || identityT,
28
+ accounts: ctx.accounts || {}
24
29
  };
25
30
  }
26
31
  get res() {
@@ -33,7 +38,10 @@ export class BaseController {
33
38
  store.metadata = { locals: {} };
34
39
  return {
35
40
  get locals() { return store.metadata.locals; },
36
- status(code) { store.metadata._code = code; return this; },
41
+ status(code) {
42
+ store.metadata._code = code;
43
+ return this;
44
+ },
37
45
  send(data) {
38
46
  if (self.protocol === ProtocolEnum.WEBSOCKET)
39
47
  return data;
@@ -75,34 +83,51 @@ export default class Controller extends BaseController {
75
83
  async schema({ checks, logic, response }) {
76
84
  try {
77
85
  const currentReq = this.req;
78
- const currentRes = this.res;
79
86
  const store = this.currentStore;
80
87
  const t = currentReq.t;
81
- const p = { req: currentReq, res: currentRes, body: currentReq.body, query: currentReq.query, params: currentReq.params, locals: store?.metadata?.locals || {}, t };
88
+ const p = {
89
+ req: currentReq,
90
+ res: this.res,
91
+ body: currentReq.body,
92
+ query: currentReq.query,
93
+ params: currentReq.params,
94
+ locals: store?.metadata?.locals || {},
95
+ t
96
+ };
82
97
  if (checks) {
83
98
  const checkRes = await checks(p);
84
99
  if (checkRes !== true) {
85
100
  const isObj = checkRes && typeof checkRes === 'object';
86
101
  const code = isObj ? checkRes.code || 400 : 400;
87
102
  const rawMsg = isObj ? checkRes.message : (typeof checkRes === 'string' ? checkRes : '@schemaMissingDataChecks');
88
- const { message, status, code: _c, data, payload, ...extras } = isObj ? checkRes : {};
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;
95
- return currentRes.status(code).send(new ResponseSchema({
103
+ const { message, status, code: _c, ...rest } = isObj ? checkRes : {};
104
+ let errCoreData = rest.data !== undefined ? rest.data : rest.payload;
105
+ let errExtras = { ...rest };
106
+ if (rest.data !== undefined || rest.payload !== undefined) {
107
+ delete errExtras.data;
108
+ delete errExtras.payload;
109
+ }
110
+ else {
111
+ errCoreData = Object.keys(rest).length > 0 ? rest : undefined;
112
+ errExtras = {};
113
+ }
114
+ const errRes = {
96
115
  status: false,
97
116
  message: this.parseMessage(rawMsg, t),
98
- payload: Object.keys(finalPayload).length > 0 ? finalPayload : undefined,
99
117
  code
100
- }).getResponse);
118
+ };
119
+ if (errCoreData !== undefined || Object.keys(errExtras).length > 0) {
120
+ errRes.payload = {
121
+ ...(errCoreData !== undefined && { data: errCoreData }),
122
+ ...(Object.keys(errExtras).length > 0 && { extraData: errExtras })
123
+ };
124
+ }
125
+ return this.res.status(code).send(new ResponseSchema(errRes).getResponse);
101
126
  }
102
127
  }
103
128
  let logicRes = logic ? await logic(p) : null;
104
129
  if (logic && logicRes === false) {
105
- return currentRes.status(400).send(new ResponseSchema({ status: false, message: this.parseMessage('@unsuccessful logic', t), code: 400 }).getResponse);
130
+ return this.res.status(400).send(new ResponseSchema({ status: false, message: this.parseMessage('@unsuccessful logic', t), code: 400 }).getResponse);
106
131
  }
107
132
  let finalRes = response ? await response({ ...p, logicResult: logicRes }) : logicRes;
108
133
  if (typeof finalRes === 'function')
@@ -117,32 +142,34 @@ export default class Controller extends BaseController {
117
142
  return finalRes;
118
143
  const isSuccess = finalRes?.status !== false;
119
144
  const finalCode = finalRes?.code || store?.metadata?._code || 200;
120
- let msgSource = finalRes?.message || (typeof finalRes === 'string' ? finalRes : '@successfully');
145
+ const msgSource = finalRes?.message || (typeof finalRes === 'string' ? finalRes : '@successfully');
121
146
  const rawOutput = (finalRes && typeof finalRes === 'object' && !Array.isArray(finalRes))
122
147
  ? finalRes
123
148
  : { data: finalRes || logicRes };
124
- const { message, status, code, data, payload, ...extraData } = rawOutput;
125
- let coreData = data !== undefined ? data : payload;
126
- if (coreData === undefined && logicRes !== null && logicRes !== undefined) {
127
- coreData = logicRes;
149
+ const { message: _m, status: _s, code: _co, data, payload, ...restExtras } = rawOutput;
150
+ let coreData;
151
+ let extraData;
152
+ if (data !== undefined || payload !== undefined) {
153
+ coreData = data !== undefined ? data : payload;
154
+ extraData = restExtras;
155
+ }
156
+ else {
157
+ coreData = Object.keys(restExtras).length > 0 ? restExtras : (finalRes || logicRes);
158
+ extraData = {};
128
159
  }
129
160
  const responseData = {
130
161
  status: isSuccess,
131
162
  message: this.parseMessage(msgSource, t)
132
163
  };
133
164
  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
- }
165
+ if (coreData !== undefined && coreData !== null)
166
+ finalPayload.data = coreData;
139
167
  if (Object.keys(extraData).length > 0)
140
168
  finalPayload.extraData = extraData;
141
- if (Object.keys(finalPayload).length > 0) {
169
+ if (Object.keys(finalPayload).length > 0)
142
170
  responseData.payload = finalPayload;
143
- }
144
171
  responseData.code = finalCode;
145
- return currentRes.status(finalCode).send(new ResponseSchema(responseData).getResponse);
172
+ return this.res.status(finalCode).send(new ResponseSchema(responseData).getResponse);
146
173
  }
147
174
  catch (error) {
148
175
  this.logger?.log(LoggerLayerEnum.ERROR, `Controller Error: ${error.message}`);