🚑 Hotfix how the result from the services are awaited

This commit is contained in:
ajnart
2022-06-21 19:59:25 +02:00
parent 45de715390
commit 7aedc4111f

View File

@@ -3,12 +3,11 @@ import { QBittorrent } from '@ctrl/qbittorrent';
import { NormalizedTorrent } from '@ctrl/shared-torrent'; import { NormalizedTorrent } from '@ctrl/shared-torrent';
import { Transmission } from '@ctrl/transmission'; import { Transmission } from '@ctrl/transmission';
import { NextApiRequest, NextApiResponse } from 'next'; import { NextApiRequest, NextApiResponse } from 'next';
import { useConfig } from '../../../tools/state';
import { Config } from '../../../tools/types'; import { Config } from '../../../tools/types';
async function Post(req: NextApiRequest, res: NextApiResponse) { async function Post(req: NextApiRequest, res: NextApiResponse) {
// Get the type of service from the request url // Get the type of service from the request url
const { config }: { config: Config } = useConfig(); const { config }: { config: Config } = req.body;
const qBittorrentServices = config.services.filter((service) => service.type === 'qBittorrent'); const qBittorrentServices = config.services.filter((service) => service.type === 'qBittorrent');
const delugeServices = config.services.filter((service) => service.type === 'Deluge'); const delugeServices = config.services.filter((service) => service.type === 'Deluge');
const transmissionServices = config.services.filter((service) => service.type === 'Transmission'); const transmissionServices = config.services.filter((service) => service.type === 'Transmission');
@@ -21,45 +20,39 @@ async function Post(req: NextApiRequest, res: NextApiResponse) {
message: 'Missing services', message: 'Missing services',
}); });
} }
if (qBittorrentServices) { await Promise.all(
qBittorrentServices.map(async (service) => qBittorrentServices.map((service) =>
torrents.push( new QBittorrent({
...(
await new QBittorrent({
baseUrl: service.url, baseUrl: service.url,
username: service.username, username: service.username,
password: service.password, password: service.password,
}).getAllData() })
).torrents .getAllData()
.then((e) => e.torrents.map((torrent) => torrents.push(torrent)))
) )
); );
} await Promise.all(
if (delugeServices) { delugeServices.map((service) =>
delugeServices.map(async (service) => new Deluge({
torrents.push(
...(
await new Deluge({
baseUrl: service.url, baseUrl: service.url,
password: 'password' in service ? service.password : '', password: service.password,
}).getAllData() })
).torrents .getAllData()
.then((e) => e.torrents.map((torrent) => torrents.push(torrent)))
) )
); );
}
if (transmissionServices) {
// Map transmissionServices // Map transmissionServices
transmissionServices.map(async (service) => { await Promise.all(
torrents.push( transmissionServices.map((service) =>
...( new Transmission({
await new Transmission({
baseUrl: service.url, baseUrl: service.url,
username: 'username' in service ? service.username : '', username: service.username,
password: 'password' in service ? service.password : '', password: service.password,
}).getAllData() })
).torrents .getAllData()
.then((e) => e.torrents.map((torrent) => torrents.push(torrent)))
)
); );
});
}
res.status(200).json(torrents); res.status(200).json(torrents);
} }