import {z} from 'zod' export type GetApiV1GstTaxPayerLedgersItcArgs = {gstin_number: string, from_date: string, to_date: string}; export type GetApiV1GstTaxPayerLedgersItcOptions = {baseUrl: string, Oauth2ClientCredentials?: string | undefined, fetch?: typeof fetch | undefined}; export const igstBalObject = z.object({intr: z.number().int(), oth: z.number().int(), tx: z.number().int(), fee: z.number().int(), pen: z.number().int(), tot: z.number().int()}); export const sgstBalObject = z.object({intr: z.number().int(), oth: z.number().int(), tx: z.number().int(), fee: z.number().int(), pen: z.number().int(), tot: z.number().int()}); export const cessBalObject = z.object({intr: z.number().int(), oth: z.number().int(), tx: z.number().int(), fee: z.number().int(), pen: z.number().int(), tot: z.number().int()}); export const cgstBalObject = z.object({intr: z.number().int(), oth: z.number().int(), tx: z.number().int(), fee: z.number().int(), pen: z.number().int(), tot: z.number().int()}); export const clBalObject = z.object({igstbal: igstBalObject, sgstbal: sgstBalObject, cessbal: cessBalObject, tot_rng_bal: z.number().int(), cgstbal: cgstBalObject, desc: z.string()}); export const itcClTrObject = z.object({sgst: sgstBalObject, ref_no: z.string(), sgstbal: sgstBalObject, cessbal: cessBalObject, cgst: cgstBalObject, cess: cessBalObject, tot_rng_bal: z.number().int(), igst: igstBalObject, tr_typ: z.string(), dt: z.string(), igstbal: igstBalObject, tot_tr_amt: z.number().int(), dschrg_typ: z.string(), cgstbal: cgstBalObject, desc: z.string()}); export const itcLedgerData = z.object({ret_period: z.string(), cl_bal: clBalObject, gstin: z.string(), tr: z.array(itcClTrObject)}); export const rESPONSEITCLEDGERSUCCESSFUL = z.object({data: itcLedgerData, code: z.number().int(), timestamp: z.number().int(), transaction_id: z.string()}); export const rESPONSEGSTAUTHENTICATIONFAILED = z.object({data: z.object({}).optional(), code: z.number().int(), timestamp: z.number().int(), transaction_id: z.string()}); export const getApiV1GstTaxPayerLedgersItcResponse = z.union([rESPONSEITCLEDGERSUCCESSFUL, rESPONSEGSTAUTHENTICATIONFAILED]); 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 }; /** * Itc Ledger * * GET /v1/gst/tax-payer/ledgers/itc */ export const getApiV1GstTaxPayerLedgersItc = async (args: GetApiV1GstTaxPayerLedgersItcArgs, options: GetApiV1GstTaxPayerLedgersItcOptions) => { 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 (args.from_date != null) query.append("from_date", String(args.from_date)) if (args.to_date != null) query.append("to_date", String(args.to_date)) if (Oauth2ClientCredentials !== undefined) headers.set('authorization', `Bearer ${Oauth2ClientCredentials}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/gst/tax-payer/ledgers/itc${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(getApiV1GstTaxPayerLedgersItcResponse, await res.json()) };