import {z} from 'zod' export type Address = {line1?: string | undefined, line2?: string | undefined, city?: string | undefined, state?: string | undefined, postal_code?: string | undefined, country?: string | undefined}; export type CreateApiEntitiesIdentityArgs = {Authorization: string, body: {name?: string | undefined, title?: string | undefined, email?: string | undefined, phone?: string | undefined, dob_day?: string | undefined, dob_month?: string | undefined, dob_year?: string | undefined, identification_number?: string | undefined, is_owner?: boolean | undefined, ownership_percentage?: number | undefined, metadata?: {[key: string]: unknown} | undefined, address?: (Address | {id?: string | undefined}) | undefined}}; export type CreateApiEntitiesIdentityOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const addressResponse = z.object({id: z.string().optional(), platform_account_id: z.string().optional(), line1: z.string().optional(), line2: z.string().optional(), city: z.string().optional(), state: z.string().optional(), postal_code: z.string().optional(), country: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional()}); export const document = z.object({id: z.string().optional(), platform_account_id: z.string().optional(), description: z.string().optional(), file_name: z.string().optional(), file_type: z.string().optional(), document_type: z.enum(["articles_of_incorporation", "balance_sheet", "bank_statement", "birth_certificate", "business_registration", "citizenship_card", "driver_license", "foreign_passport", "government_id", "nexus_card", "passport", "profit_and_loss_statement", "resident_card", "sin_card", "ssn_card", "status_card", "tax_return", "voided_check", "other"]).optional(), business_id: z.string().optional(), identity_id: z.string().optional(), presigned_url: z.string().optional(), metadata: z.record(z.string(), z.unknown()).optional(), status: z.enum(["pending", "uploaded", "canceled"]).optional(), created_at: z.string().optional(), updated_at: z.string().optional()}); export const createApiEntitiesIdentityResponse = z.object({id: z.string().optional(), type: z.string().optional(), data: z.object({id: z.string().optional(), platform_account_id: z.string().optional(), business_id: z.string().optional(), name: z.string().optional(), title: z.string().optional(), email: z.string().optional(), phone: z.string().optional(), dob_day: z.string().optional(), dob_month: z.string().optional(), dob_year: z.string().optional(), ssn_last4: z.string().optional(), is_owner: z.boolean().optional(), ownership_percentage: z.number().int().gte(0).lte(100).optional(), metadata: z.object({}).optional(), address: addressResponse.optional(), documents: z.array(document).optional(), created_at: z.string().optional(), updated_at: z.string().optional()}).optional(), page_info: z.unknown()}); 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 an Identity * * POST /entities/identity */ export const createApiEntitiesIdentity = async (args: CreateApiEntitiesIdentityArgs, options?: CreateApiEntitiesIdentityOptions) => { const {baseUrl = "https://api.justifi.ai/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/entities/identity${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(createApiEntitiesIdentityResponse, await res.json()) };