import {z} from 'zod' export type GetApiV1VerificationGstinliteArgs = {gstin_number: string}; export type GetApiV1VerificationGstinliteOptions = {baseUrl: string, Oauth2ClientCredentials?: string | undefined, fetch?: typeof fetch | undefined}; export const gstibAddressObject = z.object({bnm: z.string().optional(), st: z.string().optional(), loc: z.string().optional(), bno: z.string().optional(), stcd: z.string().optional(), dst: z.string().optional(), city: z.string().optional(), flno: z.string().optional(), lt: z.string().optional(), pncd: z.string().optional(), lg: z.string().optional(), landMark: z.string().optional(), locality: z.string().optional(), geocodelvl: z.string().optional(), full_address: z.string().optional()}); export const gstRegAddress = z.object({addr: gstibAddressObject, ntr: z.string().optional()}); export const gstinVerificationData = z.object({stjCd: z.string().optional(), dty: z.string().optional(), stj: z.string().optional(), lgnm: z.string().optional(), adadr: z.array(gstRegAddress), cxdt: z.string().optional(), gstin: z.string(), nba: z.array(z.string()), lstupdt: z.string().optional(), ctb: z.string().optional(), rgdt: z.string().optional(), pradr: gstRegAddress, ctjCd: z.string().optional(), sts: z.string().optional(), tradeNam: z.string().optional(), ctj: z.string().optional(), einvoiceStatus: z.string().optional()}); export const rESPONSEGSTINVERIFICATIONSUCCESSFUL = z.object({data: gstinVerificationData, code: z.number().int(), timestamp: z.number().int(), transaction_id: z.string()}); export const searchGstinNoRecordsFound = z.object({error_code: z.string().optional(), message: z.string().optional()}); export const rESPONSEGSTINNORECORDSFOUND = z.object({code: z.number().int(), timestamp: z.number().int(), transaction_id: z.string(), data: searchGstinNoRecordsFound}); export const searchGstinInvalid = z.object({error_code: z.string().optional(), message: z.string().optional()}); export const rESPONSEGSTININVALID = z.object({code: z.number().int(), timestamp: z.number().int(), transaction_id: z.string(), data: searchGstinInvalid}); export const getApiV1VerificationGstinliteResponse = z.union([rESPONSEGSTINVERIFICATIONSUCCESSFUL, rESPONSEGSTINNORECORDSFOUND, rESPONSEGSTININVALID]); 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 }; /** * Gstin Basic Verification * * GET /v1/verification/gstinlite */ export const getApiV1VerificationGstinlite = async (args: GetApiV1VerificationGstinliteArgs, options: GetApiV1VerificationGstinliteOptions) => { const {baseUrl, fetch: fetchFn = fetch, Oauth2ClientCredentials} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.gstin_number != null) query.append("gstin_number", String(args.gstin_number)) if (Oauth2ClientCredentials !== undefined) headers.set('authorization', `Bearer ${Oauth2ClientCredentials}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/verification/gstinlite${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiV1VerificationGstinliteResponse, await res.json()) };