import {z} from 'zod' export type GetApiV1ProjectsProjectIdDashboardsDashboardIdArgs = {dashboard_id: string, project_id: string}; export type GetApiV1ProjectsProjectIdDashboardsDashboardIdOptions = {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 getDashboardResponse = z.object({dashboard: 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 }; /** * Get Dashboard * * GET /v1/projects/{project_id}/dashboards/{dashboard_id}/ */ export const getApiV1ProjectsProjectIdDashboardsDashboardId = async (args: GetApiV1ProjectsProjectIdDashboardsDashboardIdArgs, options: GetApiV1ProjectsProjectIdDashboardsDashboardIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2AuthorizationCodeBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") 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: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getDashboardResponse, await res.json()) };