import {z} from 'zod' export type GetApiV2WalletsIdVirtualAccountsRequirementsArgs = {id: string, currency: string, assetId?: string | undefined, customerId?: string | undefined, provider?: string | undefined}; export type GetApiV2WalletsIdVirtualAccountsRequirementsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const draft07Schema = z.object({$schema: z.string(), type: z.string().optional(), properties: z.record(z.string(), z.unknown()).optional(), required: z.array(z.string()).optional()}).and(z.record(z.string(), z.unknown())); export const virtualAccountRequirements = z.object({supported: z.boolean(), unlocked: z.boolean(), additionalDataRequired: z.boolean(), additionalDataSchema: draft07Schema, provider: z.string().nullable()}); export const virtualAccountRequirementsResponse = z.object({statusCode: z.number().int(), message: z.string(), data: virtualAccountRequirements}); 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 Requirements * * GET /v2/wallets/{id}/virtual-accounts/requirements */ export const getApiV2WalletsIdVirtualAccountsRequirements = async (args: GetApiV2WalletsIdVirtualAccountsRequirementsArgs, options?: GetApiV2WalletsIdVirtualAccountsRequirementsOptions) => { const {baseUrl = "https://api.blockradar.co", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.currency != null) query.append("currency", String(args.currency)) if (args.assetId != null) query.append("assetId", String(args.assetId)) if (args.customerId != null) query.append("customerId", String(args.customerId)) if (args.provider != null) query.append("provider", String(args.provider)) if (apiKey !== undefined) headers.set("x-api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v2/wallets/${encodeURIComponent(String(args.id))}/virtual-accounts/requirements${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(virtualAccountRequirementsResponse, await res.json()) };