@xrystal/core 3.25.4 → 3.25.7

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.4",
4
+ "version": "3.25.7",
5
5
  "description": "Project core for xrystal",
6
6
  "publishConfig": {
7
7
  "access": "public",
@@ -26,13 +26,19 @@ export class BaseController {
26
26
  if (Object.keys(query).length === 0 && typeof urlStr === 'string' && urlStr.includes('?')) {
27
27
  query = qs.parse(urlStr.split('?')[1]);
28
28
  }
29
+ const params = {
30
+ ...(req.params || {}),
31
+ ...(ctx.params || {}),
32
+ ...(ctx.request?.params || {}),
33
+ ...(ctx.request?.routeParams || {})
34
+ };
29
35
  return {
30
36
  url: urlStr,
31
37
  method: ctx.method || req.method || ctx.request?.method || '',
32
38
  headers: ctx.headers || req.headers || ctx.request?.headers || {},
33
39
  body,
34
40
  query,
35
- params: ctx.params || req.params || {},
41
+ params,
36
42
  lang: ctx.lang || req.lang || 'en',
37
43
  t: ctx.t || req.t || identityT,
38
44
  accounts: ctx.accounts || req.accounts || ctx.user
@@ -90,8 +96,6 @@ export default class Controller extends BaseController {
90
96
  payloadString = parts.slice(1).join(' ');
91
97
  }
92
98
  const params = payloadString.split('-').map(p => p.trim()).filter(p => p !== '');
93
- // Kritik Düzeltme: Çiftlemeyi önlemek için parametreleri helper'a t() ile sarmadan gönderiyoruz
94
- // Çünkü helper zaten kendi içinde t() kullanıyor
95
99
  const p1 = params[0] || '';
96
100
  const p2 = params.slice(1).join(' ');
97
101
  if (responseMessageHelper[method]) {
@@ -104,6 +108,73 @@ export default class Controller extends BaseController {
104
108
  const currentRes = this.res;
105
109
  const store = this.currentStore;
106
110
  try {
111
+ let parsedBody = currentReq.body;
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) { }
122
+ }
123
+ else if (contentType.includes('application/x-www-form-urlencoded')) {
124
+ parsedBody = qs.parse(parsedBody);
125
+ }
126
+ }
127
+ else if (typeof rawReq.json === 'function') {
128
+ 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);
133
+ }
134
+ else {
135
+ try {
136
+ parsedBody = JSON.parse(text);
137
+ }
138
+ catch {
139
+ parsedBody = qs.parse(text);
140
+ }
141
+ }
142
+ }
143
+ catch (e) {
144
+ parsedBody = {};
145
+ }
146
+ }
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
+ });
172
+ }
173
+ }
174
+ else if (parsedBody && typeof parsedBody === 'object' && parsedBody.constructor?.name === 'FormData') {
175
+ parsedBody = Object.fromEntries(parsedBody.entries());
176
+ }
177
+ currentReq.body = parsedBody || {};
107
178
  const p = { req: currentReq, res: currentRes, body: currentReq.body, query: currentReq.query, params: currentReq.params, t: currentReq.t, accounts: currentReq.accounts };
108
179
  const extractMeta = (obj) => {
109
180
  if (!obj || typeof obj !== 'object')
@@ -0,0 +1 @@
1
+ export declare const toExpress: (action: () => Promise<any>) => (req: any, res: any, next: any) => Promise<any>;
@@ -0,0 +1,26 @@
1
+ import { controllerContextStorage } from "source/loader/controller";
2
+ import { ProtocolEnum } from "source/utils/models";
3
+ // => Use case - app.post('/example', toExpress(() => exampleCtrl.example()))
4
+ export const toExpress = (action) => async (req, res, next) => {
5
+ try {
6
+ const result = await controllerContextStorage.run({
7
+ req,
8
+ res,
9
+ protocol: ProtocolEnum.HTTP,
10
+ metadata: { locals: res.locals || {} }
11
+ }, action);
12
+ if (result instanceof Response) {
13
+ res.status(result.status);
14
+ result.headers.forEach((v, k) => res.setHeader(k, v));
15
+ const contentType = result.headers.get('content-type') || '';
16
+ if (contentType.includes('application/json') || contentType.includes('text/')) {
17
+ return res.send(await result.text());
18
+ }
19
+ return res.send(Buffer.from(await result.arrayBuffer()));
20
+ }
21
+ return res.send(result);
22
+ }
23
+ catch (e) {
24
+ next(e);
25
+ }
26
+ };