import {z} from 'zod' export type GetApiHealthApiV1FhirRetrieveArgs = {identifier: string}; export type GetApiHealthApiV1FhirRetrieveOptions = {baseUrl?: string | undefined, auth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiHealthApiV1FhirRetrieveResponse = z.object({resourceType: z.string().optional(), type: z.string().optional(), link: z.array(z.object({url: z.string().optional(), relation: z.string().optional()})).optional(), entry: z.array(z.object({resource: z.object({author: z.array(z.object({reference: z.string().optional()})).optional(), date: z.string().optional(), extension: z.array(z.object({url: z.string().optional(), valueString: z.string().optional()})).optional(), id: z.string().optional(), identifier: z.object({system: z.string().optional(), value: z.string().optional()}).optional(), meta: z.object({lastUpdated: z.string().optional()}).optional(), resourceType: z.string().optional(), section: z.array(z.object({entry: z.array(z.object({reference: z.string().optional()})).optional(), title: z.string().optional()})).optional(), status: z.string().optional(), subject: z.object({reference: z.string().optional()}).optional(), title: z.string().optional(), type: z.object({text: z.string().optional()}).optional()}).optional()})).optional(), id: z.string().optional(), meta: z.object({versionId: z.string().optional(), lastUpdated: z.string().optional(), profile: z.array(z.string()).optional(), security: z.array(z.object({code: z.string().optional(), display: z.string().optional(), system: z.string().optional()})).optional()}).optional(), identifier: z.object({system: z.string().optional(), value: z.string().optional()}).optional(), timestamp: 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 }; /** * Retrieve the prased data for the upload audio file * * GET /health/api/v1/fhir/retrieve */ export const getApiHealthApiV1FhirRetrieve = async (args: GetApiHealthApiV1FhirRetrieveArgs, options?: GetApiHealthApiV1FhirRetrieveOptions) => { const {baseUrl = "https://api.eka.care", fetch: fetchFn = fetch, auth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.identifier != null) query.append("identifier", String(args.identifier)) if (auth !== undefined) headers.set('authorization', `Bearer ${auth}`) const url = `${baseUrl.replace(/\/+$/, '')}/health/api/v1/fhir/retrieve${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(getApiHealthApiV1FhirRetrieveResponse, await res.json()) };