Replaced unmaintained dependency

This commit is contained in:
Alejandro Celaya
2022-03-31 20:18:05 +02:00
parent e2c8551baf
commit e875e05538
13 changed files with 189 additions and 65 deletions

View File

@@ -1,8 +1,8 @@
import { values } from 'ramda';
import { CsvJson } from 'csvjson';
import LocalStorage from '../../utils/services/LocalStorage';
import { ServersMap, serverWithIdToServerData } from '../data';
import { saveCsv } from '../../utils/helpers/files';
import { JsonToCsv } from '../../utils/helpers/csvjson';
const SERVERS_FILENAME = 'shlink-servers.csv';
@@ -10,14 +10,14 @@ export default class ServersExporter {
public constructor(
private readonly storage: LocalStorage,
private readonly window: Window,
private readonly csvjson: CsvJson,
private readonly jsonToCsv: JsonToCsv,
) {}
public readonly exportServers = async () => {
const servers = values(this.storage.get<ServersMap>('servers') ?? {}).map(serverWithIdToServerData);
try {
const csv = this.csvjson.toCSV(servers, { headers: 'key' });
const csv = this.jsonToCsv(servers);
saveCsv(this.window, csv, SERVERS_FILENAME);
} catch (e) {

View File

@@ -1,5 +1,5 @@
import { CsvJson } from 'csvjson';
import { ServerData } from '../data';
import { CsvToJson } from '../../utils/helpers/csvjson';
const validateServer = (server: any): server is ServerData =>
typeof server.url === 'string' && typeof server.apiKey === 'string' && typeof server.name === 'string';
@@ -8,7 +8,7 @@ const validateServers = (servers: any): servers is ServerData[] =>
Array.isArray(servers) && servers.every(validateServer);
export class ServersImporter {
public constructor(private readonly csvJson: CsvJson, private readonly fileReaderFactory: () => FileReader) {}
public constructor(private readonly csvToJson: CsvToJson, private readonly fileReaderFactory: () => FileReader) {}
public readonly importServersFromFile = async (file?: File | null): Promise<ServerData[]> => {
if (!file) {
@@ -18,11 +18,11 @@ export class ServersImporter {
const reader = this.fileReaderFactory();
return new Promise((resolve, reject) => {
reader.addEventListener('loadend', (e: ProgressEvent<FileReader>) => {
reader.addEventListener('loadend', async (e: ProgressEvent<FileReader>) => {
try {
// TODO Read as stream, otherwise, if the file is too big, this will block the browser tab
const content = e.target?.result?.toString() ?? '';
const servers = this.csvJson.toObject(content);
const servers = await this.csvToJson(content);
if (!validateServers(servers)) {
throw new Error('Provided file does not have the right format.');

View File

@@ -1,4 +1,3 @@
import csvjson from 'csvjson';
import Bottle from 'bottlejs';
import CreateServer from '../CreateServer';
import ServersDropdown from '../ServersDropdown';
@@ -69,10 +68,9 @@ const provideServices = (bottle: Bottle, connect: ConnectDecorator) => {
));
// Services
bottle.constant('csvjson', csvjson);
bottle.constant('fileReaderFactory', () => new FileReader());
bottle.service('ServersImporter', ServersImporter, 'csvjson', 'fileReaderFactory');
bottle.service('ServersExporter', ServersExporter, 'Storage', 'window', 'csvjson');
bottle.service('ServersImporter', ServersImporter, 'csvToJson', 'fileReaderFactory');
bottle.service('ServersExporter', ServersExporter, 'Storage', 'window', 'jsonToCsv');
// Actions
bottle.serviceFactory('selectServer', selectServer, 'buildShlinkApiClient', 'loadMercureInfo');