diff --git a/src/structures/CurrencyCache.ts b/src/structures/CurrencyCache.ts index 3cee1a3..bd90d98 100644 --- a/src/structures/CurrencyCache.ts +++ b/src/structures/CurrencyCache.ts @@ -1,3 +1,11 @@ +import { CryptoCurrency, ExchangeRate, FiatCurrency, TipccClient } from '..'; +import { + RESTGetAPICurrenciesCryptoCurrenciesResult, + RESTGetAPICurrenciesFiatsResult, + RESTGetAPICurrenciesRatesResult, + Routes, +} from '@tipccjs/tipcc-api-types/v0'; + /** * A class extending Array holding a cache of objects with type T. * @@ -5,26 +13,15 @@ * @typeParam T - The object type this {@link CurrencyCache} will hold */ export class CurrencyCache extends Array { - private refreshFunction: () => T[] | Promise; + public client: TipccClient; /** * Create a CurrencyCache. - * @param refreshFunction The refresh function which returns new values to insert to this cache + * @param client The client which instantiated this CurrencyCache */ - constructor(refreshFunction: () => T[] | Promise) { + constructor(client: TipccClient) { super(); - - this.refreshFunction = refreshFunction; - } - - /** - * Refresh this CurrencyCache with new values received from refreshFunction. - */ - public async refresh(): Promise> { - const refreshedData = await this.refreshFunction(); - this.splice(0, this.length, refreshedData); - - return this; + this.client = client; } /** @@ -37,3 +34,63 @@ export class CurrencyCache extends Array { return null; } } + +/** + * A class extending {@link CurrencyCache} holding cryptocurrencies. + * + * @category Client utilities + */ +export class CryptocurrencyCache extends CurrencyCache { + public async refresh(): Promise { + const { cryptocurrencies } = (await this.client.REST.get( + Routes.currenciesCryptocurrencies(), + )) as RESTGetAPICurrenciesCryptoCurrenciesResult; + + this.splice( + 0, + this.length, + ...cryptocurrencies.map((c) => new CryptoCurrency(c)), + ); + + return this; + } +} + +/** + * A class extending {@link CurrencyCache} holding fiats. + * + * @category Client utilities + */ +export class FiatCache extends CurrencyCache { + public async refresh(): Promise { + const { fiats } = (await this.client.REST.get( + Routes.currenciesFiats(), + )) as RESTGetAPICurrenciesFiatsResult; + + this.splice(0, this.length, ...fiats.map((f) => new FiatCurrency(f))); + + return this; + } +} + +/** + * A class extending {@link CurrencyCache} holding exchange rates. + * + * @category Client utilities + */ +export class ExchangeRateCache extends CurrencyCache { + public async refresh(): Promise { + const { rates } = (await this.client.REST.get( + Routes.currenciesRates(), + )) as RESTGetAPICurrenciesRatesResult; + //return rates.map((r) => new ExchangeRate(r, this)); + + this.splice( + 0, + this.length, + ...rates.map((r) => new ExchangeRate(r, this.client)), + ); + + return this; + } +} diff --git a/src/structures/TipccClient.ts b/src/structures/TipccClient.ts index e726bf1..cdbdca9 100644 --- a/src/structures/TipccClient.ts +++ b/src/structures/TipccClient.ts @@ -1,15 +1,10 @@ import { EventEmitter } from 'node:events'; import { RequestHandler } from './RequestHandler'; -import { CurrencyCache } from './CurrencyCache'; -import { CryptoCurrency, FiatCurrency } from './Currency'; -import { ExchangeRate } from './ExchangeRate'; - import { - RESTGetAPICurrenciesCryptoCurrenciesResult, - RESTGetAPICurrenciesFiatsResult, - RESTGetAPICurrenciesRatesResult, - Routes, -} from '@tipccjs/tipcc-api-types/v0'; + CryptocurrencyCache, + ExchangeRateCache, + FiatCache, +} from './CurrencyCache'; import { WalletManager } from './Manager/WalletManager'; import { TransactionManager } from './Manager/TransactionManager'; @@ -27,7 +22,7 @@ export class TipccClient extends EventEmitter { /** The tip.cc API token this client uses */ public token: string; - /** The {@link RequestHandler} this client uses */ + /** The {@link RequestHandler} for this client */ public REST: RequestHandler; /** The {@link WalletManager} for this client */ @@ -36,14 +31,14 @@ export class TipccClient extends EventEmitter { /** The {@link TransactionManager} for this client */ public transactions: TransactionManager; - /** The {@link CurrencyCache} for cryptocurrencies */ - public cryptos: CurrencyCache; + /** The {@link CryptocurrencyCache} for this client */ + public cryptos: CryptocurrencyCache; - /** The {@link CurrencyCache} for fiat currencies */ - public fiats: CurrencyCache; + /** The {@link FiatCache} for this client */ + public fiats: FiatCache; - /** The {@link CurrencyCache} for exchange rates */ - public exchangeRates: CurrencyCache; + /** The {@link ExchangeRateCache} for this client */ + public exchangeRates: ExchangeRateCache; /** A boolean indicating whether the client is ready */ public isReady = false; @@ -86,9 +81,9 @@ export class TipccClient extends EventEmitter { this.transactions = new TransactionManager({ client: this, }); - this.cryptos = new CurrencyCache(this._refreshCryptos); - this.fiats = new CurrencyCache(this._refreshFiats); - this.exchangeRates = new CurrencyCache(this._refreshExchangeRates); + this.cryptos = new CryptocurrencyCache(this); + this.fiats = new FiatCache(this); + this.exchangeRates = new ExchangeRateCache(this); this.token = token; this.REST = new RequestHandler(token, { @@ -126,30 +121,4 @@ export class TipccClient extends EventEmitter { public set emojis(emojis: Emoji[]) { this._emojis = new Map(emojis.map(({ name, id }) => [name, id])); } - - private async _refreshCryptos(): Promise { - const { cryptocurrencies } = (await this.REST.request( - 'GET', - Routes.currenciesCryptocurrencies(), - )) as RESTGetAPICurrenciesCryptoCurrenciesResult; - - return cryptocurrencies.map((c) => new CryptoCurrency(c)); - } - - private async _refreshFiats(): Promise { - const { fiats } = (await this.REST.request( - 'GET', - Routes.currenciesFiats(), - )) as RESTGetAPICurrenciesFiatsResult; - - return fiats.map((c) => new FiatCurrency(c)); - } - - private async _refreshExchangeRates(): Promise { - const { rates } = (await this.REST.request( - 'GET', - Routes.currenciesRates(), - )) as RESTGetAPICurrenciesRatesResult; - return rates.map((r) => new ExchangeRate(r, this)); - } }