import {z} from 'zod' export type GetApiApiV1DatasetsArgs = {limit?: number | undefined, cursor?: string | undefined, status?: string | undefined, q?: string | undefined, created_after?: string | undefined, created_before?: string | undefined, sort?: string | undefined, sort_direction?: string | undefined, created_by_user_id?: string | undefined, type?: string | undefined}; export type GetApiApiV1DatasetsOptions = {baseUrl: string, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const datasetSummaryDto = z.object({dataset_id: z.string(), name: z.string().nullable().optional(), status: z.enum(["pending", "running", "awaiting_input", "succeeded", "failed"]), updated_at: z.string(), created_at: z.string(), row_count: z.number().int().nullable().optional(), description: z.string().nullable().optional()}); export const cursorListResponseDto = z.object({datasets: z.array(datasetSummaryDto), next_cursor: 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 }; /** * List datasets * * GET /api/v1/datasets */ export const getApiApiV1Datasets = async (args: GetApiApiV1DatasetsArgs, options: GetApiApiV1DatasetsOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearer} = 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.cursor != null) query.append("cursor", String(args.cursor)) if (args.status != null) query.append("status", String(args.status)) if (args.q != null) query.append("q", String(args.q)) if (args.created_after != null) query.append("created_after", String(args.created_after)) if (args.created_before != null) query.append("created_before", String(args.created_before)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.sort_direction != null) query.append("sort_direction", String(args.sort_direction)) if (args.created_by_user_id != null) query.append("created_by_user_id", String(args.created_by_user_id)) if (args.type != null) query.append("type", String(args.type)) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/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(cursorListResponseDto, await res.json()) };