import {z} from 'zod' export type DashboardMetadataCreate = {name?: string | undefined, project?: string | undefined}; export type DashboardDisplay = {name: string | null, description?: (string | null) | undefined}; export type DashboardPlugin = {kind: string, spec: unknown}; export type DashboardDatasourceSpec = {display: DashboardDisplay | null, default?: boolean | undefined, plugin: DashboardPlugin}; export type DashboardVariableDisplay = {name?: string | undefined, description?: string | undefined, hidden?: boolean | undefined}; export type DashboardTextVariableSpec = {name: string, display?: DashboardVariableDisplay | undefined, value: string, constant?: boolean | undefined}; export type DashboardTextVariable = {kind: 'TextVariable', spec: DashboardTextVariableSpec}; export type DashboardListVariableSpec = {name: string, display?: DashboardVariableDisplay | undefined, defaultValue?: (string | Array | null) | undefined, allowMultiple: boolean, allowAllValue: boolean, customAllValue?: string | undefined, capturingRegexp?: string | undefined, sort?: string | undefined, plugin: DashboardPlugin}; export type DashboardListVariable = {kind: 'ListVariable', spec: DashboardListVariableSpec}; export type DashboardVariable = DashboardTextVariable | DashboardListVariable; export type DashboardPanelDisplay = {name: string, description?: string | undefined}; export type DashboardQuery = {kind: string, spec: unknown}; export type DashboardPanelLink = {name?: string | undefined, url?: string | undefined, tooltip?: string | undefined, renderVariables?: boolean | undefined, targetBlank?: boolean | undefined}; export type DashboardPanelSpec = {display: DashboardPanelDisplay, plugin: DashboardPlugin, queries?: Array | undefined, links?: Array | undefined, variables?: Array | undefined}; export type DashboardPanel = {kind: 'Panel', spec: DashboardPanelSpec}; export type DashboardCollapse = {open: boolean}; export type DashboardGridDisplay = {title?: string | undefined, collapse?: DashboardCollapse | undefined}; export type PanelRef = {$ref: string}; export type DashboardGridItem = {x: number, y: number, width: number, height: number, content: PanelRef}; export type DashboardGridSpec = {display?: DashboardGridDisplay | undefined, items: Array}; export type DashboardLayout = {kind: 'Grid', spec: DashboardGridSpec}; export type DashboardCreateSpec = {display: DashboardDisplay | null, datasources?: {[key: string]: DashboardDatasourceSpec} | undefined, variables?: Array | undefined, panels: {[key: string]: DashboardPanel} | null, layouts: Array, duration?: (string | null) | undefined, refreshInterval?: string | undefined}; export type DashboardCreate = {kind: 'Dashboard', metadata?: DashboardMetadataCreate | undefined, spec: DashboardCreateSpec}; export type DashboardUpdateRequest = {name?: (string | null) | undefined, definition?: (DashboardCreate | null) | undefined, version?: (number | null) | undefined}; export type UpdateApiV1ProjectsProjectIdDashboardsDashboardIdArgs = {dashboard_id: string, project_id: string, replace_layout?: boolean | undefined, body: DashboardUpdateRequest}; export type UpdateApiV1ProjectsProjectIdDashboardsDashboardIdOptions = {baseUrl: string, OAuth2AuthorizationCodeBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const dashboardProjectMetadataRead = z.object({name: z.string(), createdAt: z.string().optional(), updatedAt: z.string().optional(), version: z.number().int().optional(), project: z.string(), layoutUpdatedAt: z.string().optional()}); export const dashboardDisplay = z.object({name: z.string().nullable(), description: z.string().nullable().optional()}); export const dashboardPlugin = z.object({kind: z.string().min(1), spec: z.unknown()}); export const dashboardDatasourceSpec = z.object({display: dashboardDisplay.nullable(), default: z.boolean().optional(), plugin: dashboardPlugin}); export const dashboardVariableDisplay = z.object({name: z.string().optional(), description: z.string().optional(), hidden: z.boolean().optional()}); export const dashboardTextVariableSpec = z.object({name: z.string(), display: dashboardVariableDisplay.optional(), value: z.string(), constant: z.boolean().optional()}); export const dashboardTextVariable = z.object({kind: z.literal("TextVariable"), spec: dashboardTextVariableSpec}); export const dashboardListVariableSpec = z.object({name: z.string(), display: dashboardVariableDisplay.optional(), defaultValue: z.union([z.string(), z.array(z.string())]).nullable().optional(), allowMultiple: z.boolean(), allowAllValue: z.boolean(), customAllValue: z.string().optional(), capturingRegexp: z.string().optional(), sort: z.string().optional(), plugin: dashboardPlugin}); export const dashboardListVariable = z.object({kind: z.literal("ListVariable"), spec: dashboardListVariableSpec}); export const dashboardVariable = z.discriminatedUnion("kind", [dashboardTextVariable, dashboardListVariable]); export const dashboardPanelDisplay = z.object({name: z.string(), description: z.string().optional()}); export const dashboardQuery = z.object({kind: z.string(), spec: z.unknown()}); export const dashboardPanelLink = z.object({name: z.string().optional(), url: z.string().optional(), tooltip: z.string().optional(), renderVariables: z.boolean().optional(), targetBlank: z.boolean().optional()}); export const dashboardPanelSpec = z.object({display: dashboardPanelDisplay, plugin: dashboardPlugin, queries: z.array(dashboardQuery).optional(), links: z.array(dashboardPanelLink).optional(), variables: z.array(dashboardVariable).optional()}); export const dashboardPanel = z.object({kind: z.literal("Panel"), spec: dashboardPanelSpec}); export const dashboardCollapse = z.object({open: z.boolean()}); export const dashboardGridDisplay = z.object({title: z.string().optional(), collapse: dashboardCollapse.optional()}); export const panelRef = z.object({$ref: z.string()}); export const dashboardGridItem = z.object({x: z.number().int(), y: z.number().int(), width: z.number().int(), height: z.number().int(), content: panelRef}); export const dashboardGridSpec = z.object({display: dashboardGridDisplay.optional(), items: z.array(dashboardGridItem)}); export const dashboardLayout = z.object({kind: z.literal("Grid"), spec: dashboardGridSpec}); export const dashboardSpec = z.object({display: dashboardDisplay.nullable(), datasources: z.record(z.string(), dashboardDatasourceSpec).optional(), variables: z.array(dashboardVariable), panels: z.record(z.string(), dashboardPanel).nullable(), layouts: z.array(dashboardLayout), duration: z.string().nullable().optional(), refreshInterval: z.string().optional()}); export const dashboardDefinitionRead = z.object({kind: z.literal("Dashboard"), metadata: dashboardProjectMetadataRead, spec: dashboardSpec}); export const dashboard = z.object({id: z.string(), project_id: z.string(), created_at: z.string(), updated_at: z.string().nullable(), created_by_name: z.string().nullable(), updated_by_name: z.string().nullable(), dashboard_name: z.string(), dashboard_slug: z.string(), version: z.number().int(), definition: dashboardDefinitionRead}); 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 Dashboard * * PUT /v1/projects/{project_id}/dashboards/{dashboard_id}/ */ export const updateApiV1ProjectsProjectIdDashboardsDashboardId = async (args: UpdateApiV1ProjectsProjectIdDashboardsDashboardIdArgs, options: UpdateApiV1ProjectsProjectIdDashboardsDashboardIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2AuthorizationCodeBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.replace_layout != null) query.append("replace_layout", String(args.replace_layout)) if (OAuth2AuthorizationCodeBearer !== undefined) headers.set('authorization', `Bearer ${OAuth2AuthorizationCodeBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/projects/${encodeURIComponent(String(args.project_id))}/dashboards/${encodeURIComponent(String(args.dashboard_id))}/${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(dashboard, await res.json()) };