1
1
import { ECONNREFUSED } from 'constants' ;
2
2
import { Injectable , Logger } from '@nestjs/common' ;
3
3
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' ;
10
5
11
6
@Injectable ( )
12
7
export class HttpService {
@@ -19,18 +14,9 @@ export class HttpService {
19
14
timeout : 5000 ,
20
15
} ) ;
21
16
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
-
31
17
instance . interceptors . request . use (
32
18
// Do something before request is sent
33
- ( config : InternalAxiosRequestConfig ) => {
19
+ ( config ) => {
34
20
return config ;
35
21
} ,
36
22
// Do something with request error
@@ -47,8 +33,8 @@ export class HttpService {
47
33
return response ;
48
34
} ,
49
35
// 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 )
52
38
throw NormalException . HTTP_REQUEST_TIMEOUT ( ) ;
53
39
54
40
if ( error ?. response ?. data ) this . logger . debug ( error . response . data ) ;
@@ -68,42 +54,42 @@ export class HttpService {
68
54
url : string ,
69
55
config ?: AxiosRequestConfig < C >
70
56
) : Promise < T > {
71
- return this . instance . get ( url , config ) ;
57
+ return ( await this . instance . get ( url , config ) ) ?. data ;
72
58
}
73
59
74
60
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 ;
76
62
}
77
63
78
64
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 ;
80
66
}
81
67
82
68
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 ;
84
70
}
85
71
86
72
async post < T = any , D = any > (
87
73
url : string ,
88
74
data ?: D ,
89
75
config ?: AxiosRequestConfig
90
76
) : Promise < T > {
91
- return this . instance . post ( url , data , config ) ;
77
+ return ( await this . instance . post ( url , data , config ) ) ?. data ;
92
78
}
93
79
94
80
async put < T = any , D = any > (
95
81
url : string ,
96
82
data ?: D ,
97
83
config ?: AxiosRequestConfig
98
84
) : Promise < T > {
99
- return this . instance . put ( url , data , config ) ;
85
+ return ( await this . instance . put ( url , data , config ) ) ?. data ;
100
86
}
101
87
102
88
async patch < T = any , D = any > (
103
89
url : string ,
104
90
data ?: D ,
105
91
config ?: AxiosRequestConfig
106
92
) : Promise < T > {
107
- return this . instance . patch ( url , data , config ) ;
93
+ return ( await this . instance . patch ( url , data , config ) ) ?. data ;
108
94
}
109
95
}
0 commit comments