import {z} from 'zod' export type GetApiApiV1PatientPatientTokenPrescriptionsArgs = {patient_token: string}; export type GetApiApiV1PatientPatientTokenPrescriptionsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const prescription = z.object({date_written: z.string().optional(), days_supply: z.string().optional(), is_refill: z.number().optional(), last_filled_date: z.string().optional(), expiration_date_utc: z.string().optional(), medication_sig: z.string().optional(), number_of_refills_allowed: z.string().optional(), prescribed_brand_name: z.string().optional(), prescribed_drug_strength: z.string().optional(), prescribed_generic_name: z.string().optional(), prescribed_ndc: z.string().optional(), prescribed_quantity: z.string().optional(), prescribed_written_name: z.string().optional(), prescriber: z.string().optional(), prescriber_npi: z.string().optional(), quantity_remaining: z.string().optional(), refills_remaining: z.string().optional(), rx_number: z.string().optional(), origin: z.string().optional(), prescriber_order_number: z.string().optional(), original_prescribed_ndc: z.string().optional(), date_filled_utc: z.string().optional(), prescription_token: z.string().optional(), medication_name: z.string().optional(), fillable: z.boolean().optional()}); export const prescriptions = z.array(prescription); export const model104 = z.object({patient_token: z.string().optional(), prescriptions: prescriptions.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 }; /** * List Patient Prescriptions * * GET /api/v1/patient/{patient_token}/prescriptions */ export const getApiApiV1PatientPatientTokenPrescriptions = async (args: GetApiApiV1PatientPatientTokenPrescriptionsArgs, options?: GetApiApiV1PatientPatientTokenPrescriptionsOptions) => { const {baseUrl = "https://api.truepill.com", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("Authorization", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/patient/${encodeURIComponent(String(args.patient_token))}/prescriptions${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(model104, await res.json()) };