import {z} from 'zod' export type CampaignStatusEnabled = {value?: unknown | undefined}; export type CampaignStatusDisabled = {value?: unknown | undefined, reason?: string | undefined}; export type CampaignStatusArchived = {value?: unknown | undefined, reason?: string | undefined, date?: unknown | undefined}; export type CampaignScheduleDaily = {start?: ({hour?: number | undefined, minute?: number | undefined}) | undefined, end?: ({hour?: number | undefined, minute?: number | undefined}) | undefined, tz?: string | undefined, type?: unknown | undefined}; export type CampaignScheduleWeekly = {start?: ({day?: number | undefined, hour?: number | undefined, minute?: number | undefined}) | undefined, end?: ({day?: number | undefined, hour?: number | undefined, minute?: number | undefined}) | undefined, tz?: string | undefined, type?: unknown | undefined}; export type CampaignScheduleOneshot = {start?: unknown | undefined, end?: unknown | undefined, type?: unknown | undefined}; export type CampaignScheduleMonthly = {start?: ({day?: number | undefined, hour?: number | undefined, minute?: number | undefined}) | undefined, end?: ({day?: number | undefined, hour?: number | undefined, minute?: number | undefined}) | undefined, tz?: string | undefined, position?: number | undefined, type?: unknown | undefined}; export type RuleTarget = string; export type RuleTargets = Array<{include: {or?: Array | undefined}, exclude: {or?: Array | undefined}}>; export type CampaignSystemUpdate = {reboot?: ('as-needed' | 'disabled' | 'always' | 'services-only') | undefined, targets?: Array | undefined}; export type CampaignSoftwareUpdate = {reboot?: ('as-needed' | 'disabled' | 'always' | 'services-only') | undefined, targets?: Array | undefined, softwareUpdate?: (Array<{name?: string | undefined, version?: string | undefined, architecture?: string | undefined}>) | undefined}; export type CampaignDetails = {info?: ({id?: string | undefined, name?: string | undefined, description?: string | undefined, status?: (CampaignStatusEnabled | CampaignStatusDisabled | CampaignStatusArchived) | undefined, schedule?: (CampaignScheduleDaily | CampaignScheduleWeekly | CampaignScheduleOneshot | CampaignScheduleMonthly) | undefined}) | undefined, campaignType?: ('system-update' | 'software-update') | undefined, version?: number | undefined, details?: (CampaignSystemUpdate | CampaignSoftwareUpdate) | undefined}; export type CreateApiCampaignsArgs = {body: CampaignDetails}; export type CreateApiCampaignsOptions = {baseUrl?: string | undefined, APITokens?: string | undefined, fetch?: typeof fetch | undefined}; export const campaignStatusEnabled = z.object({value: z.string().optional()}); export const campaignStatusDisabled = z.object({value: z.string().optional(), reason: z.string().optional()}); export const campaignStatusArchived = z.object({value: z.string().optional(), reason: z.string().optional(), date: z.string().optional()}); export const campaignScheduleDaily = z.object({start: z.object({hour: z.number().int().optional(), minute: z.number().int().optional()}).optional(), end: z.object({hour: z.number().int().optional(), minute: z.number().int().optional()}).optional(), tz: z.string().optional(), type: z.string().optional()}); export const campaignScheduleWeekly = z.object({start: z.object({day: z.number().int().optional(), hour: z.number().int().optional(), minute: z.number().int().optional()}).optional(), end: z.object({day: z.number().int().optional(), hour: z.number().int().optional(), minute: z.number().int().optional()}).optional(), tz: z.string().optional(), type: z.string().optional()}); export const campaignScheduleOneshot = z.object({start: z.string().optional(), end: z.string().optional(), type: z.string().optional()}); export const campaignScheduleMonthly = z.object({start: z.object({day: z.number().int().optional(), hour: z.number().int().optional(), minute: z.number().int().optional()}).optional(), end: z.object({day: z.number().int().optional(), hour: z.number().int().optional(), minute: z.number().int().optional()}).optional(), tz: z.string().optional(), position: z.number().int().optional(), type: z.string().optional()}); export const ruleTarget = z.string(); export const ruleTargets = z.array(z.object({include: z.object({or: z.array(ruleTarget).optional()}), exclude: z.object({or: z.array(ruleTarget).optional()})})); export const campaignSystemUpdate = z.object({reboot: z.enum(["as-needed", "disabled", "always", "services-only"]).optional(), targets: z.array(ruleTargets).optional()}); export const campaignSoftwareUpdate = z.object({reboot: z.enum(["as-needed", "disabled", "always", "services-only"]).optional(), targets: z.array(ruleTargets).optional(), softwareUpdate: z.array(z.object({name: z.string().optional(), version: z.string().optional(), architecture: z.string().optional()})).optional()}); export const campaignDetails = z.object({info: z.object({id: z.string().optional(), name: z.string().optional(), description: z.string().optional(), status: z.union([campaignStatusEnabled, campaignStatusDisabled, campaignStatusArchived]).optional(), schedule: z.union([campaignScheduleDaily, campaignScheduleWeekly, campaignScheduleOneshot, campaignScheduleMonthly]).optional()}).optional(), campaignType: z.enum(["system-update", "software-update"]).optional(), version: z.number().int().optional(), details: z.union([campaignSystemUpdate, campaignSoftwareUpdate]).optional()}); export const createApiCampaignsResponse = z.object({result: z.enum(["success", "error"]), action: z.literal("saveCampaign"), data: z.object({campaigns: z.array(campaignDetails)})}); 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 }; /** * Save a campaign * * POST /campaigns */ export const createApiCampaigns = async (args: CreateApiCampaignsArgs, options?: CreateApiCampaignsOptions) => { const {baseUrl = "https://rudder.example.com/rudder/api/latest", fetch: fetchFn = fetch, APITokens} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APITokens !== undefined) headers.set("X-API-Token", APITokens) const url = `${baseUrl.replace(/\/+$/, '')}/campaigns${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(createApiCampaignsResponse, await res.json()) };