Skip to content

Commit abe3603

Browse files
committed
fix: removed http service transformer
the service should not auto extract 'data' field since some case we have to check status code
1 parent e4ec4c5 commit abe3603

File tree

1 file changed

+11
-25
lines changed

1 file changed

+11
-25
lines changed

src/modules/http/http.service.ts

+11-25
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
import { ECONNREFUSED } from 'constants';
22
import { Injectable, Logger } from '@nestjs/common';
33
import { NormalException } from '@/exception';
4-
import axios, {
5-
AxiosError,
6-
AxiosInstance,
7-
AxiosRequestConfig,
8-
InternalAxiosRequestConfig,
9-
} from 'axios';
4+
import axios, { AxiosError, AxiosInstance, AxiosRequestConfig } from 'axios';
105

116
@Injectable()
127
export class HttpService {
@@ -19,18 +14,9 @@ export class HttpService {
1914
timeout: 5000,
2015
});
2116

22-
instance.defaults.transformResponse = (response: string) => {
23-
try {
24-
const parsedRes = JSON.parse(response);
25-
return parsedRes.data || parsedRes || {};
26-
} catch (error) {
27-
return response;
28-
}
29-
};
30-
3117
instance.interceptors.request.use(
3218
// Do something before request is sent
33-
(config: InternalAxiosRequestConfig) => {
19+
(config) => {
3420
return config;
3521
},
3622
// Do something with request error
@@ -47,8 +33,8 @@ export class HttpService {
4733
return response;
4834
},
4935
// Any status codes that falls outside the range of 2xx cause this function to trigger
50-
(error: AxiosError & { errno?: number }) => {
51-
if (error?.errno === ECONNREFUSED * -1)
36+
(error: AxiosError) => {
37+
if ((error as any)?.errno === ECONNREFUSED * -1)
5238
throw NormalException.HTTP_REQUEST_TIMEOUT();
5339

5440
if (error?.response?.data) this.logger.debug(error.response.data);
@@ -68,42 +54,42 @@ export class HttpService {
6854
url: string,
6955
config?: AxiosRequestConfig<C>
7056
): Promise<T> {
71-
return this.instance.get(url, config);
57+
return (await this.instance.get(url, config))?.data;
7258
}
7359

7460
async head<T = any>(url: string, config?: AxiosRequestConfig): Promise<T> {
75-
return this.instance.head(url, config);
61+
return (await this.instance.head(url, config))?.data;
7662
}
7763

7864
async delete<T = any>(url: string, config?: AxiosRequestConfig): Promise<T> {
79-
return this.instance.delete(url, config);
65+
return (await this.instance.delete(url, config))?.data;
8066
}
8167

8268
async options<T = any>(url: string, config?: AxiosRequestConfig): Promise<T> {
83-
return this.instance.options(url, config);
69+
return (await this.instance.options(url, config))?.data;
8470
}
8571

8672
async post<T = any, D = any>(
8773
url: string,
8874
data?: D,
8975
config?: AxiosRequestConfig
9076
): Promise<T> {
91-
return this.instance.post(url, data, config);
77+
return (await this.instance.post(url, data, config))?.data;
9278
}
9379

9480
async put<T = any, D = any>(
9581
url: string,
9682
data?: D,
9783
config?: AxiosRequestConfig
9884
): Promise<T> {
99-
return this.instance.put(url, data, config);
85+
return (await this.instance.put(url, data, config))?.data;
10086
}
10187

10288
async patch<T = any, D = any>(
10389
url: string,
10490
data?: D,
10591
config?: AxiosRequestConfig
10692
): Promise<T> {
107-
return this.instance.patch(url, data, config);
93+
return (await this.instance.patch(url, data, config))?.data;
10894
}
10995
}

0 commit comments

Comments
 (0)