import {z} from 'zod' export type WorkScheduleDay = {id?: string | undefined, version?: string | undefined, breakDurationInMinutes?: number | undefined, createdDate?: number | undefined, day: 'FRIDAY' | 'MONDAY' | 'SATURDAY' | 'SUNDAY' | 'THURSDAY' | 'TUESDAY' | 'WEDNESDAY', endTimeInMinutes?: number | undefined, lastModifiedDate?: number | undefined, startTimeInMinutes?: number | undefined, workDurationInMinutes?: number | undefined}; export type WorkScheduleProfile = {id?: string | undefined, version?: string | undefined, createdDate?: number | undefined, description?: string | undefined, lastModifiedDate?: number | undefined, name: string, type: 'TICKET_ASSIGNMENT_RULE' | 'TICKET_SERVICE_LEVEL_AGREEMENT' | 'USER', weeklyWorkDays?: number | undefined, weeklyWorkDurationInMinutes?: number | undefined, workScheduleDays?: Array | undefined}; export type CreateApiWorkScheduleProfileArgs = {dryRun?: boolean | undefined, body: WorkScheduleProfile}; export type CreateApiWorkScheduleProfileOptions = {baseUrl: string, APIToken?: string | undefined, fetch?: typeof fetch | undefined}; export const workScheduleDay = z.object({id: z.string().optional(), version: z.string().optional(), breakDurationInMinutes: z.number().int().optional(), createdDate: z.number().int().optional(), day: z.enum(["FRIDAY", "MONDAY", "SATURDAY", "SUNDAY", "THURSDAY", "TUESDAY", "WEDNESDAY"]), endTimeInMinutes: z.number().int().optional(), lastModifiedDate: z.number().int().optional(), startTimeInMinutes: z.number().int().optional(), workDurationInMinutes: z.number().int().optional()}); export const workScheduleProfile = z.object({id: z.string().optional(), version: z.string().optional(), createdDate: z.number().int().optional(), description: z.string().optional(), lastModifiedDate: z.number().int().optional(), name: z.string(), type: z.enum(["TICKET_ASSIGNMENT_RULE", "TICKET_SERVICE_LEVEL_AGREEMENT", "USER"]), weeklyWorkDays: z.number().int().optional(), weeklyWorkDurationInMinutes: z.number().int().optional(), workScheduleDays: z.array(workScheduleDay).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 }; /** * create a workScheduleProfile * * POST /workScheduleProfile */ export const createApiWorkScheduleProfile = async (args: CreateApiWorkScheduleProfileArgs, options: CreateApiWorkScheduleProfileOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.dryRun != null) query.append("dryRun", String(args.dryRun)) if (APIToken !== undefined) headers.set("AuthenticationToken", APIToken) const url = `${baseUrl.replace(/\/+$/, '')}/workScheduleProfile${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(workScheduleProfile, await res.json()) };