import {z} from 'zod' export type CreateApiDocumentsArgs = {cookie_name?: (string | null) | undefined, body: BodyInit}; export type CreateApiDocumentsOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const document = z.object({id: z.number().int().nullable().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), creator_user_id: z.number().int(), tenant_id: z.number().int(), project_id: z.number().int().nullable(), chat_id: z.number().int().nullable(), assistant_id: z.number().int().nullable().optional(), message_id: z.number().int().nullable().optional(), library_id: z.number().int().nullable().optional(), filename: z.string(), content_type: z.string().optional(), description: z.string().nullable(), content_id: z.number().int().nullable().optional(), src: z.string().nullable(), url: z.string().nullable(), sp_site_id: z.string().nullable(), sp_drive_id: z.string().nullable(), sp_drive_item_id: z.string().nullable(), sp_parent_folder_id: z.string().nullable(), sp_parent_path: z.string().nullable(), sp_last_modified_date_time: z.string().nullable(), sp_import_folder_id: z.string().nullable(), autosync: z.boolean().nullable(), import_token: z.string().nullable(), import_started_at: z.string().nullable(), import_finished_at: z.string().nullable(), import_error: z.string().nullable().optional()}); export const createApiDocumentsResponse = z.array(document); 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 }; /** * Upload documents * * POST /documents/ */ export const createApiDocuments = async (args: CreateApiDocumentsArgs, options?: CreateApiDocumentsOptions) => { const {baseUrl = "https://api.your-domain.com", fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.cookie_name != null) query.append("cookie_name", String(args.cookie_name)) if (APIKeyHeader !== undefined) headers.set("X-API-KEY", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/documents/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: args.body}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiDocumentsResponse, await res.json()) };