import {z} from 'zod' export type DashboardTagList = Array; export type DashboardKey = {id: string, rawKey: string, maskedKey: string, created_at: string, updated_at?: (string | null) | undefined}; export type DashboardCreate = {customUrl?: (string | null) | undefined, customDomain?: (string | null) | undefined, logo?: (string | '' | unknown) | undefined, favicon?: (string | '' | unknown) | undefined, link?: (string | '' | unknown) | undefined, header: string, description?: (string | null) | undefined, width?: ('FULL' | '960PX') | undefined, refreshRate?: number | undefined, paginate?: boolean | undefined, paginationRate?: number | undefined, sortByStatus?: boolean | undefined, checksPerPage?: (number | null) | undefined, useTagsAndOperator?: (boolean | null) | undefined, hideTags?: boolean | undefined, enableIncidents?: boolean | undefined, expandChecks?: boolean | undefined, tags?: DashboardTagList | undefined, showHeader?: boolean | undefined, showCheckRunLinks?: boolean | undefined, showGroupNames?: boolean | undefined, customCSS?: (string | null) | undefined, isPrivate?: boolean | undefined, showP95?: boolean | undefined, showP99?: boolean | undefined, keys?: Array | undefined}; export type CreateApiV1DashboardsArgs = {'x-checkly-account'?: string | undefined, body: DashboardCreate}; export type CreateApiV1DashboardsOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const dashboardTagList = z.array(z.string()); export const dashboardKey = z.object({id: z.string(), rawKey: z.string(), maskedKey: z.string(), created_at: z.string(), updated_at: z.string().nullable().optional()}); export const dashboard = z.object({customUrl: z.string().nullable().optional(), customDomain: z.string().nullable().optional(), logo: z.union([z.string(), z.literal(""), z.unknown()]).optional(), favicon: z.union([z.string(), z.literal(""), z.unknown()]).optional(), link: z.union([z.string(), z.literal(""), z.unknown()]).optional(), header: z.string().optional(), description: z.string().nullable().optional(), width: z.enum(["FULL", "960PX"]).optional(), refreshRate: z.number().optional(), paginate: z.boolean().optional(), paginationRate: z.number().optional(), sortByStatus: z.boolean().optional(), checksPerPage: z.number().gte(1).lte(20).nullable().optional(), useTagsAndOperator: z.boolean().nullable().optional(), hideTags: z.boolean().optional(), enableIncidents: z.boolean().optional(), expandChecks: z.boolean().optional(), tags: dashboardTagList.optional(), showHeader: z.boolean().optional(), showCheckRunLinks: z.boolean().optional(), showGroupNames: z.boolean().optional(), customCSS: z.string().nullable().optional(), isPrivate: z.boolean().optional(), showP95: z.boolean().optional(), showP99: z.boolean().optional(), keys: z.array(dashboardKey).optional(), id: z.number(), dashboardId: z.string(), created_at: z.string()}); 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 dashboard * * POST /v1/dashboards */ export const createApiV1Dashboards = async (args: CreateApiV1DashboardsArgs, options?: CreateApiV1DashboardsOptions) => { const {baseUrl = "https://api.checklyhq.com", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['x-checkly-account'] != null) headers.set("x-checkly-account", String(args['x-checkly-account'])) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/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(dashboard, await res.json()) };