import {z} from 'zod' export type PublicDashboardTimeRange = {secs: number, type: 'relative'} | {end: number, start: number, type: 'absolute'}; export type TimeRangePolicy = {default: PublicDashboardTimeRange, ranges: Array}; export type PublicDashboardVisibility = 'draft' | 'public'; export type PublicDashboardConfig = {expires_at?: (number | null) | undefined, frozen_variables?: Record | undefined, name?: string | undefined, refresh_secs?: number | undefined, time_range: TimeRangePolicy, visibility?: PublicDashboardVisibility | undefined}; export type CreateApiApiOrgIdDashboardsDashboardIdPublicLinksArgs = {org_id: string, dashboard_id: string, body: PublicDashboardConfig}; export type CreateApiApiOrgIdDashboardsDashboardIdPublicLinksOptions = {baseUrl: string, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const publicLinkStatus = z.enum(["live", "paused", "not_viewed", "needs_attention", "expired", "dashboard_deleted", "disabled"]); export const publicDashboardTimeRange = z.union([z.object({secs: z.number().int(), type: z.literal("relative")}), z.object({end: z.number().int(), start: z.number().int(), type: z.literal("absolute")})]); export const timeRangePolicy = z.object({default: publicDashboardTimeRange, ranges: z.array(publicDashboardTimeRange)}); export const publicLinkView = z.object({access_count: z.number().int(), created_at: z.number().int(), dashboard_id: z.string(), dashboard_title: z.string().nullable().optional(), enabled: z.boolean(), expires_at: z.number().int().nullable().optional(), folder_id: z.string().nullable().optional(), folder_name: z.string().nullable().optional(), frozen_variables: z.record(z.string(), z.unknown()), id: z.string(), last_accessed_at: z.number().int().nullable().optional(), name: z.string(), published_by: z.string(), publisher_left: z.boolean(), refresh_secs: z.number().int(), slug: z.string().nullable().optional(), status: publicLinkStatus, time_range: timeRangePolicy, updated_at: z.number().int(), updated_by: z.string().nullable().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 }; /** * Create a public link for a dashboard * * POST /api/{org_id}/dashboards/{dashboard_id}/public_links */ export const createApiApiOrgIdDashboardsDashboardIdPublicLinks = async (args: CreateApiApiOrgIdDashboardsDashboardIdPublicLinksArgs, options: CreateApiApiOrgIdDashboardsDashboardIdPublicLinksOptions) => { const {baseUrl, fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Authorization !== undefined) headers.set("Authorization", Authorization) const url = `${baseUrl.replace(/\/+$/, '')}/api/${encodeURIComponent(String(args.org_id))}/dashboards/${encodeURIComponent(String(args.dashboard_id))}/public_links${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(publicLinkView, await res.json()) };