xssbook/public/js/api.js

120 lines
3.2 KiB
JavaScript
Raw Normal View History

2023-01-26 22:29:16 +00:00
const endpoint = '/api'
2023-02-01 03:21:19 +00:00
const fileRequest = async (url, file, method) => {
if (method === undefined) method = 'POST'
const response = await fetch(endpoint + url, {
method,
body: file,
headers: {}
});
if (response.status == 401) {
location.href = 'login'
}
const contentType = response.headers.get("content-type");
if (contentType && contentType.indexOf("application/json") !== -1) {
const json = await response.json()
return { status: response.status, msg: json.msg, json }
} else {
const msg = await response.text();
return { status: response.status, msg }
}
}
2023-01-26 22:29:16 +00:00
const request = async (url, body, method) => {
2023-02-01 03:21:19 +00:00
2023-01-26 22:29:16 +00:00
if (method === undefined) method = 'POST'
const response = await fetch(endpoint + url, {
method,
body: JSON.stringify(body),
headers: {
'Content-Type': 'application/json'
}
});
if (response.status == 401) {
location.href = 'login'
}
const contentType = response.headers.get("content-type");
if (contentType && contentType.indexOf("application/json") !== -1) {
const json = await response.json()
return { status: response.status, msg: json.msg, json }
} else {
const msg = await response.text();
return { status: response.status, msg }
}
}
const login = async (email, password) => {
return await request('/auth/login', {email, password})
}
const register = async (firstname, lastname, email, password, gender, day, month, year) => {
return await request('/auth/register', {firstname, lastname, email, password, gender, day, month, year})
}
const logout = async () => {
return await request('/auth/logout', {})
}
2023-01-26 22:29:16 +00:00
const loadpostspage = async (page) => {
return await request('/posts/page', {page})
}
2023-02-02 16:29:37 +00:00
const loadusersposts = async (user_id, page) => {
return await request('/posts/user', {user_id, page})
2023-01-26 22:29:16 +00:00
}
const loadusers = async (ids) => {
return await request('/users/load', {ids})
}
const loaduserspage = async (page) => {
return await request('/users/page', {page})
}
const loadself = async () => {
return await request("/users/self", {})
}
const postcomment = async (post_id, content) => {
return await request('/posts/comment', {post_id, content}, 'PATCH')
}
const postlike = async (post_id, state) => {
return await request('/posts/like', {post_id, state}, 'PATCH')
}
const createpost = async (content) => {
return await request('/posts/create', {content})
2023-01-30 00:28:48 +00:00
}
const adminauth = async (secret) => {
return await request('/admin/auth', {secret})
}
const admincheck = async () => {
return await request('/admin/check', {})
}
const adminquery = async (query) => {
return await request('/admin/query', {query})
}
const adminposts = async () => {
return await request('/admin/posts', {})
}
const adminusers = async () => {
return await request('/admin/users', {})
}
const adminsessions = async () => {
return await request('/admin/sessions', {})
2023-02-01 03:21:19 +00:00
}
const updateavatar = async (file) => {
return await fileRequest('/users/avatar', file, 'PUT')
}
const updatebanner = async (file) => {
return await fileRequest('/users/banner', file, 'PUT')
2023-01-26 22:29:16 +00:00
}