import {z} from 'zod' export type UserJourneyId = string; export type ApplicantId = string; export type DeclaredData = {name: string, birth_date?: string | undefined}; export type AddressDocumentVerificationBase = {user_journey_id?: UserJourneyId | undefined, applicant_id?: ApplicantId | undefined, webhook_url?: string | undefined, declared_data?: DeclaredData | undefined}; export type AddressDocumentVerificationInputPost = AddressDocumentVerificationBase; export type CreateApiV2AddressDocumentVerificationsArgs = {body: AddressDocumentVerificationInputPost}; export type CreateApiV2AddressDocumentVerificationsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const addressDocumentVerificationId = z.string(); export const addressDocumentVerificationStatus = z.enum(["created", "quality_checks_in_progress", "checks_in_progress", "approved", "declined", "retry_required", "inconclusive"]); export const responseCode = z.number().int().gte(10000).lte(69999); export const responseSummary = z.string(); export const responseCodes = z.object({code: responseCode, summary: responseSummary}); export const riskLabel = z.enum(["multiple_faces_detected", "mcc_not_confident", "risky_document_format", "no_uk_footprint"]); export const riskLabels = z.array(riskLabel); export const countryCodeIso2 = z.string(); export const address = z.object({address_line1: z.string().max(250).optional(), address_line2: z.string().max(250).optional(), city: z.string().max(50).optional(), state: z.string().max(50).optional(), zip: z.string().max(50).optional(), country: countryCodeIso2.optional()}); export const proofOfAddressDocument = z.object({document_type: z.string().optional(), issuer: z.string().optional(), full_names: z.array(z.string()).optional(), address: address.optional(), issue_date: z.string().optional()}); export const halLink = z.object({href: z.string()}); export const addressDocumentVerificationLinks = z.object({self: halLink}); export const userJourneyId = z.string(); export const applicantId = z.string(); export const declaredData = z.object({name: z.string().min(2).max(255), birth_date: z.string().optional()}); export const addressDocumentVerificationOutputPost = z.object({id: addressDocumentVerificationId.optional(), status: addressDocumentVerificationStatus.optional(), response_codes: z.array(responseCodes).optional(), risk_labels: riskLabels.optional(), address_document: proofOfAddressDocument.optional(), _links: addressDocumentVerificationLinks.optional(), user_journey_id: userJourneyId.optional(), applicant_id: applicantId.optional(), webhook_url: z.string().optional(), declared_data: declaredData.optional(), created_on: z.string(), modified_on: z.string()}); 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 Address Document Verification * * POST /v2/address-document-verifications */ export const createApiV2AddressDocumentVerifications = async (args: CreateApiV2AddressDocumentVerificationsArgs, options?: CreateApiV2AddressDocumentVerificationsOptions) => { const {baseUrl = "https://api.ubble.ai", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v2/address-document-verifications${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(addressDocumentVerificationOutputPost, await res.json()) };