import {z} from 'zod' export type WeekDayInputModel = {startTime?: number | undefined, endTime?: number | undefined}; export type AvailabilityInputModel = {sun?: WeekDayInputModel | undefined, mon?: WeekDayInputModel | undefined, tue?: WeekDayInputModel | undefined, wed?: WeekDayInputModel | undefined, thu?: WeekDayInputModel | undefined, fri?: WeekDayInputModel | undefined, sat?: WeekDayInputModel | undefined}; export type ScheduleInputModel = {locationId?: (string | null) | undefined, name?: (string | null) | undefined, type?: (string | null) | undefined, interval?: number | undefined, bookingsPerSlot?: number | undefined, resourceGroupId?: (string | null) | undefined, availability?: AvailabilityInputModel | undefined}; export type CreateApiSetupV1CalendarsArgs = {body: ScheduleInputModel}; export type CreateApiSetupV1CalendarsOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const calendarHourViewModel = z.object({startTime: z.number().int().optional(), endTime: z.number().int().optional()}); export const calendarHoursViewModel = z.object({mon: calendarHourViewModel.optional(), tue: calendarHourViewModel.optional(), wed: calendarHourViewModel.optional(), thu: calendarHourViewModel.optional(), fri: calendarHourViewModel.optional(), sat: calendarHourViewModel.optional(), sun: calendarHourViewModel.optional()}); export const scheduleViewModel = z.object({object: z.string().nullable().optional(), id: z.string().nullable().optional(), locationId: z.string().nullable().optional(), name: z.string().nullable().optional(), type: z.string().nullable().optional(), primary: z.boolean().optional(), interval: z.number().int().optional(), bookingsPerSlot: z.number().int().optional(), resourceGroupId: z.string().nullable().optional(), deletedStatus: z.boolean().optional(), deletedTime: z.string().nullable().optional(), availability: calendarHoursViewModel.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 }; /** * DEPRECATING: Create * * POST /setup/v1/calendars */ export const createApiSetupV1Calendars = async (args: CreateApiSetupV1CalendarsArgs, options?: CreateApiSetupV1CalendarsOptions) => { const {baseUrl = "https://sandbox-api.onsched.com", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/setup/v1/calendars${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(scheduleViewModel, await res.json()) };