import {z} from 'zod' export type CreateApiPersonArgs = {body: {name_first: string, name_last: string, name_other: string, phone: string, email: string, dob: string, id_type?: ('passport' | 'drivers_license' | 'national_id' | 'voters_card') | undefined, id_number?: string | undefined, id_country: string, bank_id_number?: string | undefined, kyc_level?: ('preliminary' | 'basic') | undefined, address: {line1: string, line2?: string | undefined, city: string, state: string, country: string, postal_code: string}, background_information?: ({employment_status?: ('employed' | 'self_employed' | 'unemployed' | 'student' | 'retired') | undefined, occupation?: string | undefined, primary_purpose?: ('business' | 'personal' | 'salary' | 'freelance') | undefined, source_of_funds?: ('salary' | 'savings' | 'business' | 'freelance' | 'investment' | 'government_benefits' | 'pension') | undefined, expected_monthly_inflow?: number | undefined}) | undefined, documents?: (Array<{type: 'passport' | 'national_id' | 'drivers_licence' | 'residence_permit' | 'voters_card' | 'bank_statement' | 'utility_bill' | 'incorporation_certificate' | 'registration_certificate' | 'memorandum_articles' | 'business_licence' | 'cac', url: string, issue_date?: string | undefined, expiry_date?: string | undefined}>) | undefined}}; export type CreateApiPersonOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiPersonResponse = z.object({data: z.object({id: z.string().optional(), name_first: z.string().optional(), name_last: z.string().optional(), name_other: z.string().optional(), email: z.string().optional(), phone: z.string().optional(), dob: z.string().optional(), id_level: z.string().optional(), id_type: z.string().optional(), id_number: z.string().optional(), id_country: z.string().optional(), id_upload_type: z.string().optional(), id_upload: z.string().optional(), address_line1: z.string().optional(), address_city: z.string().optional(), address_state: z.string().optional(), address_country: z.string().optional(), status: z.string().optional(), kyc_status: z.string().optional(), kyc_level: z.string().optional(), is_deleted: z.boolean().optional(), created_at: z.string().optional(), updated_at: z.string().optional()}).optional(), message: z.string().optional(), meta: z.unknown(), status: 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 Person * * POST /person */ export const createApiPerson = async (args: CreateApiPersonArgs, options?: CreateApiPersonOptions) => { const {baseUrl = "https://api.useoval.com", 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("Authorization", sec0) const url = `${baseUrl.replace(/\/+$/, '')}/person${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(createApiPersonResponse, await res.json()) };