import {z} from 'zod' export type SuggestionType = 'SAVED' | 'TENANT' | 'ASSISTANT' | 'PATIENT' | 'NOTE_TEMPLATE' | 'EVENT'; export type GetApiSuggestionsArgs = {TENANT: string, 'API-KEY': string, suggestion_types: Array, patient_id?: (string | null) | undefined, event_id?: (string | null) | undefined}; export type GetApiSuggestionsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const suggestionType = z.enum(["SAVED", "TENANT", "ASSISTANT", "PATIENT", "NOTE_TEMPLATE", "EVENT"]); export const suggestionItem = z.object({id: z.string(), label: z.string(), content: z.string(), type: suggestionType}); export const getApiSuggestionsResponse = z.array(suggestionItem); 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 Suggestions * * GET /suggestions */ export const getApiSuggestions = async (args: GetApiSuggestionsArgs, options: GetApiSuggestionsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.TENANT != null) headers.set("TENANT", String(args.TENANT)) if (args['API-KEY'] != null) headers.set("API-KEY", String(args['API-KEY'])) for (const item of args.suggestion_types ?? []) query.append("suggestion_types", String(item)) if (args.patient_id != null) query.append("patient_id", String(args.patient_id)) if (args.event_id != null) query.append("event_id", String(args.event_id)) const url = `${baseUrl.replace(/\/+$/, '')}/suggestions${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(getApiSuggestionsResponse, await res.json()) };