import {z} from 'zod' export type CounterpartiesCreateCounterparty = {name: string, metadata?: Record | undefined}; export type CreateApiCounterpartiesArgs = {body: CounterpartiesCreateCounterparty}; export type CreateApiCounterpartiesOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const counterpartyAccountsStatus = z.enum(["pending_approval", "approved", "denied", "disabled"]); export const counterpartyAccountsHolderAddress = z.object({line_1: z.string().optional(), line_2: z.string().optional(), building_number: z.string().optional(), street_name: z.string().optional(), postal_code: z.string().optional(), region_state: z.string().optional(), city: z.string().optional(), country: z.string().optional()}); export const counterpartyAccountsCounterpartyAccountEmbedded = z.object({id: z.string(), name: z.string().optional(), status: counterpartyAccountsStatus, status_details: z.string(), holder_name: z.string(), account_number: z.string(), bank_code: z.string(), holder_address: counterpartyAccountsHolderAddress, created_at: z.string(), disabled_at: z.string().optional()}); export const counterpartyAccountsVirtualAccountEmbedded = z.object({id: z.string(), connected_account_id: z.string(), name: z.string().max(100).optional(), virtual_account_number: z.string(), created_at: z.string(), disabled_at: z.string().optional()}); export const counterpartiesCounterparty = z.object({id: z.string(), name: z.string(), object: z.string(), created_at: z.string(), disabled_at: z.string().optional(), counterparty_accounts: z.array(counterpartyAccountsCounterpartyAccountEmbedded).optional(), virtual_accounts: z.array(counterpartyAccountsVirtualAccountEmbedded).optional(), metadata: z.object({}).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 counterparty * * POST /counterparties */ export const createApiCounterparties = async (args: CreateApiCounterpartiesArgs, options?: CreateApiCounterpartiesOptions) => { const {baseUrl = "https://sandbox.numeral.io/v1", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (sec0 !== undefined) headers.set("x-api-key", sec0) const url = `${baseUrl.replace(/\/+$/, '')}/counterparties${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(counterpartiesCounterparty, await res.json()) };