import {z} from 'zod' export type GetApiDocumentsV1FilesArgs = {cursor?: string | undefined, limit?: number | undefined, direction?: ('next' | 'prev') | undefined, filename?: string | undefined, created_after?: string | undefined, created_before?: string | undefined}; export type GetApiDocumentsV1FilesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const pageFileResponse = z.object({items: z.array(z.object({id: z.string(), name: z.string().optional(), fileSize: z.number().int().gte(0), mimeType: z.string().optional(), createdAt: z.string(), updatedAt: z.string()})), hasMore: z.boolean(), prevCursor: z.string().nullable().optional(), nextCursor: 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 }; /** * GET /documents/v1/files */ export const getApiDocumentsV1Files = async (args: GetApiDocumentsV1FilesArgs, options?: GetApiDocumentsV1FilesOptions) => { const {baseUrl = "https://api.tensorlake.ai", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.direction != null) query.append("direction", String(args.direction)) if (args.filename != null) query.append("filename", String(args.filename)) if (args.created_after != null) query.append("created_after", String(args.created_after)) if (args.created_before != null) query.append("created_before", String(args.created_before)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/documents/v1/files${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(pageFileResponse, await res.json()) };