mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-03-03 14:21:49 +00:00
Extracted helper fetch function and migrated remoteServers redux action from axios to fetch
This commit is contained in:
@@ -119,29 +119,19 @@ export class ShlinkApiClient {
|
||||
const normalizedQuery = stringifyQuery(rejectNilProps(query));
|
||||
const stringifiedQuery = isEmpty(normalizedQuery) ? '' : `?${normalizedQuery}`;
|
||||
|
||||
return this.fetch(`${buildShlinkBaseUrl(this.baseUrl, this.apiVersion)}${url}${stringifiedQuery}`, {
|
||||
return this.fetch<T>(`${buildShlinkBaseUrl(this.baseUrl, this.apiVersion)}${url}${stringifiedQuery}`, {
|
||||
method,
|
||||
body: body && JSON.stringify(body),
|
||||
headers: { 'X-Api-Key': this.apiKey },
|
||||
})
|
||||
.then(async (resp) => {
|
||||
const parsed = await resp.json();
|
||||
}).catch((e: unknown) => {
|
||||
if (!isRegularNotFound(parseApiError(e))) {
|
||||
throw e;
|
||||
}
|
||||
|
||||
if (!resp.ok) {
|
||||
throw parsed; // eslint-disable-line @typescript-eslint/no-throw-literal
|
||||
}
|
||||
|
||||
return parsed as T; // TODO Improve type inference here without explicit casting
|
||||
})
|
||||
.catch((e: unknown) => {
|
||||
if (!isRegularNotFound(parseApiError(e))) {
|
||||
throw e;
|
||||
}
|
||||
|
||||
// If we capture a not found error, let's assume this Shlink version does not support API v3, so we decrease to
|
||||
// v2 and retry
|
||||
this.apiVersion = 2;
|
||||
return this.performRequest(url, method, query, body);
|
||||
});
|
||||
// If we capture a not found error, let's assume this Shlink version does not support API v3, so we decrease to
|
||||
// v2 and retry
|
||||
this.apiVersion = 2;
|
||||
return this.performRequest(url, method, query, body);
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ import Bottle from 'bottlejs';
|
||||
import { buildShlinkApiClient } from './ShlinkApiClientBuilder';
|
||||
|
||||
const provideServices = (bottle: Bottle) => {
|
||||
bottle.serviceFactory('buildShlinkApiClient', buildShlinkApiClient, 'fetch');
|
||||
bottle.serviceFactory('buildShlinkApiClient', buildShlinkApiClient, 'jsonFetch');
|
||||
};
|
||||
|
||||
export default provideServices;
|
||||
|
||||
Reference in New Issue
Block a user