-
Notifications
You must be signed in to change notification settings - Fork 218
/
Copy pathmakeRequest.ts
36 lines (28 loc) · 959 Bytes
/
makeRequest.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
import { configContext } from "./configContext";
type CustomError = Error & {
status: number
}
const getBaseURL = (): string => {
const { apiURL } = configContext();
const BASE_URI = apiURL || (process.env.NODE_ENV === "production" ? 'https://chat-e2ee-2.azurewebsites.net' : '');
return BASE_URI;
}
const makeRequest = async (url: string, { method = 'GET', body }: { method: string, body?: any }) => {
const res = await window.fetch(`${getBaseURL()}/api/${url}`, {
method,
headers: {
'Content-Type': 'application/json'
},
...(body && { body: JSON.stringify(body) })
});
if (!res.ok) {
const json = res.headers.get('Content-Type').includes('application/json')
? await res.json()
: await res.text();
const err = new Error(json.message || json.error || JSON.stringify(json)) as CustomError;
err.status = res.status;
throw err;
}
return await res.json();
};
export default makeRequest;