import {z} from 'zod' export type GetApiV1WorkspacesSearchArgs = {query: string, from?: string | undefined, size?: string | undefined, sort_by?: ('created_at' | 'modified_at' | 'relevance') | undefined, sort_direction?: ('asc' | 'desc') | undefined, project_keys?: string | undefined, title_only?: ('true' | 'false') | undefined, created_by?: string | undefined}; export type GetApiV1WorkspacesSearchOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const dateTime = z.string(); export const nullableDateTime = z.string(); export const getApiV1WorkspacesSearchResponse = z.object({success: z.literal(true), request_id: z.string(), result: z.object({results: z.array(z.object({key: z.string(), name: z.string(), project_key: z.string(), project_name: z.string(), created_at: dateTime, created_by: z.string(), modified_at: nullableDateTime, modified_by: z.string().nullable(), preview: z.string().nullable(), object_results: z.array(z.object({key: z.string(), name: z.string(), text: z.string(), type: z.union([z.literal("cell"), z.literal("ellipse"), z.literal("rectangle"), z.literal("sticky_note"), z.literal("text"), z.literal("thread"), z.literal("overview")])}))})), more_results: z.boolean()})}); 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 }; /** * Search workspace * * GET /v1/workspaces/search */ export const getApiV1WorkspacesSearch = async (args: GetApiV1WorkspacesSearchArgs, options?: GetApiV1WorkspacesSearchOptions) => { 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.query != null) query.append("query", String(args.query)) if (args.from != null) query.append("from", String(args.from)) if (args.size != null) query.append("size", String(args.size)) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (args.sort_direction != null) query.append("sort_direction", String(args.sort_direction)) if (args.project_keys != null) query.append("project_keys", String(args.project_keys)) if (args.title_only != null) query.append("title_only", String(args.title_only)) if (args.created_by != null) query.append("created_by", String(args.created_by)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/workspaces/search${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(getApiV1WorkspacesSearchResponse, await res.json()) };