import {z} from 'zod' export type GetApiV0ScheduleArgs = {start: string, end: string, questionnaire: string, state: string, duration?: number | undefined}; export type GetApiV0ScheduleOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const model10 = z.object({start: z.string(), end: z.string()}); export const availabilities = z.array(model10); export const model11 = z.object({availabilities: availabilities.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 }; /** * Return available open slots for any provider that can be used to create consults and schedule appointments on these slots. * * GET /v0/schedule */ export const getApiV0Schedule = async (args: GetApiV0ScheduleArgs, options?: GetApiV0ScheduleOptions) => { 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 (args.start != null) query.append("start", String(args.start)) if (args.end != null) query.append("end", String(args.end)) if (args.questionnaire != null) query.append("questionnaire", String(args.questionnaire)) if (args.state != null) query.append("state", String(args.state)) if (args.duration != null) query.append("duration", String(args.duration)) if (apiKey !== undefined) headers.set("Authorization", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v0/schedule${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(model11, await res.json()) };