import {z} from 'zod' export type GetApiV1CentersCenterIdBlockouttimesArgs = {center_id: string, start_date?: string | undefined, end_date?: string | undefined, source?: number | undefined}; export type GetApiV1CentersCenterIdBlockouttimesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1CentersCenterIdBlockouttimesResponse = z.object({block_out_times: z.array(z.object({block_out_time_id: z.string().optional(), employee: z.object({id: z.string().optional(), name: z.string().optional()}).optional(), room: z.unknown(), block_out_time_type: z.object({id: z.number().int().optional(), name: z.string().optional(), duration: z.number().int().optional()}).optional(), start_time: z.string().optional(), end_time: z.string().optional(), start_time_in_center: z.string().optional(), end_time_in_center: z.string().optional(), notes: z.string().optional(), recurring_block_out_time: z.object({id: z.unknown(), time_period: z.unknown(), interval: z.number().int().optional(), rec_start_date: z.string().optional(), rec_end_date: z.unknown(), week_days: z.number().int().optional(), occurences: z.unknown()}).optional()})).optional(), error: z.unknown()}); 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 all blockout times of a center * * GET /v1/centers/{center_id}/blockouttimes */ export const getApiV1CentersCenterIdBlockouttimes = async (args: GetApiV1CentersCenterIdBlockouttimesArgs, options?: GetApiV1CentersCenterIdBlockouttimesOptions) => { const {baseUrl = "https://api.zenoti.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (args.source != null) query.append("source", String(args.source)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/centers/${encodeURIComponent(String(args.center_id))}/blockouttimes${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(getApiV1CentersCenterIdBlockouttimesResponse, await res.json()) };