Shioriko/web/app/src/api.js

198 lines
5.0 KiB
JavaScript
Raw Normal View History

2021-05-09 15:07:23 +00:00
import { token } from "./stores.js"
2021-05-10 17:37:27 +00:00
import axios from "axios";
2021-05-09 15:07:23 +00:00
let url = window.BASE_URL;
let current_token;
2021-05-10 17:37:27 +00:00
token.subscribe(value => {
current_token = value;
2021-05-09 15:07:23 +00:00
})
2021-05-10 17:37:27 +00:00
2021-05-09 15:07:23 +00:00
export async function login({ username, password }) {
const endpoint = url + "/api/auth/login";
2021-05-10 17:37:27 +00:00
const response = await axios({
url: endpoint,
2021-05-09 15:07:23 +00:00
method: "POST",
2021-05-10 17:37:27 +00:00
data: JSON.stringify({
2021-05-09 15:07:23 +00:00
username,
password,
}),
})
2021-05-10 17:37:27 +00:00
token.set(response.data.token);
return response.data;
2021-05-09 15:07:23 +00:00
}
2021-05-21 11:39:00 +00:00
export async function register({ email, username, password }) {
const endpoint = url + "/api/user/register";
const response = await axios({
url: endpoint,
method: "POST",
data: JSON.stringify({
email,
username,
password,
}),
})
token.set(response.data.token);
return response.data;
}
2021-05-10 20:34:59 +00:00
export async function getTags() {
const endpoint = url + "/api/tag";
const response = await axios.get(endpoint);
return response.data;
}
2022-04-15 19:20:14 +00:00
export async function getTag({ tag }) {
const endpoint = url + "/api/tag/" + tag;
const response = await axios.get(endpoint);
return response.data;
}
2021-09-10 18:56:42 +00:00
export async function getTagAutocomplete() {
const endpoint = url + "/api/tag/autocomplete";
const response = await axios.get(endpoint);
return response.data;
}
2021-05-09 15:07:23 +00:00
export async function getPosts({ page }) {
const endpoint = url + "/api/post?page=" + page;
2021-05-10 17:37:27 +00:00
const response = await axios.get(endpoint);
return response.data;
2021-05-09 15:07:23 +00:00
}
2021-05-10 20:25:33 +00:00
export async function getPostSearchTag({ page, q }) {
if (q) {
const endpoint = url + "/api/post?tags=" + q + "&page=" + page;
const response = await axios(endpoint);
return response.data;
} else {
const endpoint = url + "/api/post?page=" + page;
const response = await axios(endpoint);
return response.data;
}
2021-05-09 15:07:23 +00:00
}
export async function getPost({ id }) {
const endpoint = url + "/api/post/" + id;
2021-05-10 17:37:27 +00:00
const response = await axios(endpoint);
return response.data;
}
export async function uploadBlob({ file, onProgress }) {
var formData = new FormData();
formData.append("file", file);
const endpoint = url + "/api/blob/upload";
const response = await axios({
url: endpoint,
method: "POST",
headers: {
'Authorization': 'Bearer ' + current_token,
'Content-Type': 'multipart/form-data',
},
withCredentials: true,
data: formData,
onUploadProgress: e => {
if (onProgress) {
onProgress(e)
}
}
})
return response.data;
}
export async function postCreate({ blob_id, source_url, tags }) {
const endpoint = url + "/api/post/create";
const response = await axios({
url: endpoint,
method: "POST",
headers: {
'Authorization': 'Bearer ' + current_token,
},
withCredentials: true,
data: {
blob_id, source_url, tags
}
})
return response.data;
2021-05-10 20:25:33 +00:00
}
export async function postUpdate(id, { source_url, tags }) {
2021-05-21 11:39:00 +00:00
const endpoint = url + "/api/post/" + id;
2021-05-10 20:25:33 +00:00
const response = await axios({
url: endpoint,
method: "POST",
headers: {
'Authorization': 'Bearer ' + current_token,
},
withCredentials: true,
data: {
source_url, tags
}
})
return response.data;
2021-05-11 18:22:46 +00:00
}
2022-04-15 19:20:14 +00:00
2021-05-21 11:39:00 +00:00
export async function postDelete({ id }) {
const endpoint = url + "/api/post/" + id;
2021-05-11 18:22:46 +00:00
const response = await axios({
url: endpoint,
method: "DELETE",
headers: {
'Authorization': 'Bearer ' + current_token,
},
withCredentials: true,
})
return response.status == 200;
2022-04-15 19:20:14 +00:00
}
export async function getUserProfile() {
const endpoint = url + "/api/user/profile";
const response = await axios({
url: endpoint,
method: "GET",
headers: {
'Authorization': 'Bearer ' + current_token,
},
withCredentials: true,
});
console.log(response.data);
return response.data;
}
export async function updateTagNotes(id, { note }) {
const endpoint = url + "/api/tag/" + id + "/note";
const response = await axios({
url: endpoint,
method: "PUT",
headers: {
'Authorization': 'Bearer ' + current_token,
},
withCredentials: true,
data: {
note
}
})
return response.data;
}
export async function updateTag(id, { name, tagTypeId }) {
const endpoint = url + "/api/tag/" + id;
const response = await axios({
url: endpoint,
method: "PUT",
headers: {
'Authorization': 'Bearer ' + current_token,
},
withCredentials: true,
data: {
name, tagTypeId
}
})
return response.data;
}
export async function getTagTypes() {
const endpoint = url + "/api/tagtype";
const response = await axios.get(endpoint);
return response.data;
2021-05-09 15:07:23 +00:00
}