44 lines
942 B
TypeScript
44 lines
942 B
TypeScript
![]() |
const PORT = 3000;
|
||
|
const API_URL = `http://localhost:${PORT}/api/`;
|
||
|
const API_URL2 = new URL(API_URL);
|
||
|
|
||
|
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`);
|
||
|
const res = await fetch(API_URL2.href);
|
||
|
const data = await res.json();
|
||
|
return data;
|
||
|
}
|
||
|
|
||
|
export async function getPost(id: string): Promise<Post> {
|
||
|
const res = await fetch(`${API_URL}/posts/${id}`);
|
||
|
const data = await res.json();
|
||
|
return data;
|
||
|
}
|
||
|
|
||
|
export async function createPost(post: NewPost): Promise<void> {
|
||
|
// await fetch(`${API_URL}`, {
|
||
|
await fetch(API_URL2.href, {
|
||
|
method: "POST",
|
||
|
headers: {
|
||
|
"Content-Type": "application/json",
|
||
|
},
|
||
|
body: JSON.stringify(post),
|
||
|
});
|
||
|
}
|