@mixxtor/currencyx-adonisjs
Version:
AdonisJS integration for CurrencyX.js with database exchange provider and cache support
57 lines (56 loc) • 1.69 kB
JavaScript
import { exchanges as currencyExchanges } from '@mixxtor/currencyx-js';
import { DatabaseExchange } from './exchanges/database.js';
import { configProvider } from '@adonisjs/core';
/**
* Define database exchange provider configuration
* Returns a factory function to avoid eager instantiation
*/
function database(config) {
if (!config.model) {
throw new Error('Database exchange requires a model');
}
const dbConfig = {
model: config.model,
base: config.base || 'USD',
columns: {
code: 'code',
rate: 'exchange_rate',
...config.columns,
},
cache: config.cache,
};
return new DatabaseExchange(dbConfig);
}
/**
* Exchange configuration helpers
*/
export const exchanges = {
...currencyExchanges,
database,
};
/**
* Define currency configuration with type inference
* Following AdonisJS pattern for better type safety
*/
export function defineConfig(config) {
return configProvider.create(async (_app) => {
const { exchanges: exchangesFactory, default: defaultExchange } = config;
const exchangesNames = Object.keys(exchangesFactory);
/**
* Configured exchanges
*/
const exchangeExchanges = {};
/**
* Looping over providers and resolving their config providers
* to get factory functions
*/
for (let providerName of exchangesNames) {
const exchange = exchangesFactory[providerName];
exchangeExchanges[providerName] = exchange;
}
return {
default: defaultExchange,
exchanges: exchangeExchanges,
};
});
}