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

56 lines
1.7 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';
2023-10-20 10:50:54 -05:00
import fetchBlitz from './fetchBlitz';
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-10-14 11:15:42 -05:00
export default async function searchClansAcrossRegions(
search: string,
2023-11-01 11:45:19 -05:00
limit = 25,
2023-10-14 11:15:42 -05:00
) {
2023-05-20 01:22:20 +00:00
const trimmed = search.trim();
if (trimmed.length < 3 && trimmed.length > 100) return [];
2023-11-01 11:45:19 -05:00
const normalizedLimit = Math.floor(limit / 3);
// TODO: remove splice once wargaming fixes the functionality of the limit felid
2023-05-16 02:02:50 +00:00
2023-05-20 01:22:20 +00:00
return (
await Promise.all([
2023-10-14 10:58:48 -05:00
fetchBlitz<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-11-01 11:45:19 -05:00
).then((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-11-01 11:45:19 -05:00
}))
.splice(0, normalizedLimit),
2023-05-20 01:22:20 +00:00
),
2023-10-14 10:58:48 -05:00
fetchBlitz<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-11-01 11:45:19 -05:00
).then((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-11-01 11:45:19 -05:00
}))
.splice(0, normalizedLimit),
2023-05-20 01:22:20 +00:00
),
2023-10-14 10:58:48 -05:00
fetchBlitz<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-11-01 11:45:19 -05:00
).then((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-11-01 11:45:19 -05:00
}))
.splice(0, normalizedLimit),
2023-05-20 01:22:20 +00:00
),
])
)
.filter(Boolean)
.flat();
2023-05-16 02:02:50 +00:00
}