mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-03-03 06:11:48 +00:00
Updates ShlinkApiClint to use different methods to fetch, and fixed tests
This commit is contained in:
@@ -3,21 +3,23 @@ import { Fetch } from '../../utils/types';
|
||||
export class HttpClient {
|
||||
constructor(private readonly fetch: Fetch) {}
|
||||
|
||||
public fetchJson<T>(url: string, options?: RequestInit): Promise<T> {
|
||||
return this.fetch(url, options).then(async (resp) => {
|
||||
public readonly fetchJson = <T>(url: string, options?: RequestInit): Promise<T> =>
|
||||
this.fetch(url, options).then(async (resp) => {
|
||||
const json = await resp.json();
|
||||
|
||||
if (!resp.ok) {
|
||||
throw json;
|
||||
}
|
||||
|
||||
return json as T;
|
||||
});
|
||||
|
||||
public readonly fetchEmpty = (url: string, options?: RequestInit): Promise<void> =>
|
||||
this.fetch(url, options).then(async (resp) => {
|
||||
if (!resp.ok) {
|
||||
throw await resp.json();
|
||||
}
|
||||
|
||||
try {
|
||||
return (await resp.json()) as T;
|
||||
} catch (e) {
|
||||
return undefined as T;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public fetchBlob(url: string): Promise<Blob> {
|
||||
return this.fetch(url).then((resp) => resp.blob());
|
||||
}
|
||||
public readonly fetchBlob = (url: string): Promise<Blob> => this.fetch(url).then((resp) => resp.blob());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user