Add advanced options to servers

This commit is contained in:
Alejandro Celaya
2025-04-20 11:12:43 +02:00
parent 4947e0490a
commit e997d11c2c
6 changed files with 52 additions and 22 deletions

View File

@@ -4,7 +4,7 @@ import type { GetState } from '../../container/types';
import type { ServerWithId } from '../../servers/data';
import { hasServerData } from '../../servers/data';
const apiClients: Record<string, ShlinkApiClient> = {};
const apiClients: Map<string, ShlinkApiClient> = new Map();
const isGetState = (getStateOrSelectedServer: GetState | ServerWithId): getStateOrSelectedServer is GetState =>
typeof getStateOrSelectedServer === 'function';
@@ -18,19 +18,22 @@ const getSelectedServerFromState = (getState: GetState): ServerWithId => {
};
export const buildShlinkApiClient = (httpClient: HttpClient) => (getStateOrSelectedServer: GetState | ServerWithId) => {
const { url: baseUrl, apiKey } = isGetState(getStateOrSelectedServer)
const { url: baseUrl, apiKey, forwardCredentials } = isGetState(getStateOrSelectedServer)
? getSelectedServerFromState(getStateOrSelectedServer)
: getStateOrSelectedServer;
const serverKey = `${apiKey}_${baseUrl}`;
const serverKey = `${apiKey}_${baseUrl}_${forwardCredentials ? 'forward' : 'no-forward'}`;
const existingApiClient = apiClients.get(serverKey);
const apiClient = apiClients[serverKey] ?? new ShlinkApiClient(
if (existingApiClient) {
return existingApiClient;
}
const apiClient = new ShlinkApiClient(
httpClient,
{ apiKey, baseUrl },
// FIXME Disabling this as it's breaking existing Shlink servers as configured out of the box
// { requestCredentials: 'include' },
{ requestCredentials: forwardCredentials ? 'include' : undefined },
);
apiClients[serverKey] = apiClient;
apiClients.set(serverKey, apiClient);
return apiClient;
};