import {z} from 'zod' export type PassportOcrPayload = {document1: string, document2?: string | undefined}; export type CreateApiV1DocumentsExtractionIndPassportArgs = {body: PassportOcrPayload}; export type CreateApiV1DocumentsExtractionIndPassportOptions = {baseUrl: string, Oauth2ClientCredentials?: string | undefined, fetch?: typeof fetch | undefined}; export const dRIVINGLICENSEDOCUMENTDATA = z.object({address: z.string(), date_of_birth: z.string(), date_of_validity: z.string(), district: z.string(), fathers_name: z.string(), id_number: z.string(), is_scanned: z.string(), issue_dates: z.object({}), name_on_card: z.string(), pincode: z.string(), state: z.string(), street_address: z.string(), type: z.array(z.string()), validity: z.object({})}); export const rESPONSEDRIVINGLICENSEDOCUMENTEXTRACTION = z.object({data: dRIVINGLICENSEDOCUMENTDATA, message: z.string().optional(), transaction_id: z.string(), code: 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 }; /** * Ind Passport Ocr * * POST /v1/documents/extraction/ind_passport */ export const createApiV1DocumentsExtractionIndPassport = async (args: CreateApiV1DocumentsExtractionIndPassportArgs, options: CreateApiV1DocumentsExtractionIndPassportOptions) => { const {baseUrl, fetch: fetchFn = fetch, Oauth2ClientCredentials} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Oauth2ClientCredentials !== undefined) headers.set('authorization', `Bearer ${Oauth2ClientCredentials}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/documents/extraction/ind_passport${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(rESPONSEDRIVINGLICENSEDOCUMENTEXTRACTION, await res.json()) };