import {z} from 'zod' export type LaneATransferDraftLinkSnapshotDto = {draftId: string, customerId: string, amountMinor: string, sourceCurrency: string, destinationCurrency: string, destinationAccountId: string, destinationRail: string, sourceAccountId: string | null, sourceRail: string | null, paymentReason: string | null, reference: string | null, destinationCountry: string | null, destinationCountryRequested?: (string | null) | undefined, requestDigest: string, destinationAmountMinor?: (string | null) | undefined}; export type RegisterTransferDraftDto = {draftId: string, draftHash: string, customerId: string, draftSnapshot?: LaneATransferDraftLinkSnapshotDto | undefined, destinationAccountIds?: Array | undefined, quotedDestinationAmountMinor?: string | undefined}; export type CreateApiV1TransferDraftsArgs = {'X-API-Key'?: string | undefined, body: RegisterTransferDraftDto}; export type CreateApiV1TransferDraftsOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const transferDraftPublicResponseDto = z.object({id: z.string(), draftId: z.string(), customerId: z.string(), draftHash: z.string(), destinationAccountIds: z.array(z.string()), status: z.enum(["PENDING_APPROVAL", "SCREENING", "APPROVED", "REJECTED"]), createdAt: z.string(), updatedAt: z.string()}); 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 }; /** * Register a transfer draft for platform approval * * POST /v1/transfer-drafts * * Also accepts auth: bearer */ export const createApiV1TransferDrafts = async (args: CreateApiV1TransferDraftsArgs, options?: CreateApiV1TransferDraftsOptions) => { const {baseUrl = "https://sandbox.api.infinite.dev", 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-API-Key'] != null) headers.set("X-API-Key", String(args['X-API-Key'])) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/transfer-drafts${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(transferDraftPublicResponseDto, await res.json()) };