somali-exchange-rates
Version:
πΈπ΄ Comprehensive Somali Exchange Rates platform with real-time rates, transfer fees, alerts, multi-language support, and advanced financial tools
157 lines (149 loc) β’ 4.24 kB
JavaScript
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
}) : x)(function(x) {
if (typeof require !== "undefined") return require.apply(this, arguments);
throw Error('Dynamic require of "' + x + '" is not supported');
});
// src/index.ts
import os from "os";
import path2 from "path";
// src/utils.ts
import fs from "fs/promises";
import path from "path";
async function tryReadJSON(p) {
try {
const raw = await fs.readFile(p, "utf8");
return JSON.parse(raw);
} catch {
return null;
}
}
async function tryWriteJSON(p, data) {
try {
await fs.mkdir(path.dirname(p), { recursive: true });
await fs.writeFile(p, JSON.stringify(data, null, 2), "utf8");
} catch {
}
}
function invert(baseToOthers, sosPerBase) {
const out = {};
for (const [k, v] of Object.entries(baseToOthers)) {
out[k] = v / sosPerBase;
}
return out;
}
function nice(n) {
return Number(n.toFixed(n < 1 ? 4 : 2));
}
// src/providers/exchangeratehost.ts
var API = "https://api.exchangerate.host/latest";
var ExchangerateHostProvider = class {
name = "exchangerate.host";
async fetchRatesSOS() {
const symbols = ["SOS", "EUR", "GBP", "KES", "ETB", "AED", "SAR", "TRY", "CNY", "USD"].join(",");
const url = `${API}?base=USD&symbols=${symbols}`;
const res = await fetch(url);
if (!res.ok) throw new Error(`Provider error ${res.status}`);
const data = await res.json();
const usdTo = data.rates;
const sosPerUsd = usdTo["SOS"];
if (!sosPerUsd) throw new Error("Provider returned no SOS rate");
return invert(usdTo, sosPerUsd);
}
};
// src/data/seed.json
var seed_default = {
USD: 175e-5,
EUR: 16e-4,
GBP: 135e-5,
KES: 0.225,
ETB: 0.102,
AED: 64e-4,
SAR: 66e-4,
TRY: 0.056,
CNY: 0.012
};
// src/cache.ts
var memoryCache = null;
function getMemoryCache() {
return memoryCache;
}
function setMemoryCache(c) {
memoryCache = c;
}
// src/index.ts
function defaultPersistPath() {
return path2.join(os.homedir(), ".sosx", "cache.json");
}
async function readCache(persistPath) {
const mem = getMemoryCache();
if (mem) return mem;
if (persistPath) return await tryReadJSON(persistPath);
return null;
}
async function writeCache(c, persistPath) {
setMemoryCache(c);
if (persistPath) await tryWriteJSON(persistPath, c);
}
async function getRates(options = {}) {
const {
provider = new ExchangerateHostProvider(),
ttlMs = 1e3 * 60 * 60 * 6,
// 6 hours
persistPath = defaultPersistPath(),
offline = false
} = options;
const cached = await readCache(persistPath);
const fresh = cached && Date.now() - cached.at < ttlMs;
if (fresh) return cached.rates;
if (offline) {
if (cached) return cached.rates;
return seed_default;
}
try {
const live = await provider.fetchRatesSOS();
const c = { at: Date.now(), rates: live };
await writeCache(c, persistPath);
return live;
} catch {
if (cached) return cached.rates;
return seed_default;
}
}
async function getRate(target, options) {
const table = await getRates(options);
return table[target];
}
async function convert(amount, from, to, options) {
if (from === to) return amount;
const table = await getRates(options);
if (from === "SOS") {
return amount * table[to];
}
if (to === "SOS") {
return amount * (1 / table[from]);
}
const inSOS = amount * (1 / table[from]);
return inSOS * table[to];
}
function formatSOS(value) {
return new Intl.NumberFormat("so-SO", { style: "currency", currency: "SOS", currencyDisplay: "symbol" }).format(value);
}
function formatCurrency(value, currency) {
return new Intl.NumberFormat("so-SO", { style: "currency", currency, currencyDisplay: "symbol" }).format(value);
}
async function quote(from, to, amount = 1, options) {
const out = await convert(amount, from, to, options);
const left = formatCurrency(amount, from);
const right = to === "SOS" ? formatSOS(nice(out)) : formatCurrency(nice(out), to);
return `${left} = ${right}`;
}
export {
__require,
getRates,
getRate,
convert,
formatSOS,
formatCurrency,
quote
};