import {z} from 'zod' export type AssetName = string; export type NetworkName = string; export type WithdrawalRequest = {wallet_address?: (string | null) | undefined, external_account_id?: (string | null) | undefined, amount: string, asset: AssetName, network: NetworkName, purpose_code?: (string | null) | undefined, memorandum?: (string | null) | undefined, mode?: ('prefunding' | 'null' | null) | undefined, on_behalf_of?: (string | null) | undefined, amount_type?: ('SEND_AMOUNT' | 'RECEIVE_AMOUNT' | 'null' | null) | undefined}; export type CreateApiV1CustomersCustomerIdWithdrawalsArgs = {customer_id: string, 'Idempotency-Key': string, body: WithdrawalRequest}; export type CreateApiV1CustomersCustomerIdWithdrawalsOptions = {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 }; /** * POST /v1/customers/{customer_id}/withdrawals */ export const createApiV1CustomersCustomerIdWithdrawals = async (args: CreateApiV1CustomersCustomerIdWithdrawalsArgs, options?: CreateApiV1CustomersCustomerIdWithdrawalsOptions) => { const {baseUrl = "https://api.1money.com", fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Idempotency-Key'] != null) headers.set("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: "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(withdrawalResponse, await res.json()) };