import {z} from 'zod' export type DateTime = string; export type GetApiV1CanvasesArgs = {project_key?: string | undefined, creator?: string | undefined, created_after?: DateTime | undefined, modified_after?: DateTime | undefined, tag_names?: string | undefined}; export type GetApiV1CanvasesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const dateTime = z.string(); export const nullableDateTime = z.string(); export const canvas = z.object({key: z.string(), name: z.string(), created_at: dateTime, modified_at: nullableDateTime, modified_by: z.string().nullable(), creator: z.string(), permissions: z.array(z.object({key: z.string(), role: z.union([z.literal("analyst"), z.literal("editor"), z.literal("viewer"), z.literal("presentation_viewer")]), type: z.union([z.literal("user"), z.literal("group"), z.literal("service_account"), z.literal("workspace"), z.literal("project"), z.literal("public")])})), permitted_actions: z.object({create_agent: z.boolean()}), preview: z.string().nullable(), project_key: z.string(), archived_at: nullableDateTime, background: z.string().optional(), cache_duration: z.number().optional(), automatic_cell_execution: z.boolean().optional(), refresh_schedule: z.string().optional(), refresh_strategy: z.union([z.literal("all"), z.literal("automatic"), z.literal("visible"), z.literal("visible_automatic")]).optional(), refreshed_at: dateTime.optional(), tags: z.array(z.string()).optional()}); export const getApiV1CanvasesResponse = z.object({success: z.literal(true), request_id: z.string(), result: z.object({canvases: z.array(canvas)})}); 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 canvases * * GET /v1/canvases */ export const getApiV1Canvases = async (args: GetApiV1CanvasesArgs, options?: GetApiV1CanvasesOptions) => { const {baseUrl = "https://api.app.count.co/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.project_key != null) query.append("project_key", String(args.project_key)) if (args.creator != null) query.append("creator", String(args.creator)) if (args.created_after != null) query.append("created_after", String(args.created_after)) if (args.modified_after != null) query.append("modified_after", String(args.modified_after)) if (args.tag_names != null) query.append("tag_names", String(args.tag_names)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/canvases${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(getApiV1CanvasesResponse, await res.json()) };