import {z} from 'zod' export type GetApiV1DashboardsArgs = {limit?: number | undefined, page?: number | undefined, 'x-checkly-account'?: string | undefined}; export type GetApiV1DashboardsOptions = {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 bindingProjectV1 = z.object({id: z.string(), logicalId: z.string(), name: z.string()}); export const projectBindingDetailsV1 = z.object({logicalId: z.string(), pending: z.boolean(), sourceFile: z.string().nullable().optional()}); export const projectBindingOwnerV1 = z.object({project: bindingProjectV1, binding: projectBindingDetailsV1}).nullable(); export const projectBindingV1 = z.object({project: bindingProjectV1, binding: projectBindingDetailsV1}); export const projectBindingReferencesV1 = z.array(projectBindingV1); export const projectBindingsV1 = z.object({owner: projectBindingOwnerV1, references: projectBindingReferencesV1}); export const dashboardListItem = 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(), projectBindings: projectBindingsV1.optional()}); export const dashboardsList = z.array(dashboardListItem); 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 }; /** * List all dashboards * * GET /v1/dashboards */ export const getApiV1Dashboards = async (args: GetApiV1DashboardsArgs, options?: GetApiV1DashboardsOptions) => { const {baseUrl = "https://api.checklyhq.com", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.page != null) query.append("page", String(args.page)) 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: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(dashboardsList, await res.json()) };