import {z} from 'zod' export type GetApiReportsArgs = {sort?: ('CREATED_AT' | 'EXPIRATION' | 'REQUESTER_EMAIL' | 'STATUS' | 'TYPE' | 'UPDATED_AT' | '-CREATED_AT' | '-EXPIRATION' | '-REQUESTER_EMAIL' | '-STATUS' | '-TYPE' | '-UPDATED_AT') | undefined}; export type GetApiReportsOptions = {baseUrl?: string | undefined, xApiKey?: string | undefined, xOrgId?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiReportsResponse = z.array(z.object({artifacts: z.array(z.object({format: z.enum(["CSV", "JSON"]).optional(), id: z.string().optional()})).optional(), created_at: z.string().optional(), expiration: z.string().optional(), id: z.string().optional(), parameters: z.array(z.record(z.string(), z.unknown())).optional(), requester_email: z.string().optional(), requester_id: z.string().optional(), status: z.enum(["PENDING", "IN_PROGRESS", "COMPLETED", "FAILED", "DELETED"]).optional(), type: z.enum(["browser-patch-policy", "os-patch-policy", "os-version", "software-inventory", "users-account-health", "users-to-devices", "users-to-directories", "users-to-ldap-servers", "users-to-radius-servers", "users-to-sso-applications", "users-to-user-groups"]).optional(), updated_at: 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 }; /** * Get a list of report metadata * * GET /reports */ export const getApiReports = async (args: GetApiReportsArgs, options?: GetApiReportsOptions) => { const {baseUrl = "https://api.jumpcloud.com/insights/directory/v1", fetch: fetchFn = fetch, xApiKey, xOrgId} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.sort != null) query.append("sort", String(args.sort)) if (xApiKey !== undefined) headers.set("x-api-key", xApiKey) if (xOrgId !== undefined) headers.set("x-org-id", xOrgId) const url = `${baseUrl.replace(/\/+$/, '')}/reports${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(getApiReportsResponse, await res.json()) };