Created client API helper

This commit is contained in:
2025-03-16 23:42:23 +01:00
parent 0647d5f90e
commit 04b2536aac

53
app/api/client.ts Normal file
View File

@@ -0,0 +1,53 @@
interface paths {
"/item": {
parameters: {
query: {
uuid: string;
};
};
return: Item;
};
"/items": {
parameters: {
query?: never;
};
return?: never;
};
}
export interface Item {
id: number;
uuid: string;
name: string;
rarity: number;
price: number;
image: string;
}
class Client {
baseUrl: string;
constructor(baseUrl: string) {
this.baseUrl = baseUrl;
}
GET<T extends keyof paths>(
path: T,
parameters: paths[T]["parameters"]
): Promise<paths[T]["return"]> {
let query = parameters["query"]
? "?" + new URLSearchParams(parameters["query"])
: "";
return fetch(this.baseUrl + path + query, {
method: "GET",
headers: {
"Content-Type": "application/json",
},
// body: parameters ? JSON.stringify(parameters) : undefined,
}).then((response) => response.json());
}
}
const client = new Client("http://127.0.0.1:8000");
export default client;