import {z} from 'zod' export type CreateApiBankingEntitiesArgs = {'x-idempotency-key': string, body: {account_id: string, merchant_entity_id: string, national_entity: 'INDIVIDUAL' | 'ENTITY', phone: {country_code: string, number: string}, address: {address_line_1: string, address_line_2?: (string | null) | undefined, building_number_1?: (string | null) | undefined, building_number_2?: (string | null) | undefined, city: string, state: string, zip_code: string, country: string}, entity_detail: {individual?: ({first_name?: string | undefined, last_name?: string | undefined, email?: string | undefined, date_of_birth?: string | undefined, gender?: ('M' | 'F') | undefined, country_of_residence?: string | undefined, tax_information?: (Array<{country: string, tax_id: string, tax_id_type: 'SSN' | 'EIN' | 'ITIN' | 'VAT' | 'ABN' | 'NIF' | 'TIN' | 'OTHER', is_primary: boolean}>) | undefined, document?: ({document_type: string, document_number: string, issuing_country: string, issued_at?: (string | null) | undefined, expires_at?: (string | null) | undefined}) | undefined} | null) | undefined, entity?: ({name?: string | undefined, country?: string | undefined, tax_information?: (Array<{country: string, tax_id: string, tax_id_type: 'SSN' | 'EIN' | 'ITIN' | 'VAT' | 'ABN' | 'NIF' | 'TIN' | 'OTHER', is_primary: boolean}>) | undefined, entity_type?: 'CORPORATION' | undefined, beneficial_owners?: (Array<{first_name: string, last_name: string, email: string, date_of_birth: string, gender?: ('M' | 'F') | undefined, country_of_residence: string, tax_information?: (Array<{country?: string | undefined, tax_id?: string | undefined, tax_id_type?: ('SSN' | 'EIN' | 'ITIN' | 'VAT' | 'ABN' | 'NIF' | 'TIN' | 'OTHER') | undefined, is_primary?: boolean | undefined}>) | undefined, document?: ({document_type?: string | undefined, document_number?: string | undefined, issuing_country?: string | undefined, issued_at?: (string | null) | undefined, expires_at?: (string | null) | undefined}) | undefined, ownership_percentage: number, title?: string | undefined, address?: ({address_line_1?: string | undefined, address_line_2?: (string | null) | undefined, building_number_1?: (string | null) | undefined, building_number_2?: (string | null) | undefined, city?: string | undefined, state?: string | undefined, zip_code?: string | undefined, country?: string | undefined}) | undefined}>) | undefined} | null) | undefined}}}; export type CreateApiBankingEntitiesOptions = {baseUrl?: string | undefined, publicApiKey?: string | undefined, privateSecretKey?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiBankingEntitiesResponse = z.object({id: z.string().optional(), account_id: z.string().optional(), merchant_entity_id: z.string().optional(), national_entity: z.string().optional(), phone: z.object({country_code: z.string().optional(), number: z.string().optional()}).optional(), address: z.object({address_line_1: z.string().optional(), address_line_2: z.unknown(), building_number_1: z.string().optional(), building_number_2: z.unknown(), city: z.string().optional(), state: z.string().optional(), zip_code: z.string().optional(), country: z.string().optional()}).optional(), entity_detail: z.object({individual: z.unknown(), entity: z.object({name: z.string().optional(), country: z.string().optional(), tax_information: z.array(z.object({country: z.string().optional(), tax_id_last_4: z.string().optional(), tax_id_type: z.string().optional(), is_primary: z.boolean().optional()})).optional(), entity_type: z.string().optional(), beneficial_owners: z.array(z.object({first_name: z.string().optional(), last_name: z.string().optional(), email: z.string().optional(), date_of_birth: z.string().optional(), gender: z.string().optional(), country_of_residence: z.string().optional(), tax_information: z.array(z.object({country: z.string().optional(), tax_id_last_4: z.string().optional(), tax_id_type: z.string().optional(), is_primary: z.boolean().optional()})).optional(), document: z.object({document_type: z.string().optional(), document_number_last_4: z.string().optional(), issuing_country: z.string().optional(), issued_at: z.string().optional(), expires_at: z.unknown()}).optional(), ownership_percentage: z.number().int().optional(), title: z.string().optional(), address: z.object({address_line_1: z.string().optional(), address_line_2: z.unknown(), building_number_1: z.string().optional(), building_number_2: z.unknown(), city: z.string().optional(), state: z.string().optional(), zip_code: z.string().optional(), country: z.string().optional()}).optional()})).optional()}).optional()}).optional(), created_at: z.string().optional(), updated_at: 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 Entity (Banking Connectivity) * * POST /banking/entities */ export const createApiBankingEntities = async (args: CreateApiBankingEntitiesArgs, options?: CreateApiBankingEntitiesOptions) => { const {baseUrl = "https://api-sandbox.y.uno/v1", fetch: fetchFn = fetch, publicApiKey, privateSecretKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['x-idempotency-key'] != null) headers.set("x-idempotency-key", String(args['x-idempotency-key'])) if (publicApiKey !== undefined) headers.set("PUBLIC-API-KEY", publicApiKey) if (privateSecretKey !== undefined) headers.set("PRIVATE-SECRET-KEY", privateSecretKey) const url = `${baseUrl.replace(/\/+$/, '')}/banking/entities${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(createApiBankingEntitiesResponse, await res.json()) };