import {z} from 'zod' export type GetApiV1FxRatesArgs = {'X-API-Key'?: string | undefined, sourceCurrency: string, sourceNetwork?: string | undefined, sourceRail?: string | undefined, destinationCurrency: string, destinationNetwork?: string | undefined, destinationRail?: string | undefined, onBehalfOf?: string | undefined}; export type GetApiV1FxRatesOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const fxRateResponseDto = z.object({rate: z.string(), asOf: z.string(), pairKey: z.string(), source: z.literal("reference")}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Get an indicative FX rate for a pair * * GET /v1/fx/rates * * Also accepts auth: bearer */ export const getApiV1FxRates = async (args: GetApiV1FxRatesArgs, options?: GetApiV1FxRatesOptions) => { const {baseUrl = "https://sandbox.api.infinite.dev", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['X-API-Key'] != null) headers.set("X-API-Key", String(args['X-API-Key'])) if (args.sourceCurrency != null) query.append("sourceCurrency", String(args.sourceCurrency)) if (args.sourceNetwork != null) query.append("sourceNetwork", String(args.sourceNetwork)) if (args.sourceRail != null) query.append("sourceRail", String(args.sourceRail)) if (args.destinationCurrency != null) query.append("destinationCurrency", String(args.destinationCurrency)) if (args.destinationNetwork != null) query.append("destinationNetwork", String(args.destinationNetwork)) if (args.destinationRail != null) query.append("destinationRail", String(args.destinationRail)) if (args.onBehalfOf != null) query.append("onBehalfOf", String(args.onBehalfOf)) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/fx/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 validateResponse(fxRateResponseDto, await res.json()) };