import {z} from 'zod' export type GetApiSetupV1ResourcesIdAvailabilityArgs = {id: string}; export type GetApiSetupV1ResourcesIdAvailabilityOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const weekDayViewModel2 = z.object({startTime: z.number().int().optional(), endTime: z.number().int().optional(), displayStartTime: z.string().nullable().optional(), displayEndTime: z.string().nullable().optional()}); export const availabilityViewModel2 = z.object({sun: weekDayViewModel2.optional(), mon: weekDayViewModel2.optional(), tue: weekDayViewModel2.optional(), wed: weekDayViewModel2.optional(), thu: weekDayViewModel2.optional(), fri: weekDayViewModel2.optional(), sat: weekDayViewModel2.optional()}); export const resourceAvailabilityViewModel = z.object({businessId: z.number().int().optional(), resourceId: z.number().int().optional(), resourceName: z.string().nullable().optional(), resourceTzo: z.number().int().optional(), ignoreBusinessHours: z.boolean().optional(), weekdays: availabilityViewModel2.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 }; /** * List Weekly Availability * * GET /setup/v1/resources/{id}/availability */ export const getApiSetupV1ResourcesIdAvailability = async (args: GetApiSetupV1ResourcesIdAvailabilityArgs, options?: GetApiSetupV1ResourcesIdAvailabilityOptions) => { const {baseUrl = "https://sandbox-api.onsched.com", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/setup/v1/resources/${encodeURIComponent(String(args.id))}/availability${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(resourceAvailabilityViewModel, await res.json()) };