import {z} from 'zod' export type GetApiV1WorkspacesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const dateTime = z.string(); export const workspace = z.object({key: z.string(), name: z.string(), created_at: dateTime, creator: z.string(), domain_whitelist: z.array(z.string()), tags: z.array(z.object({key: z.string(), name: z.string(), color: z.string(), description: z.string()})), allow_ai: z.boolean(), allow_csv_downloads: z.boolean(), allow_embeds: z.boolean(), allow_guests: z.boolean(), allow_public: z.boolean(), agent_context: z.string().optional()}); export const getApiV1WorkspacesResponse = z.object({success: z.literal(true), request_id: z.string(), result: workspace}); 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 }; /** * Get the workspace * * GET /v1/workspaces */ export const getApiV1Workspaces = async (options?: GetApiV1WorkspacesOptions) => { 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 (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/workspaces${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(getApiV1WorkspacesResponse, await res.json()) };