Files
blitzkit/packages/website-ue/src/api/abstract.ts
T

116 lines
3.4 KiB
TypeScript
Raw Normal View History

2026-05-15 10:54:36 -05:00
import type { MetadataAccessor } from "@blitzkit/closed";
2026-01-09 13:38:00 -06:00
import type { Strings } from "@blitzkit/i18n";
import locales from "@blitzkit/i18n/locales.json";
2026-05-15 22:30:52 -05:00
import type { TankUpgradePricePresetComponent } from "@protos/blitz/blitz_static_tank_upgrade_price_preset_component";
2026-04-14 20:34:28 -05:00
import type { Avatar } from "@protos/blitzkit/avatar";
import type { AvatarList } from "@protos/blitzkit/avatar_list";
import type { Avatars } from "@protos/blitzkit/avatars";
import type { Background } from "@protos/blitzkit/background";
import type { Backgrounds } from "@protos/blitzkit/backgrounds";
import type { PopularTanks } from "@protos/blitzkit/popular_tanks";
import type { Tank } from "@protos/blitzkit/tank";
import type { TankList } from "@protos/blitzkit/tank_list";
import type { Tanks } from "@protos/blitzkit/tanks";
2026-04-27 19:34:06 -05:00
import type { Tiers } from "@protos/blitzkit/tiers";
2025-11-28 13:51:17 -06:00
export abstract class AbstractAPI {
2026-05-15 10:54:36 -05:00
abstract metadata: MetadataAccessor;
2026-03-20 20:38:04 -05:00
abstract tankList(): Promise<TankList>;
abstract tank(id: string): Promise<Tank>;
2026-03-23 18:06:26 -05:00
abstract tanks(): Promise<Tanks>;
abstract popularTanks(): Promise<PopularTanks>;
2026-05-15 10:54:36 -05:00
abstract tankUpgradePreset(
name: string,
): Promise<TankUpgradePricePresetComponent>;
2026-01-11 13:46:54 -06:00
2026-04-27 19:34:06 -05:00
abstract tiers(): Promise<Tiers>;
2026-03-23 18:06:26 -05:00
abstract strings(locale: string): Promise<Strings>;
abstract gameStrings(locale: string): Promise<Record<string, string>>;
protected abstract _groupedGameStrings(
locale: string,
group: string,
): Promise<Record<string, string>>;
abstract gameStringGroups(): Promise<string[]>;
2026-01-11 13:46:54 -06:00
2026-03-23 18:06:26 -05:00
abstract avatar(id: string): Promise<Avatar>;
abstract avatars(): Promise<Avatars>;
abstract avatarList(): Promise<AvatarList>;
abstract background(id: string): Promise<Background>;
abstract backgrounds(): Promise<Backgrounds>;
2026-01-11 13:46:54 -06:00
2026-05-15 18:16:06 -05:00
abstract currencyIcon(id: string): Promise<string>;
2026-01-09 13:38:00 -06:00
private assertLocale(locale: string) {
const supported = locales.supported.find(
2026-02-03 09:04:36 -06:00
(supported) => supported.locale === locale,
2026-01-09 13:38:00 -06:00
);
if (supported === undefined) {
throw new Error(`Unsupported locale: ${locale}`);
}
return supported;
}
2026-01-11 13:40:01 -06:00
2026-04-29 16:27:13 -05:00
@Cache()
2026-01-14 11:48:55 -06:00
async groupedGameStrings(locale: string, group: string, prefix: boolean) {
2026-01-13 13:18:55 -06:00
this.assertLocale(locale);
2026-03-23 18:06:26 -05:00
const gameStrings = await this._groupedGameStrings(locale, group);
2026-01-11 13:40:01 -06:00
2026-03-23 18:06:26 -05:00
if (!prefix) return gameStrings;
2026-01-14 11:48:55 -06:00
const strings: Record<string, string> = {};
2026-03-23 18:06:26 -05:00
for (const key in gameStrings) {
strings[`${group}__${key}`] = gameStrings[key];
2026-01-14 11:48:55 -06:00
}
return strings;
2026-01-09 13:38:00 -06:00
}
2025-11-28 13:51:17 -06:00
}
2026-03-20 20:38:04 -05:00
2026-04-29 16:27:13 -05:00
export function Cache<Arguments extends unknown[]>(disableInDev = false) {
2026-04-17 23:52:33 -05:00
const cache = new WeakMap<object, Map<string, Promise<unknown>>>();
2026-03-20 20:38:04 -05:00
return function (
_target: unknown,
_propertyKey: string,
descriptor: PropertyDescriptor,
) {
const original = descriptor.value;
2026-04-17 23:52:33 -05:00
descriptor.value = function (...args: Arguments) {
2026-04-29 16:27:13 -05:00
const key = args.join("-");
2026-03-20 20:38:04 -05:00
2026-04-17 23:52:33 -05:00
let thisCache = cache.get(this);
2026-03-20 20:38:04 -05:00
2026-04-17 23:52:33 -05:00
if (!thisCache) {
thisCache = new Map();
cache.set(this, thisCache);
}
2026-03-20 20:38:04 -05:00
2026-04-29 16:27:13 -05:00
const disabled = disableInDev && import.meta.env.DEV;
if (thisCache.has(key) && !disabled) {
2026-04-17 23:52:33 -05:00
return thisCache.get(key);
}
2026-03-20 20:38:04 -05:00
2026-04-17 23:52:33 -05:00
const promise = (async () => {
try {
return await original.apply(this, args);
} catch (error) {
thisCache!.delete(key);
throw error;
}
})();
thisCache.set(key, promise);
return promise;
2026-03-20 20:38:04 -05:00
};
};
}