Removed duplicated code when building ShlinkApiClient

This commit is contained in:
Alejandro Celaya
2019-04-21 11:31:40 +02:00
parent 48f7103205
commit 7b80d78dc5
15 changed files with 60 additions and 52 deletions

View File

@@ -1,9 +1,20 @@
import * as axios from 'axios';
import { wait } from '../utils';
import ShlinkApiClient from './ShlinkApiClient';
const apiClients = {};
const buildShlinkApiClient = (axios) => ({ url, apiKey }) => {
const getSelectedServerFromState = async (getState) => {
const { selectedServer } = getState();
if (!selectedServer) {
return wait(250).then(() => getSelectedServerFromState(getState));
}
return selectedServer;
};
const buildShlinkApiClient = (axios) => async (getState) => {
const { url, apiKey } = await getSelectedServerFromState(getState);
const clientKey = `${url}_${apiKey}`;
if (!apiClients[clientKey]) {
@@ -14,5 +25,3 @@ const buildShlinkApiClient = (axios) => ({ url, apiKey }) => {
};
export default buildShlinkApiClient;
export const buildShlinkApiClientWithAxios = buildShlinkApiClient(axios);

View File

@@ -51,3 +51,5 @@ export const useToggle = (initialValue = false) => {
return [ flag, () => setFlag(!flag) ];
};
export const wait = (seconds) => new Promise((resolve) => setTimeout(resolve, seconds));