mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2024-12-23 17:40:23 +03:00
32 lines
1.3 KiB
TypeScript
32 lines
1.3 KiB
TypeScript
import type { HttpClient } from '@shlinkio/shlink-js-sdk';
|
|
import { ShlinkApiClient } from '@shlinkio/shlink-js-sdk';
|
|
import type { GetState } from '../../container/types';
|
|
import type { ServerWithId } from '../../servers/data';
|
|
import { hasServerData } from '../../servers/data';
|
|
|
|
const apiClients: Record<string, ShlinkApiClient> = {};
|
|
|
|
const isGetState = (getStateOrSelectedServer: GetState | ServerWithId): getStateOrSelectedServer is GetState =>
|
|
typeof getStateOrSelectedServer === 'function';
|
|
const getSelectedServerFromState = (getState: GetState): ServerWithId => {
|
|
const { selectedServer } = getState();
|
|
if (!hasServerData(selectedServer)) {
|
|
throw new Error('There\'s no selected server or it is not found');
|
|
}
|
|
|
|
return selectedServer;
|
|
};
|
|
|
|
export const buildShlinkApiClient = (httpClient: HttpClient) => (getStateOrSelectedServer: GetState | ServerWithId) => {
|
|
const { url: baseUrl, apiKey } = isGetState(getStateOrSelectedServer)
|
|
? getSelectedServerFromState(getStateOrSelectedServer)
|
|
: getStateOrSelectedServer;
|
|
const serverKey = `${apiKey}_${baseUrl}`;
|
|
|
|
const apiClient = apiClients[serverKey] ?? new ShlinkApiClient(httpClient, { apiKey, baseUrl });
|
|
apiClients[serverKey] = apiClient;
|
|
|
|
return apiClient;
|
|
};
|
|
|
|
export type ShlinkApiClientBuilder = ReturnType<typeof buildShlinkApiClient>;
|