mirror of
https://github.com/Spythere/stacjownik.git
synced 2026-05-03 05:18:11 +00:00
refactor(http): removed axios; changed all requests to native fetch
This commit is contained in:
+23
@@ -0,0 +1,23 @@
|
||||
export class HttpClient {
|
||||
constructor(private readonly baseURL: string) {}
|
||||
|
||||
async get<T>(url: string, params?: Record<string, any>): Promise<T> {
|
||||
const absoluteURL = new URL(this.baseURL + '/' + url);
|
||||
|
||||
if (params) {
|
||||
Object.keys(params).forEach((key) => {
|
||||
if (params[key] === undefined) return;
|
||||
|
||||
absoluteURL.searchParams.append(key, params[key]);
|
||||
});
|
||||
}
|
||||
|
||||
const data = await fetch(absoluteURL);
|
||||
|
||||
if (!data.ok) {
|
||||
throw new Error(`Cannot fetch: ${absoluteURL}`);
|
||||
}
|
||||
|
||||
return data.json();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user