import {z} from 'zod' export type PatchPersonEntities = {external_entity_id?: string | undefined, gender?: string | undefined, birth_date?: string | undefined, name_first?: string | undefined, name_middle?: string | undefined, name_last?: string | undefined, document_ssn?: string | undefined, document_id_card?: string | undefined, document_license?: string | undefined, document_passport?: string | undefined, addresses?: Array> | undefined, emails?: (Array<{email_address?: string | undefined}>) | undefined, phones?: (Array<{phone_number?: string | undefined}>) | undefined, pii_status?: ('successful' | 'failed' | 'pending') | undefined, external_entity_ids?: (Array<{external_entity_id: string, external_entity_id_type?: string | undefined, designation?: 'primary' | undefined}>) | undefined}; export type PatchApiEntitiesPersonsEntityTokenArgs = {body: PatchPersonEntities}; export type PatchApiEntitiesPersonsEntityTokenOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const getEntityGroupsEntity = z.object({external_entity_group_ids: z.array(z.string()).optional(), entity_group_token: z.string().optional(), entity_group_name: z.string().optional(), entity_group_type: z.enum(["business", "joint_account", "membership", "merchant"]).optional(), roles: z.array(z.enum(["primary", "shareholder", "secondary", "beneficiary", "beneficial_owner", "control_prong", "director", "authorized_signer", "principal_owner", "coborrower", "joint", "guarantor"])).optional(), ownership_percentage: z.string().optional(), title: z.string().optional(), entity_group_relationship_status: z.string().optional(), relationship_created_at: z.string().optional(), entity_group_created_at: z.string().optional()}); export const getEntityNotes = z.object({text: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), email: z.string().optional(), name: z.string().optional()}); export const entityDocumentSSN = z.object({document_ssn: z.string().optional(), evaluation_token: z.string().optional(), created_at: z.number().optional()}); export const entityExternalIds = z.object({external_entity_id: z.string().optional(), external_entity_id_type: z.string().optional()}); export const entityBirthDates = z.object({birth_date: z.string().optional(), evaluation_token: z.string().optional(), created_at: z.number().optional()}); export const entityLinks = z.object({self: z.object({href: z.string().optional()}).optional()}); export const personEntitiesResponse = z.object({status_code: z.string().optional(), entity_token: z.string().optional(), external_entity_id: z.string().optional(), created_at: z.number().optional(), person_details_updated_at: z.number().optional(), gender: z.string().optional(), birth_date: z.string().optional(), name_first: z.string().optional(), name_middle: z.string().optional(), name_last: z.string().optional(), document_ssn: z.string().optional(), document_id_card: z.string().optional(), document_license: z.string().optional(), document_passport: z.string().optional(), addresses: z.array(z.object({})).optional(), emails: z.array(z.object({email_address: z.string().optional()})).optional(), phones: z.array(z.object({phone_number: z.string().optional(), type: z.string().optional()})).optional(), entity_groups: z.array(getEntityGroupsEntity).optional(), type: z.string().optional(), _embedded: z.object({notes: z.array(getEntityNotes).optional(), document_ssns: z.array(entityDocumentSSN).optional(), external_entity_ids: z.array(entityExternalIds).optional(), birth_dates: z.array(entityBirthDates).optional()}).optional(), _links: entityLinks.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 }; /** * Update a person entity * * PATCH /entities/persons/{entity_token} * * Also accepts auth: basic */ export const patchApiEntitiesPersonsEntityToken = async (args: PatchApiEntitiesPersonsEntityTokenArgs, options?: PatchApiEntitiesPersonsEntityTokenOptions) => { const {baseUrl = "https://sandbox.alloy.co/v1", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/entities/persons/${encodeURIComponent(String(args.entity_token))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(personEntitiesResponse, await res.json()) };