import {z} from 'zod' export type CreateApiEntitiesDocumentArgs = {Authorization: string, body: {description?: string | undefined, file_name: string, file_type: string, document_type: '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', business_id?: string | undefined, identity_id?: string | undefined, metadata?: Record | undefined}}; export type CreateApiEntitiesDocumentOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiEntitiesDocumentResponse = z.object({id: z.string().optional(), type: z.string().optional(), data: 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()}).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 a Document * * POST /entities/document */ export const createApiEntitiesDocument = async (args: CreateApiEntitiesDocumentArgs, options?: CreateApiEntitiesDocumentOptions) => { 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/document${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(createApiEntitiesDocumentResponse, await res.json()) };