FrostByte/client-solid/src/api.ts

44 lines
927 B
TypeScript
Raw Normal View History

2023-10-19 08:11:52 +02:00
// const PORT = 3000;
// const API_URL = `http://localhost:${PORT}/api/`;
// const API_URL2 = new URL(API_URL);
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 {
uuid: string;
createdAt: string;
votes: Votes;
}
export async function getPosts(): Promise<Post[]> {
// const res = await fetch(`${API_URL}/posts`);
2023-10-19 08:11:52 +02:00
const res = await fetch("/api/");
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-19 08:11:52 +02:00
const res = await fetch(`/api/${id}`);
2023-10-19 02:42:37 +02:00
const data = await res.json();
return data;
}
export async function createPost(post: NewPost): Promise<void> {
// await fetch(`${API_URL}`, {
2023-10-19 08:11:52 +02:00
await fetch("/api/", {
2023-10-19 02:42:37 +02:00
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(post),
});
}