-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathcallApi.ts
37 lines (32 loc) · 1.05 KB
/
callApi.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { camelizeKeys } from 'humps';
import 'isomorphic-fetch';
import { startsWith } from 'lodash';
import { kfetch, KFetchOptions } from 'ui/kfetch';
function fetchOptionsWithDebug(fetchOptions: KFetchOptions) {
const debugEnabled =
sessionStorage.getItem('apm_debug') === 'true' &&
startsWith(fetchOptions.pathname, '/api/apm');
if (!debugEnabled) {
return fetchOptions;
}
return {
...fetchOptions,
query: {
...fetchOptions.query,
_debug: true
}
};
}
export async function callApi<T = void>(
fetchOptions: KFetchOptions,
{ camelcase = true, prependBasePath = true } = {}
): Promise<T> {
const combinedFetchOptions = fetchOptionsWithDebug(fetchOptions);
const res = await kfetch(combinedFetchOptions, { prependBasePath });
return camelcase ? camelizeKeys(res) : res;
}