import {z} from 'zod' export type IDSEnum = 'pdf' | 'doc' | 'http' | 'tool' | 'text_blob' | 'image' | 'text_image'; export type IDSStatusEnum = 'empty' | 'initialized' | 'queued' | 'crawling' | 'in_process' | 'retry_pending' | 'ready_for_use' | 'failed'; export type IDataSourceCreate = {ds_type: IDSEnum | null, name?: (string | null) | undefined, description?: (string | null) | undefined, attributes?: {[key: string]: unknown} | undefined, ds_status?: (IDSStatusEnum | null) | undefined, org_id?: (string | null) | undefined}; export type CreateApiApiV1DatasourceArgs = {body: IDataSourceCreate}; export type CreateApiApiV1DatasourceOptions = {baseUrl: string, YourAPIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const iDSEnum = z.enum(["pdf", "doc", "http", "tool", "text_blob", "image", "text_image"]); export const iDSStatusEnum = z.enum(["empty", "initialized", "queued", "crawling", "in_process", "retry_pending", "ready_for_use", "failed"]); export const iDataSourceRead = z.object({ds_type: iDSEnum.nullable(), name: z.string().nullable().optional(), description: z.string().nullable().optional(), attributes: z.record(z.string(), z.unknown()).optional(), ds_status: iDSStatusEnum.nullable().optional(), org_id: z.string().nullable().optional(), id: z.string()}); export const iPostResponseBaseIDataSourceRead = z.object({message: z.string().nullable().optional(), meta: z.union([z.record(z.string(), z.unknown()), z.unknown()]).nullable().optional(), data: iDataSourceRead.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 }; /** * Create Data Source * * POST /api/v1/datasource * * Also accepts auth: HTTPBearer */ export const createApiApiV1Datasource = async (args: CreateApiApiV1DatasourceArgs, options: CreateApiApiV1DatasourceOptions) => { const {baseUrl, fetch: fetchFn = fetch, YourAPIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (YourAPIKey !== undefined) query.set("api_key", YourAPIKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/datasource${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(iPostResponseBaseIDataSourceRead, await res.json()) };