Merge branch 'master' of git.silversoft.se:Imbus/frostbyte-native

This commit is contained in:
Imbus 2023-12-14 20:22:02 +01:00
commit a1c8cd95d3

View file

@ -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<Post[]>} A promise that resolves to an array of posts.
*/
export async function getPosts(): Promise<Post[]> {
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<Post>} A promise that resolves to the requested post.
*/
export async function getPost(id: string): Promise<Post> {
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<void>} A promise that resolves when the post is created.
*/
export async function createPost(post: NewPost): Promise<void> {
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<void> {
});
}
// 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<AuthResponse | undefined>} A promise that resolves to the authentication response.
*/
export async function submitRegistration(
username: string,
password: string,
captcha: string
): Promise<AuthResponse | undefined> {
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<AuthResponse | undefined>} A promise that resolves to the authentication response.
*/
export async function submitLogin(
username: string,
password: string
): Promise<AuthResponse | undefined> {
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 }),