mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-23 11:39:50 +02:00
feat: cleaner fetch blitz func
This commit is contained in:
1 parent
076785a9ad
commit
3e7a73c528
13 files changed
+82
-85
No files matched your search
@@ -1,3 +1,5 @@
|
||||
import { Region } from '../../constants/regions';
|
||||
import { WARGAMING_APPLICATION_ID } from '../../constants/wargamingApplicationID';
|
||||
import { patientFetch } from '../blitzkrieg/patientFetch';
|
||||
|
||||
type BlitzResponse<Data extends object> =
|
||||
@@ -58,10 +60,20 @@ async function manageQueue() {
|
||||
}
|
||||
}
|
||||
|
||||
export default function fetchBlitz<Data extends object>(url: string) {
|
||||
export default function fetchBlitz<Data extends object>(
|
||||
region: Region,
|
||||
path: string,
|
||||
params: Record<string, string | number | undefined> = {},
|
||||
) {
|
||||
return new Promise<Data>((resolve) => {
|
||||
queue.push({
|
||||
url,
|
||||
url: `https://api.wotblitz.${region}/wotb/${path}/?${Object.entries({
|
||||
application_id: WARGAMING_APPLICATION_ID,
|
||||
...params,
|
||||
})
|
||||
.filter(([_, value]) => value !== undefined)
|
||||
.map(([key, value]) => `${key}=${encodeURIComponent(value!)}`)
|
||||
.join('&')}`,
|
||||
resolve(data) {
|
||||
resolve(data as Data);
|
||||
},
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { Region } from '../../constants/regions';
|
||||
import { WARGAMING_APPLICATION_ID } from '../../constants/wargamingApplicationID';
|
||||
import fetchBlitz from './fetchBlitz';
|
||||
import { normalizeIds } from './normalizeIds';
|
||||
|
||||
@@ -23,9 +22,9 @@ export async function getAccountAchievements<
|
||||
: IndividualAccountAchievements[],
|
||||
>(region: Region, ids: Ids) {
|
||||
const object = await fetchBlitz<AccountAchievements>(
|
||||
`https://api.wotblitz.${region}/wotb/account/achievements/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${normalizeIds(
|
||||
ids,
|
||||
)}`,
|
||||
region,
|
||||
'account/achievements',
|
||||
{ account_id: normalizeIds(ids) },
|
||||
);
|
||||
|
||||
if (typeof ids === 'number') {
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { Region } from '../../constants/regions';
|
||||
import { WARGAMING_APPLICATION_ID } from '../../constants/wargamingApplicationID';
|
||||
import fetchBlitz from './fetchBlitz';
|
||||
import { normalizeIds } from './normalizeIds';
|
||||
|
||||
@@ -97,11 +96,10 @@ export async function getAccountInfo<
|
||||
? IndividualAccountInfo
|
||||
: IndividualAccountInfo[],
|
||||
>(region: Region, ids: Ids, extra: string[] = []) {
|
||||
const object = await fetchBlitz<AccountInfo>(
|
||||
`https://api.wotblitz.${region}/wotb/account/info/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${normalizeIds(
|
||||
ids,
|
||||
)}${extra.length === 0 ? '' : `&extra=${extra.join(',')}`}`,
|
||||
);
|
||||
const object = await fetchBlitz<AccountInfo>(region, 'account/info', {
|
||||
account_id: normalizeIds(ids),
|
||||
extra: extra.length === 0 ? undefined : extra.join(','),
|
||||
});
|
||||
|
||||
if (typeof ids === 'number') {
|
||||
return object[ids as number] as ReturnType;
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
import { Region } from '../../constants/regions';
|
||||
import { WARGAMING_APPLICATION_ID } from '../../constants/wargamingApplicationID';
|
||||
import fetchBlitz from './fetchBlitz';
|
||||
import { normalizeExtra } from './normalizeExtra';
|
||||
import { normalizeIds } from './normalizeIds';
|
||||
|
||||
type IndividualClanAccountInfo = {
|
||||
@@ -30,11 +28,14 @@ export async function getClanAccountInfo<
|
||||
ReturnType = Ids extends number
|
||||
? IndividualClanAccountInfo
|
||||
: IndividualClanAccountInfo[],
|
||||
>(region: Region, ids: Ids, extra?: string[]) {
|
||||
>(region: Region, ids: Ids, extra: string[] = []) {
|
||||
const object = await fetchBlitz<ClanAccountInfo>(
|
||||
`https://api.wotblitz.${region}/wotb/clans/accountinfo/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${normalizeIds(
|
||||
ids,
|
||||
)}${normalizeExtra(extra)}`,
|
||||
region,
|
||||
'clans/accountinfo',
|
||||
{
|
||||
account_id: normalizeIds(ids),
|
||||
extra: extra.length === 0 ? undefined : extra.join(','),
|
||||
},
|
||||
);
|
||||
|
||||
if (typeof ids === 'number') {
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { Region } from '../../constants/regions';
|
||||
import { WARGAMING_APPLICATION_ID } from '../../constants/wargamingApplicationID';
|
||||
import fetchBlitz from './fetchBlitz';
|
||||
import { normalizeIds } from './normalizeIds';
|
||||
|
||||
@@ -40,11 +39,9 @@ export async function getClanInfo<
|
||||
Ids extends number | number[],
|
||||
ReturnType = Ids extends number ? IndividualClanInfo : IndividualClanInfo[],
|
||||
>(region: Region, ids: Ids) {
|
||||
const object = await fetchBlitz<ClanInfo>(
|
||||
`https://api.wotblitz.${region}/wotb/clans/info/?application_id=${WARGAMING_APPLICATION_ID}&clan_id=${normalizeIds(
|
||||
ids,
|
||||
)}`,
|
||||
);
|
||||
const object = await fetchBlitz<ClanInfo>(region, 'clans/info', {
|
||||
clan_id: normalizeIds(ids),
|
||||
});
|
||||
|
||||
if (typeof ids === 'number') {
|
||||
return object[ids as number] as ReturnType;
|
||||
|
||||
@@ -1,19 +1,11 @@
|
||||
import { Locale } from 'discord.js';
|
||||
import { Region } from '../../constants/regions';
|
||||
import { WARGAMING_APPLICATION_ID } from '../../constants/wargamingApplicationID';
|
||||
import { TanksStats } from '../../types/tanksStats';
|
||||
import { translator } from '../localization/translator';
|
||||
import fetchBlitz from './fetchBlitz';
|
||||
|
||||
export default async function getTankStats(
|
||||
region: Region,
|
||||
id: number,
|
||||
locale: Locale,
|
||||
) {
|
||||
const { t } = translator(locale);
|
||||
const tankStats = await fetchBlitz<TanksStats>(
|
||||
`https://api.wotblitz.${region}/wotb/tanks/stats/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}`,
|
||||
);
|
||||
export default async function getTankStats(region: Region, id: number) {
|
||||
const tankStats = await fetchBlitz<TanksStats>(region, 'tanks/stats', {
|
||||
account_id: id,
|
||||
});
|
||||
|
||||
return tankStats[id];
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
import { Region } from '../../constants/regions';
|
||||
import { WARGAMING_APPLICATION_ID } from '../../constants/wargamingApplicationID';
|
||||
import { ClanList } from '../../types/clanList';
|
||||
import fetchBlitz from './fetchBlitz';
|
||||
|
||||
@@ -15,9 +14,10 @@ export default async function searchClansAcrossRegions(
|
||||
|
||||
return (
|
||||
await Promise.all([
|
||||
fetchBlitz<ClanList>(
|
||||
`https://api.wotblitz.com/wotb/clans/list/?application_id=${WARGAMING_APPLICATION_ID}&search=${search}&limit=${normalizedLimit}`,
|
||||
).then((value) =>
|
||||
fetchBlitz<ClanList>('com', 'clans/list', {
|
||||
search,
|
||||
limit: normalizedLimit,
|
||||
}).then((value) =>
|
||||
value
|
||||
.map((account) => ({
|
||||
...account,
|
||||
@@ -25,9 +25,10 @@ export default async function searchClansAcrossRegions(
|
||||
}))
|
||||
.splice(0, normalizedLimit),
|
||||
),
|
||||
fetchBlitz<ClanList>(
|
||||
`https://api.wotblitz.eu/wotb/clans/list/?application_id=${WARGAMING_APPLICATION_ID}&search=${search}&limit=${normalizedLimit}`,
|
||||
).then((value) =>
|
||||
fetchBlitz<ClanList>('eu', 'clans/list', {
|
||||
search,
|
||||
limit: normalizedLimit,
|
||||
}).then((value) =>
|
||||
value
|
||||
.map((account) => ({
|
||||
...account,
|
||||
@@ -35,9 +36,10 @@ export default async function searchClansAcrossRegions(
|
||||
}))
|
||||
.splice(0, normalizedLimit),
|
||||
),
|
||||
fetchBlitz<ClanList>(
|
||||
`https://api.wotblitz.asia/wotb/clans/list/?application_id=${WARGAMING_APPLICATION_ID}&search=${search}&limit=${normalizedLimit}`,
|
||||
).then((value) =>
|
||||
fetchBlitz<ClanList>('asia', 'clans/list', {
|
||||
search,
|
||||
limit: normalizedLimit,
|
||||
}).then((value) =>
|
||||
value
|
||||
.map((account) => ({
|
||||
...account,
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { Region } from '../../../constants/regions';
|
||||
import { WARGAMING_APPLICATION_ID } from '../../../constants/wargamingApplicationID';
|
||||
import fetchBlitz from '../fetchBlitz';
|
||||
import { usernamePattern } from './constants';
|
||||
|
||||
@@ -18,14 +17,14 @@ export default async function searchPlayersAcrossRegions(
|
||||
) {
|
||||
const trimmedSearch = search.trim();
|
||||
const normalizedLimit = Math.floor(limit / 3);
|
||||
const encodedSearch = encodeURIComponent(trimmedSearch);
|
||||
|
||||
if (usernamePattern.test(trimmedSearch)) {
|
||||
return (
|
||||
await Promise.all([
|
||||
fetchBlitz<AccountList>(
|
||||
`https://api.wotblitz.com/wotb/account/list/?application_id=${WARGAMING_APPLICATION_ID}&search=${encodedSearch}&limit=${normalizedLimit}`,
|
||||
).then(
|
||||
fetchBlitz<AccountList>('com', 'account/list', {
|
||||
search,
|
||||
limit: normalizedLimit,
|
||||
}).then(
|
||||
(value) =>
|
||||
value &&
|
||||
value.map((account) => ({
|
||||
@@ -33,9 +32,10 @@ export default async function searchPlayersAcrossRegions(
|
||||
region: 'com' as Region,
|
||||
})),
|
||||
),
|
||||
fetchBlitz<AccountList>(
|
||||
`https://api.wotblitz.eu/wotb/account/list/?application_id=${WARGAMING_APPLICATION_ID}&search=${encodedSearch}&limit=${normalizedLimit}`,
|
||||
).then(
|
||||
fetchBlitz<AccountList>('eu', 'account/list', {
|
||||
search,
|
||||
limit: normalizedLimit,
|
||||
}).then(
|
||||
(value) =>
|
||||
value &&
|
||||
value.map((account) => ({
|
||||
@@ -43,9 +43,10 @@ export default async function searchPlayersAcrossRegions(
|
||||
region: 'eu' as Region,
|
||||
})),
|
||||
),
|
||||
fetchBlitz<AccountList>(
|
||||
`https://api.wotblitz.asia/wotb/account/list/?application_id=${WARGAMING_APPLICATION_ID}&search=${encodedSearch}&limit=${normalizedLimit}`,
|
||||
).then(
|
||||
fetchBlitz<AccountList>('asia', 'account/list', {
|
||||
search,
|
||||
limit: normalizedLimit,
|
||||
}).then(
|
||||
(value) =>
|
||||
value &&
|
||||
value.map((account) => ({
|
||||
|
||||
Reference in new issue
Block a user