Update to shlink-web-client 0.3.1

This commit is contained in:
Alejandro Celaya
2023-08-27 18:22:04 +02:00
parent 30318faf23
commit 1e31444c67
6 changed files with 51 additions and 56 deletions

View File

@@ -1,7 +1,13 @@
type Fetch = typeof window.fetch;
export type RequestOptions = {
method: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
body?: string;
headers?: Record<string, string>;
};
const applicationJsonHeader = { 'Content-Type': 'application/json' };
const withJsonContentType = (options?: RequestInit): RequestInit | undefined => {
const withJsonContentType = (options?: RequestOptions): RequestInit | undefined => {
if (!options?.body) {
return options;
}
@@ -20,7 +26,7 @@ const withJsonContentType = (options?: RequestInit): RequestInit | undefined =>
export class HttpClient {
constructor(private readonly fetch: Fetch) {}
public readonly fetchJson = <T>(url: string, options?: RequestInit): Promise<T> =>
public readonly fetchJson = <T>(url: string, options?: RequestOptions): Promise<T> =>
this.fetch(url, withJsonContentType(options)).then(async (resp) => {
const json = await resp.json();
@@ -31,7 +37,7 @@ export class HttpClient {
return json as T;
});
public readonly fetchEmpty = (url: string, options?: RequestInit): Promise<void> =>
public readonly fetchEmpty = (url: string, options?: RequestOptions): Promise<void> =>
this.fetch(url, withJsonContentType(options)).then(async (resp) => {
if (!resp.ok) {
throw await resp.json();