import {z} from 'zod' export type Configv1CollectionReferenceType = 'SIMPLE' | 'SERVICE'; export type Configv1CollectionReference = {slug?: string | undefined, type?: Configv1CollectionReferenceType | undefined}; export type Configv1GrafanaDashboard = {bucket_slug?: string | undefined, collection?: Configv1CollectionReference | undefined, collection_slug?: string | undefined, created_at?: string | undefined, dashboard_json?: string | undefined, name: string, slug?: string | undefined, updated_at?: string | undefined}; export type Configv1CreateGrafanaDashboardRequest = {dry_run?: boolean | undefined, grafana_dashboard?: Configv1GrafanaDashboard | undefined}; export type CreateApiApiV1ConfigGrafanaDashboardsArgs = {body: Configv1CreateGrafanaDashboardRequest}; export type CreateApiApiV1ConfigGrafanaDashboardsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const configv1CollectionReferenceType = z.enum(["SIMPLE", "SERVICE"]); export const configv1CollectionReference = z.object({slug: z.string().optional(), type: configv1CollectionReferenceType.optional()}); export const configv1GrafanaDashboard = z.object({bucket_slug: z.string().optional(), collection: configv1CollectionReference.optional(), collection_slug: z.string().optional(), created_at: z.string().optional(), dashboard_json: z.string().optional(), name: z.string(), slug: z.string().optional(), updated_at: z.string().optional()}); export const configv1CreateGrafanaDashboardResponse = z.object({grafana_dashboard: configv1GrafanaDashboard.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 }; /** * POST /api/v1/config/grafana-dashboards */ export const createApiApiV1ConfigGrafanaDashboards = async (args: CreateApiApiV1ConfigGrafanaDashboardsArgs, options?: CreateApiApiV1ConfigGrafanaDashboardsOptions) => { const {baseUrl = "https://tenant.chronosphere.io", fetch: fetchFn = fetch, ApiKeyAuth} = 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("API-Token", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/config/grafana-dashboards${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(configv1CreateGrafanaDashboardResponse, await res.json()) };