FrostByte/client-solid/src/api.ts
2023-11-13 09:34:32 +01:00

39 lines
825 B
TypeScript

// This file contains types and functions related to interacting with the API.
export interface NewPost {
content: string;
token: string;
}
interface Votes {
up: number;
down: number;
}
export interface Post extends NewPost {
id: string;
createdAt: string;
votes: Votes;
}
export async function getPosts(): Promise<Post[]> {
const res = await fetch("/api/posts");
const data = await res.json();
return data;
}
export async function getPost(id: string): Promise<Post> {
const res = await fetch(`/api/posts/${id}`);
const data = await res.json();
return data;
}
export async function createPost(post: NewPost): Promise<void> {
await fetch("/api/posts", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(post),
});
}