import {z} from 'zod' export type Configv1CollectionReferenceType = 'SIMPLE' | 'SERVICE'; export type Configv1CollectionReference = {type?: Configv1CollectionReferenceType | undefined, slug?: string | undefined}; export type Configv1Dashboard = {slug?: string | undefined, name?: string | undefined, created_at?: string | undefined, updated_at?: string | undefined, collection_slug?: string | undefined, collection?: Configv1CollectionReference | undefined, dashboard_json?: string | undefined, labels?: {[key: string]: string} | undefined}; export type Configv1CreateDashboardRequest = {dashboard?: Configv1Dashboard | undefined, dry_run?: boolean | undefined}; export type CreateApiApiV1ConfigDashboardsArgs = {body: Configv1CreateDashboardRequest}; export type CreateApiApiV1ConfigDashboardsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const configv1CollectionReferenceType = z.enum(["SIMPLE", "SERVICE"]); export const configv1CollectionReference = z.object({type: configv1CollectionReferenceType.optional(), slug: z.string().optional()}); export const configv1Dashboard = z.object({slug: z.string().optional(), name: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), collection_slug: z.string().optional(), collection: configv1CollectionReference.optional(), dashboard_json: z.string().optional(), labels: z.record(z.string(), z.string()).optional()}); export const configv1CreateDashboardResponse = z.object({dashboard: configv1Dashboard.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/dashboards */ export const createApiApiV1ConfigDashboards = async (args: CreateApiApiV1ConfigDashboardsArgs, options: CreateApiApiV1ConfigDashboardsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/config/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(configv1CreateDashboardResponse, await res.json()) };