import {z} from 'zod' export type GetApiBusyTimesArgs = {id_gt?: (number | null) | undefined, id_lt?: (number | null) | undefined, update_gt?: (string | string) | undefined, update_lt?: (string | string) | undefined, include_archived?: ('true' | 'false' | null) | undefined, start_gt?: (string | string | unknown) | undefined, start_lt?: (string | string | unknown) | undefined, busyTimeTypeId?: (number | null) | undefined, practitionerId?: (number | null) | undefined}; export type GetApiBusyTimesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiBusyTimesResponse = z.object({data: z.array(z.object({id: z.number().int().gt(0), start: z.union([z.string(), z.string()]), end: z.union([z.string(), z.string()]), locationId: z.number().int().gt(0), practitionerIds: z.array(z.number().int().gt(0)), busyTimeTypeId: z.number().int().gt(0).nullable().optional(), repeatId: z.string().nullable(), recurringRule: z.string().nullable(), note: z.string().nullable(), archived: z.boolean().nullable().optional(), createdAt: z.union([z.string(), z.string()]).optional(), updatedAt: z.union([z.string(), z.string()]).optional(), deletedAt: z.union([z.string(), z.string(), z.unknown()])})), links: z.object({previousPage: z.string().optional(), nextPage: z.string().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 a paged array of busy-times * * GET /busy-times */ export const getApiBusyTimes = async (args: GetApiBusyTimesArgs, options?: GetApiBusyTimesOptions) => { const {baseUrl = "https://api.splose.com/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.id_gt != null) query.append("id_gt", String(args.id_gt)) if (args.id_lt != null) query.append("id_lt", String(args.id_lt)) if (args.update_gt != null) query.append("update_gt", String(args.update_gt)) if (args.update_lt != null) query.append("update_lt", String(args.update_lt)) if (args.include_archived != null) query.append("include_archived", String(args.include_archived)) if (args.start_gt != null) query.append("start_gt", String(args.start_gt)) if (args.start_lt != null) query.append("start_lt", String(args.start_lt)) if (args.busyTimeTypeId != null) query.append("busyTimeTypeId", String(args.busyTimeTypeId)) if (args.practitionerId != null) query.append("practitionerId", String(args.practitionerId)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/busy-times${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(getApiBusyTimesResponse, await res.json()) };