import {z} from 'zod' export type GetApiConsumerV1AvailabilityServiceIdStartDateEndDateArgs = {serviceId: string, startDate: string, endDate: string, startTime?: number | undefined, endTime?: number | undefined, locationId?: string | undefined, resourceId?: string | undefined, resourceGroupId?: string | undefined, resourceIds?: string | undefined, roundRobin?: string | undefined, duration?: number | undefined, interval?: number | undefined, timezoneName?: string | undefined, tzOffset?: number | undefined, destination?: string | undefined, dayAvailabilityStartDate?: string | undefined, dayAvailability?: number | undefined, firstDayAvailable?: boolean | undefined}; export type GetApiConsumerV1AvailabilityServiceIdStartDateEndDateOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const availableDayViewModel = z.object({object: z.string().nullable().optional(), date: z.string().nullable().optional(), closed: z.boolean().optional(), available: z.boolean().optional(), reasonCode: z.number().int().optional(), reason: z.string().nullable().optional(), bookingLimit: z.number().int().optional(), bookingCount: z.number().int().optional()}); export const availableTimeViewModel = z.object({startDateTime: z.string().nullable().optional(), endDateTime: z.string().nullable().optional(), date: z.string().nullable().optional(), time: z.number().int().optional(), displayTime: z.string().nullable().optional(), duration: z.number().int().optional(), allowableBookings: z.number().int().optional(), availableBookings: z.number().int().optional(), allowableCapacity: z.number().int().optional(), availableCapacity: z.number().int().optional(), resourceId: z.string().nullable().optional(), travelTimeMins: z.number().int().optional(), travelAppointmentId: z.string().nullable().optional()}); export const availabilityViewModel = z.object({url: z.string().nullable().optional(), object: z.string().nullable().optional(), businessName: z.string().nullable().optional(), locationId: z.string().nullable().optional(), serviceId: z.string().nullable().optional(), serviceName: z.string().nullable().optional(), serviceDescription: z.string().nullable().optional(), serviceDuration: z.number().int().optional(), startDate: z.string().nullable().optional(), endDate: z.string().nullable().optional(), resourceId: z.string().nullable().optional(), resourceIds: z.string().nullable().optional(), resourceName: z.string().nullable().optional(), resourceDescription: z.string().nullable().optional(), calendarId: z.string().nullable().optional(), calendarResourceGroupId: z.string().nullable().optional(), timezoneName: z.string().nullable().optional(), tzRequested: z.number().int().nullable().optional(), firstAvailableDate: z.string().nullable().optional(), availableDays: z.array(availableDayViewModel).nullable().optional(), availableTimes: z.array(availableTimeViewModel).nullable().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 Available Times * * GET /consumer/v1/availability/{serviceId}/{startDate}/{endDate} */ export const getApiConsumerV1AvailabilityServiceIdStartDateEndDate = async (args: GetApiConsumerV1AvailabilityServiceIdStartDateEndDateArgs, options?: GetApiConsumerV1AvailabilityServiceIdStartDateEndDateOptions) => { 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 (args.startTime != null) query.append("startTime", String(args.startTime)) if (args.endTime != null) query.append("endTime", String(args.endTime)) if (args.locationId != null) query.append("locationId", String(args.locationId)) if (args.resourceId != null) query.append("resourceId", String(args.resourceId)) if (args.resourceGroupId != null) query.append("resourceGroupId", String(args.resourceGroupId)) if (args.resourceIds != null) query.append("resourceIds", String(args.resourceIds)) if (args.roundRobin != null) query.append("roundRobin", String(args.roundRobin)) if (args.duration != null) query.append("duration", String(args.duration)) if (args.interval != null) query.append("interval", String(args.interval)) if (args.timezoneName != null) query.append("timezoneName", String(args.timezoneName)) if (args.tzOffset != null) query.append("tzOffset", String(args.tzOffset)) if (args.destination != null) query.append("destination", String(args.destination)) if (args.dayAvailabilityStartDate != null) query.append("dayAvailabilityStartDate", String(args.dayAvailabilityStartDate)) if (args.dayAvailability != null) query.append("dayAvailability", String(args.dayAvailability)) if (args.firstDayAvailable != null) query.append("firstDayAvailable", String(args.firstDayAvailable)) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/consumer/v1/availability/${encodeURIComponent(String(args.serviceId))}/${encodeURIComponent(String(args.startDate))}/${encodeURIComponent(String(args.endDate))}${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(availabilityViewModel, await res.json()) };