1
1
/* eslint-disable no-console */
2
2
import axios , {
3
- Method , AxiosResponse , AxiosRequestConfig , AxiosError ,
3
+ Method ,
4
+ AxiosResponse ,
5
+ AxiosRequestConfig ,
6
+ AxiosError ,
4
7
} from 'axios' ;
5
8
import axiosRetry from 'axios-retry' ;
6
9
import querystring from 'querystring' ;
@@ -11,32 +14,35 @@ import { Query } from './api-diff/query';
11
14
import config from './config' ;
12
15
13
16
type AxiosMetadata = {
14
- startTime : number
15
- }
17
+ startTime : number ;
18
+ } ;
16
19
17
20
type WithAxiosMetadata = {
18
- metadata : AxiosMetadata
19
- }
21
+ metadata : AxiosMetadata ;
22
+ } ;
20
23
21
- export type AxiosResponseWithDuration =
22
- AxiosResponse & { config : WithAxiosMetadata } & { duration : number }
24
+ export type AxiosResponseWithDuration = AxiosResponse & {
25
+ config : WithAxiosMetadata ;
26
+ } & { duration : number } ;
23
27
24
28
// Response time middleware. Tracks the duration of the axios request/response
25
- axios . interceptors . request . use ( ( axiosConfig : AxiosRequestConfig & WithAxiosMetadata ) => {
26
- // eslint-disable-next-line no-param-reassign
27
- axiosConfig . metadata = { startTime : Date . now ( ) } ;
28
- return axiosConfig ;
29
- } ) ;
29
+ axios . interceptors . request . use (
30
+ ( axiosConfig : AxiosRequestConfig & WithAxiosMetadata ) => {
31
+ // eslint-disable-next-line no-param-reassign
32
+ axiosConfig . metadata = { startTime : Date . now ( ) } ;
33
+ return axiosConfig ;
34
+ } ,
35
+ ) ;
30
36
axios . interceptors . response . use ( ( response : AxiosResponseWithDuration ) => {
31
37
response . duration = Date . now ( ) - response . config . metadata . startTime ;
32
38
return response ;
33
39
} ) ;
34
40
35
41
type RunQueryOptions = {
36
42
/** Request timeout in milliseconds */
37
- timeout : number ,
38
- retries : number ,
39
- }
43
+ timeout : number ;
44
+ retries : number ;
45
+ } ;
40
46
41
47
/**
42
48
* Run one query against specified apiEnv
@@ -65,7 +71,9 @@ export default async function runQuery(
65
71
} ;
66
72
67
73
if ( config . authStyle === 'header' ) {
68
- headers . Authorization = [ config . authType , apiEnv . key ] . filter ( ( u ) => ! _ . isEmpty ( u ) ) . join ( ' ' ) ;
74
+ headers . Authorization = [ config . authType , apiEnv . key ]
75
+ . filter ( ( u ) => ! _ . isEmpty ( u ) )
76
+ . join ( ' ' ) ;
69
77
} else if ( config . authStyle === 'param' ) {
70
78
params [ config . authParam ] = apiEnv . key ;
71
79
}
@@ -74,7 +82,10 @@ export default async function runQuery(
74
82
const response = await axios ( url , {
75
83
headers,
76
84
params : method === 'GET' ? params : undefined ,
77
- data : method === 'POST' ? params : undefined ,
85
+ data :
86
+ method === 'POST' || method === 'PUT' || method === 'UPDATE'
87
+ ? params
88
+ : undefined ,
78
89
method : method as Method ,
79
90
timeout,
80
91
} ) ;
@@ -83,12 +94,16 @@ export default async function runQuery(
83
94
if ( error . response ) {
84
95
// The request was made and the server responded with a status code
85
96
// that falls out of the range of 2xx
86
- console . error ( `Got error code: ${ error . response . status } for ${ error . response . request ?. res . responseUrl } ` ) ;
97
+ console . error (
98
+ `Got error code: ${ error . response . status } for ${ error . response . request ?. res . responseUrl } ` ,
99
+ ) ;
87
100
return error . response ;
88
101
}
89
102
if ( error . request ) {
90
103
const axiosError = error as AxiosError < any > ;
91
- console . error ( `Error ${ axiosError . code } on ${ url } ?${ querystring . stringify ( params ) } ` ) ;
104
+ console . error (
105
+ `Error ${ axiosError . code } on ${ url } ?${ querystring . stringify ( params ) } ` ,
106
+ ) ;
92
107
93
108
// likely a timeout, don't throw, keep soldiering on
94
109
return {
0 commit comments