import {z} from 'zod' export type GetApiV1DatasetsArgs = {limit?: number | undefined, after?: (string | null) | undefined, ai_project_id?: (string | null) | undefined, sort_by?: ('created_at' | 'name') | undefined, sort_order?: ('asc' | 'desc') | undefined}; export type GetApiV1DatasetsOptions = {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 datasetResponse = 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().nullable(), type: z.string().optional(), created_at: z.number().int(), error: z.string().nullable(), ai_project_id: z.string().nullable().optional()}); export const datasetsResponse = z.object({object: z.string().optional(), first_id: z.string().nullable(), last_id: z.string().nullable(), has_more: z.boolean(), data: z.array(datasetResponse)}); 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 a list of datasets in project * * GET /v1/datasets */ export const getApiV1Datasets = async (args: GetApiV1DatasetsArgs, options?: GetApiV1DatasetsOptions) => { const {baseUrl = "https://api.tokenfactory.nebius.com", fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.after != null) query.append("after", String(args.after)) if (args.ai_project_id != null) query.append("ai_project_id", String(args.ai_project_id)) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (args.sort_order != null) query.append("sort_order", String(args.sort_order)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/datasets${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(datasetsResponse, await res.json()) };