xssbook/public/js/api.js

144 lines
3.9 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) {
2023-02-13 16:59:00 +00:00
location.href = '/login'
2023-02-01 03:21:19 +00:00
}
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) {
2023-02-13 16:59:00 +00:00
location.href = '/login'
2023-01-26 22:29:16 +00:00
}
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-02-12 19:11:50 +00:00
export const login = async (email, password) => {
2023-01-26 22:29:16 +00:00
return await request('/auth/login', {email, password})
}
2023-02-12 19:11:50 +00:00
export const register = async (firstname, lastname, email, password, gender, day, month, year) => {
2023-01-26 22:29:16 +00:00
return await request('/auth/register', {firstname, lastname, email, password, gender, day, month, year})
}
2023-02-12 19:11:50 +00:00
export const logout = async () => {
return await request('/auth/logout', {})
}
2023-02-12 19:11:50 +00:00
export const loadpostspage = async (page) => {
2023-01-26 22:29:16 +00:00
return await request('/posts/page', {page})
}
2023-02-12 19:11:50 +00:00
export const loadcommentspage = async (page, post_id) => {
return await request('/posts/comments', {page, post_id})
}
export const loadusersposts = async (user_id, page) => {
2023-02-02 16:29:37 +00:00
return await request('/posts/user', {user_id, page})
2023-01-26 22:29:16 +00:00
}
2023-02-12 19:11:50 +00:00
export const loadusers = async (ids) => {
2023-01-26 22:29:16 +00:00
return await request('/users/load', {ids})
}
2023-02-12 19:11:50 +00:00
export const loaduserspage = async (page) => {
2023-01-26 22:29:16 +00:00
return await request('/users/page', {page})
}
2023-02-12 19:11:50 +00:00
export const loadself = async () => {
2023-01-26 22:29:16 +00:00
return await request("/users/self", {})
}
2023-02-15 00:28:10 +00:00
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})
}
2023-02-12 19:11:50 +00:00
export const postcomment = async (post_id, content) => {
2023-01-26 22:29:16 +00:00
return await request('/posts/comment', {post_id, content}, 'PATCH')
}
2023-02-12 19:11:50 +00:00
export const postlike = async (post_id, state) => {
2023-01-26 22:29:16 +00:00
return await request('/posts/like', {post_id, state}, 'PATCH')
}
2023-02-12 19:11:50 +00:00
export const createpost = async (content) => {
2023-01-26 22:29:16 +00:00
return await request('/posts/create', {content})
2023-01-30 00:28:48 +00:00
}
2023-02-12 19:11:50 +00:00
export const adminauth = async (secret) => {
2023-01-30 00:28:48 +00:00
return await request('/admin/auth', {secret})
}
2023-02-12 19:11:50 +00:00
export const admincheck = async () => {
2023-01-30 00:28:48 +00:00
return await request('/admin/check', {})
}
2023-02-12 19:11:50 +00:00
export const adminquery = async (query) => {
2023-01-30 00:28:48 +00:00
return await request('/admin/query', {query})
}
2023-02-12 19:11:50 +00:00
export const adminposts = async () => {
2023-01-30 00:28:48 +00:00
return await request('/admin/posts', {})
}
2023-02-12 19:11:50 +00:00
export const adminusers = async () => {
2023-01-30 00:28:48 +00:00
return await request('/admin/users', {})
}
2023-02-12 19:11:50 +00:00
export const adminsessions = async () => {
2023-01-30 00:28:48 +00:00
return await request('/admin/sessions', {})
2023-02-01 03:21:19 +00:00
}
2023-02-12 19:11:50 +00:00
export const admincomments = async () => {
return await request('/admin/comments', {})
}
export const adminlikes = async () => {
return await request('/admin/likes', {})
}
export const updateavatar = async (file) => {
2023-02-01 03:21:19 +00:00
return await fileRequest('/users/avatar', file, 'PUT')
}
2023-02-12 19:11:50 +00:00
export const updatebanner = async (file) => {
2023-02-01 03:21:19 +00:00
return await fileRequest('/users/banner', file, 'PUT')
2023-01-26 22:29:16 +00:00
}