import {z} from 'zod' export type GetApiGenaiV1betaFilesArgs = {pageSize?: number | undefined, pageToken?: string | undefined}; export type GetApiGenaiV1betaFilesOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiGenaiV1betaFilesResponse = z.object({files: z.array(z.object({name: z.string().optional(), displayName: z.string().optional(), mimeType: z.string().optional(), sizeBytes: z.string().optional(), createTime: z.string().optional(), updateTime: z.string().optional(), expirationTime: z.string().optional(), sha256Hash: z.string().optional(), uri: z.string().optional(), state: z.enum(["STATE_UNSPECIFIED", "PROCESSING", "ACTIVE", "FAILED"]).optional(), error: z.object({code: z.number().int().optional(), message: z.string().optional()}).optional(), videoMetadata: z.object({videoDuration: z.string().optional()}).optional()})).optional(), nextPageToken: z.string().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 files (Gemini format) * * GET /genai/v1beta/files * * Also accepts auth: BasicAuth, VirtualKeyAuth, ApiKeyAuth */ export const getApiGenaiV1betaFiles = async (args: GetApiGenaiV1betaFilesArgs, options?: GetApiGenaiV1betaFilesOptions) => { const {baseUrl = "http://localhost:8080", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.pageToken != null) query.append("pageToken", String(args.pageToken)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/genai/v1beta/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(getApiGenaiV1betaFilesResponse, await res.json()) };