Created client API helper
This commit is contained in:
53
app/api/client.ts
Normal file
53
app/api/client.ts
Normal 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;
|
||||
Reference in New Issue
Block a user