import {z} from 'zod' export type TransactionId = string; export type GetApiV1CustomersCustomerIdConversionsOrderArgs = {customer_id: string, transaction_id: TransactionId}; export type GetApiV1CustomersCustomerIdConversionsOrderOptions = {baseUrl?: string | undefined, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const assetName = z.string(); export const feeMeta = z.object({value: z.string(), asset: assetName}); export const conversionOrderResponse = z.object({transaction_id: z.string(), order_status: z.string(), quote_id: z.string(), user_pay_amount: z.string(), user_pay_asset: z.string(), user_obtain_amount: z.string(), user_obtain_asset: z.string(), symbol: z.string(), rate: z.string(), fee: z.string(), fee_currency: z.string(), platform_fee: feeMeta, partner_fee: feeMeta}); 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 /v1/customers/{customer_id}/conversions/order */ export const getApiV1CustomersCustomerIdConversionsOrder = async (args: GetApiV1CustomersCustomerIdConversionsOrderArgs, options?: GetApiV1CustomersCustomerIdConversionsOrderOptions) => { const {baseUrl = "https://api.1money.com", fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.transaction_id != null) query.append("transaction_id", String(args.transaction_id)) if (Authorization !== undefined) headers.set('authorization', `Bearer ${Authorization}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/customers/${encodeURIComponent(String(args.customer_id))}/conversions/order${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(conversionOrderResponse, await res.json()) };