@xrystal/core 3.25.5 → 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.5",
4
+ "version": "3.25.7",
5
5
  "description": "Project core for xrystal",
6
6
  "publishConfig": {
7
7
  "access": "public",
@@ -110,35 +110,72 @@ export default class Controller extends BaseController {
110
110
  try {
111
111
  let parsedBody = currentReq.body;
112
112
  const rawReq = store?.req || store?.ctx?.request || store?.ctx?.req;
113
- if (this.protocol === ProtocolEnum.WEBSOCKET && typeof parsedBody === 'string') {
114
- try {
115
- parsedBody = JSON.parse(parsedBody);
116
- }
117
- catch (e) { }
118
- }
119
- else if (rawReq && (parsedBody instanceof ReadableStream || (parsedBody && typeof parsedBody.getReader === 'function'))) {
120
- try {
121
- const cloned = rawReq.clone ? rawReq.clone() : rawReq;
122
- parsedBody = await cloned.json();
123
- }
124
- catch (e) {
125
- try {
126
- parsedBody = await rawReq.text();
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')) {
127
118
  try {
128
119
  parsedBody = JSON.parse(parsedBody);
129
120
  }
130
121
  catch (e) { }
131
122
  }
132
- catch (e2) {
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) {
133
144
  parsedBody = {};
134
145
  }
135
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
+ }
136
173
  }
137
174
  else if (parsedBody && typeof parsedBody === 'object' && parsedBody.constructor?.name === 'FormData') {
138
175
  parsedBody = Object.fromEntries(parsedBody.entries());
139
176
  }
140
- currentReq.body = parsedBody;
141
- const p = { req: currentReq, res: currentRes, body: parsedBody, query: currentReq.query, params: currentReq.params, t: currentReq.t, accounts: currentReq.accounts };
177
+ currentReq.body = parsedBody || {};
178
+ const p = { req: currentReq, res: currentRes, body: currentReq.body, query: currentReq.query, params: currentReq.params, t: currentReq.t, accounts: currentReq.accounts };
142
179
  const extractMeta = (obj) => {
143
180
  if (!obj || typeof obj !== 'object')
144
181
  return;
@@ -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
+ };