import {z} from 'zod' export type GetApiSchedulesArgs = {id?: string | undefined, take?: number | undefined, page?: number | undefined, group_id?: Array | undefined, group_name?: Array | undefined, device_id?: string | undefined}; export type GetApiSchedulesOptions = {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 getApiSchedulesResponse = z.array(schedule); 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 }; /** * Gets schedules in an account. * * GET /schedules * * Also accepts auth: ApiKeyInHeader, ApiKeyInQuery */ export const getApiSchedules = async (args: GetApiSchedulesArgs, options?: GetApiSchedulesOptions) => { const {baseUrl = "https://api.reveldigital.com", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.id != null) query.append("id", String(args.id)) if (args.take != null) query.append("take", String(args.take)) if (args.page != null) query.append("page", String(args.page)) for (const item of args.group_id ?? []) query.append("group_id", String(item)) for (const item of args.group_name ?? []) query.append("group_name", String(item)) if (args.device_id != null) query.append("device_id", String(args.device_id)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/schedules${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(getApiSchedulesResponse, await res.json()) };