diff --git a/src/util/api.ts b/src/util/api.ts index 710e195..a72078b 100644 --- a/src/util/api.ts +++ b/src/util/api.ts @@ -1,42 +1,83 @@ // This file contains types and functions related to interacting with the API. + +/** + * Base URL for the API. + * @type {string} + */ const URL = "https://shitpost.se"; +/** + * Represents a new post to be created. + * @interface + */ export interface NewPost { content: string; token: string; } +/** + * Represents the votes on a post. + * @interface + */ interface Votes { up: number; down: number; } +/** + * Represents a post. + * @interface + * @extends NewPost + */ export interface Post extends NewPost { id: string; createdAt: string; votes: Votes; } -// This is what the login and registration responses look like +/** + * Represents the response after a successful login or registration. + * @interface + */ export interface AuthResponse { username: string; token: string; } +/** + * Fetches all posts from the API. + * @async + * @function + * @returns {Promise} A promise that resolves to an array of posts. + */ export async function getPosts(): Promise { const res = await fetch(URL + "/api/posts"); const data = await res.json(); return data; } +/** + * Fetches a specific post by its ID from the API. + * @async + * @function + * @param {string} id - The ID of the post to fetch. + * @returns {Promise} A promise that resolves to the requested post. + */ export async function getPost(id: string): Promise { - const res = await fetch(`/api/posts/${id}`); + const res = await fetch(URL + `/api/posts/${id}`); const data = await res.json(); return data; } +/** + * Creates a new post on the API. + * @async + * @function + * @param {NewPost} post - The post to create. + * @returns {Promise} A promise that resolves when the post is created. + */ export async function createPost(post: NewPost): Promise { - await fetch("/api/posts", { + await fetch(URL + "/api/posts", { method: "POST", headers: { "Content-Type": "application/json", @@ -45,13 +86,21 @@ export async function createPost(post: NewPost): Promise { }); } -// Send the registration request to the server +/** + * Submits a registration request to the server. + * @async + * @function + * @param {string} username - The username for registration. + * @param {string} password - The password for registration. + * @param {string} captcha - The captcha for registration. + * @returns {Promise} A promise that resolves to the authentication response. + */ export async function submitRegistration( username: string, password: string, captcha: string ): Promise { - const response = await fetch("/api/register", { + const response = await fetch(URL + "/api/register", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ username, password, captcha }), @@ -60,13 +109,21 @@ export async function submitRegistration( if (response.ok) return await response.json(); } -// Send the login request to the server +/** + * Submits a login request to the server. + * @async + * @function + * @param {string} username - The username for login. + * @param {string} password - The password for login. + * @returns {Promise} A promise that resolves to the authentication response. + */ export async function submitLogin( username: string, password: string ): Promise { if (username == "" || password == "") return; - const response = await fetch("/api/login", { + + const response = await fetch(URL + "/api/login", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ username, password }),