@xrystal/core 3.23.0 → 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.23.0",
4
+ "version": "3.23.1",
5
5
  "description": "Project core for xrystal",
6
6
  "publishConfig": {
7
7
  "access": "public",
@@ -70,6 +70,7 @@ export interface CustomRequest {
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>;
@@ -31,7 +31,13 @@ export class BaseController {
31
31
  get res() {
32
32
  const store = this.currentStore;
33
33
  const self = this;
34
- 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
+ };
35
41
  if (!store)
36
42
  return fallbackRes;
37
43
  if (!store.metadata)
@@ -42,13 +48,22 @@ export class BaseController {
42
48
  store.metadata._code = code;
43
49
  return this;
44
50
  },
51
+ setRaw(isRaw = true) {
52
+ store.metadata._isRaw = isRaw;
53
+ return this;
54
+ },
45
55
  send(data) {
46
56
  if (self.protocol === ProtocolEnum.WEBSOCKET)
47
57
  return data;
48
58
  if (data instanceof Response)
49
59
  return data;
50
60
  const status = store.metadata?._code || 200;
51
- 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);
52
67
  return new Response(body, { status, headers: { 'content-type': 'application/json' } });
53
68
  },
54
69
  json(data) { return this.send(data); }
@@ -100,27 +115,15 @@ export default class Controller extends BaseController {
100
115
  const isObj = checkRes && typeof checkRes === 'object';
101
116
  const code = isObj ? checkRes.code || 400 : 400;
102
117
  const rawMsg = isObj ? checkRes.message : (typeof checkRes === 'string' ? checkRes : '@schemaMissingDataChecks');
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
- }
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);
114
120
  const errRes = {
115
121
  status: false,
116
122
  message: this.parseMessage(rawMsg, t),
117
123
  code
118
124
  };
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
- };
125
+ if (errCoreData !== undefined) {
126
+ errRes.payload = { data: errCoreData };
124
127
  }
125
128
  return this.res.status(code).send(new ResponseSchema(errRes).getResponse);
126
129
  }
@@ -132,43 +135,41 @@ export default class Controller extends BaseController {
132
135
  let finalRes = response ? await response({ ...p, logicResult: logicRes }) : logicRes;
133
136
  if (typeof finalRes === 'function')
134
137
  finalRes = await finalRes(p);
135
- if (Array.isArray(finalRes)) {
136
- finalRes = { message: `@successfully ${finalRes.join(' ')}`, data: logicRes };
137
- }
138
- else if (typeof finalRes === 'string' && !finalRes.startsWith('@')) {
139
- finalRes = { message: `@successfully ${finalRes}`, data: logicRes };
140
- }
141
138
  if (finalRes instanceof Response || finalRes?.constructor?.name === 'Response')
142
139
  return finalRes;
140
+ if (store?.metadata?._isRaw)
141
+ return this.res.send(finalRes);
143
142
  const isSuccess = finalRes?.status !== false;
144
143
  const finalCode = finalRes?.code || store?.metadata?._code || 200;
145
144
  const msgSource = finalRes?.message || (typeof finalRes === 'string' ? finalRes : '@successfully');
146
- const rawOutput = (finalRes && typeof finalRes === 'object' && !Array.isArray(finalRes))
147
- ? finalRes
148
- : { data: finalRes || logicRes };
149
- const { message: _m, status: _s, code: _co, data, payload, ...restExtras } = rawOutput;
150
145
  let coreData;
151
- let extraData;
152
- if (data !== undefined || payload !== undefined) {
153
- coreData = data !== undefined ? data : payload;
154
- extraData = restExtras;
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
+ }
155
156
  }
156
157
  else {
157
- coreData = Object.keys(restExtras).length > 0 ? restExtras : (finalRes || logicRes);
158
- extraData = {};
158
+ coreData = finalRes ?? logicRes;
159
159
  }
160
160
  const responseData = {
161
161
  status: isSuccess,
162
- message: this.parseMessage(msgSource, t)
162
+ message: this.parseMessage(msgSource, t),
163
+ code: finalCode
163
164
  };
164
- const finalPayload = {};
165
+ const payloadContainer = {};
165
166
  if (coreData !== undefined && coreData !== null)
166
- finalPayload.data = coreData;
167
+ payloadContainer.data = coreData;
167
168
  if (Object.keys(extraData).length > 0)
168
- finalPayload.extraData = extraData;
169
- if (Object.keys(finalPayload).length > 0)
170
- responseData.payload = finalPayload;
171
- responseData.code = finalCode;
169
+ payloadContainer.extraData = extraData;
170
+ if (Object.keys(payloadContainer).length > 0) {
171
+ responseData.payload = payloadContainer;
172
+ }
172
173
  return this.res.status(finalCode).send(new ResponseSchema(responseData).getResponse);
173
174
  }
174
175
  catch (error) {