import {z} from 'zod' export type OcrPayload = {document1: string, document2?: string | undefined, name?: string | undefined}; export type CreateApiV1DocumentsExtractionIndPancardArgs = {body: OcrPayload}; export type CreateApiV1DocumentsExtractionIndPancardOptions = {baseUrl: string, Oauth2ClientCredentials?: string | undefined, fetch?: typeof fetch | undefined}; export const pANCARDDOCUMENTDATA = z.object({age: z.number().int(), date_of_birth: z.string(), date_of_issue: z.string(), fathers_name: z.string(), id_number: z.string(), is_scanned: z.boolean(), minor: z.boolean(), name_on_card: z.string(), pan_type: z.string()}); export const rESPONSEPANDOCUMENTEXTRACTION = z.object({data: pANCARDDOCUMENTDATA, message: z.string().optional(), transaction_id: z.string().optional(), code: z.number().int().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 Pan Ocr * * POST /v1/documents/extraction/ind_pancard */ export const createApiV1DocumentsExtractionIndPancard = async (args: CreateApiV1DocumentsExtractionIndPancardArgs, options: CreateApiV1DocumentsExtractionIndPancardOptions) => { 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_pancard${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(rESPONSEPANDOCUMENTEXTRACTION, await res.json()) };