import {z} from 'zod' export type GetApiV1EkycAadhaarReloadCaptchaArgs = {session_id: string, consent: string, purpose: string}; export type GetApiV1EkycAadhaarReloadCaptchaOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const aadhaarReloadCaptchaData = z.object({captcha: z.string()}); export const rESPONSEAADHAARRELOADCAPTCHA = z.object({code: z.number().int(), timestamp: z.number().int(), transaction_id: z.string(), data: aadhaarReloadCaptchaData}); 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 }; /** * Reload Captcha * * GET /v1/ekyc/aadhaar/reload-captcha */ export const getApiV1EkycAadhaarReloadCaptcha = async (args: GetApiV1EkycAadhaarReloadCaptchaArgs, options: GetApiV1EkycAadhaarReloadCaptchaOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.session_id != null) query.append("session_id", String(args.session_id)) if (args.consent != null) query.append("consent", String(args.consent)) if (args.purpose != null) query.append("purpose", String(args.purpose)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/ekyc/aadhaar/reload-captcha${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(rESPONSEAADHAARRELOADCAPTCHA, await res.json()) };