@xrystal/core 3.25.7 → 3.26.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.25.7",
4
+ "version": "3.26.1",
5
5
  "description": "Project core for xrystal",
6
6
  "publishConfig": {
7
7
  "access": "public",
@@ -30,6 +30,8 @@ export declare const controllerContextStorage: AsyncLocalStorage<{
30
30
  _statusCode?: number;
31
31
  _contentType?: string;
32
32
  _isRaw?: boolean;
33
+ _parsedBody?: any;
34
+ _parsedQuery?: any;
33
35
  };
34
36
  }>;
35
37
  export declare const getControllerCtx: () => {
@@ -43,6 +45,8 @@ export declare const getControllerCtx: () => {
43
45
  _statusCode?: number;
44
46
  _contentType?: string;
45
47
  _isRaw?: boolean;
48
+ _parsedBody?: any;
49
+ _parsedQuery?: any;
46
50
  };
47
51
  };
48
52
  export declare abstract class BaseController {
@@ -61,6 +65,8 @@ export declare abstract class BaseController {
61
65
  _statusCode?: number;
62
66
  _contentType?: string;
63
67
  _isRaw?: boolean;
68
+ _parsedBody?: any;
69
+ _parsedQuery?: any;
64
70
  };
65
71
  };
66
72
  protected get req(): CustomRequest;
@@ -20,11 +20,14 @@ export class BaseController {
20
20
  const ctx = store?.ctx || {};
21
21
  const req = store?.req || {};
22
22
  const identityT = (k) => k;
23
- const body = ctx.body || req.body || ctx.request?.body || {};
24
- let query = ctx.query || req.query || {};
23
+ // Önce store içindeki parse edilmiş veriye bak, yoksa orijinaline git
24
+ const body = store?.metadata?._parsedBody || ctx.body || req.body || ctx.request?.body || {};
25
+ let query = store?.metadata?._parsedQuery || ctx.query || req.query || ctx.request?.query || {};
25
26
  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]);
27
+ if ((!query || Object.keys(query).length === 0) && typeof urlStr === 'string' && urlStr.includes('?')) {
28
+ const parts = urlStr.split('?');
29
+ if (parts[1])
30
+ query = qs.parse(parts[1]);
28
31
  }
29
32
  const params = {
30
33
  ...(req.params || {}),
@@ -37,7 +40,7 @@ export class BaseController {
37
40
  method: ctx.method || req.method || ctx.request?.method || '',
38
41
  headers: ctx.headers || req.headers || ctx.request?.headers || {},
39
42
  body,
40
- query,
43
+ query: query || {},
41
44
  params,
42
45
  lang: ctx.lang || req.lang || 'en',
43
46
  t: ctx.t || req.t || identityT,
@@ -104,77 +107,98 @@ export default class Controller extends BaseController {
104
107
  return isError ? responseMessageHelper.unsuccessful(p1, p2, t) : responseMessageHelper.successfully(p1, p2, t);
105
108
  }
106
109
  async schema({ checks, logic, response }) {
107
- const currentReq = this.req;
108
110
  const currentRes = this.res;
109
111
  const store = this.currentStore;
110
112
  try {
111
- let parsedBody = currentReq.body;
112
113
  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) { }
114
+ let parsedBody = this.req.body;
115
+ let parsedQuery = this.req.query;
116
+ const contentType = (this.req.headers['content-type'] || '').toLowerCase();
117
+ const consumeBody = async (bodySource) => {
118
+ let text = '';
119
+ if (bodySource instanceof ReadableStream || (bodySource && typeof bodySource.getReader === 'function')) {
120
+ text = await new Response(bodySource).text();
121
+ }
122
+ else if (typeof bodySource === 'string') {
123
+ text = bodySource;
124
+ }
125
+ else {
126
+ return bodySource;
127
+ }
128
+ if (!text)
129
+ return {};
130
+ if (contentType.includes('application/json')) {
131
+ try {
132
+ return JSON.parse(text);
122
133
  }
123
- else if (contentType.includes('application/x-www-form-urlencoded')) {
124
- parsedBody = qs.parse(parsedBody);
134
+ catch {
135
+ return text;
125
136
  }
126
137
  }
127
- else if (typeof rawReq.json === 'function') {
138
+ else if (contentType.includes('application/x-www-form-urlencoded')) {
139
+ return qs.parse(text);
140
+ }
141
+ else {
128
142
  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);
143
+ return JSON.parse(text);
144
+ }
145
+ catch {
146
+ try {
147
+ return qs.parse(text);
148
+ }
149
+ catch {
150
+ return text;
133
151
  }
134
- else {
152
+ }
153
+ }
154
+ };
155
+ if (this.protocol !== ProtocolEnum.WEBSOCKET) {
156
+ const isStream = parsedBody instanceof ReadableStream || (parsedBody && typeof parsedBody.getReader === 'function');
157
+ const isProcessed = !isStream && parsedBody && typeof parsedBody === 'object' && Object.keys(parsedBody).length > 0 && parsedBody.constructor?.name !== 'ReadableStream';
158
+ if (!isProcessed) {
159
+ if (isStream || typeof parsedBody === 'string') {
160
+ parsedBody = await consumeBody(parsedBody);
161
+ }
162
+ else if (rawReq) {
163
+ if (typeof rawReq.json === 'function') {
135
164
  try {
136
- parsedBody = JSON.parse(text);
165
+ const cloned = rawReq.clone ? rawReq.clone() : rawReq;
166
+ parsedBody = await consumeBody(await cloned.text());
137
167
  }
138
168
  catch {
139
- parsedBody = qs.parse(text);
169
+ parsedBody = {};
140
170
  }
141
171
  }
142
- }
143
- catch (e) {
144
- parsedBody = {};
172
+ else if (typeof rawReq.on === 'function') {
173
+ parsedBody = await new Promise((resolve) => {
174
+ let bodyStr = '';
175
+ rawReq.on('data', (chunk) => { bodyStr += chunk; });
176
+ rawReq.on('end', async () => resolve(await consumeBody(bodyStr)));
177
+ rawReq.on('error', () => resolve({}));
178
+ });
179
+ }
145
180
  }
146
181
  }
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
- });
182
+ }
183
+ else if (typeof parsedBody === 'string') {
184
+ try {
185
+ parsedBody = JSON.parse(parsedBody);
172
186
  }
187
+ catch { }
173
188
  }
174
- else if (parsedBody && typeof parsedBody === 'object' && parsedBody.constructor?.name === 'FormData') {
189
+ if (parsedBody && typeof parsedBody === 'object' && parsedBody.constructor?.name === 'FormData') {
175
190
  parsedBody = Object.fromEntries(parsedBody.entries());
176
191
  }
177
- currentReq.body = parsedBody || {};
192
+ const finalBody = parsedBody || {};
193
+ const finalQuery = parsedQuery || {};
194
+ // readonly hatasını önlemek için asıl objeyi değil metadata'yı güncelliyoruz
195
+ if (store) {
196
+ if (!store.metadata)
197
+ store.metadata = { locals: {} };
198
+ store.metadata._parsedBody = finalBody;
199
+ store.metadata._parsedQuery = finalQuery;
200
+ }
201
+ const currentReq = this.req;
178
202
  const p = { req: currentReq, res: currentRes, body: currentReq.body, query: currentReq.query, params: currentReq.params, t: currentReq.t, accounts: currentReq.accounts };
179
203
  const extractMeta = (obj) => {
180
204
  if (!obj || typeof obj !== 'object')
@@ -257,7 +281,7 @@ export default class Controller extends BaseController {
257
281
  }
258
282
  catch (error) {
259
283
  this.logger?.log(LoggerLayerEnum.ERROR, `Controller Error: ${error.message}`);
260
- const t = currentReq?.t || ((k) => k);
284
+ const t = this.req?.t || ((k) => k);
261
285
  return this.res.status(500).send(new ResponseSchema({ status: false, message: this.parseMessage(error.message, t, true), code: 500 }).getResponse);
262
286
  }
263
287
  }