import {z} from 'zod' export type DepositWalletNetwork = string; export type CreateDepositInstructionRequest = {network: DepositWalletNetwork, nickname?: (string | null) | undefined}; export type CreateApiV1CustomersCustomerIdDepositInstructionArgs = {customer_id: string, 'Idempotency-Key': string, body: CreateDepositInstructionRequest}; export type CreateApiV1CustomersCustomerIdDepositInstructionOptions = {baseUrl?: string | undefined, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const depositWalletNetwork = z.string(); export const depositInstructionStatus = z.enum(["PENDING", "READY", "FAILED"]); export const chronoDateTimeSchema = z.string(); export const depositInstructionResponse = z.object({deposit_instruction_id: z.string(), network: depositWalletNetwork, address: z.string(), is_default: z.boolean(), nickname: z.string(), status: depositInstructionStatus, created_at: chronoDateTimeSchema}); 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 }; /** * Accept an additional deposit wallet for asynchronous provisioning. * A new request returns PENDING with an empty address and a stable deposit_instruction_id. * Retrieve that ID (or the same idempotency key) until READY, or consume deposit_instruction.ready. * An idempotent replay returns the current state and may already be READY. * * POST /v1/customers/{customer_id}/deposit_instruction */ export const createApiV1CustomersCustomerIdDepositInstruction = async (args: CreateApiV1CustomersCustomerIdDepositInstructionArgs, options?: CreateApiV1CustomersCustomerIdDepositInstructionOptions) => { 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))}/deposit_instruction${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(depositInstructionResponse, await res.json()) };