import {z} from 'zod' export type CreateApiV1QuotesArgs = {'X-Request-Id'?: string | undefined, body: {accountId: string, pair: string, side: 'BUY' | 'SELL', deliverQuantity: string | null, clientFeeRate?: (unknown | null) | undefined, clientFeeAmount?: (unknown | null) | undefined, clientReference?: string | undefined} | {accountId: string, pair: string, side: 'BUY' | 'SELL', receiveQuantity: string | null, clientFeeRate?: (unknown | null) | undefined, clientFeeAmount?: (unknown | null) | undefined, clientReference?: string | undefined}}; export type CreateApiV1QuotesOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const datetime = z.string(); export const quoteStatus = z.enum(["AWAITING_RESPONSE", "CANCELED", "EXPIRED", "ACCEPTED"]); export const createApiV1QuotesResponse = z.object({quoteId: z.string().optional(), requestedAt: datetime.optional(), expiresAt: datetime.optional(), accountId: z.string().optional(), pair: z.string().optional(), side: z.enum(["BUY", "SELL"]).optional(), deliverQuantity: z.string().optional(), deliverCurrency: z.string().optional(), receiveQuantity: z.string().optional(), receiveCurrency: z.string().optional(), price: z.string().optional(), baseQuantity: z.string().optional(), quoteQuantity: z.string().optional(), feeCurrency: z.string().optional(), clientFeeRate: z.string().nullable().optional(), clientFeeAmount: z.string().nullable().optional(), clientFeeQuantity: z.string().optional(), providerFeeRate: z.string().optional(), providerFeeQuantity: z.string().optional(), quoteStatus: quoteStatus.optional(), clientReference: z.string().nullable().optional(), tradeTaxRate: z.string().optional(), clientTaxQuantity: z.string().optional(), providerTaxQuantity: z.string().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 }; /** * Request Quote (RFQ) * * POST /v1/quotes */ export const createApiV1Quotes = async (args: CreateApiV1QuotesArgs, options?: CreateApiV1QuotesOptions) => { const {baseUrl = "https://cams.aquanow.com/api", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['X-Request-Id'] != null) headers.set("X-Request-Id", String(args['X-Request-Id'])) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/quotes${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiV1QuotesResponse, await res.json()) };