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

@@ -25,13 +25,13 @@ import {
ErrorTypeV3,
} from '@shlinkio/shlink-web-component/api-contract';
import { isEmpty, isNil, reject } from 'ramda';
import type { HttpClient } from '../../common/services/HttpClient';
import type { HttpClient, RequestOptions } from '../../common/services/HttpClient';
import { replaceAuthorityFromUri } from '../../utils/helpers/uri';
import type { OptionalString } from '../../utils/utils';
type ApiVersion = 2 | 3;
type RequestOptions = {
type ShlinkRequestOptions = {
url: string;
method?: 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
query?: object;
@@ -145,12 +145,12 @@ export class ShlinkApiClient implements BaseShlinkApiClient {
): Promise<ShlinkDomainRedirects> =>
this.performRequest<ShlinkDomainRedirects>({ url: '/domains/redirects', method: 'PATCH', body: domainRedirects });
private readonly performRequest = async <T>(requestOptions: RequestOptions): Promise<T> =>
private readonly performRequest = async <T>(requestOptions: ShlinkRequestOptions): Promise<T> =>
this.httpClient.fetchJson<T>(...this.toFetchParams(requestOptions)).catch(
this.handleFetchError(() => this.httpClient.fetchJson<T>(...this.toFetchParams(requestOptions))),
);
private readonly performEmptyRequest = async (requestOptions: RequestOptions): Promise<void> =>
private readonly performEmptyRequest = async (requestOptions: ShlinkRequestOptions): Promise<void> =>
this.httpClient.fetchEmpty(...this.toFetchParams(requestOptions)).catch(
this.handleFetchError(() => this.httpClient.fetchEmpty(...this.toFetchParams(requestOptions))),
);
@@ -161,7 +161,7 @@ export class ShlinkApiClient implements BaseShlinkApiClient {
query = {},
body,
domain,
}: RequestOptions): [string, RequestInit] => {
}: ShlinkRequestOptions): [string, RequestOptions] => {
const normalizedQuery = stringifyQuery(rejectNilProps(query));
const stringifiedQuery = isEmpty(normalizedQuery) ? '' : `?${normalizedQuery}`;
const baseUrl = domain ? replaceAuthorityFromUri(this.baseUrl, domain) : this.baseUrl;

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

View File

@@ -6,8 +6,6 @@ import { container } from './container';
import { setUpStore } from './container/store';
import { register as registerServiceWorker } from './serviceWorkerRegistration';
import './index.scss';
import 'leaflet/dist/leaflet.css';
import 'react-datepicker/dist/react-datepicker.css';
const store = setUpStore(container);
const { App, ScrollToTop, ErrorHandler, appUpdateAvailable } = container;