import {z} from 'zod' export type ScheduleType = 'Playlist' | 'Template' | 'Campaign'; export type ScheduleDevice = {id?: (string | null) | undefined}; export type ConditionType = 'AfterDate' | 'AfterTime' | 'Always' | 'BeaconActive' | 'BeforeDate' | 'BeforeTime' | 'Command' | 'DateRange' | 'DayOfMonth' | 'DaysOfWeek' | 'DeviceByGroup' | 'DeviceByNestedGroup' | 'DeviceByName' | 'DeviceByTag' | 'DeviceByOrg' | 'Everywhere' | 'GeoLocation' | 'GpsWithinRadius' | 'KeyEvent' | 'MonthOfYear' | 'Motion' | 'Never' | 'Nowhere' | 'PctAdult' | 'PctChild' | 'PctFemale' | 'PctMale' | 'PctSenior' | 'PctYoung' | 'PctYoungAdult' | 'PlaylistByName' | 'PlaylistByTag' | 'SendCommand' | 'SpecificDevice' | 'SpecificPlaylist' | 'SpecificTemplate' | 'SpecificZone' | 'TemplateByName' | 'TemplateByTag' | 'TimeRange' | 'TopicSet' | 'TotalViewersGreaterThan' | 'TotalViewersLessThan' | 'TouchEvent' | 'WeekOfMonth' | 'WeekOfYear'; export type ConditionOperator = 'And' | 'Or' | 'AndNot' | 'OrNot'; export type Condition = {type?: ConditionType | undefined, operator?: ConditionOperator | undefined, value_1?: (string | null) | undefined, value_2?: (string | null) | undefined, value_3?: (string | null) | undefined, value_4?: (string | null) | undefined}; export type SchedulePriorityType = 'Normal' | 'High' | 'Highest' | 'Lowest' | 'Low'; export type ScheduleRequest = {name?: (string | null) | undefined, group_id?: (string | null) | undefined, tags?: (string | null) | undefined, monday?: (boolean | null) | undefined, tuesday?: (boolean | null) | undefined, wednesday?: (boolean | null) | undefined, thursday?: (boolean | null) | undefined, friday?: (boolean | null) | undefined, saturday?: (boolean | null) | undefined, sunday?: (boolean | null) | undefined, start_date?: (string | null) | undefined, end_date?: (string | null) | undefined, start_time?: (string | null) | undefined, end_time?: (string | null) | undefined, template_id?: (string | null) | undefined, playlist_id?: (string | null) | undefined, type?: ScheduleType | undefined, devices?: (Array | null) | undefined, conditions?: (Array | null) | undefined, priority?: SchedulePriorityType | undefined}; export type CreateApiSchedulesArgs = {body: ScheduleRequest}; export type CreateApiSchedulesOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const scheduleType = z.enum(["Playlist", "Template", "Campaign"]); export const scheduleDevice = z.object({id: z.string().nullable().optional()}); export const conditionType = z.enum(["AfterDate", "AfterTime", "Always", "BeaconActive", "BeforeDate", "BeforeTime", "Command", "DateRange", "DayOfMonth", "DaysOfWeek", "DeviceByGroup", "DeviceByNestedGroup", "DeviceByName", "DeviceByTag", "DeviceByOrg", "Everywhere", "GeoLocation", "GpsWithinRadius", "KeyEvent", "MonthOfYear", "Motion", "Never", "Nowhere", "PctAdult", "PctChild", "PctFemale", "PctMale", "PctSenior", "PctYoung", "PctYoungAdult", "PlaylistByName", "PlaylistByTag", "SendCommand", "SpecificDevice", "SpecificPlaylist", "SpecificTemplate", "SpecificZone", "TemplateByName", "TemplateByTag", "TimeRange", "TopicSet", "TotalViewersGreaterThan", "TotalViewersLessThan", "TouchEvent", "WeekOfMonth", "WeekOfYear"]); export const conditionOperator = z.enum(["And", "Or", "AndNot", "OrNot"]); export const condition = z.object({type: conditionType.optional(), operator: conditionOperator.optional(), value_1: z.string().nullable().optional(), value_2: z.string().nullable().optional(), value_3: z.string().nullable().optional(), value_4: z.string().nullable().optional()}); export const schedulePriorityType = z.enum(["Normal", "High", "Highest", "Lowest", "Low"]); export const schedule = z.object({name: z.string().nullable().optional(), group_id: z.string().nullable().optional(), tags: z.string().nullable().optional(), monday: z.boolean().nullable().optional(), tuesday: z.boolean().nullable().optional(), wednesday: z.boolean().nullable().optional(), thursday: z.boolean().nullable().optional(), friday: z.boolean().nullable().optional(), saturday: z.boolean().nullable().optional(), sunday: z.boolean().nullable().optional(), start_date: z.string().nullable().optional(), end_date: z.string().nullable().optional(), start_time: z.string().nullable().optional(), end_time: z.string().nullable().optional(), template_id: z.string().nullable().optional(), playlist_id: z.string().nullable().optional(), type: scheduleType.optional(), devices: z.array(scheduleDevice).nullable().optional(), conditions: z.array(condition).nullable().optional(), priority: schedulePriorityType.optional(), id: z.string().nullable().optional(), group_name: z.string().nullable().optional(), created_on: z.string().nullable().optional(), created_by_id: z.string().nullable().optional(), modified_on: z.string().nullable().optional(), modified_by_id: z.string().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 }; /** * Creates a new schedule. * * POST /schedules * * Also accepts auth: ApiKeyInHeader, ApiKeyInQuery */ export const createApiSchedules = async (args: CreateApiSchedulesArgs, options?: CreateApiSchedulesOptions) => { const {baseUrl = "https://api.reveldigital.com", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/schedules${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(schedule, await res.json()) };