-
Notifications
You must be signed in to change notification settings - Fork 220
/
Copy pathmakeApiRequest.ts
44 lines (41 loc) · 1.12 KB
/
makeApiRequest.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
38
39
40
41
42
43
44
import type { AxiosRequestConfig } from 'axios';
import { httpClient } from './httpClient';
export function makeApiRequest<T>(
urlOrConfig: string | AxiosRequestConfig
): Promise<T> {
const config: AxiosRequestConfig =
typeof urlOrConfig === 'string' ? { url: urlOrConfig } : urlOrConfig;
return httpClient
.request(config)
.then((response) => {
const token = response.headers.authorization;
if (token) {
sessionStorage.setItem('token', token);
}
return response.data;
})
.catch((error) => {
switch (error.response.status) {
case 401:
sessionStorage.clear();
localStorage.clear();
return {
...error,
errorText:
'Authentication failed, please check your credentials and try again'
};
break;
case 409:
return {
...error,
errorText: 'User already exists'
};
break;
default:
return {
...error,
errorText: 'Something went wrong. Please try again later'
};
}
});
}