@xrystal/core 3.25.7 → 3.26.0

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.25.7",
4
+ "version": "3.26.0",
5
5
  "description": "Project core for xrystal",
6
6
  "publishConfig": {
7
7
  "access": "public",
@@ -21,10 +21,12 @@ export class BaseController {
21
21
  const req = store?.req || {};
22
22
  const identityT = (k) => k;
23
23
  const body = ctx.body || req.body || ctx.request?.body || {};
24
- let query = ctx.query || req.query || {};
24
+ let query = ctx.query || req.query || ctx.request?.query || {};
25
25
  const urlStr = ctx.url || req.url || ctx.request?.url || '';
26
- if (Object.keys(query).length === 0 && typeof urlStr === 'string' && urlStr.includes('?')) {
27
- query = qs.parse(urlStr.split('?')[1]);
26
+ if ((!query || Object.keys(query).length === 0) && typeof urlStr === 'string' && urlStr.includes('?')) {
27
+ const parts = urlStr.split('?');
28
+ if (parts[1])
29
+ query = qs.parse(parts[1]);
28
30
  }
29
31
  const params = {
30
32
  ...(req.params || {}),
@@ -37,7 +39,7 @@ export class BaseController {
37
39
  method: ctx.method || req.method || ctx.request?.method || '',
38
40
  headers: ctx.headers || req.headers || ctx.request?.headers || {},
39
41
  body,
40
- query,
42
+ query: query || {},
41
43
  params,
42
44
  lang: ctx.lang || req.lang || 'en',
43
45
  t: ctx.t || req.t || identityT,
@@ -104,77 +106,105 @@ export default class Controller extends BaseController {
104
106
  return isError ? responseMessageHelper.unsuccessful(p1, p2, t) : responseMessageHelper.successfully(p1, p2, t);
105
107
  }
106
108
  async schema({ checks, logic, response }) {
107
- const currentReq = this.req;
108
109
  const currentRes = this.res;
109
110
  const store = this.currentStore;
110
111
  try {
111
- let parsedBody = currentReq.body;
112
112
  const rawReq = store?.req || store?.ctx?.request || store?.ctx?.req;
113
- const contentType = (currentReq.headers['content-type'] || '').toLowerCase();
114
- const hasData = parsedBody && typeof parsedBody === 'object' && Object.keys(parsedBody).length > 0;
115
- if (!hasData && this.protocol !== ProtocolEnum.WEBSOCKET && rawReq) {
116
- if (typeof parsedBody === 'string' && parsedBody.length > 0) {
117
- if (contentType.includes('application/json')) {
118
- try {
119
- parsedBody = JSON.parse(parsedBody);
120
- }
121
- catch (e) { }
113
+ let parsedBody = this.req.body;
114
+ let parsedQuery = this.req.query;
115
+ const contentType = (this.req.headers['content-type'] || '').toLowerCase();
116
+ const consumeBody = async (bodySource) => {
117
+ let text = '';
118
+ if (bodySource instanceof ReadableStream || (bodySource && typeof bodySource.getReader === 'function')) {
119
+ text = await new Response(bodySource).text();
120
+ }
121
+ else if (typeof bodySource === 'string') {
122
+ text = bodySource;
123
+ }
124
+ else {
125
+ return bodySource;
126
+ }
127
+ if (!text)
128
+ return {};
129
+ if (contentType.includes('application/json')) {
130
+ try {
131
+ return JSON.parse(text);
122
132
  }
123
- else if (contentType.includes('application/x-www-form-urlencoded')) {
124
- parsedBody = qs.parse(parsedBody);
133
+ catch {
134
+ return text;
125
135
  }
126
136
  }
127
- else if (typeof rawReq.json === 'function') {
137
+ else if (contentType.includes('application/x-www-form-urlencoded')) {
138
+ return qs.parse(text);
139
+ }
140
+ else {
128
141
  try {
129
- const cloned = rawReq.clone ? rawReq.clone() : rawReq;
130
- const text = await cloned.text();
131
- if (contentType.includes('application/x-www-form-urlencoded')) {
132
- parsedBody = qs.parse(text);
142
+ return JSON.parse(text);
143
+ }
144
+ catch {
145
+ try {
146
+ return qs.parse(text);
147
+ }
148
+ catch {
149
+ return text;
133
150
  }
134
- else {
151
+ }
152
+ }
153
+ };
154
+ if (this.protocol !== ProtocolEnum.WEBSOCKET) {
155
+ const isStream = parsedBody instanceof ReadableStream || (parsedBody && typeof parsedBody.getReader === 'function');
156
+ const isProcessed = !isStream && parsedBody && typeof parsedBody === 'object' && Object.keys(parsedBody).length > 0 && parsedBody.constructor?.name !== 'ReadableStream';
157
+ if (!isProcessed) {
158
+ if (isStream || typeof parsedBody === 'string') {
159
+ parsedBody = await consumeBody(parsedBody);
160
+ }
161
+ else if (rawReq) {
162
+ if (typeof rawReq.json === 'function') {
135
163
  try {
136
- parsedBody = JSON.parse(text);
164
+ const cloned = rawReq.clone ? rawReq.clone() : rawReq;
165
+ parsedBody = await consumeBody(await cloned.text());
137
166
  }
138
167
  catch {
139
- parsedBody = qs.parse(text);
168
+ parsedBody = {};
140
169
  }
141
170
  }
142
- }
143
- catch (e) {
144
- parsedBody = {};
171
+ else if (typeof rawReq.on === 'function') {
172
+ parsedBody = await new Promise((resolve) => {
173
+ let bodyStr = '';
174
+ rawReq.on('data', (chunk) => { bodyStr += chunk; });
175
+ rawReq.on('end', async () => resolve(await consumeBody(bodyStr)));
176
+ rawReq.on('error', () => resolve({}));
177
+ });
178
+ }
145
179
  }
146
180
  }
147
- else if (typeof rawReq.on === 'function') {
148
- parsedBody = await new Promise((resolve) => {
149
- let bodyStr = '';
150
- rawReq.on('data', (chunk) => { bodyStr += chunk; });
151
- rawReq.on('end', () => {
152
- try {
153
- if (contentType.includes('application/json'))
154
- resolve(JSON.parse(bodyStr));
155
- else if (contentType.includes('application/x-www-form-urlencoded'))
156
- resolve(qs.parse(bodyStr));
157
- else {
158
- try {
159
- resolve(JSON.parse(bodyStr));
160
- }
161
- catch {
162
- resolve(qs.parse(bodyStr));
163
- }
164
- }
165
- }
166
- catch (e) {
167
- resolve({});
168
- }
169
- });
170
- rawReq.on('error', () => resolve({}));
171
- });
181
+ }
182
+ else if (typeof parsedBody === 'string') {
183
+ try {
184
+ parsedBody = JSON.parse(parsedBody);
172
185
  }
186
+ catch { }
173
187
  }
174
- else if (parsedBody && typeof parsedBody === 'object' && parsedBody.constructor?.name === 'FormData') {
188
+ if (parsedBody && typeof parsedBody === 'object' && parsedBody.constructor?.name === 'FormData') {
175
189
  parsedBody = Object.fromEntries(parsedBody.entries());
176
190
  }
177
- currentReq.body = parsedBody || {};
191
+ const finalBody = parsedBody || {};
192
+ const finalQuery = parsedQuery || {};
193
+ if (store) {
194
+ if (store.ctx) {
195
+ store.ctx.body = finalBody;
196
+ store.ctx.query = finalQuery;
197
+ }
198
+ if (store.req) {
199
+ store.req.body = finalBody;
200
+ store.req.query = finalQuery;
201
+ }
202
+ if (store.ctx?.request) {
203
+ store.ctx.request.body = finalBody;
204
+ store.ctx.request.query = finalQuery;
205
+ }
206
+ }
207
+ const currentReq = this.req;
178
208
  const p = { req: currentReq, res: currentRes, body: currentReq.body, query: currentReq.query, params: currentReq.params, t: currentReq.t, accounts: currentReq.accounts };
179
209
  const extractMeta = (obj) => {
180
210
  if (!obj || typeof obj !== 'object')
@@ -257,7 +287,7 @@ export default class Controller extends BaseController {
257
287
  }
258
288
  catch (error) {
259
289
  this.logger?.log(LoggerLayerEnum.ERROR, `Controller Error: ${error.message}`);
260
- const t = currentReq?.t || ((k) => k);
290
+ const t = this.req?.t || ((k) => k);
261
291
  return this.res.status(500).send(new ResponseSchema({ status: false, message: this.parseMessage(error.message, t, true), code: 500 }).getResponse);
262
292
  }
263
293
  }