import {z} from 'zod' export type GetApiV1QuotesArgs = {sourceCountry: string, sourceCurrency: string, destinationCountry: string, destinationCurrency: string, amountCurrency: string, amount: number, finInstIdType: string, finInstId: string, destFinInstIdType?: string | undefined, destFinInstId?: string | undefined}; export type GetApiV1QuotesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1QuotesResponse = z.object({quoteRequestId: z.string().optional(), sourceCountry: z.string().optional(), sourceCurrency: z.string().optional(), destinationCountry: z.string().optional(), destinationCurrency: z.string().optional(), amountCurrency: z.string().optional(), amount: z.number().optional(), quotes: z.array(z.object({quoteId: z.string().optional(), issuedDateTime: z.string().optional(), expiryDateTime: z.string().optional(), fxpFinInstIdType: z.string().optional(), fxpFinInstId: z.string().optional(), exchangeRate: z.number().optional(), debtorAgent: z.object({interbankSettlementAmount: z.object({currency: z.string().optional(), amount: z.number().optional(), cappedToMaxAmount: z.boolean().optional()}).optional()}).optional(), intermediaryAgent1: z.object({finInstId: z.object({name: z.string().optional(), BICFI: z.string().optional(), LEI: z.string().optional(), clearingSystemMemberId: z.object({clearingSystemId: z.string().optional(), memberId: z.string().optional()}).optional(), other: z.object({id: z.string().optional()}).optional()}).optional()}).optional(), intermediaryAgent1Account: z.object({id: z.string().optional()}).optional(), intermediaryAgent2: z.object({finInstId: z.object({name: z.string().optional(), BICFI: z.string().optional(), LEI: z.string().optional(), clearingSystemMemberId: z.object({clearingSystemId: z.string().optional(), memberId: z.string().optional()}).optional(), other: z.object({id: z.string().optional()}).optional()}).optional()}).optional(), intermediaryAgent2Account: z.object({id: z.string().optional()}).optional(), creditorAgent: z.object({interbankSettlementAmount: z.object({amount: z.number().optional(), currency: z.string().optional(), cappedToMaxAmount: z.boolean().optional()}).optional(), chargesInformation: z.object({amount: z.object({amount: z.number().optional(), currency: z.string().optional()}).optional()}).optional(), creditorAccountAmount: z.number().optional()}).optional()})).optional()}); 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 quotes for a specific currency pair and amount * * GET /v1/quotes */ export const getApiV1Quotes = async (args: GetApiV1QuotesArgs, options?: GetApiV1QuotesOptions) => { const {baseUrl = "http://localhost:8080", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.sourceCountry != null) query.append("sourceCountry", String(args.sourceCountry)) if (args.sourceCurrency != null) query.append("sourceCurrency", String(args.sourceCurrency)) if (args.destinationCountry != null) query.append("destinationCountry", String(args.destinationCountry)) if (args.destinationCurrency != null) query.append("destinationCurrency", String(args.destinationCurrency)) if (args.amountCurrency != null) query.append("amountCurrency", String(args.amountCurrency)) if (args.amount != null) query.append("amount", String(args.amount)) if (args.finInstIdType != null) query.append("finInstIdType", String(args.finInstIdType)) if (args.finInstId != null) query.append("finInstId", String(args.finInstId)) if (args.destFinInstIdType != null) query.append("destFinInstIdType", String(args.destFinInstIdType)) if (args.destFinInstId != null) query.append("destFinInstId", String(args.destFinInstId)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/quotes${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(getApiV1QuotesResponse, await res.json()) };