xssbook/public/js/api.js
Tyler Murphy c4c26f42b6 dms
2023-08-21 23:19:53 -04:00

169 lines
4.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 follow = async (state, user_id) => {
return await request('/users/follow', {state, user_id}, 'PUT')
}
export const follow_status = async (user_id) => {
return await request('/users/follow', {user_id})
}
export const friends = async (user_id) => {
return await request('/users/friends', {user_id})
}
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')
}
export const chatlist = async () => {
return await request('/chat/list', {})
}
export const chatcreate = async (name) => {
return await request('/chat/create', {name})
}
export const chatadd = async (email, room_id) => {
return await request('/chat/add', {email, room_id}, 'PATCH')
}
export const chatleave = async (room_id) => {
return await request('/chat/leave', {room_id}, 'DELETE')
}
export const chatsend = async (content, room_id) => {
return await request('/chat/send', {content, room_id})
}
export const chatload = async (newest_msg, page, room_id) => {
return await request('/chat/load', {newest_msg, page, room_id})
}