@xrystal/core 3.16.2 → 3.16.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.16.2",
4
+ "version": "3.16.4",
5
5
  "description": "Project core for xrystal",
6
6
  "publishConfig": {
7
7
  "access": "public",
@@ -23,7 +23,7 @@ class Controller {
23
23
  if (!store)
24
24
  return { url: '', method: '', headers: {}, params: {}, query: {}, t: identityT };
25
25
  const { ctx, req: nativeReq } = store;
26
- const reqSource = nativeReq?.url ? nativeReq : (ctx?.request?.url ? ctx.request : nativeReq);
26
+ const reqSource = nativeReq || ctx?.request || ctx?.data?.request;
27
27
  return {
28
28
  url: reqSource?.url || '',
29
29
  method: reqSource?.method || 'GET',
@@ -34,22 +34,23 @@ class Controller {
34
34
  params: ctx?.params || nativeReq?.params || {},
35
35
  query: ctx?.query || nativeReq?.query || {},
36
36
  accounts: ctx?.user || ctx?.data?.user || nativeReq?.accounts,
37
- t: ctx?.t || nativeReq?.t || identityT
37
+ t: ctx?.t || ctx?.data?.t || nativeReq?.t || identityT
38
38
  };
39
39
  }
40
40
  get res() {
41
41
  const store = this.currentStore;
42
+ const fallbackRes = {
43
+ status: function () { return this; },
44
+ send: (d) => new Response(typeof d === 'string' ? d : JSON.stringify(d), { status: 500, headers: { 'content-type': 'application/json' } }),
45
+ json: function (d) { return this.send(d); },
46
+ locals: {}
47
+ };
42
48
  if (!store)
43
- return {
44
- status: function () { return this; },
45
- send: (data) => new Response(JSON.stringify(data), { status: 500, headers: { 'content-type': 'application/json' } }),
46
- json: function (data) { return this.send(data); },
47
- locals: {}
48
- };
49
- const currentProtocol = this.protocol;
49
+ return fallbackRes;
50
50
  if (store.ctx || store.req) {
51
51
  if (!store.metadata)
52
52
  store.metadata = { locals: {} };
53
+ const currentProtocol = this.protocol;
53
54
  return {
54
55
  locals: store.metadata.locals,
55
56
  status(code) {
@@ -57,11 +58,19 @@ class Controller {
57
58
  return this;
58
59
  },
59
60
  send: (data) => {
60
- const isRaw = data instanceof Blob || data instanceof Uint8Array || store.metadata?._isRaw;
61
- const contentType = store.metadata?._contentType || (isRaw ? 'application/octet-stream' : 'application/json');
62
61
  if (currentProtocol === ProtocolEnum.WEBSOCKET)
63
62
  return data;
64
- const body = isRaw ? data : JSON.stringify(data && typeof data === 'object' ? JSON.parse(JSON.stringify(data)) : data);
63
+ const isBinary = data instanceof Blob || data instanceof Uint8Array || data instanceof ArrayBuffer;
64
+ const isRaw = store.metadata?._isRaw || isBinary;
65
+ const contentType = store.metadata?._contentType || (isRaw ? 'application/octet-stream' : 'application/json');
66
+ let body;
67
+ if (isRaw || typeof data === 'string') {
68
+ body = data;
69
+ }
70
+ else {
71
+ const rawData = data?.getResponse ? data.getResponse : data;
72
+ body = JSON.stringify(rawData);
73
+ }
65
74
  return new Response(body, {
66
75
  status: store.metadata?._code || 200,
67
76
  headers: { 'content-type': contentType }
@@ -70,7 +79,7 @@ class Controller {
70
79
  json(data) { return this.send(data); }
71
80
  };
72
81
  }
73
- return store.res;
82
+ return store.res || fallbackRes;
74
83
  }
75
84
  responseProtocolSwitch = async ({ res, resStatus = 200, context }) => {
76
85
  const responseData = context({ localeLanguageConverter: this.req?.t });
@@ -106,27 +115,24 @@ export class ControllerService extends Controller {
106
115
  }
107
116
  }
108
117
  const logicResult = await logic({ payload, convertedPayload });
109
- if (logicResult.response instanceof Function)
118
+ if (logicResult?.response instanceof Function)
110
119
  return logicResult.response(logicResult.payload);
111
- if (logicResult.message) {
120
+ if (logicResult?.message) {
112
121
  return await this.responseProtocolSwitch({ res: currentRes, resStatus: 400, context: () => new ResponseSchema(logicResult).getResponse });
113
122
  }
114
123
  if (response) {
115
124
  const resResult = await response({ payload, convertedPayload, logicResult });
116
125
  const successObj = {
117
126
  status: true,
118
- message: Array.isArray(resResult.message)
119
- ? responseMessageHelper.successFully(resResult.message[0], resResult.message[1], currentReq.t)
120
- : resResult.message,
121
- payload: logicResult.payload
127
+ message: Array.isArray(resResult.message) ? responseMessageHelper.successFully(resResult.message[0], resResult.message[1], currentReq.t) : resResult.message,
128
+ payload: logicResult?.payload || logicResult
122
129
  };
123
130
  return await this.responseProtocolSwitch({ res: currentRes, resStatus: 200, context: () => new ResponseSchema(successObj).getResponse });
124
131
  }
125
- const finalData = logicResult.payload !== undefined ? logicResult.payload : logicResult;
126
- return finalData && typeof finalData === 'object' ? JSON.parse(JSON.stringify(finalData)) : finalData;
132
+ return currentRes.send(logicResult?.payload !== undefined ? logicResult.payload : logicResult);
127
133
  }
128
134
  catch (error) {
129
- return { status: false, message: error.message };
135
+ return this.res.status(500).send({ status: false, message: error.message });
130
136
  }
131
137
  }
132
138
  }