xssbook/public/js/api.js
Tyler Murphy 3d71da4909 refactor
2023-02-12 14:11:50 -05:00

132 lines
No EOL
3.6 KiB
JavaScript

const endpoint = '/api'
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 }
}
}
const request = async (url, body, method) => {
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 }
}
}
export const login = async (email, password) => {
return await request('/auth/login', {email, password})
}
export const register = async (firstname, lastname, email, password, gender, day, month, year) => {
return await request('/auth/register', {firstname, lastname, email, password, gender, day, month, year})
}
export const logout = async () => {
return await request('/auth/logout', {})
}
export const loadpostspage = async (page) => {
return await request('/posts/page', {page})
}
export const loadcommentspage = async (page, post_id) => {
return await request('/posts/comments', {page, post_id})
}
export const loadusersposts = async (user_id, page) => {
return await request('/posts/user', {user_id, page})
}
export const loadusers = async (ids) => {
return await request('/users/load', {ids})
}
export const loaduserspage = async (page) => {
return await request('/users/page', {page})
}
export const loadself = async () => {
return await request("/users/self", {})
}
export const postcomment = async (post_id, content) => {
return await request('/posts/comment', {post_id, content}, 'PATCH')
}
export const postlike = async (post_id, state) => {
return await request('/posts/like', {post_id, state}, 'PATCH')
}
export const createpost = async (content) => {
return await request('/posts/create', {content})
}
export const adminauth = async (secret) => {
return await request('/admin/auth', {secret})
}
export const admincheck = async () => {
return await request('/admin/check', {})
}
export const adminquery = async (query) => {
return await request('/admin/query', {query})
}
export const adminposts = async () => {
return await request('/admin/posts', {})
}
export const adminusers = async () => {
return await request('/admin/users', {})
}
export const adminsessions = async () => {
return await request('/admin/sessions', {})
}
export const admincomments = async () => {
return await request('/admin/comments', {})
}
export const adminlikes = async () => {
return await request('/admin/likes', {})
}
export const updateavatar = async (file) => {
return await fileRequest('/users/avatar', file, 'PUT')
}
export const updatebanner = async (file) => {
return await fileRequest('/users/banner', file, 'PUT')
}