2023-07-11 19:54:57 +00:00
|
|
|
import { BlitzServer } from '../../constants/servers';
|
|
|
|
|
import { Clan, ClanList } from '../../types/clanList';
|
|
|
|
|
import { WARGAMING_APPLICATION_ID } from '../node/arguments';
|
|
|
|
|
import getWargamingResponse from './getWargamingResponse';
|
2023-05-16 02:02:50 +00:00
|
|
|
|
|
|
|
|
export type ClanListWithServer = (Clan & {
|
|
|
|
|
server: 'com' | 'eu' | 'asia';
|
|
|
|
|
})[];
|
|
|
|
|
|
2023-06-27 02:13:57 +00:00
|
|
|
export default async function listClans(search: string, limit = 9) {
|
2023-05-20 01:22:20 +00:00
|
|
|
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
|
|
|
}
|