54 lines
993 B
TypeScript
54 lines
993 B
TypeScript
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;
|