export type GetApiApiRatesArgs = {api_key: string, amount?: number | undefined, from?: string | undefined, to?: string | undefined, format?: ('json' | 'xml' | 'csv' | 'tsv') | undefined, callback?: string | undefined}; export type GetApiApiRatesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; /** * Get exchange rates between currencies with optional amount conversion * * GET /api/rates */ export const getApiApiRates = async (args: GetApiApiRatesArgs, options: GetApiApiRatesOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "single_conversion") if (args.api_key != null) query.append("api_key", String(args.api_key)) if (args.amount != null) query.append("amount", String(args.amount)) if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.format != null) query.append("format", String(args.format)) if (args.callback != null) query.append("callback", String(args.callback)) const url = `${baseUrl.replace(/\/+$/, '')}/api/rates${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return res.blob() };