import {z} from 'zod' export type IdempotencyKey = string; export type GetApiV1CustomersCustomerIdWithdrawalsArgs = {customer_id: string, idempotency_key: IdempotencyKey}; export type GetApiV1CustomersCustomerIdWithdrawalsOptions = {baseUrl?: string | undefined, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const assetName = z.string(); export const networkName = z.string(); export const idempotencyKey = z.string(); export const withdrawalStatus = z.enum(["PENDING", "COMPLETED", "FAILED", "RETURNED", "MICRO_DEPOSIT_REVERSED"]); export const rfiCaseId = z.string(); export const actionRequired = z.object({type: z.string(), case_id: rfiCaseId, action: z.string()}); export const feeMeta = z.object({value: z.string(), asset: assetName}); export const withdrawalAction = z.literal("WITHDRAWAL"); export const chronoDateTimeSchema = z.string(); export const pid = z.string(); export const withdrawalResponse = z.object({wallet_address: z.string().nullable().optional(), external_account_id: z.string().nullable().optional(), amount: z.string(), asset: assetName, network: networkName, purpose_code: z.string().nullable().optional(), memorandum: z.string().max(256).nullable().optional(), mode: z.enum(["prefunding", "null"]).nullable().optional(), on_behalf_of: z.string().nullable().optional(), amount_type: z.enum(["SEND_AMOUNT", "RECEIVE_AMOUNT", "null"]).nullable().optional(), transaction_id: z.string(), idempotency_key: idempotencyKey, status: withdrawalStatus, action_required: actionRequired.nullable().optional(), transaction_fee: feeMeta, platform_fee: feeMeta, partner_fee: feeMeta, transaction_action: withdrawalAction, created_at: chronoDateTimeSchema, modified_at: chronoDateTimeSchema, reference: z.string().nullable().optional(), funder_pid: pid.nullable().optional(), funder_account_id: z.string().nullable().optional(), recipient_id: z.string().nullable().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 /v1/customers/{customer_id}/withdrawals */ export const getApiV1CustomersCustomerIdWithdrawals = async (args: GetApiV1CustomersCustomerIdWithdrawalsArgs, options?: GetApiV1CustomersCustomerIdWithdrawalsOptions) => { 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.idempotency_key != null) query.append("idempotency_key", String(args.idempotency_key)) if (Authorization !== undefined) headers.set('authorization', `Bearer ${Authorization}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/customers/${encodeURIComponent(String(args.customer_id))}/withdrawals${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(withdrawalResponse, await res.json()) };