import {z} from 'zod' export type TransferStatus = 'PROCESSING' | 'COMPLETED' | 'FAILED'; export type GetApiV1TransfersArgs = {after?: string | undefined, limit?: unknown | undefined, transferId?: string | undefined, sourceAccountId?: string | undefined, sourceCustomerId?: string | undefined, beneficiaryAccountId?: string | undefined, beneficiaryId?: string | undefined, transferCurrency?: string | undefined, externalReferenceId?: string | undefined, transactionStatus?: TransferStatus | undefined, requestedAtStart?: string | undefined, requestedAtEnd?: string | undefined}; export type GetApiV1TransfersOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const transactionType = z.enum(["DEPOSIT", "WITHDRAWAL", "TRANSFER"]); export const transactionStatus = z.enum(["FAILED", "PROCESSING", "CONFIRMING", "COMPLETED"]); export const financial = z.string(); export const transfer = z.object({transactionType: transactionType, transactionStatus: transactionStatus, transferId: z.string(), sourceAccountId: z.string(), sourceCustomerId: z.string(), beneficiaryAccountId: z.string(), beneficiaryId: z.string(), transferQuantity: financial, transferCurrency: z.string(), transferReason: z.string().max(255).nullable().optional(), externalReferenceId: z.string().max(255).nullable().optional(), requestedAt: z.string(), completedAt: z.string().nullable()}); export const getApiV1TransfersResponse = z.object({after: z.string().nullable().optional(), totalCount: z.string().optional(), items: z.array(transfer).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 }; /** * Transfers * * GET /v1/transfers */ export const getApiV1Transfers = async (args: GetApiV1TransfersArgs, options?: GetApiV1TransfersOptions) => { const {baseUrl = "https://cams.aquanow.com/api", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.after != null) query.append("after", String(args.after)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.transferId != null) query.append("transferId", String(args.transferId)) if (args.sourceAccountId != null) query.append("sourceAccountId", String(args.sourceAccountId)) if (args.sourceCustomerId != null) query.append("sourceCustomerId", String(args.sourceCustomerId)) if (args.beneficiaryAccountId != null) query.append("beneficiaryAccountId", String(args.beneficiaryAccountId)) if (args.beneficiaryId != null) query.append("beneficiaryId", String(args.beneficiaryId)) if (args.transferCurrency != null) query.append("transferCurrency", String(args.transferCurrency)) if (args.externalReferenceId != null) query.append("externalReferenceId", String(args.externalReferenceId)) if (args.transactionStatus != null) query.append("transactionStatus", String(args.transactionStatus)) if (args.requestedAtStart != null) query.append("requestedAtStart", String(args.requestedAtStart)) if (args.requestedAtEnd != null) query.append("requestedAtEnd", String(args.requestedAtEnd)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/transfers${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(getApiV1TransfersResponse, await res.json()) };