@xrystal/core 3.22.9 → 3.23.1

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.1",
5
5
  "description": "Project core for xrystal",
6
6
  "publishConfig": {
7
7
  "access": "public",
@@ -64,12 +64,13 @@ 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
  }
71
71
  export interface CustomResponse {
72
72
  status: (code: number) => CustomResponse;
73
+ setRaw: (isRaw?: boolean) => CustomResponse;
73
74
  send: (data: any) => any;
74
75
  json: (data: any) => any;
75
76
  locals: Record<string, any>;
@@ -17,30 +17,53 @@ 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() {
27
32
  const store = this.currentStore;
28
33
  const self = this;
29
- const fallbackRes = { status: function () { return this; }, send: (d) => d, json: function (d) { return this.send(d); }, locals: {} };
34
+ const fallbackRes = {
35
+ status: function () { return this; },
36
+ send: (d) => d,
37
+ json: function (d) { return this.send(d); },
38
+ setRaw: function () { return this; },
39
+ locals: {}
40
+ };
30
41
  if (!store)
31
42
  return fallbackRes;
32
43
  if (!store.metadata)
33
44
  store.metadata = { locals: {} };
34
45
  return {
35
46
  get locals() { return store.metadata.locals; },
36
- status(code) { store.metadata._code = code; return this; },
47
+ status(code) {
48
+ store.metadata._code = code;
49
+ return this;
50
+ },
51
+ setRaw(isRaw = true) {
52
+ store.metadata._isRaw = isRaw;
53
+ return this;
54
+ },
37
55
  send(data) {
38
56
  if (self.protocol === ProtocolEnum.WEBSOCKET)
39
57
  return data;
40
58
  if (data instanceof Response)
41
59
  return data;
42
60
  const status = store.metadata?._code || 200;
43
- const body = (store.metadata?._isRaw) ? data : JSON.stringify(data?.getResponse ? data.getResponse : data);
61
+ const isRaw = store.metadata?._isRaw;
62
+ if (isRaw) {
63
+ const contentType = store.metadata?._contentType || (typeof data === 'string' ? 'text/plain' : 'application/octet-stream');
64
+ return new Response(data, { status, headers: { 'content-type': contentType } });
65
+ }
66
+ const body = JSON.stringify(data?.getResponse ? data.getResponse : data);
44
67
  return new Response(body, { status, headers: { 'content-type': 'application/json' } });
45
68
  },
46
69
  json(data) { return this.send(data); }
@@ -75,74 +98,79 @@ export default class Controller extends BaseController {
75
98
  async schema({ checks, logic, response }) {
76
99
  try {
77
100
  const currentReq = this.req;
78
- const currentRes = this.res;
79
101
  const store = this.currentStore;
80
102
  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 };
103
+ const p = {
104
+ req: currentReq,
105
+ res: this.res,
106
+ body: currentReq.body,
107
+ query: currentReq.query,
108
+ params: currentReq.params,
109
+ locals: store?.metadata?.locals || {},
110
+ t
111
+ };
82
112
  if (checks) {
83
113
  const checkRes = await checks(p);
84
114
  if (checkRes !== true) {
85
115
  const isObj = checkRes && typeof checkRes === 'object';
86
116
  const code = isObj ? checkRes.code || 400 : 400;
87
117
  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({
118
+ const { message: _m, status: _s, code: _c, data, payload, ...rest } = isObj ? checkRes : {};
119
+ const errCoreData = data ?? payload ?? (Object.keys(rest).length > 0 ? rest : undefined);
120
+ const errRes = {
96
121
  status: false,
97
122
  message: this.parseMessage(rawMsg, t),
98
- payload: Object.keys(finalPayload).length > 0 ? finalPayload : undefined,
99
123
  code
100
- }).getResponse);
124
+ };
125
+ if (errCoreData !== undefined) {
126
+ errRes.payload = { data: errCoreData };
127
+ }
128
+ return this.res.status(code).send(new ResponseSchema(errRes).getResponse);
101
129
  }
102
130
  }
103
131
  let logicRes = logic ? await logic(p) : null;
104
132
  if (logic && logicRes === false) {
105
- return currentRes.status(400).send(new ResponseSchema({ status: false, message: this.parseMessage('@unsuccessful logic', t), code: 400 }).getResponse);
133
+ return this.res.status(400).send(new ResponseSchema({ status: false, message: this.parseMessage('@unsuccessful logic', t), code: 400 }).getResponse);
106
134
  }
107
135
  let finalRes = response ? await response({ ...p, logicResult: logicRes }) : logicRes;
108
136
  if (typeof finalRes === 'function')
109
137
  finalRes = await finalRes(p);
110
- if (Array.isArray(finalRes)) {
111
- finalRes = { message: `@successfully ${finalRes.join(' ')}`, data: logicRes };
112
- }
113
- else if (typeof finalRes === 'string' && !finalRes.startsWith('@')) {
114
- finalRes = { message: `@successfully ${finalRes}`, data: logicRes };
115
- }
116
138
  if (finalRes instanceof Response || finalRes?.constructor?.name === 'Response')
117
139
  return finalRes;
140
+ if (store?.metadata?._isRaw)
141
+ return this.res.send(finalRes);
118
142
  const isSuccess = finalRes?.status !== false;
119
143
  const finalCode = finalRes?.code || store?.metadata?._code || 200;
120
- let msgSource = finalRes?.message || (typeof finalRes === 'string' ? finalRes : '@successfully');
121
- const rawOutput = (finalRes && typeof finalRes === 'object' && !Array.isArray(finalRes))
122
- ? finalRes
123
- : { 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;
144
+ const msgSource = finalRes?.message || (typeof finalRes === 'string' ? finalRes : '@successfully');
145
+ let coreData;
146
+ let extraData = {};
147
+ if (finalRes && typeof finalRes === 'object' && !Array.isArray(finalRes)) {
148
+ const { message: _m, status: _s, code: _co, data, payload, ...rest } = finalRes;
149
+ coreData = data ?? payload;
150
+ if (coreData === undefined) {
151
+ coreData = Object.keys(rest).length > 0 ? rest : logicRes;
152
+ }
153
+ else {
154
+ extraData = rest;
155
+ }
156
+ }
157
+ else {
158
+ coreData = finalRes ?? logicRes;
128
159
  }
129
160
  const responseData = {
130
161
  status: isSuccess,
131
- message: this.parseMessage(msgSource, t)
162
+ message: this.parseMessage(msgSource, t),
163
+ code: finalCode
132
164
  };
133
- 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
+ const payloadContainer = {};
166
+ if (coreData !== undefined && coreData !== null)
167
+ payloadContainer.data = coreData;
139
168
  if (Object.keys(extraData).length > 0)
140
- finalPayload.extraData = extraData;
141
- if (Object.keys(finalPayload).length > 0) {
142
- responseData.payload = finalPayload;
169
+ payloadContainer.extraData = extraData;
170
+ if (Object.keys(payloadContainer).length > 0) {
171
+ responseData.payload = payloadContainer;
143
172
  }
144
- responseData.code = finalCode;
145
- return currentRes.status(finalCode).send(new ResponseSchema(responseData).getResponse);
173
+ return this.res.status(finalCode).send(new ResponseSchema(responseData).getResponse);
146
174
  }
147
175
  catch (error) {
148
176
  this.logger?.log(LoggerLayerEnum.ERROR, `Controller Error: ${error.message}`);