import {z} from 'zod' export type GetApiBetaBusinessHoursSchedulesScheduleIdDefinitionArgs = {scheduleId: string}; export type GetApiBetaBusinessHoursSchedulesScheduleIdDefinitionOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const businessHourRegularType = z.object({}); export const closed = z.object({}); export const open = z.object({}); export const businessHourStatus = z.union([closed, open]); export const timeInterval = z.object({startTime: z.string(), endTime: z.string()}); export const businessHourRegular = z.object({name: z.string(), dayOfWeek: z.number().int().optional(), businessHourType: businessHourRegularType, status: businessHourStatus, intervals: z.array(timeInterval).optional(), startDate: z.string()}); export const exception = z.object({}); export const holiday = z.object({}); export const businessHourExceptionType = z.union([exception, holiday]); export const recurring = z.object({dayOfMonth: z.number().int().optional(), monthOfYear: z.number().int().optional(), holiday: z.string().optional()}); export const businessHourException = z.object({name: z.string(), businessHourType: businessHourExceptionType, status: businessHourStatus, intervals: z.array(timeInterval).optional(), recurring: recurring.optional(), startDate: z.string(), nextDate: z.string().optional()}); export const businessHoursScheduleDefinition = z.object({id: z.string(), name: z.string(), timezone: z.string(), businessHours: z.array(businessHourRegular).optional(), exceptions: z.array(businessHourException).optional()}); export const getBusinessHoursScheduleDefinitionResult = z.object({schedule: businessHoursScheduleDefinition}); export const getBusinessHoursScheduleDefinitionOutput = z.object({data: getBusinessHoursScheduleDefinitionResult}); 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 a business hours schedule definition. * * GET /beta/business-hours/schedules/{scheduleId}/definition */ export const getApiBetaBusinessHoursSchedulesScheduleIdDefinition = async (args: GetApiBetaBusinessHoursSchedulesScheduleIdDefinitionArgs, options?: GetApiBetaBusinessHoursSchedulesScheduleIdDefinitionOptions) => { const {baseUrl = "https://dev.dixa.io", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("Authorization", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/beta/business-hours/schedules/${encodeURIComponent(String(args.scheduleId))}/definition${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(getBusinessHoursScheduleDefinitionOutput, await res.json()) };