2023-10-21 07:54:47 +02:00
|
|
|
// This file contains types and functions related to interacting with the API.
|
2023-10-19 02:42:37 +02:00
|
|
|
|
|
|
|
export interface NewPost {
|
2023-11-15 07:29:59 +01:00
|
|
|
content: string;
|
|
|
|
token: string;
|
2023-10-19 02:42:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
interface Votes {
|
2023-11-15 07:29:59 +01:00
|
|
|
up: number;
|
|
|
|
down: number;
|
2023-10-19 02:42:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface Post extends NewPost {
|
2023-11-15 07:29:59 +01:00
|
|
|
id: string;
|
|
|
|
createdAt: string;
|
2024-03-12 18:57:14 +01:00
|
|
|
updatedAt: string;
|
2023-11-15 07:29:59 +01:00
|
|
|
votes: Votes;
|
2023-10-19 02:42:37 +02:00
|
|
|
}
|
|
|
|
|
2024-03-06 00:48:19 +01:00
|
|
|
export interface NewComment {
|
|
|
|
content: string;
|
|
|
|
user_token: string;
|
|
|
|
parent_post_id: number;
|
2024-03-11 15:16:55 +01:00
|
|
|
parent_comment_id?: number;
|
2024-03-06 00:48:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface Comment extends NewComment {
|
|
|
|
content: string;
|
|
|
|
token: string;
|
|
|
|
}
|
|
|
|
|
2023-11-15 14:21:14 +01:00
|
|
|
// This is what the login and registration responses look like
|
|
|
|
export interface AuthResponse {
|
|
|
|
username: string;
|
|
|
|
token: string;
|
|
|
|
}
|
|
|
|
|
2024-02-27 03:54:55 +01:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2023-10-19 02:42:37 +02:00
|
|
|
export async function getPosts(): Promise<Post[]> {
|
2023-10-20 22:49:09 +02:00
|
|
|
const res = await fetch("/api/posts");
|
2023-10-19 02:42:37 +02:00
|
|
|
const data = await res.json();
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getPost(id: string): Promise<Post> {
|
2023-10-20 22:49:09 +02:00
|
|
|
const res = await fetch(`/api/posts/${id}`);
|
2023-10-19 02:42:37 +02:00
|
|
|
const data = await res.json();
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function createPost(post: NewPost): Promise<void> {
|
2023-10-20 22:49:09 +02:00
|
|
|
await fetch("/api/posts", {
|
2023-10-19 02:42:37 +02:00
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify(post),
|
|
|
|
});
|
|
|
|
}
|
2023-11-15 14:21:14 +01:00
|
|
|
|
2024-03-06 00:48:19 +01:00
|
|
|
export async function createComment(comment: NewComment): Promise<void> {
|
|
|
|
await fetch("/api/comments", {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: JSON.stringify(comment),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-02-27 03:54:55 +01:00
|
|
|
// Gets the comments for a specific post
|
|
|
|
export async function getComments(
|
|
|
|
postId: string,
|
|
|
|
limit: number,
|
|
|
|
offset: number
|
|
|
|
): Promise<PublicComment[]> {
|
|
|
|
const res = await fetch(
|
|
|
|
`/api/comments?post_id=${postId}&limit=${limit}&offset=${offset}`
|
|
|
|
);
|
|
|
|
const data = await res.json();
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2024-03-12 18:57:14 +01:00
|
|
|
/** Incomplete */
|
2024-03-22 20:39:34 +01:00
|
|
|
export async function engagePost(postId: string): Promise<void> {
|
2024-03-12 18:57:14 +01:00
|
|
|
const res = await fetch(`/api/engage_post?post_id=${postId}`);
|
|
|
|
const data = await res.json();
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2023-11-15 14:21:14 +01:00
|
|
|
// Send the registration request to the server
|
|
|
|
export async function submitRegistration(
|
|
|
|
username: string,
|
|
|
|
password: string,
|
|
|
|
captcha: string
|
|
|
|
): Promise<AuthResponse | undefined> {
|
|
|
|
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<AuthResponse | undefined> {
|
|
|
|
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();
|
2023-11-22 15:29:27 +01:00
|
|
|
}
|
2024-03-22 21:28:20 +01:00
|
|
|
/**
|
|
|
|
* 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<Response>} A promise that resolves to a Response object.
|
|
|
|
*/
|
|
|
|
export async function engage(postId: string, token: string): Promise<Response> {
|
|
|
|
return await fetch(`/api/posts/${postId}/engage`, {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
Authorization: `Bearer ${token}`,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|