@mixxtor/currencyx-js
Version:
Modern TypeScript currency converter with type inference and multiple providers (Google Finance, Fixer.io). Framework agnostic with clean architecture.
37 lines (34 loc) • 1.06 kB
text/typescript
/**
* Factory Function
*
* Creates a typed currency service with exchange inference
*/
import type { CurrencyConfig, CurrencyExchanges } from './types/index.js'
import { CurrencyService } from './services/index.js'
import type { BaseCurrencyExchange } from './exchanges/base_exchange.js'
/**
* Create a typed currency service with exchange inference
*
* @example
* ```typescript
* const config = defineConfig({
* default: 'google' as const,
* exchanges: {
* google: exchanges.google({ base: 'USD' }),
* fixer: exchanges.fixer({ accessKey: 'your-key' })
* }
* })
*
* const currency = createCurrency(config)
*
* // TypeScript knows these are valid
* currency.use('google') // ✅
* currency.use('fixer') // ✅
* // currency.use('invalid') // ❌ TypeScript error
* ```
*/
export function createCurrency<KnownExchanges extends Record<keyof CurrencyExchanges, BaseCurrencyExchange>>(
config: CurrencyConfig<KnownExchanges>
): CurrencyService<KnownExchanges> {
return new CurrencyService<KnownExchanges>(config)
}