import {z} from 'zod' export type SmeRegistrationSubmitData = {onboardingRequestId: string, companyName: string, certNumber: string, kraPin: string, operatingMode: 1 | 2 | 3 | 4, businessAddress: string, businessIndustry: number, specifyIndustry?: string | undefined, directors: Array<{idType: '101' | '102' | '103', idNumber: string, gender?: (0 | 1) | undefined, email?: string | undefined, mobile: string, firstName: string, middleName?: string | undefined, lastName: string, kraPin: string, idFrontSideFileKey: string, idFrontSideFileType: 'image' | 'pdf', idBackSideFileKey: string, idBackSideFileType: 'image' | 'pdf', selfieFileKey: string, selfieFileType: 'image' | 'pdf', kraPinFileKey: string, kraPinFileType: 'image' | 'pdf'}>, organizationShareholders?: (Array<{companyName: string, registrationFileKey: string, registrationFileType: 'image' | 'pdf'}>) | undefined, media?: (Array<{fileKey: string, mediaType: string, contentType?: ('pdf' | 'image') | undefined}>) | undefined}; export type CreateApiApiV1SmeRegistrationArgs = {body: {step: 'INITIATE', data: {mobile: string, email: string, businessType: 2}} | {step: 'CONFIRM_OTP', data: {onboardingRequestId: string, otp: string}} | {step: 'SUBMIT', data: SmeRegistrationSubmitData}}; export type CreateApiApiV1SmeRegistrationOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiV1SmeRegistrationResponse = z.object({data: z.object({onboardingRequestId: z.string().optional(), step: z.enum(["INITIATE", "CONFIRM_OTP", "SUBMIT"]).optional(), status: z.enum(["OTP_PENDING", "DETAILS_PENDING", "SUBMITTED", "REJECTED", "EXPIRED"]).optional(), nextStep: z.enum(["CONFIRM_OTP", "SUBMIT", "null"]).nullable().optional(), expiresAt: z.string().optional()}).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 }; /** * Register a business as an SME (step-based) * * POST /api/v1/sme-registration */ export const createApiApiV1SmeRegistration = async (args: CreateApiApiV1SmeRegistrationArgs, options?: CreateApiApiV1SmeRegistrationOptions) => { const {baseUrl = "https://sandbox.api.afriex.com", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKey !== undefined) headers.set("x-api-key", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/sme-registration${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(createApiApiV1SmeRegistrationResponse, await res.json()) };