Merge branch 'master' of git.silversoft.se:Imbus/frostbyte-native
This commit is contained in:
commit
a1c8cd95d3
1 changed files with 64 additions and 7 deletions
|
@ -1,42 +1,83 @@
|
||||||
// This file contains types and functions related to interacting with the API.
|
// This file contains types and functions related to interacting with the API.
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base URL for the API.
|
||||||
|
* @type {string}
|
||||||
|
*/
|
||||||
const URL = "https://shitpost.se";
|
const URL = "https://shitpost.se";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a new post to be created.
|
||||||
|
* @interface
|
||||||
|
*/
|
||||||
export interface NewPost {
|
export interface NewPost {
|
||||||
content: string;
|
content: string;
|
||||||
token: string;
|
token: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents the votes on a post.
|
||||||
|
* @interface
|
||||||
|
*/
|
||||||
interface Votes {
|
interface Votes {
|
||||||
up: number;
|
up: number;
|
||||||
down: number;
|
down: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a post.
|
||||||
|
* @interface
|
||||||
|
* @extends NewPost
|
||||||
|
*/
|
||||||
export interface Post extends NewPost {
|
export interface Post extends NewPost {
|
||||||
id: string;
|
id: string;
|
||||||
createdAt: string;
|
createdAt: string;
|
||||||
votes: Votes;
|
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 {
|
export interface AuthResponse {
|
||||||
username: string;
|
username: string;
|
||||||
token: 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[]> {
|
export async function getPosts(): Promise<Post[]> {
|
||||||
const res = await fetch(URL + "/api/posts");
|
const res = await fetch(URL + "/api/posts");
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
return data;
|
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> {
|
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();
|
const data = await res.json();
|
||||||
return data;
|
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> {
|
export async function createPost(post: NewPost): Promise<void> {
|
||||||
await fetch("/api/posts", {
|
await fetch(URL + "/api/posts", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"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(
|
export async function submitRegistration(
|
||||||
username: string,
|
username: string,
|
||||||
password: string,
|
password: string,
|
||||||
captcha: string
|
captcha: string
|
||||||
): Promise<AuthResponse | undefined> {
|
): Promise<AuthResponse | undefined> {
|
||||||
const response = await fetch("/api/register", {
|
const response = await fetch(URL + "/api/register", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
body: JSON.stringify({ username, password, captcha }),
|
body: JSON.stringify({ username, password, captcha }),
|
||||||
|
@ -60,13 +109,21 @@ export async function submitRegistration(
|
||||||
if (response.ok) return await response.json();
|
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(
|
export async function submitLogin(
|
||||||
username: string,
|
username: string,
|
||||||
password: string
|
password: string
|
||||||
): Promise<AuthResponse | undefined> {
|
): Promise<AuthResponse | undefined> {
|
||||||
if (username == "" || password == "") return;
|
if (username == "" || password == "") return;
|
||||||
const response = await fetch("/api/login", {
|
|
||||||
|
const response = await fetch(URL + "/api/login", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
body: JSON.stringify({ username, password }),
|
body: JSON.stringify({ username, password }),
|
||||||
|
|
Loading…
Reference in a new issue