import {z} from 'zod' export type CreateAccountRequest = {name: unknown, reference: unknown, type: 'CryptoPayIn', chain_id: number, address: unknown, currencies: Array<'ETH' | 'USDC' | 'USDT' | 'WETH' | 'DAI' | 'SOL'>}; export type CreateApiV1AccountsArgs = {body: CreateAccountRequest}; export type CreateApiV1AccountsOptions = {baseUrl: string, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const accountDetails = z.object({id: z.number().int().optional(), org_id: z.string().optional(), parent_id: z.number().int().nullable().optional(), reference: z.string().optional(), account_type: z.enum(["CryptoPayIn", "CryptoPayOut", "FiatPayIn", "FiatPayOut", "Crypto", "Bank", "Card", "MPCVault", "GooglePay", "ApplePay"]).optional(), name: z.string().optional(), chain_id: z.number().int().optional(), address: z.string().optional(), currencies: z.array(z.enum(["ETH", "USDC", "USDT", "WETH", "DAI", "SOL"])).optional(), account_number: z.string().optional(), sort_code: z.string().optional(), enabled: z.boolean().optional(), state: z.enum(["Unknown", "Pending", "Ready"]).optional(), created_by: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), deleted_at: z.string().nullable().optional()}); export const account = z.object({data: accountDetails.optional(), message: z.string().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 }; /** * Create a Merchant Settlement Account * * POST /v1/accounts */ export const createApiV1Accounts = async (args: CreateApiV1AccountsArgs, options: CreateApiV1AccountsOptions) => { const {baseUrl, fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("x-api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/accounts${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(account, await res.json()) };