Skip to content

Commit ab0f278

Browse files
author
David Blackman
committed
PUT goes in body
1 parent c10196e commit ab0f278

File tree

1 file changed

+34
-19
lines changed

1 file changed

+34
-19
lines changed

src/run-query.ts

+34-19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
/* eslint-disable no-console */
22
import axios, {
3-
Method, AxiosResponse, AxiosRequestConfig, AxiosError,
3+
Method,
4+
AxiosResponse,
5+
AxiosRequestConfig,
6+
AxiosError,
47
} from 'axios';
58
import axiosRetry from 'axios-retry';
69
import querystring from 'querystring';
@@ -11,32 +14,35 @@ import { Query } from './api-diff/query';
1114
import config from './config';
1215

1316
type AxiosMetadata = {
14-
startTime: number
15-
}
17+
startTime: number;
18+
};
1619

1720
type WithAxiosMetadata = {
18-
metadata: AxiosMetadata
19-
}
21+
metadata: AxiosMetadata;
22+
};
2023

21-
export type AxiosResponseWithDuration =
22-
AxiosResponse & {config: WithAxiosMetadata} & { duration: number}
24+
export type AxiosResponseWithDuration = AxiosResponse & {
25+
config: WithAxiosMetadata;
26+
} & { duration: number };
2327

2428
// 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+
);
3036
axios.interceptors.response.use((response: AxiosResponseWithDuration) => {
3137
response.duration = Date.now() - response.config.metadata.startTime;
3238
return response;
3339
});
3440

3541
type RunQueryOptions = {
3642
/** Request timeout in milliseconds */
37-
timeout: number,
38-
retries: number,
39-
}
43+
timeout: number;
44+
retries: number;
45+
};
4046

4147
/**
4248
* Run one query against specified apiEnv
@@ -65,7 +71,9 @@ export default async function runQuery(
6571
};
6672

6773
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(' ');
6977
} else if (config.authStyle === 'param') {
7078
params[config.authParam] = apiEnv.key;
7179
}
@@ -74,7 +82,10 @@ export default async function runQuery(
7482
const response = await axios(url, {
7583
headers,
7684
params: method === 'GET' ? params : undefined,
77-
data: method === 'POST' ? params : undefined,
85+
data:
86+
method === 'POST' || method === 'PUT' || method === 'UPDATE'
87+
? params
88+
: undefined,
7889
method: method as Method,
7990
timeout,
8091
});
@@ -83,12 +94,16 @@ export default async function runQuery(
8394
if (error.response) {
8495
// The request was made and the server responded with a status code
8596
// 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+
);
87100
return error.response;
88101
}
89102
if (error.request) {
90103
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+
);
92107

93108
// likely a timeout, don't throw, keep soldiering on
94109
return {

0 commit comments

Comments
 (0)