import {z} from 'zod' export type CreateApiConsultationSchedulesArgs = {body: {consultation_schedule?: ({cause?: string | undefined, starts_at?: string | undefined, ends_at?: string | undefined, reminder?: boolean | undefined, sms_reminder?: boolean | undefined, schedule_type?: string | undefined, price?: number | undefined, metadata?: ({share_payment_link?: boolean | undefined, send_payment_link?: boolean | undefined}) | undefined, person_id?: string | undefined, account_id?: string | undefined}) | undefined}}; export type CreateApiConsultationSchedulesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiConsultationSchedulesResponse = z.object({consultation_schedule: z.object({cause: z.unknown(), created_at: z.string().optional(), ends_at: z.string().optional(), id: z.number().int().optional(), links: z.object({consultation: z.unknown(), encounter: z.unknown(), invoice: z.unknown(), person: z.unknown()}).optional(), person_id: z.unknown(), starts_at: z.string().optional(), confirmed_at: z.unknown(), schedule_type: z.string().optional(), encounter_id: z.unknown(), blocked_cause: z.string().optional(), account_id: z.number().int().optional(), telehealth_url: z.unknown(), price: z.number().int().optional(), arrival_at: z.unknown(), account_confirmed: z.boolean().optional(), organization_schedule_id: z.unknown(), account_username: z.string().optional(), status: z.unknown()}).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 }; /** * POST /consultation_schedules * * POST /consultation_schedules */ export const createApiConsultationSchedules = async (args: CreateApiConsultationSchedulesArgs, options?: CreateApiConsultationSchedulesOptions) => { const {baseUrl = "https://api.example.test", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/consultation_schedules${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(createApiConsultationSchedulesResponse, await res.json()) };