import {z} from 'zod' export type GetApiAbdmV1ProfileArgs = {'X-Pt-Id'?: string | undefined, 'X-Partner-Pt-Id'?: string | undefined, 'X-Hip-Id'?: string | undefined, oid?: string | undefined}; export type GetApiAbdmV1ProfileOptions = {baseUrl?: string | undefined, authApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const profileResponse = z.object({abha_address: z.string().optional(), abha_addresses: z.array(z.string()).optional(), abha_number: z.unknown(), address: z.unknown(), day_of_birth: z.unknown(), district_code: z.string().optional(), district_name: z.string().optional(), first_name: z.unknown(), full_name: z.string().optional(), gender: z.enum(["M", "F", "O"]).optional(), kyc_verified: z.unknown(), last_name: z.unknown(), middle_name: z.unknown(), mobile: z.unknown(), month_of_birth: z.unknown(), pincode: z.unknown(), profile_photo: z.string().optional(), state_code: z.string().optional(), state_name: z.string().optional(), sub_district_code: z.string().optional(), sub_district_name: z.string().optional(), town_code: z.string().optional(), town_name: z.string().optional(), village_code: z.string().optional(), village_name: z.string().optional(), ward_code: z.string().optional(), ward_name: z.string().optional(), year_of_birth: z.unknown()}); 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 }; /** * Fetches the ABHA profile of the User. Please cache these results locally. We do not recommend calling this API on every page load. Evict the cache when there is profile update. * * GET /abdm/v1/profile */ export const getApiAbdmV1Profile = async (args: GetApiAbdmV1ProfileArgs, options?: GetApiAbdmV1ProfileOptions) => { const {baseUrl = "https://api.eka.care", fetch: fetchFn = fetch, authApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['X-Pt-Id'] != null) headers.set("X-Pt-Id", String(args['X-Pt-Id'])) if (args['X-Partner-Pt-Id'] != null) headers.set("X-Partner-Pt-Id", String(args['X-Partner-Pt-Id'])) if (args['X-Hip-Id'] != null) headers.set("X-Hip-Id", String(args['X-Hip-Id'])) if (args.oid != null) query.append("oid", String(args.oid)) if (authApiKey !== undefined) headers.set('authorization', `Bearer ${authApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/abdm/v1/profile${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(profileResponse, await res.json()) };