mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-03-03 06:11:48 +00:00
Update ServersImporter so that it takes into consideration forwardCredentials
This commit is contained in:
@@ -54,3 +54,22 @@ export const serializeServer = ({ name, url, apiKey, forwardCredentials }: Serve
|
||||
apiKey,
|
||||
forwardCredentials: forwardCredentials ? 'true' : 'false',
|
||||
});
|
||||
|
||||
const validateServerData = (server: any): server is ServerData =>
|
||||
typeof server.url === 'string' && typeof server.apiKey === 'string' && typeof server.name === 'string';
|
||||
|
||||
/**
|
||||
* Provided a record, it picks the right properties to build a ServerData object.
|
||||
* @throws Error If any of the required ServerData properties is missing.
|
||||
*/
|
||||
export const deserializeServer = (potentialServer: Record<string, unknown>): ServerData => {
|
||||
const { forwardCredentials, ...serverData } = potentialServer;
|
||||
if (!validateServerData(serverData)) {
|
||||
throw new Error('Server is missing required "url", "apiKey" and/or "name" properties');
|
||||
}
|
||||
|
||||
return {
|
||||
...serverData,
|
||||
forwardCredentials: forwardCredentials === 'true',
|
||||
};
|
||||
};
|
||||
|
||||
@@ -1,14 +1,20 @@
|
||||
import type { CsvToJson } from '../../utils/helpers/csvjson';
|
||||
import type { ServerData } from '../data';
|
||||
import { deserializeServer } from '../data';
|
||||
|
||||
const validateServer = (server: any): server is ServerData =>
|
||||
typeof server.url === 'string' && typeof server.apiKey === 'string' && typeof server.name === 'string';
|
||||
|
||||
const validateServers = (servers: any): servers is ServerData[] =>
|
||||
Array.isArray(servers) && servers.every(validateServer);
|
||||
const validateAndDeserializeServers = (servers: unknown): ServerData[] => {
|
||||
if (!Array.isArray(servers)) {
|
||||
throw new Error('Provided file does not have the right format.');
|
||||
}
|
||||
return servers.map(deserializeServer);
|
||||
};
|
||||
|
||||
export class ServersImporter {
|
||||
public constructor(private readonly csvToJson: CsvToJson) {}
|
||||
readonly #csvToJson: CsvToJson;
|
||||
|
||||
public constructor(csvToJson: CsvToJson) {
|
||||
this.#csvToJson = csvToJson;
|
||||
}
|
||||
|
||||
public async importServersFromFile(file: File | null | undefined): Promise<ServerData[]> {
|
||||
if (!file) {
|
||||
@@ -16,12 +22,8 @@ export class ServersImporter {
|
||||
}
|
||||
|
||||
const content = await file.text();
|
||||
const servers = await this.csvToJson(content);
|
||||
const servers = await this.#csvToJson(content);
|
||||
|
||||
if (!validateServers(servers)) {
|
||||
throw new Error('Provided file does not have the right format.');
|
||||
}
|
||||
|
||||
return servers;
|
||||
return validateAndDeserializeServers(servers);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user