@xrystal/core 3.16.0 → 3.16.3

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.0",
4
+ "version": "3.16.3",
5
5
  "description": "Project core for xrystal",
6
6
  "publishConfig": {
7
7
  "access": "public",
@@ -22,45 +22,35 @@ class Controller {
22
22
  const identityT = (k) => k;
23
23
  if (!store)
24
24
  return { url: '', method: '', headers: {}, params: {}, query: {}, t: identityT };
25
- const ctx = store.ctx;
26
- if (ctx) {
27
- const request = ctx.request || ctx.data?.request;
28
- return {
29
- url: request?.url || '',
30
- method: request?.method || '',
31
- headers: ctx.headers || {},
32
- body: ctx.body,
33
- params: ctx.params || {},
34
- query: ctx.query || {},
35
- accounts: ctx.user || ctx.data?.user || ctx.accounts,
36
- t: ctx.t || ctx.data?.t || identityT
37
- };
38
- }
39
- const req = store.req;
25
+ const { ctx, req: nativeReq } = store;
26
+ const reqSource = nativeReq || ctx?.request || ctx?.data?.request;
40
27
  return {
41
- url: req?.originalUrl || req?.url || '',
42
- method: req?.method || 'GET',
43
- headers: req?.headers || {},
44
- body: req?.body,
45
- params: req?.params || {},
46
- query: req?.query || {},
47
- accounts: req?.accounts,
48
- t: req?.t || identityT
28
+ url: reqSource?.url || '',
29
+ method: reqSource?.method || 'GET',
30
+ headers: reqSource?.headers && typeof reqSource.headers.entries === 'function'
31
+ ? Object.fromEntries(reqSource.headers.entries())
32
+ : (ctx?.headers || nativeReq?.headers || {}),
33
+ body: ctx?.body || nativeReq?.body,
34
+ params: ctx?.params || nativeReq?.params || {},
35
+ query: ctx?.query || nativeReq?.query || {},
36
+ accounts: ctx?.user || ctx?.data?.user || nativeReq?.accounts,
37
+ t: ctx?.t || ctx?.data?.t || nativeReq?.t || identityT
49
38
  };
50
39
  }
51
40
  get res() {
52
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
+ };
53
48
  if (!store)
54
- return {
55
- status: function () { return this; },
56
- send: (data) => new Response(JSON.stringify(data), { status: 500, headers: { 'content-type': 'application/json' } }),
57
- json: function (data) { return this.send(data); },
58
- locals: {}
59
- };
60
- const currentProtocol = this.protocol;
61
- if (store.ctx) {
49
+ return fallbackRes;
50
+ if (store.ctx || store.req) {
62
51
  if (!store.metadata)
63
52
  store.metadata = { locals: {} };
53
+ const currentProtocol = this.protocol;
64
54
  return {
65
55
  locals: store.metadata.locals,
66
56
  status(code) {
@@ -68,11 +58,12 @@ class Controller {
68
58
  return this;
69
59
  },
70
60
  send: (data) => {
71
- const isRaw = data instanceof Blob || data instanceof Uint8Array || store.metadata?._isRaw;
72
- const contentType = store.metadata?._contentType || (isRaw ? 'application/octet-stream' : 'application/json');
73
61
  if (currentProtocol === ProtocolEnum.WEBSOCKET)
74
62
  return data;
75
- 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 || (isBinary ? 'application/octet-stream' : 'application/json');
66
+ const body = isRaw ? data : JSON.stringify(data && typeof data === 'object' ? (data.getResponse || data) : data);
76
67
  return new Response(body, {
77
68
  status: store.metadata?._code || 200,
78
69
  headers: { 'content-type': contentType }
@@ -81,15 +72,20 @@ class Controller {
81
72
  json(data) { return this.send(data); }
82
73
  };
83
74
  }
84
- return store.res;
75
+ return store.res || fallbackRes;
85
76
  }
86
77
  responseProtocolSwitch = async ({ res, resStatus = 200, context }) => {
87
78
  const responseData = context({ localeLanguageConverter: this.req?.t });
88
79
  return res.status(resStatus).send(responseData);
89
80
  };
90
81
  parsedQuerys = (url) => {
91
- const queryString = url.includes('?') ? url.split('?')[1] : '';
92
- return queryString ? qs.parse(queryString, { decoder: decodeURIComponent }) : {};
82
+ try {
83
+ const queryString = url.includes('?') ? url.split('?')[1] : '';
84
+ return queryString ? qs.parse(queryString, { decoder: decodeURIComponent }) : {};
85
+ }
86
+ catch {
87
+ return {};
88
+ }
93
89
  };
94
90
  }
95
91
  export class ControllerService extends Controller {
@@ -101,6 +97,8 @@ export class ControllerService extends Controller {
101
97
  try {
102
98
  const currentReq = this.req;
103
99
  const currentRes = this.res;
100
+ if (!currentReq.url)
101
+ throw new Error("Request URL is missing from context");
104
102
  const payload = { req: currentReq, res: currentRes };
105
103
  const convertedPayload = { ...payload, parsedQuerys: this.parsedQuerys(currentReq.url) };
106
104
  if (checks) {
@@ -110,9 +108,9 @@ export class ControllerService extends Controller {
110
108
  }
111
109
  }
112
110
  const logicResult = await logic({ payload, convertedPayload });
113
- if (logicResult.response instanceof Function)
111
+ if (logicResult?.response instanceof Function)
114
112
  return logicResult.response(logicResult.payload);
115
- if (logicResult.message) {
113
+ if (logicResult?.message) {
116
114
  return await this.responseProtocolSwitch({ res: currentRes, resStatus: 400, context: () => new ResponseSchema(logicResult).getResponse });
117
115
  }
118
116
  if (response) {
@@ -122,15 +120,14 @@ export class ControllerService extends Controller {
122
120
  message: Array.isArray(resResult.message)
123
121
  ? responseMessageHelper.successFully(resResult.message[0], resResult.message[1], currentReq.t)
124
122
  : resResult.message,
125
- payload: logicResult.payload
123
+ payload: logicResult?.payload || logicResult
126
124
  };
127
125
  return await this.responseProtocolSwitch({ res: currentRes, resStatus: 200, context: () => new ResponseSchema(successObj).getResponse });
128
126
  }
129
- const finalData = logicResult.payload !== undefined ? logicResult.payload : logicResult;
130
- return finalData && typeof finalData === 'object' ? JSON.parse(JSON.stringify(finalData)) : finalData;
127
+ return currentRes.send(logicResult?.payload !== undefined ? logicResult.payload : logicResult);
131
128
  }
132
129
  catch (error) {
133
- return { status: false, message: error.message };
130
+ return this.res.status(500).send({ status: false, message: error.message });
134
131
  }
135
132
  }
136
133
  }