-
Notifications
You must be signed in to change notification settings - Fork 385
/
httpClient.ts
124 lines (109 loc) · 3.57 KB
/
httpClient.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
import { ErrorUnauthorized } from '@/api/errors/ErrorUnauthorized';
import { useConfigStore } from '@/store/modules/configStore';
import { RouteConfig } from '@/types/router';
/**
* HttpClient is a custom wrapper around fetch api.
* Exposes get, post and delete methods for JSON strings.
*/
export class HttpClient {
/**
*
* @param method holds http method type
* @param path
* @param body serialized JSON
*/
private async sendJSON(method: string, path: string, body: string | null): Promise<Response> {
const request: RequestInit = {
method: method,
body: body,
};
request.headers = {
'Content-Type': 'application/json',
};
const response = await fetch(path, request);
if (response.status === 401) {
await this.handleUnauthorized();
throw new ErrorUnauthorized();
}
return response;
}
/**
* Performs POST http request with JSON body.
* @param path
* @param body serialized JSON
*/
public async post(path: string, body: string | null): Promise<Response> {
return this.sendJSON('POST', path, body);
}
/**
* Performs PATCH http request with JSON body.
* @param path
* @param body serialized JSON
*/
public async patch(path: string, body: string | null): Promise<Response> {
return this.sendJSON('PATCH', path, body);
}
/**
* Performs PUT http request with JSON body.
* @param path
* @param body serialized JSON
*/
public async put(path: string, body: string | null): Promise<Response> {
return this.sendJSON('PUT', path, body);
}
/**
* Performs GET http request.
* @param path
*/
public async get(path: string): Promise<Response> {
return this.sendJSON('GET', path, null);
}
/**
* Performs DELETE http request.
* @param path
* @param body serialized JSON
*/
public async delete(path: string, body: string | null = null): Promise<Response> {
return this.sendJSON('DELETE', path, body);
}
/**
* Handles unauthorized actions.
* Call logout and redirect to login.
*/
private async handleUnauthorized(): Promise<void> {
let path = window.location.href;
if (!this.isAuthRoute(path)) {
try {
const logoutPath = '/api/v0/auth/logout';
const request: RequestInit = {
method: 'POST',
body: null,
};
request.headers = {
'Content-Type': 'application/json',
};
await fetch(logoutPath, request);
// eslint-disable-next-line no-empty
} catch (error) {
}
setTimeout(() => {
// path may have changed after timeout.
path = window.location.href;
if (this.isAuthRoute(path)) {
return;
}
const origin = window.location.origin;
if (document.querySelector('.v-overlay-container')) {
window.location.href = origin + useConfigStore().optionalV2Path + '/login';
return;
}
window.location.href = origin + '/login';
}, 2000);
}
}
private isAuthRoute(path: string): boolean {
return RouteConfig.AuthRoutes.some((route) => path.includes(route));
}
}