-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathapi.ts
201 lines (158 loc) · 6.13 KB
/
api.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import axios from 'axios'
import { matchPath } from 'react-router'
import { paths } from './apiSchema'
import authStore from './authStore'
for (const key of ['REACT_APP_API_URL', 'REACT_APP_API_KEY']) {
if (!process.env[key]) {
throw new Error(`Missing environment variable: ${key}`)
}
}
const instance = axios.create({
baseURL: process.env.REACT_APP_API_URL,
params: {
api_key: process.env.REACT_APP_API_KEY,
},
})
instance.interceptors.request.use((config) => {
const anonymousGetUrls = [
'/types',
'/types/counts',
'/locations',
'/locations/:id',
'/locations/:id/reviews',
'/locations/changes',
'/reviews/:id',
'/clusters',
'/imports',
'/imports/:id',
'/users/:id',
]
const isAnonymous =
config.method === 'get' &&
config.url &&
matchPath(config.url, { path: anonymousGetUrls })
const accessToken = authStore.getAccessToken()
if (accessToken && !isAnonymous) {
config.headers.Authorization = `Bearer ${accessToken}`
}
return config
})
instance.interceptors.response.use(
(response) => response?.data,
async (error) => {
const originalRequest = error.config
if (
error.response &&
error.response.status === 401 &&
error.response.data.error === 'Expired access token' &&
!originalRequest._retry
) {
const refreshToken = authStore.getRefreshToken()
if (refreshToken) {
originalRequest._retry = true
const newToken = await refreshUserToken(refreshToken)
authStore.setToken(newToken)
return instance(originalRequest)
}
}
if (error?.response?.data?.error) {
throw { ...error, message: error.response.data.error }
} else {
throw error
}
},
)
export const addUser = (
data: paths['/user']['post']['requestBody']['content']['application/json'],
) => instance.post('/user', data)
export const editUser = (
data: paths['/user']['put']['requestBody']['content']['application/json'],
) => instance.put('/user', data)
export const getUser = (accessToken: string) =>
instance.get('/user', {
headers: { Authorization: `Bearer ${accessToken}` },
})
export const deleteUser = () => instance.delete('/user')
export const confirmUser = (token: string) =>
instance.post('/user/confirmation', { token })
export const requestConfirmUser = (data: any) =>
instance.post('/user/confirmation/retry', data)
export const resetPassword = (data: any) => instance.put('/user/password', data)
export const requestResetPassword = (data: any) =>
instance.post('/user/password/reset', data)
export const getUserToken = (username: string, password: string) => {
const formData = new FormData()
formData.append('username', username)
formData.append('password', password)
return instance.post('/user/token', formData)
}
export const refreshUserToken = (refreshToken: string) => {
const formData = new FormData()
formData.append('refresh_token', refreshToken)
return instance.post('/user/token/refresh', formData)
}
export const getClusters = (
params: paths['/clusters']['get']['parameters']['query'],
) => instance.get('/clusters', { params })
export const getLocations = (
params: paths['/locations']['get']['parameters']['query'],
) => instance.get('/locations', { params })
export const getLocationsCount = (
params: paths['/locations/count']['get']['parameters']['query'],
) => instance.get('/locations/count', { params })
export const addLocation = (
data: paths['/locations']['post']['requestBody']['content']['application/json'],
) => instance.post('/locations', data)
export const getLocationById = (
id: paths['/locations/{id}']['get']['parameters']['path']['id'],
embed: paths['/locations/{id}']['get']['parameters']['query']['embed'],
) => instance.get(`/locations/${id}`, { params: { embed } })
export const editLocation = (
id: paths['/locations/{id}']['put']['parameters']['path']['id'],
data: paths['/locations/{id}']['put']['requestBody']['content']['application/json'],
) => instance.put(`/locations/${id}`, data)
export const getLocationsChanges = (
params: paths['/locations/changes']['get']['parameters']['query'],
) => instance.get('/locations/changes', { params })
export const getTypes = () => instance.get('/types')
export const getTypeCounts = (
params: paths['/types/counts']['get']['parameters']['query'],
) => instance.get('/types/counts', { params })
export const getTypeById = (
id: paths['/types/{id}']['get']['parameters']['path']['id'],
) => instance.get(`/types/${id}`)
export const addType = (
data: paths['/types']['post']['requestBody']['content']['application/json'],
) => instance.post('/types', data)
export const getReviews = (
locationId: paths['/locations/{id}/reviews']['get']['parameters']['path']['id'],
) => instance.get(`/locations/${locationId}/reviews`)
export const getReviewById = (
id: paths['/reviews/{id}']['get']['parameters']['path']['id'],
) => instance.get(`/reviews/${id}`)
export const addReview = (
locationId: paths['/locations/{id}/reviews']['post']['parameters']['path']['id'],
data: paths['/locations/{id}/reviews']['post']['requestBody']['content']['application/json'],
) => instance.post(`/locations/${locationId}/reviews`, data)
export const editReview = (
id: paths['/reviews/{id}']['put']['parameters']['path']['id'],
data: paths['/reviews/{id}']['put']['requestBody']['content']['application/json'],
) => instance.put(`/reviews/${id}`, data)
export const deleteReview = (id: any) => instance.delete(`/reviews/${id}`)
export const addPhoto = (
file: paths['/photos']['post']['requestBody']['content']['multipart/form-data']['file'],
) => {
const formData = new FormData()
formData.append('file', file)
return instance.post('/photos', formData)
}
export const addReport = (
data: paths['/reports']['post']['requestBody']['content']['application/json'],
) => instance.post('/reports', data)
export const getImports = () => instance.get(`/imports`)
export const getImportById = (
id: paths['/imports/{id}']['get']['parameters']['path']['id'],
) => instance.get(`/imports/${id}`)
export const getUserById = (
id: paths['/users/{id}']['get']['parameters']['path']['id'],
) => instance.get(`/users/${id}`)