shlink-web-client/src/utils/services/ShlinkApiClientBuilder.js

24 lines
652 B
JavaScript

import ShlinkApiClient from './ShlinkApiClient';
const apiClients = {};
const getSelectedServerFromState = (getState) => {
const { selectedServer } = getState();
return selectedServer;
};
const buildShlinkApiClient = (axios) => (getStateOrSelectedServer) => {
const { url, apiKey } = typeof getStateOrSelectedServer === 'function'
? getSelectedServerFromState(getStateOrSelectedServer)
: getStateOrSelectedServer;
const clientKey = `${url}_${apiKey}`;
if (!apiClients[clientKey]) {
apiClients[clientKey] = new ShlinkApiClient(axios, url, apiKey);
}
return apiClients[clientKey];
};
export default buildShlinkApiClient;