import {z} from 'zod' export type GstOcrPayload = {document1: string}; export type CreateApiV1DocumentsExtractionGstinCertificateArgs = {body: GstOcrPayload}; export type CreateApiV1DocumentsExtractionGstinCertificateOptions = {baseUrl: string, Oauth2ClientCredentials?: string | undefined, fetch?: typeof fetch | undefined}; export const gSTINDOCUMENTOCR = z.object({address: z.string(), constitution_of_business: z.string(), date_of_liability: z.string(), gstin: z.string(), is_provisional: z.string(), legal_name: z.string(), pan_number: z.string(), trade_name: z.string(), type_of_registration: z.string(), valid_from: z.string(), valid_upto: z.string()}); export const rESPONSEGSTINOCR = z.object({data: gSTINDOCUMENTOCR, 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 Gstin Certificate Ocr * * POST /v1/documents/extraction/gstin-certificate */ export const createApiV1DocumentsExtractionGstinCertificate = async (args: CreateApiV1DocumentsExtractionGstinCertificateArgs, options: CreateApiV1DocumentsExtractionGstinCertificateOptions) => { 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/gstin-certificate${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(rESPONSEGSTINOCR, await res.json()) };