import {z} from 'zod' export type Identifiers = {unique_id: string, eka_code?: (string | null) | undefined, loinc?: (string | null) | undefined}; export type VitalValue = {value_type: 'NUM' | 'BOOL' | 'STR', value: number | boolean | string}; export type VitalUnit = {display_unit: string, ucum_code?: (string | null) | undefined}; export type MeasuredAt = {measurement_type: 'INSTANT' | 'DURATION', start: string, end?: (string | null) | undefined}; export type Vital = {display_name: string, identifiers: Identifiers, vital_value: VitalValue, vital_unit?: VitalUnit | undefined, measured_at: MeasuredAt}; export type VitalRequest = {vitals: Array}; export type PatchApiTrackersApiV1VitalsTxnIdArgs = {txn_id: string, body: VitalRequest}; export type PatchApiTrackersApiV1VitalsTxnIdOptions = {baseUrl?: string | undefined, auth?: string | undefined, fetch?: typeof fetch | undefined}; export const processedItem = z.object({resource_type: z.string().optional(), fhir_id: z.string().optional(), identifier: z.string().optional()}); export const failedItem = z.object({resource_type: z.string().optional(), identifier: z.string().optional(), error: z.object({code: z.string().optional(), message: z.string().optional()}).optional()}); export const successResponseWithABHA = z.object({message: z.string().optional(), data: z.object({abha_linking: z.string().optional(), abha_message: z.string().optional(), processing_status: z.object({processed: z.array(processedItem).optional(), failed: z.array(failedItem).optional()}).optional()}).optional()}); export const successResponseWithoutABHA = z.object({message: z.string().optional(), data: z.object({abha_linking: z.string().optional(), processing_status: z.object({processed: z.array(processedItem).optional(), failed: z.array(failedItem).optional()}).optional()}).optional()}); export const patchApiTrackersApiV1VitalsTxnIdResponse = z.union([successResponseWithABHA, successResponseWithoutABHA]); 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 }; /** * Update Patient Vitals and Link to ABHA * * PATCH /trackers/api/v1/vitals/{txn_id} */ export const patchApiTrackersApiV1VitalsTxnId = async (args: PatchApiTrackersApiV1VitalsTxnIdArgs, options?: PatchApiTrackersApiV1VitalsTxnIdOptions) => { const {baseUrl = "https://api.eka.care", fetch: fetchFn = fetch, auth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (auth !== undefined) headers.set('authorization', `Bearer ${auth}`) const url = `${baseUrl.replace(/\/+$/, '')}/trackers/api/v1/vitals/${encodeURIComponent(String(args.txn_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(patchApiTrackersApiV1VitalsTxnIdResponse, await res.json()) };