Files
blitzkit/src/core/blitz/listClansPanServer.ts
T

51 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-05-16 02:02:50 +00:00
import { BlitzServer } from '../../constants/servers.js';
import { Clan, ClanList } from '../../types/clanList.js';
2023-06-27 01:43:30 +00:00
import { WARGAMING_APPLICATION_ID } from '../node/args.js';
2023-05-16 02:02:50 +00:00
import getWargamingResponse from './getWargamingResponse.js';
export type ClanListWithServer = (Clan & {
server: 'com' | 'eu' | 'asia';
})[];
2023-05-20 01:22:20 +00:00
export default async function listClansPanServer(search: string, limit = 9) {
const trimmed = search.trim();
if (trimmed.length < 3 && trimmed.length > 100) return [];
const normalizedLimit = Math.round(limit / 3);
2023-05-16 02:02:50 +00:00
2023-05-20 01:22:20 +00:00
return (
await Promise.all([
getWargamingResponse<ClanList>(
2023-06-12 01:17:26 +00:00
`https://api.wotblitz.com/wotb/clans/list/?application_id=${WARGAMING_APPLICATION_ID}&search=${search}&limit=${normalizedLimit}`,
2023-05-20 01:22:20 +00:00
).then(
(value) =>
value &&
value.map((account) => ({
2023-05-16 02:02:50 +00:00
...account,
server: 'com' as BlitzServer,
})),
2023-05-20 01:22:20 +00:00
),
getWargamingResponse<ClanList>(
2023-06-12 01:17:26 +00:00
`https://api.wotblitz.eu/wotb/clans/list/?application_id=${WARGAMING_APPLICATION_ID}&search=${search}&limit=${normalizedLimit}`,
2023-05-20 01:22:20 +00:00
).then(
(value) =>
value &&
value.map((account) => ({
2023-05-16 02:02:50 +00:00
...account,
server: 'eu' as BlitzServer,
})),
2023-05-20 01:22:20 +00:00
),
getWargamingResponse<ClanList>(
2023-06-12 01:17:26 +00:00
`https://api.wotblitz.asia/wotb/clans/list/?application_id=${WARGAMING_APPLICATION_ID}&search=${search}&limit=${normalizedLimit}`,
2023-05-20 01:22:20 +00:00
).then(
(value) =>
value &&
value.map((account) => ({
2023-05-16 02:02:50 +00:00
...account,
server: 'asia' as BlitzServer,
})),
2023-05-20 01:22:20 +00:00
),
])
)
.filter(Boolean)
.flat();
2023-05-16 02:02:50 +00:00
}