import {z} from 'zod' export type GetApiV2AvailabilityArgs = {startDate?: string | undefined, endDate?: string | undefined, slotLength?: number | undefined, jobTypes?: string | undefined, availableBased?: number | undefined, totalSpots?: number | undefined, scheduleOffset?: number | undefined, showHolidays?: number | undefined, ignoreUserAvailability?: boolean | undefined, Authorization: string}; export type GetApiV2AvailabilityOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const responseAvailabilitySlotV2Dto = z.object({startTime: z.string().optional(), startTimestamp: z.number().optional(), slotLength: z.number().optional(), totalSpots: z.number().optional(), openSpots: z.number().optional(), userIds: z.array(z.string()).optional()}); export const responseAvailabilitySlotsV2Dto = z.object({totalResults: z.number().optional(), data: z.array(responseAvailabilitySlotV2Dto).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 }; /** * Retrieve booking availability * * GET /v2/availability */ export const getApiV2Availability = async (args: GetApiV2AvailabilityArgs, options?: GetApiV2AvailabilityOptions) => { const {baseUrl = "https://api.workiz.com/crm/api", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.startDate != null) query.append("startDate", String(args.startDate)) if (args.endDate != null) query.append("endDate", String(args.endDate)) if (args.slotLength != null) query.append("slotLength", String(args.slotLength)) if (args.jobTypes != null) query.append("jobTypes", String(args.jobTypes)) if (args.availableBased != null) query.append("availableBased", String(args.availableBased)) if (args.totalSpots != null) query.append("totalSpots", String(args.totalSpots)) if (args.scheduleOffset != null) query.append("scheduleOffset", String(args.scheduleOffset)) if (args.showHolidays != null) query.append("showHolidays", String(args.showHolidays)) if (args.ignoreUserAvailability != null) query.append("ignoreUserAvailability", String(args.ignoreUserAvailability)) if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/v2/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(responseAvailabilitySlotsV2Dto, await res.json()) };