-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.ts
171 lines (129 loc) · 4.81 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
import { Account, ApiError, CollectionForm, ExtendedAccountInfo,
FeedPicture, Picture, PictureForm } from './types'
export const API_BASE_URL = 'https://netscapes-rest-api.onrender.com'
export async function logInForJwt(username : string, password : string) {
const response = await fetch(API_BASE_URL + '/auth/log-in', {
method: 'POST',
body: JSON.stringify({ username, password }),
headers: { 'Content-Type': 'application/json' }
})
await throwIfError(response)
return await response.text()
}
export async function signUpForJwt(username : string, password : string) {
const response = await fetch(API_BASE_URL + '/auth/sign-up', {
method: 'POST',
body: JSON.stringify({ username, password }),
headers: { 'Content-Type': 'application/json' }
})
await throwIfError(response)
return await logInForJwt(username, password)
}
export async function getPictures(accessToken? : string) : Promise<Picture[]> {
const response = await fetch(API_BASE_URL + '/pictures', {
headers: accessToken ? { 'Authorization': 'Bearer ' + accessToken } : {}
})
await throwIfError(response)
return await response.json()
}
export async function getPictureById(id : number, accessToken? : string) {
const response = await fetch(`${API_BASE_URL}/pictures/${id}`, {
headers: accessToken ? { 'Authorization': 'Bearer ' + accessToken } : {}
})
await throwIfError(response)
return await response.json()
}
export async function getCurrentAccount(accessToken : string) : Promise<ExtendedAccountInfo> {
const response = await fetch(API_BASE_URL + '/accounts/me', {
headers: { 'Authorization': 'Bearer ' + accessToken }
})
await throwIfError(response)
return await response.json()
}
export async function getAccount(accountId : number) : Promise<Account> {
const response = await fetch(`${API_BASE_URL}/accounts/${accountId}`)
await throwIfError(response)
return await response.json()
}
export async function setAvatar(accessToken : string, avatarUrl : string) {
const response = await fetch(API_BASE_URL + '/accounts/me/avatar?avatar_url=' + avatarUrl, {
method: 'POST',
headers: {
'Authorization': 'Bearer ' + accessToken,
}
})
await throwIfError(response)
}
export async function addComment(accessToken : string, text : string, targetPictureId : number) {
const response = await fetch(API_BASE_URL + `/pictures/${targetPictureId}/comments`, {
method: 'POST',
headers: {
'Authorization': 'Bearer ' + accessToken,
'Content-Type': 'application/json'
},
body: `{ "text": "${text}" }`
})
await throwIfError(response)
}
export async function searchPictures(query : String) : Promise<FeedPicture[]> {
const response = await fetch(API_BASE_URL + `/pictures/search?query=${query}`)
await throwIfError(response)
return await response.json()
}
export async function addCollection(accessToken : string, collection: CollectionForm) {
const response = await fetch(`${API_BASE_URL}/collections`, {
method: 'POST',
headers: {
'Authorization': 'Bearer ' + accessToken,
'Content-Type': 'application/json'
},
body: JSON.stringify(collection)
})
await throwIfError(response)
}
export async function addPictureToCollection(accessToken : string, pictureId : number, collectionId : number) {
const buildedUrl = `${API_BASE_URL}/collections/${collectionId}?picture_id=${pictureId}`
const response = await fetch(buildedUrl, {
method: 'PUT',
headers: {
'Authorization': 'Bearer ' + accessToken
}
})
await throwIfError(response)
}
export async function addPicture(accessToken : string, picture : PictureForm) : Promise<number> {
const response = await fetch(`${API_BASE_URL}/pictures`, {
method: 'POST',
headers: {
'Authorization': 'Bearer ' + accessToken,
'Content-Type': 'application/json'
},
body: JSON.stringify(picture)
})
await throwIfError(response)
const { id } = await response.json()
return id
}
export async function deletePictureById(accessToken : string, id : number) {
const response = await fetch(API_BASE_URL + '/pictures/' + id, {
method: 'DELETE',
headers: {
'Authorization': 'Bearer ' + accessToken,
}
})
await throwIfError(response)
}
export async function isTokenValid(accessToken : string) {
try {
await getCurrentAccount(accessToken)
return true
}
catch(error) {
return false
}
}
async function throwIfError(response : Response) {
if(!response.ok) {
throw new ApiError(response.status, await response.text())
}
}