import {z} from 'zod' export type GetApiV1LibrariesArgs = {page_size?: number | undefined, page_token?: (string | null) | undefined, page?: number | undefined, search?: (string | null) | undefined, filter_owned_by_me?: (boolean | null) | undefined}; export type GetApiV1LibrariesOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const paginationInfo = z.object({total_items: z.number().int(), total_pages: z.number().int(), current_page: z.number().int(), page_size: z.number().int(), has_more: z.boolean()}); export const library = z.object({id: z.string(), name: z.string(), created_at: z.string(), updated_at: z.string(), owner_id: z.string().nullable(), owner_type: z.string(), total_size: z.number().int(), nb_documents: z.number().int(), chunk_size: z.number().int().nullable(), emoji: z.string().nullable().optional(), description: z.string().nullable().optional(), generated_description: z.string().nullable().optional(), explicit_user_members_count: z.number().int().nullable().optional(), explicit_workspace_members_count: z.number().int().nullable().optional(), org_sharing_role: z.string().nullable().optional(), generated_name: z.string().nullable().optional()}); export const listLibrariesResponse = z.object({pagination: paginationInfo.nullable().optional(), data: z.array(library), next_page_token: z.string().nullable().optional()}); 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 libraries you have access to. * * GET /v1/libraries */ export const getApiV1Libraries = async (args: GetApiV1LibrariesArgs, options?: GetApiV1LibrariesOptions) => { const {baseUrl = "https://api.mistral.ai", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.page_token != null) query.append("page_token", String(args.page_token)) if (args.page != null) query.append("page", String(args.page)) if (args.search != null) query.append("search", String(args.search)) if (args.filter_owned_by_me != null) query.append("filter_owned_by_me", String(args.filter_owned_by_me)) if (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/libraries${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(listLibrariesResponse, await res.json()) };