Updates ShlinkApiClint to use different methods to fetch, and fixed tests

This commit is contained in:
Alejandro Celaya
2022-11-20 12:51:07 +01:00
parent 1e155af948
commit 059fa37ca7
5 changed files with 80 additions and 62 deletions

View File

@@ -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());
}