-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathapi.ts
74 lines (72 loc) · 1.9 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
import axios from "axios"
import {
IUserProfile,
IUserProfileUpdate,
IUserProfileCreate,
IUserOpenProfileCreate,
} from "./interfaces"
function authHeaders(token: string) {
return {
headers: {
Authorization: `Bearer ${token}`,
},
}
}
export const api = {
async logInGetToken(username: string, password: string) {
const params = new URLSearchParams()
params.append("username", username)
params.append("password", password)
return await axios.post(
`${process.env.apiUrl}/api/v1/login/access-token`,
params
)
},
async createMe(data: IUserOpenProfileCreate) {
return await axios.post(`${process.env.apiUrl}/api/v1/users/open`, data)
},
async getMe(token: string) {
return await axios.get<IUserProfile>(
`${process.env.apiUrl}/api/v1/users/me`,
authHeaders(token)
)
},
async updateMe(token: string, data: IUserProfileUpdate) {
return await axios.put<IUserProfile>(
`${process.env.apiUrl}/api/v1/users/me`,
data,
authHeaders(token)
)
},
async getUsers(token: string) {
return await axios.get<IUserProfile[]>(
`${process.env.apiUrl}/api/v1/users/`,
authHeaders(token)
)
},
async updateUser(token: string, userId: number, data: IUserProfileUpdate) {
return await axios.put(
`${process.env.apiUrl}/api/v1/users/${userId}`,
data,
authHeaders(token)
)
},
async createUser(token: string, data: IUserProfileCreate) {
return await axios.post(
`${process.env.apiUrl}/api/v1/users/`,
data,
authHeaders(token)
)
},
async passwordRecovery(email: string) {
return await axios.post(
`${process.env.apiUrl}/api/v1/password-recovery/${email}`
)
},
async resetPassword(password: string, token: string) {
return await axios.post(`${process.env.apiUrl}/api/v1/reset-password/`, {
new_password: password,
token,
})
},
}