// This file contains types and functions related to interacting with the API. export interface NewPost { content: string; token: string; } interface Votes { up: number; down: number; } export interface Post extends NewPost { id: string; createdAt: string; updatedAt: string; votes: Votes; } export interface NewComment { content: string; user_token: string; parent_post_id: number; parent_comment_id?: number; } export interface Comment extends NewComment { content: string; token: string; } // This is what the login and registration responses look like export interface AuthResponse { username: string; token: string; } // This is what a public comment looks like, as it arrives from the server export interface PublicComment { id: number; parent_post_id: number; parent_comment_id: number | null; content: string; created_at: string; updated_at: string; } export async function getPosts(): Promise { const res = await fetch("/api/posts"); const data = await res.json(); return data; } export async function getPost(id: string): Promise { const res = await fetch(`/api/posts/${id}`); const data = await res.json(); return data; } export async function createPost(post: NewPost): Promise { await fetch("/api/posts", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify(post), }); } export async function createComment(comment: NewComment): Promise { await fetch("/api/comments", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify(comment), }); } // Gets the comments for a specific post export async function getComments( postId: string, limit: number, offset: number ): Promise { const res = await fetch( `/api/comments?post_id=${postId}&limit=${limit}&offset=${offset}` ); const data = await res.json(); return data; } /** Incomplete */ export async function engagePost(postId: string): Promise { const res = await fetch(`/api/engage_post?post_id=${postId}`); const data = await res.json(); return data; } // Send the registration request to the server export async function submitRegistration( username: string, password: string, captcha: string ): Promise { const response = await fetch("/api/register", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ username, password, captcha }), }); if (response.ok) return await response.json(); } // Send the login request to the server export async function submitLogin( username: string, password: string ): Promise { if (username == "" || password == "") return; const response = await fetch("/api/login", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ username, password }), }); if (response.ok) return await response.json(); } /** * Engage with a post. * @param postId The id of the post to engage with. * @param token The token of the user engaging with the post. * @returns {Promise} A promise that resolves to a Response object. */ export async function engage(postId: string, token: string): Promise { return await fetch(`/api/posts/${postId}/engage`, { method: "POST", headers: { "Content-Type": "application/json", Authorization: `Bearer ${token}`, }, }); }