import {z} from 'zod' export type SchemaPrimitiveType = 'string' | 'integer' | 'boolean' | 'float' | 'double' | 'json' | 'null'; export type SchemaPrimitive = {name: SchemaPrimitiveType}; export type SchemaOptionType = 'option'; export type SchemaOptionInput = {name: SchemaOptionType, item: SchemaPrimitive | SchemaOptionInput}; export type ColumnSchemaInput = {name: string, type: SchemaPrimitive | SchemaOptionInput}; export type CreateDatasetRequestUpload = {name: string, schema: Array, folder: string, rows: Array<{[key: string]: unknown}>, ai_project_id?: (string | null) | undefined}; export type CreateDatasetRequestS3 = {name: string, folder: string, s3_service_url: string, s3_region: string, s3_uri: string, s3_key_id: string, s3_secret_key: string, ai_project_id?: (string | null) | undefined}; export type CreateApiV1DatasetsArgs = {body: CreateDatasetRequestUpload | CreateDatasetRequestS3}; export type CreateApiV1DatasetsOptions = {baseUrl?: string | undefined, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const datasetStatus = z.enum(["READY", "PENDING", "FAILED", "TEMPORARY", "DRAFT"]); export const schemaPrimitiveType = z.enum(["string", "integer", "boolean", "float", "double", "json", "null"]); export const schemaPrimitive = z.object({name: schemaPrimitiveType}); export const schemaOptionType = z.literal("option"); export const schemaOptionOutput: z.ZodType = z.object({name: schemaOptionType, item: z.union([schemaPrimitive, z.lazy(() => schemaOptionOutput)])}); export const columnSchemaOutput = z.object({name: z.string(), type: z.union([schemaPrimitive, schemaOptionOutput])}); export const datasetOriginType = z.enum(["upload", "s3", "demo"]); export const datasetOriginResponse = z.object({type: datasetOriginType}); export const createDatasetResponse = z.object({id: z.string(), name: z.string(), status: datasetStatus, schema: z.array(columnSchemaOutput), metadata: z.record(z.string(), z.union([z.string(), z.array(z.string())])), current_version_origin: datasetOriginResponse.nullable().optional(), folder: z.string(), current_version: z.string(), created_at: z.number().int(), error: z.string().nullable(), ai_project_id: 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 }; /** * Create a dataset by uploading data * * POST /v1/datasets */ export const createApiV1Datasets = async (args: CreateApiV1DatasetsArgs, options?: CreateApiV1DatasetsOptions) => { const {baseUrl = "https://api.tokenfactory.nebius.com", fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/datasets${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(createDatasetResponse, await res.json()) };