Ensure forwardCredentials is included when exporting servers as CSV

This commit is contained in:
Alejandro Celaya
2025-04-20 12:45:33 +02:00
parent 1467c8e416
commit 4895cbb9dc
3 changed files with 48 additions and 27 deletions

View File

@@ -45,4 +45,12 @@ export const isNotFoundServer = (server: SelectedServer): server is NotFoundServ
export const getServerId = (server: SelectedServer) => (isServerWithId(server) ? server.id : '');
export const serverWithIdToServerData = ({ name, url, apiKey }: ServerWithId): ServerData => ({ name, url, apiKey });
/**
* Expose values that represent provided server, in a way that can be serialized in JSON or CSV strings.
*/
export const serializeServer = ({ name, url, apiKey, forwardCredentials }: ServerData): Record<string, string> => ({
name,
url,
apiKey,
forwardCredentials: forwardCredentials ? 'true' : 'false',
});

View File

@@ -2,24 +2,27 @@ import type { JsonToCsv } from '../../utils/helpers/csvjson';
import { saveCsv } from '../../utils/helpers/files';
import type { LocalStorage } from '../../utils/services/LocalStorage';
import type { ServersMap } from '../data';
import { serverWithIdToServerData } from '../data';
import { serializeServer } from '../data';
const SERVERS_FILENAME = 'shlink-servers.csv';
export class ServersExporter {
public constructor(
private readonly storage: LocalStorage,
private readonly window: Window,
private readonly jsonToCsv: JsonToCsv,
) {}
readonly #storage: LocalStorage;
readonly #window: Window;
readonly #jsonToCsv: JsonToCsv;
public constructor(storage: LocalStorage, window: Window, jsonToCsv: JsonToCsv) {
this.#storage = storage;
this.#window = window;
this.#jsonToCsv = jsonToCsv;
}
public readonly exportServers = async () => {
const servers = Object.values(this.storage.get<ServersMap>('servers') ?? {}).map(serverWithIdToServerData);
const servers = Object.values(this.#storage.get<ServersMap>('servers') ?? {}).map(serializeServer);
try {
const csv = this.jsonToCsv(servers);
saveCsv(this.window, csv, SERVERS_FILENAME);
const csv = this.#jsonToCsv(servers);
saveCsv(this.#window, csv, SERVERS_FILENAME);
} catch (e) {
// FIXME Handle error
console.error(e);