import {z} from 'zod' export type GetApiApiV1AutoRefillPrescriptionTokenArgs = {prescription_token: string}; export type GetApiApiV1AutoRefillPrescriptionTokenOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const autoRefillMedications = z.object({auto_refill_id: z.number().optional(), auto_refill_date: z.string().optional(), auto_reminder: z.boolean().optional(), refills_remaining: z.number().optional()}); export const scheduledFillAddress = z.object({name: z.string().optional(), street1: z.string().optional(), street2: z.string().optional(), city: z.string().optional(), state: z.string().optional(), zip: z.number().optional(), phone: z.string().optional(), email: z.string().optional(), country: z.string().optional()}); export const scheduledFillSurvey = z.object({patient_token: z.string().optional(), url_token: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), asthma_breathing: z.number().optional(), heart_disease: z.number().optional(), arthritis: z.number().optional(), lung_disorder: z.number().optional(), bleeding_clotting_disorder: z.number().optional(), neurological_chronic_headaches: z.number().optional(), blood_transfusion: z.number().optional(), psychiatric_disorder: z.number().optional(), bowel_stomach: z.number().optional(), pulmonary_embolism: z.number().optional(), cancer: z.number().optional(), stroke: z.number().optional(), cholesterol_disorder: z.number().optional(), seizure_epilepsy: z.number().optional(), diabetes: z.number().optional(), thyroid_disorder: z.number().optional(), eye_disorder: z.number().optional(), urinary_kidney_disorder: z.number().optional(), conditions: z.number().optional(), smoking_frequency: z.number().optional(), drinking_frequency: z.number().optional(), other_medications: z.number().optional(), known_allergies: z.number().optional(), pregnancy: z.boolean().optional()}); export const model41 = z.object({auto_refill_medications: autoRefillMedications.optional(), scheduled_fill_address: scheduledFillAddress.optional(), scheduled_fill_survey: scheduledFillSurvey.optional()}); export const model42 = z.object({timestamp: z.string().optional(), status: z.string().optional(), details: model41.optional()}); export const model43 = z.object({code: z.number().optional(), err: z.boolean().optional(), data: model42.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 }; /** * GET auto refill subscription * * GET /api/v1/auto-refill/{prescription_token} */ export const getApiApiV1AutoRefillPrescriptionToken = async (args: GetApiApiV1AutoRefillPrescriptionTokenArgs, options?: GetApiApiV1AutoRefillPrescriptionTokenOptions) => { 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/auto-refill/${encodeURIComponent(String(args.prescription_token))}${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(model43, await res.json()) };