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 {
|
|
|
|
content: string;
|
|
|
|
token: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Votes {
|
|
|
|
up: number;
|
|
|
|
down: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface Post extends NewPost {
|
2023-10-27 16:04:58 +02:00
|
|
|
id: string;
|
2023-10-19 02:42:37 +02:00
|
|
|
createdAt: string;
|
|
|
|
votes: Votes;
|
|
|
|
}
|
|
|
|
|
|
|
|
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),
|
|
|
|
});
|
|
|
|
}
|