@@ -33,60 +33,55 @@ function getCorrectUrl(baseURL: string | undefined, url: string | undefined, par
33
33
34
34
export default function axiosAdapter < T > ( ) {
35
35
return async ( config : AxiosRequestConfig ) : Promise < AxiosResponse < T > > => {
36
- try {
37
-
38
- // Use filter to help remove undefined headers
39
- const headers = Object . entries ( config . headers || { } )
40
- . filter ( ( [ , value ] ) => value !== undefined ) ;
41
-
42
- const requestData = {
43
- method : config . method ?. toUpperCase ( ) ,
44
- url : getCorrectUrl ( config . baseURL , config . url , config . params ) ,
45
- headers,
46
- data : getCorrectBodyType ( config . data ) ,
47
- } ;
48
-
49
- const rid = await invoke < number > ( "plugin:http|fetch" , requestData ) ;
50
-
51
- interface FetchSendResponse {
52
- status : number ;
53
- statusText : string ;
54
- headers : [ [ string , string ] ] ;
55
- url : string ;
56
- }
57
-
58
- const response = await invoke < FetchSendResponse > ( "plugin:http|fetch_send" , { rid} ) ;
36
+ // Use filter to help remove undefined headers
37
+ const headers = Object . entries ( config . headers || { } )
38
+ . filter ( ( [ , value ] ) => value !== undefined ) ;
39
+
40
+ const requestData = {
41
+ method : config . method ?. toUpperCase ( ) ,
42
+ url : getCorrectUrl ( config . baseURL , config . url , config . params ) ,
43
+ headers,
44
+ data : getCorrectBodyType ( config . data ) ,
45
+ } ;
46
+
47
+ const rid = await invoke < number > ( "plugin:http|fetch" , requestData ) ;
48
+
49
+ interface FetchSendResponse {
50
+ status : number ;
51
+ statusText : string ;
52
+ headers : [ [ string , string ] ] ;
53
+ url : string ;
54
+ }
59
55
60
- const body = await invoke < number [ ] > ( "plugin:http|fetch_read_body " , { rid} ) ;
56
+ const response = await invoke < FetchSendResponse > ( "plugin:http|fetch_send " , { rid} ) ;
61
57
62
- const isOk = response . status >= 200 && response . status < 300 ;
58
+ const body = await invoke < number [ ] > ( "plugin:http|fetch_read_body" , { rid } ) ;
63
59
64
- let stringData = new TextDecoder ( ) . decode ( new Uint8Array ( body ) ) ;
60
+ const isOk = response . status >= 200 && response . status < 300 ;
65
61
66
- let data ;
67
- try {
68
- data = ( config . responseType === 'json' || config . responseType === undefined ) ? JSON . parse ( stringData ) : stringData ;
69
- } catch ( e ) {
70
- data = new TextDecoder ( ) . decode ( new Uint8Array ( body ) ) ;
71
- }
62
+ let stringData = new TextDecoder ( ) . decode ( new Uint8Array ( body ) ) ;
72
63
73
- const axiosResponse : AxiosResponse < T > = {
74
- data,
75
- status : response . status ,
76
- statusText : getReasonPhrase ( response . status ) ,
77
- headers : Object . fromEntries ( response . headers ) ,
78
- config : config as InternalAxiosRequestConfig
79
- } ;
64
+ let data ;
65
+ try {
66
+ data = ( config . responseType === 'json' || config . responseType === undefined ) ? JSON . parse ( stringData ) : stringData ;
67
+ } catch ( e ) {
68
+ data = new TextDecoder ( ) . decode ( new Uint8Array ( body ) ) ;
69
+ }
80
70
81
- if ( isOk ) {
82
- return axiosResponse ;
83
- } else {
84
- const code = [ AxiosError . ERR_BAD_REQUEST , AxiosError . ERR_BAD_RESPONSE ] [ Math . floor ( response . status / 100 ) - 4 ] ;
85
- const message = 'Request failed with status code ' + axiosResponse . status ;
86
- throw new AxiosError ( message , code , config as InternalAxiosRequestConfig , requestData , axiosResponse ) ;
87
- }
88
- } catch ( error ) {
89
- throw error ;
71
+ const axiosResponse : AxiosResponse < T > = {
72
+ data,
73
+ status : response . status ,
74
+ statusText : getReasonPhrase ( response . status ) ,
75
+ headers : Object . fromEntries ( response . headers ) ,
76
+ config : config as InternalAxiosRequestConfig
77
+ } ;
78
+
79
+ if ( isOk ) {
80
+ return axiosResponse ;
81
+ } else {
82
+ const code = [ AxiosError . ERR_BAD_REQUEST , AxiosError . ERR_BAD_RESPONSE ] [ Math . floor ( response . status / 100 ) - 4 ] ;
83
+ const message = 'Request failed with status code ' + axiosResponse . status ;
84
+ throw new AxiosError ( message , code , config as InternalAxiosRequestConfig , requestData , axiosResponse ) ;
90
85
}
91
86
}
92
87
}
0 commit comments