import {z} from 'zod' export type CreateApiV1UploadArgs = {c: number, o: number, d?: boolean | undefined, body: BodyInit}; export type CreateApiV1UploadOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1UploadResponse = z.object({response: z.object({status: z.object({}).optional(), quotaConsumed: z.object({numChars: z.string().optional(), numMetadataChars: z.string().optional()}).optional()}).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 }; /** * The File Upload API can be used to index binary files like PDFs, Word Documents, and similar. * * POST /v1/upload * * Also accepts auth: oAuth */ export const createApiV1Upload = async (args: CreateApiV1UploadArgs, options?: CreateApiV1UploadOptions) => { const {baseUrl = "https://api.vectara.io", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.c != null) query.append("c", String(args.c)) if (args.o != null) query.append("o", String(args.o)) if (args.d != null) query.append("d", String(args.d)) if (ApiKeyAuth !== undefined) headers.set("x-api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/upload${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(createApiV1UploadResponse, await res.json()) };