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
|
|
|
}
|
|
|
|
|
|
|
|
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 }) {
|
|
|
|
const endpoint = url + "/api/post/"+id;
|
|
|
|
const response = await axios({
|
|
|
|
url: endpoint,
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
'Authorization': 'Bearer ' + current_token,
|
|
|
|
},
|
|
|
|
withCredentials: true,
|
|
|
|
data: {
|
|
|
|
source_url, tags
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return response.data;
|
2021-05-09 15:07:23 +00:00
|
|
|
}
|