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

51 lines
1.6 KiB
TypeScript
Raw Normal View History

import { Region } from '../../constants/regions';
2023-09-23 21:51:00 +00:00
import { WARGAMING_APPLICATION_ID } from '../../constants/wargamingApplicationID';
2023-07-11 19:54:57 +00:00
import { Clan, ClanList } from '../../types/clanList';
import getWargamingResponse from './getWargamingResponse';
2023-05-16 02:02:50 +00:00
2023-09-07 03:24:07 +00:00
export type ClanListWithRegion = (Clan & {
region: 'com' | 'eu' | 'asia';
2023-05-16 02:02:50 +00:00
})[];
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-09-23 21:51:00 +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,
2023-09-07 03:24:07 +00:00
region: 'com' as Region,
2023-05-16 02:02:50 +00:00
})),
2023-05-20 01:22:20 +00:00
),
getWargamingResponse<ClanList>(
2023-09-23 21:51:00 +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,
2023-09-07 03:24:07 +00:00
region: 'eu' as Region,
2023-05-16 02:02:50 +00:00
})),
2023-05-20 01:22:20 +00:00
),
getWargamingResponse<ClanList>(
2023-09-23 21:51:00 +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,
2023-09-07 03:24:07 +00:00
region: 'asia' as Region,
2023-05-16 02:02:50 +00:00
})),
2023-05-20 01:22:20 +00:00
),
])
)
.filter(Boolean)
.flat();
2023-05-16 02:02:50 +00:00
}