import {z} from 'zod' export type HipLocation = {latitude: unknown, longitude: unknown}; export type ScanandshareAddress = {district?: string | undefined, line?: string | undefined, pin_code: string, state?: string | undefined}; export type ScanandsharePatient = {abha_address: string, address?: ScanandshareAddress | undefined, day_of_birth: string, gender: string, mobile: string, month_of_birth: string, name: string, year_of_birth: string}; export type ScanandshareProfileShareRequest = {counter_id: string, hip_id: string, location?: HipLocation | undefined, patient?: ScanandsharePatient | undefined, request_id?: string | undefined, user_x_token?: string | undefined}; export type CreateApiAbdmV2ProfileShareArgs = {'X-Pt-Id'?: string | undefined, 'X-Partner-Pt-Id'?: string | undefined, 'X-Hip-Id'?: string | undefined, oid?: string | undefined, body: ScanandshareProfileShareRequest}; export type CreateApiAbdmV2ProfileShareOptions = {baseUrl?: string | undefined, authApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiAbdmV2ProfileShareResponse = z.object({request_id: z.string().optional(), status: z.enum(["success", " processing", " failed"]).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 }; /** * Request to generate appointment token at the facility. Get the hip_id and counter_id by reading the QR code of the Hospital/Clinic. Use the device location for the location data * * POST /abdm/v2/profile/share */ export const createApiAbdmV2ProfileShare = async (args: CreateApiAbdmV2ProfileShareArgs, options?: CreateApiAbdmV2ProfileShareOptions) => { const {baseUrl = "https://api.eka.care", fetch: fetchFn = fetch, authApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") 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/v2/profile/share${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiAbdmV2ProfileShareResponse, await res.json()) };