26 lines
839 B
TypeScript
26 lines
839 B
TypeScript
import { formatMoney } from "@vmall/shared";
|
|
import type { Currency } from "@vmall/shared";
|
|
|
|
/** Currency-aware money formatter: exponents come from the API, cached app-wide. */
|
|
export function useMoney() {
|
|
const { $api } = useNuxtApp();
|
|
const { locale } = useI18n();
|
|
const currencies = useState<Currency[]>("vmall.currencies", () => []);
|
|
|
|
async function load(): Promise<void> {
|
|
if (currencies.value.length > 0) return;
|
|
try {
|
|
currencies.value = await $api.listCurrencies();
|
|
} catch {
|
|
/* keep empty; fmt falls back to exponent 2 */
|
|
}
|
|
}
|
|
|
|
function fmt(amountMinor: number, currency: string): string {
|
|
const exponent = currencies.value.find((c) => c.code === currency)?.exponent ?? 2;
|
|
return formatMoney(amountMinor, currency, exponent, locale.value);
|
|
}
|
|
|
|
return { currencies, load, fmt };
|
|
}
|