import {z} from 'zod' export type GetApiBetaDatasetArgs = {cursor?: (string | null) | undefined, limit?: (number | null) | undefined, direction?: ('asc' | 'desc' | null) | undefined, projectId: string, nameSearch?: (string | null) | undefined, 'X-Friendli-Team'?: (string | null) | undefined}; export type GetApiBetaDatasetOptions = {baseUrl?: string | undefined, token?: string | undefined, fetch?: typeof fetch | undefined}; export const dedicatedDatasetModalityType = z.enum(["VIDEO", "IMAGE", "AUDIO", "TEXT"]); export const dedicatedDatasetModality = z.object({input_modals: z.array(dedicatedDatasetModalityType).optional(), output_modals: z.array(dedicatedDatasetModalityType).optional()}); export const datasetInfo = z.object({id: z.string(), name: z.string(), createdAt: z.number().int(), updatedAt: z.number().int(), modality: dedicatedDatasetModality}); export const listDatasetsResponse = z.object({data: z.array(datasetInfo), nextCursor: 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 /beta/dataset */ export const getApiBetaDataset = async (args: GetApiBetaDatasetArgs, options?: GetApiBetaDatasetOptions) => { const {baseUrl = "https://api.friendli.ai", fetch: fetchFn = fetch, token} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.direction != null) query.append("direction", String(args.direction)) if (args.projectId != null) query.append("projectId", String(args.projectId)) if (args.nameSearch != null) query.append("nameSearch", String(args.nameSearch)) if (args['X-Friendli-Team'] != null) headers.set("X-Friendli-Team", String(args['X-Friendli-Team'])) if (token !== undefined) headers.set('authorization', `Bearer ${token}`) const url = `${baseUrl.replace(/\/+$/, '')}/beta/dataset${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(listDatasetsResponse, await res.json()) };