-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPIStable.ts
More file actions
74 lines (61 loc) · 1.76 KB
/
Copy pathAPIStable.ts
File metadata and controls
74 lines (61 loc) · 1.76 KB
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
import { UserDao } from "@/entities/user"
import ObservableLocalStorage from "@/utils/transform/observableLocalStorage"
import APIDocs from "./data/docs.json"
import API from "../API"
import queryClient from "../client"
import { QueryClientError } from "../QueryError"
import QuerySwagger from "../QuerySwagger"
import { HTTPStatus } from "../types"
export const APIDocsSwagger = new QuerySwagger(APIDocs)
export const StreamAPI = new API({
baseURL: import.meta.env.VITE_API_HOST,
queryClient: queryClient,
swagger: APIDocsSwagger,
options: {
endpoint: {
includeVersion: true,
includeTrailingSlash: true
},
request: {
security: {
tokens: [{
type: "JWT",
header: "Authorization",
read: () => ObservableLocalStorage.getItem("user-token"),
async refresh(expiredToken) {
const response = await this.fetch.POST["/auth/refresh-token"]({
body: { accessToken: expiredToken },
noSecurity: true
})
const newToken = response.payload.accessToken
ObservableLocalStorage.setItem("user-token", newToken)
return newToken
}
}]
}
},
response: {
statusCodeFromPayload(payload) {
if ("error" in payload) {
return HTTPStatus.BadRequest
}
return HTTPStatus.OK
}
},
debug: {
enabled: import.meta.env.MODE === "development",
mock: "auto"
}
},
default: {
headers: {
"Content-Type": "application/json",
"Accept": "application/json"
}
}
})
StreamAPI.on("error", (action, request, error) => {
if (error instanceof QueryClientError === false) return
if (error.status !== 401) return
void UserDao.logOut()
})