import {z} from 'zod' export type GetApiAbdmV1RequestArgs = {'X-Pt-Id'?: string | undefined, 'X-Partner-Pt-Id'?: string | undefined, 'X-Hip-Id'?: string | undefined, id: string, type?: ('consent' | 'subscription') | undefined, status?: ('requested' | 'granted' | 'expired' | 'denied' | 'revoked') | undefined, oid?: string | undefined}; export type GetApiAbdmV1RequestOptions = {baseUrl?: string | undefined, authApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const commonsDuration = z.object({from: z.string().optional(), to: z.string().optional()}); export const commonsHiType = z.object({display: z.string().optional(), enabled: z.boolean().optional(), id: z.enum(["OPConsultation", "Prescription", "DischargeSummary", "DiagnosticReport", "ImmunizationRecord", "HealthDocumentRecord", "WellnessRecord"]).optional()}); export const commonsHiu = z.object({id: z.string().optional(), name: z.string().optional()}); export const detailsCareContext = z.object({display: z.string().optional(), id: z.string().optional()}); export const commonsHip = z.object({id: z.string().optional(), name: z.string().optional()}); export const detailsProviders = z.object({care_contexts: z.array(detailsCareContext).optional(), consent_artefact_id: z.unknown(), hip: commonsHip.optional()}); export const commonsPurpose = z.object({code: z.string().optional(), text: z.string().optional()}); export const commonsRequester = z.object({name: z.string().optional()}); export const detailsResponse = z.object({access_mode: z.literal("view").optional(), created_at: z.string().optional(), duration: commonsDuration.optional(), erase_at: z.string().optional(), hi_types: z.array(commonsHiType).optional(), hiu: commonsHiu.optional(), id: z.string().optional(), providers: z.array(detailsProviders).optional(), purpose: commonsPurpose.optional(), requester: commonsRequester.optional(), status: z.enum(["requested", "granted", "expired", "denied", "revoked"]).optional(), updated_at: 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 }; /** * GET /abdm/v1/request */ export const getApiAbdmV1Request = async (args: GetApiAbdmV1RequestArgs, options?: GetApiAbdmV1RequestOptions) => { 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.id != null) query.append("id", String(args.id)) if (args.type != null) query.append("type", String(args.type)) if (args.status != null) query.append("status", String(args.status)) if (args.oid != null) query.append("oid", String(args.oid)) if (authApiKey !== undefined) headers.set('authorization', `Bearer ${authApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/abdm/v1/request${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(detailsResponse, await res.json()) };