Files
blitzkit/packages/core/src/blitz/isValidBlitzId.ts
T

30 lines
792 B
TypeScript
Raw Normal View History

2024-09-07 21:45:00 -05:00
import { Region, assertSecret, idToRegion } from '@blitzkit/core';
2024-09-01 15:46:39 -05:00
export async function isValidBlitzId(id: number, token?: string) {
let region: Region;
try {
region = idToRegion(id);
} catch (error) {
return false;
}
const json = await fetch(
2024-09-07 21:45:00 -05:00
`https://api.wotblitz.${region}/wotb/account/info/?account_id=${id}&application_id=${assertSecret(
2024-11-29 12:11:51 -06:00
import.meta.env.PUBLIC_WARGAMING_APPLICATION_ID,
2024-09-07 21:45:00 -05:00
)}${
2024-09-01 15:46:39 -05:00
token === undefined ? '' : `&access_token=${token}`
}&fields=account_id%2C-account_id`,
).then(
(response) =>
response.json() as Promise<
| {
status: 'ok';
data: Record<number, {} | null>;
}
| { status: 'error' }
>,
);
return json.status === 'ok' && json.data[id] !== null;
}