import {z} from 'zod' export type ReportScheduleResourceType = 'dashboard' | 'integration_dashboard'; export type ReportScheduleTemplateVariable = {name: string, values: Array}; export type PrintReportRequestAttributes = {from_ts?: number | undefined, resource_id: string, resource_type: ReportScheduleResourceType, template_variables: Array, timeframe?: string | undefined, timezone: string, to_ts?: number | undefined}; export type PrintReportType = 'report'; export type PrintReportRequestData = {attributes: PrintReportRequestAttributes, type: PrintReportType}; export type PrintReportRequest = {data: PrintReportRequestData}; export type CreateApiApiV2ReportingPrintArgs = {body: PrintReportRequest}; export type CreateApiApiV2ReportingPrintOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, appKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const reportScheduleResourceType = z.enum(["dashboard", "integration_dashboard"]); export const reportScheduleTemplateVariable = z.object({name: z.string(), values: z.array(z.string())}); export const printReportResponseAttributes = z.object({download_url: z.string(), from_ts: z.number().int(), resource_id: z.string(), resource_type: reportScheduleResourceType, template_variables: z.array(reportScheduleTemplateVariable), timeframe: z.string().optional(), timezone: z.string(), to_ts: z.number().int()}); export const printReportType = z.literal("report"); export const printReportResponseData = z.object({attributes: printReportResponseAttributes, id: z.string(), type: printReportType}); export const printReportResponse = z.object({data: printReportResponseData}); 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 }; /** * Print a report * * POST /api/v2/reporting/print * * Also accepts auth: AuthZ */ export const createApiApiV2ReportingPrint = async (args: CreateApiApiV2ReportingPrintArgs, options?: CreateApiApiV2ReportingPrintOptions) => { const {baseUrl = "https://api.datadoghq.com", fetch: fetchFn = fetch, apiKeyAuth, appKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKeyAuth !== undefined) headers.set("DD-API-KEY", apiKeyAuth) if (appKeyAuth !== undefined) headers.set("DD-APPLICATION-KEY", appKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/reporting/print${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(printReportResponse, await res.json()) };