import {z} from 'zod' export type JumpcloudInsightsRecipient = {channelType: string, value: Array}; export type JumpcloudInsightsScheduledReport = {createdAt?: string | undefined, cronExpression?: string | undefined, days?: Array | undefined, description?: string | undefined, displayName: string, endDate?: (string | null) | undefined, exportType: string, id?: string | undefined, isActive: boolean, lastRunAt?: (string | null) | undefined, lastRunStatus?: string | undefined, nextRunAt?: string | undefined, recipients: Array, reportId?: (string | null) | undefined, reportType?: (string | null) | undefined, scheduleFrequency: string, scheduleTime: string, startDate?: (string | null) | undefined, timeZone: string, updatedAt?: string | undefined}; export type UpdateApiApiV2ReportsScheduledObjectIdArgs = {objectId: string, body: {scheduledReport: JumpcloudInsightsScheduledReport}}; export type UpdateApiApiV2ReportsScheduledObjectIdOptions = {baseUrl?: string | undefined, xApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const jumpcloudInsightsRecipient = z.object({channelType: z.string(), value: z.array(z.string())}); export const jumpcloudInsightsScheduledReport = z.object({createdAt: z.string().optional(), cronExpression: z.string().optional(), days: z.array(z.string()).optional(), description: z.string().optional(), displayName: z.string(), endDate: z.string().nullable().optional(), exportType: z.string(), id: z.string().optional(), isActive: z.boolean(), lastRunAt: z.string().nullable().optional(), lastRunStatus: z.string().optional(), nextRunAt: z.string().optional(), recipients: z.array(jumpcloudInsightsRecipient), reportId: z.string().nullable().optional(), reportType: z.string().nullable().optional(), scheduleFrequency: z.string(), scheduleTime: z.string(), startDate: z.string().nullable().optional(), timeZone: z.string(), updatedAt: z.string().optional()}); export const jumpcloudInsightsScheduledReportResponse = z.object({scheduledReport: jumpcloudInsightsScheduledReport.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 }; /** * Update a Scheduled Report * * PUT /api/v2/reports/scheduled/{objectId} */ export const updateApiApiV2ReportsScheduledObjectId = async (args: UpdateApiApiV2ReportsScheduledObjectIdArgs, options?: UpdateApiApiV2ReportsScheduledObjectIdOptions) => { const {baseUrl = "https://console.jumpcloud.com", fetch: fetchFn = fetch, xApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (xApiKey !== undefined) headers.set("x-api-key", xApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/reports/scheduled/${encodeURIComponent(String(args.objectId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(jumpcloudInsightsScheduledReportResponse, await res.json()) };