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 JumpcloudInsightsCreateScheduledReportRequest = {scheduledReport: JumpcloudInsightsScheduledReport}; export type CreateApiApiV2ReportsScheduledArgs = {body: JumpcloudInsightsCreateScheduledReportRequest}; export type CreateApiApiV2ReportsScheduledOptions = {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 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 Scheduled Report * * POST /api/v2/reports/scheduled */ export const createApiApiV2ReportsScheduled = async (args: CreateApiApiV2ReportsScheduledArgs, options?: CreateApiApiV2ReportsScheduledOptions) => { 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${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(jumpcloudInsightsScheduledReport, await res.json()) };