Skip to content

Commit 89c267e

Browse files
committed
fix: resolved merge conflicts [SPRW-1794]
2 parents 8b4a859 + 3a92bfe commit 89c267e

File tree

1 file changed

+53
-31
lines changed

1 file changed

+53
-31
lines changed

src/proxy/http/http.service.ts

Lines changed: 53 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import * as ipaddr from 'ipaddr.js';
99
export class HttpService {
1010
constructor(private readonly httpService: NestHttpService) {}
1111

12-
private base64ToBuffer(base64: string): { buffer: Buffer; mime: string } {
12+
private base64ToBuffer(base64: string): { buffer: Buffer; mime: string } {
1313
const arr = base64.split(',');
1414
const mime = arr[0].match(/:(.*?);/)?.[1];
1515
const bstr = Buffer.from(arr[1], 'base64');
@@ -23,7 +23,7 @@ export class HttpService {
2323
101: 'Switching Protocols',
2424
102: 'Processing',
2525
103: 'Early Hints',
26-
26+
2727
// 2xx Success
2828
200: 'OK',
2929
201: 'Created',
@@ -35,7 +35,7 @@ export class HttpService {
3535
207: 'Multi-Status',
3636
208: 'Already Reported',
3737
226: 'IM Used',
38-
38+
3939
// 3xx Redirection
4040
300: 'Multiple Choices',
4141
301: 'Moved Permanently',
@@ -45,7 +45,7 @@ export class HttpService {
4545
305: 'Use Proxy',
4646
307: 'Temporary Redirect',
4747
308: 'Permanent Redirect',
48-
48+
4949
// 4xx Client Errors
5050
400: 'Bad Request',
5151
401: 'Unauthorized',
@@ -76,7 +76,7 @@ export class HttpService {
7676
429: 'Too Many Requests',
7777
431: 'Request Header Fields Too Large',
7878
451: 'Unavailable For Legal Reasons',
79-
79+
8080
// 5xx Server Errors
8181
500: 'Internal Server Error',
8282
501: 'Not Implemented',
@@ -89,9 +89,9 @@ export class HttpService {
8989
508: 'Loop Detected',
9090
509: 'Bandwidth Limit Exceeded',
9191
510: 'Not Extended',
92-
511: 'Network Authentication Required'
92+
511: 'Network Authentication Required',
9393
};
94-
94+
9595
return statusMap[statusCode] || 'Unknown Status';
9696
}
9797

@@ -165,7 +165,22 @@ export class HttpService {
165165
try {
166166
switch (contentType) {
167167
case 'application/json':
168-
config.data = typeof body === 'string' ? JSON.parse(body) : body;
168+
if (typeof body === 'string') {
169+
// Check if the body is a numeric string
170+
const isNumeric = !isNaN(body as any) && !isNaN(parseFloat(body));
171+
if (isNumeric) {
172+
config.data = body; // Keep numeric string as is
173+
} else {
174+
// Try parsing as JSON only if it's not a numeric string
175+
try {
176+
config.data = JSON.parse(body);
177+
} catch (e) {
178+
config.data = body; // If parsing fails, use the original string
179+
}
180+
}
181+
} else {
182+
config.data = body;
183+
}
169184
break;
170185

171186
case 'application/x-www-form-urlencoded':
@@ -180,7 +195,7 @@ export class HttpService {
180195
// Filter and transform the body into key-value pairs
181196
const formBody: Record<string, string> = {};
182197
formParsedBody.forEach((item: any) => {
183-
formBody[item.key] = item.value;
198+
formBody[item.key] = item.value;
184199
});
185200

186201
const formUrlEncoded = new URLSearchParams(formBody);
@@ -195,14 +210,17 @@ export class HttpService {
195210
typeof body === 'string' ? JSON.parse(body) : body;
196211
if (Array.isArray(parsedBody)) {
197212
for (const field of parsedBody || []) {
198-
try{
213+
try {
199214
if (field?.base) {
200215
const { buffer, mime } = this.base64ToBuffer(field.base);
201-
formData.append(field.key, buffer, { filename: field.value, contentType: mime });
202-
}else {
216+
formData.append(field.key, buffer, {
217+
filename: field.value,
218+
contentType: mime,
219+
});
220+
} else {
203221
formData.append(field.key, field.value);
204222
}
205-
}catch(e){
223+
} catch (e) {
206224
formData.append(field.key, field.value);
207225
}
208226
}
@@ -258,40 +276,44 @@ export class HttpService {
258276
});
259277

260278
let contentType = response.headers['content-type'];
261-
let responseData = "";
279+
let responseData = '';
262280
if (contentType?.startsWith('image/')) {
263281
const base64 = Buffer.from(response.data).toString('base64');
264282
responseData = `data:${contentType};base64,${base64}`;
265283
} else {
266-
responseData = Buffer.from(response.data).toString('utf-8');
284+
responseData = Buffer.from(response.data).toString('utf-8');
267285
}
268286

269287
return {
270-
status: response.status + ' ' + (response.statusText || this.getStatusText(response.status)),
288+
status:
289+
response.status +
290+
' ' +
291+
(response.statusText || this.getStatusText(response.status)),
271292
data: `${responseData}`,
272293
headers: response.headers,
273294
};
274295
} catch (axiosError: any) {
275-
try{
276-
const responseData = Buffer.from(axiosError.response?.data).toString('utf-8');
277-
return {
278-
status: axiosError.response?.status
279-
? axiosError.response?.status +
280-
' ' +
281-
(axiosError.response?.statusText || this.getStatusText(axiosError.response?.status))
282-
: null,
283-
data: responseData || { message: axiosError.message },
284-
headers: axiosError.response?.headers,
285-
};
286-
287-
}
288-
catch(e){
296+
try {
297+
const responseData = Buffer.from(axiosError.response?.data).toString(
298+
'utf-8',
299+
);
300+
return {
301+
status: axiosError.response?.status
302+
? axiosError.response?.status +
303+
' ' +
304+
(axiosError.response?.statusText ||
305+
this.getStatusText(axiosError.response?.status))
306+
: null,
307+
data: responseData || { message: axiosError.message },
308+
headers: axiosError.response?.headers,
309+
};
310+
} catch (e) {
289311
return {
290312
status: null,
291313
data: { message: axiosError.message },
292314
headers: axiosError.response?.headers,
293315
};
294-
}
316+
}
295317
}
296318
} catch (error: any) {
297319
console.error('HTTP Service Error:', error);

0 commit comments

Comments
 (0)