import {z} from 'zod' export type CreateApiV1RoomsRoomIdBlockouttimesArgs = {room_id: string}; export type CreateApiV1RoomsRoomIdBlockouttimesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1RoomsRoomIdBlockouttimesResponse = z.object({block_out_times: z.array(z.object({block_out_time_id: z.string().optional(), employee: z.unknown(), room: z.object({id: z.string().optional(), name: z.string().optional()}).optional(), 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.string().optional(), time_period: z.string().optional(), interval: z.number().int().optional(), rec_start_date: z.string().optional(), rec_end_date: z.unknown(), week_days: z.number().int().optional(), occurences: z.number().int().optional()}).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 }; /** * Create the blockout time of a room * * POST /v1/rooms/{room_id}/blockouttimes */ export const createApiV1RoomsRoomIdBlockouttimes = async (args: CreateApiV1RoomsRoomIdBlockouttimesArgs, options?: CreateApiV1RoomsRoomIdBlockouttimesOptions) => { const {baseUrl = "https://api.zenoti.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/rooms/${encodeURIComponent(String(args.room_id))}/blockouttimes${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiV1RoomsRoomIdBlockouttimesResponse, await res.json()) };