import {z} from 'zod' export type SubmitHeadlessKycWithImageUrlRequest = {customerReferenceId: string, tier: 'TIER_2', email: string, firstName: string, lastName: string, middleName?: string | undefined, dateOfBirth: string, country: string, phoneNumber: string, address: {line1: string, line2?: string | undefined, city: string, country: string, state?: string | undefined, postalCode?: (string | string) | undefined}, taxId?: string | undefined, document: {type: 'DRIVERS_LICENSE' | 'NATIONAL_ID' | 'RESIDENCE_PERMIT' | 'PERMANENT_RESIDENCE_CARD', frontImageUrl: string, backImageUrl: string, documentNumber: string} | {type: 'PASSPORT' | 'PASSPORT_CARD', frontImageUrl: string, documentNumber: string}, consent: {termsAcceptedAt: number, ipAddress: string | string, userAgent?: string | undefined}}; export type CreateApiV1HeadlessKycArgs = {body: SubmitHeadlessKycWithImageUrlRequest}; export type CreateApiV1HeadlessKycOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const submitHeadlessKycWithImageUrlResponse = z.object({id: z.string(), customerId: z.string().optional(), accountId: z.string().optional(), kycId: z.string().optional(), kycStatus: z.enum(["processing", "approved", "rejected", "under_review", "pending_user_info"]), customerReferenceId: z.string(), tier: z.enum(["TIER_0", "TIER_1", "TIER_2", "TIER_3"]), rejectionReason: z.string().optional(), actionLink: z.string().optional(), createdAt: z.number(), updatedAt: z.number()}); export const createApiV1HeadlessKycResponse = z.object({status: z.literal("SUCCEEDED"), data: submitHeadlessKycWithImageUrlResponse}); 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 }; /** * Submit KYC for a customer (via image URL) * * POST /v1/headless_kyc */ export const createApiV1HeadlessKyc = async (args: CreateApiV1HeadlessKycArgs, options?: CreateApiV1HeadlessKycOptions) => { const {baseUrl = "https://api.breeze.cash", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/headless_kyc${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(createApiV1HeadlessKycResponse, await res.json()) };