@xrystal/core 3.24.2 → 3.24.4

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.24.2",
4
+ "version": "3.24.4",
5
5
  "description": "Project core for xrystal",
6
6
  "publishConfig": {
7
7
  "access": "public",
@@ -10,6 +10,7 @@ export declare const controllerContextStorage: AsyncLocalStorage<{
10
10
  metadata?: {
11
11
  locals: Record<string, any>;
12
12
  _code?: number;
13
+ _statusCode?: number;
13
14
  _contentType?: string;
14
15
  _isRaw?: boolean;
15
16
  };
@@ -22,6 +23,7 @@ export declare const getControllerCtx: () => {
22
23
  metadata?: {
23
24
  locals: Record<string, any>;
24
25
  _code?: number;
26
+ _statusCode?: number;
25
27
  _contentType?: string;
26
28
  _isRaw?: boolean;
27
29
  };
@@ -39,6 +41,7 @@ export declare abstract class BaseController {
39
41
  metadata?: {
40
42
  locals: Record<string, any>;
41
43
  _code?: number;
44
+ _statusCode?: number;
42
45
  _contentType?: string;
43
46
  _isRaw?: boolean;
44
47
  };
@@ -26,16 +26,14 @@ export class BaseController {
26
26
  if (Object.keys(query).length === 0 && typeof urlStr === 'string' && urlStr.includes('?')) {
27
27
  query = qs.parse(urlStr.split('?')[1]);
28
28
  }
29
- const params = ctx.params || req.params || {};
30
- const headers = ctx.headers || req.headers || ctx.request?.headers || {};
31
29
  return {
32
30
  url: urlStr,
33
31
  method: ctx.method || req.method || ctx.request?.method || '',
34
- headers,
32
+ headers: ctx.headers || req.headers || ctx.request?.headers || {},
35
33
  body,
36
34
  query,
37
- params,
38
- lang: ctx.lang || req.lang,
35
+ params: ctx.params || req.params || {},
36
+ lang: ctx.lang || req.lang || 'en',
39
37
  t: ctx.t || req.t || identityT,
40
38
  accounts: ctx.accounts || req.accounts || ctx.user
41
39
  };
@@ -66,12 +64,14 @@ export class BaseController {
66
64
  return data;
67
65
  const isBinary = data instanceof Blob || data instanceof Uint8Array || data instanceof ArrayBuffer || (typeof data?.pipe === 'function');
68
66
  const isRaw = store.metadata?._isRaw || isBinary;
69
- let contentType = store.metadata?._contentType;
70
- if (!contentType) {
71
- contentType = isRaw ? 'application/octet-stream' : 'application/json';
67
+ let contentType = store.metadata?._contentType || (isRaw ? 'application/octet-stream' : 'application/json');
68
+ let bizCode = store.metadata._code || 200;
69
+ let httpStatus = store.metadata._statusCode;
70
+ if (!httpStatus) {
71
+ httpStatus = (bizCode >= 100 && bizCode <= 101) || (bizCode >= 200 && bizCode <= 599)
72
+ ? bizCode
73
+ : (bizCode === 0 || (bizCode >= 200 && bizCode <= 299) ? 200 : 400);
72
74
  }
73
- let rawCode = store.metadata._code || 200;
74
- let httpStatus = (rawCode >= 100 && rawCode <= 101) || (rawCode >= 200 && rawCode <= 599) ? rawCode : (rawCode === 0 || (rawCode >= 200 && rawCode <= 299) ? 200 : 400);
75
75
  const body = isRaw || typeof data === 'string'
76
76
  ? data
77
77
  : JSON.stringify(data?.getResponse ? data.getResponse : data);
@@ -94,25 +94,33 @@ export default class Controller extends BaseController {
94
94
  }
95
95
  parseMessage(msg, t, isError = false) {
96
96
  const content = String(msg || (isError ? 'unsuccessful' : 'success_process'));
97
- const parts = content.split(' ');
98
97
  let method = isError ? 'unsuccessful' : 'successfully';
99
- let startIdx = 0;
98
+ let payloadString = content;
100
99
  if (content.startsWith('@')) {
101
- method = parts[0].substring(1);
102
- startIdx = 1;
100
+ const firstSpace = content.indexOf(' ');
101
+ if (firstSpace !== -1) {
102
+ method = content.substring(1, firstSpace);
103
+ payloadString = content.substring(firstSpace + 1);
104
+ }
105
+ else {
106
+ method = content.substring(1);
107
+ payloadString = '';
108
+ }
103
109
  }
104
- const p1Raw = parts[startIdx] || '';
110
+ const params = payloadString.split('-').map(p => p.trim()).filter(p => p !== '');
111
+ const p1Raw = params[0] || '';
105
112
  const p1 = (p1Raw === method || p1Raw === 'unsuccessful' || p1Raw === 'success_process') ? '' : t(p1Raw);
106
- const p2 = parts.slice(startIdx + 1).map(v => t(v)).join(' ');
113
+ const p2 = params.slice(1).map(v => t(v)).join(' ');
107
114
  if (responseMessageHelper[method]) {
108
115
  return responseMessageHelper[method](p1, p2, t);
109
116
  }
110
117
  return isError ? responseMessageHelper.unsuccessful(p1, p2, t) : responseMessageHelper.successfully(p1, p2, t);
111
118
  }
112
119
  async schema({ checks, logic, response }) {
113
- let currentReq = this.req;
120
+ const currentReq = this.req;
121
+ const currentRes = this.res;
122
+ const store = this.currentStore;
114
123
  try {
115
- const currentRes = this.res;
116
124
  const p = {
117
125
  req: currentReq,
118
126
  res: currentRes,
@@ -122,17 +130,27 @@ export default class Controller extends BaseController {
122
130
  t: currentReq.t,
123
131
  accounts: currentReq.accounts
124
132
  };
133
+ const extractMeta = (obj) => {
134
+ if (!obj || typeof obj !== 'object')
135
+ return;
136
+ if (obj.code !== undefined && store?.metadata)
137
+ store.metadata._code = obj.code;
138
+ if (obj.statusCode !== undefined && store?.metadata)
139
+ store.metadata._statusCode = obj.statusCode;
140
+ };
125
141
  if (checks) {
126
142
  const checkResult = await checks(p);
127
143
  if (checkResult !== true) {
128
- const bizCode = checkResult?.code ?? 400;
144
+ extractMeta(checkResult);
145
+ const bizCode = store?.metadata?._code ?? 400;
129
146
  const msg = checkResult?.message || checkResult;
130
147
  let errorPayload = undefined;
131
148
  if (checkResult?.payload) {
132
- errorPayload = {
133
- data: checkResult.payload.data ?? checkResult.payload,
134
- extraData: checkResult.payload.extraData ?? {}
135
- };
149
+ const d = checkResult.payload.data ?? checkResult.payload;
150
+ const e = checkResult.payload.extraData;
151
+ errorPayload = { data: d };
152
+ if (e !== undefined)
153
+ errorPayload.extraData = e;
136
154
  }
137
155
  return currentRes.status(bizCode).send(new ResponseSchema({
138
156
  status: false,
@@ -143,14 +161,15 @@ export default class Controller extends BaseController {
143
161
  }
144
162
  }
145
163
  let logicResult = logic ? await logic(p) : {};
146
- if (logicResult instanceof Response || logicResult?.constructor?.name === 'Response') {
164
+ extractMeta(logicResult);
165
+ if (logicResult instanceof Response || logicResult?.constructor?.name === 'Response')
147
166
  return logicResult;
148
- }
149
167
  if (logicResult && typeof logicResult === 'object' && typeof logicResult.response === 'function') {
150
168
  logicResult = await logicResult.response();
169
+ extractMeta(logicResult);
151
170
  }
152
171
  if (logicResult?.status === false) {
153
- const bizCode = logicResult.code ?? 400;
172
+ const bizCode = store?.metadata?._code ?? 400;
154
173
  return currentRes.status(bizCode).send(new ResponseSchema({
155
174
  status: false,
156
175
  message: this.parseMessage(logicResult.message, currentReq.t, true),
@@ -158,14 +177,15 @@ export default class Controller extends BaseController {
158
177
  }).getResponse);
159
178
  }
160
179
  const resResult = response ? await response({ ...p, logicResult }) : logicResult;
180
+ extractMeta(resResult);
161
181
  if (resResult instanceof Response || resResult instanceof Blob || resResult instanceof Uint8Array || resResult instanceof ArrayBuffer) {
162
182
  return currentRes.status(200).send(resResult);
163
183
  }
164
184
  let finalData = undefined;
165
185
  let finalExtraData = undefined;
166
186
  let messageSource = 'success_process';
167
- let bizSuccessCode = this.currentStore?.metadata?._code || 200;
168
- const extract = (obj) => {
187
+ let bizSuccessCode = store?.metadata?._code || 200;
188
+ const extractData = (obj) => {
169
189
  if (!obj || typeof obj !== 'object')
170
190
  return;
171
191
  if (obj.payload && typeof obj.payload === 'object') {
@@ -177,28 +197,27 @@ export default class Controller extends BaseController {
177
197
  finalExtraData = obj.extraData;
178
198
  }
179
199
  else {
180
- const hasMeta = obj.status !== undefined || obj.message !== undefined || obj.code !== undefined;
200
+ const hasMeta = obj.status !== undefined || obj.message !== undefined || obj.code !== undefined || obj.statusCode !== undefined;
181
201
  if (!hasMeta && Object.keys(obj).length > 0)
182
202
  finalData = obj;
183
203
  }
184
204
  if (obj.message)
185
205
  messageSource = obj.message;
186
- if (obj.code !== undefined)
187
- bizSuccessCode = obj.code;
188
206
  };
189
207
  if (typeof resResult === 'string') {
190
208
  messageSource = resResult;
191
- extract(logicResult);
209
+ extractData(logicResult);
192
210
  }
193
211
  else {
194
- extract(resResult !== undefined ? resResult : logicResult);
212
+ extractData(resResult !== undefined ? resResult : logicResult);
195
213
  }
196
214
  let payload = undefined;
197
215
  if (finalData !== undefined || finalExtraData !== undefined) {
198
- payload = {
199
- data: finalData ?? {},
200
- extraData: finalExtraData ?? {}
201
- };
216
+ payload = {};
217
+ if (finalData !== undefined)
218
+ payload.data = finalData;
219
+ if (finalExtraData !== undefined)
220
+ payload.extraData = finalExtraData;
202
221
  }
203
222
  return currentRes.status(bizSuccessCode).send(new ResponseSchema({
204
223
  status: true,