import {z} from 'zod' export type GetApiApiV1DatasetsArgs = {datasourceName?: string | undefined, from?: string | undefined, page?: number | undefined, search?: string | undefined, size?: number | undefined}; export type GetApiApiV1DatasetsOptions = {baseUrl?: string | undefined, basicAuthApiKey?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const mapOfStringToObject = z.record(z.string(), z.unknown()); export const dataQualityStatusDTO = z.enum(["pass", "warn", "fail"]); export const modelVersionDTO = z.enum(["3", "4"]); export const datasourcePropertiesDTO = z.object({label: z.string().optional(), name: z.string().optional(), prefix: z.string().optional(), type: z.string().optional(), version: modelVersionDTO.optional()}); export const datasetOwnerTypeDTO = z.enum(["user", "userGroup"]); export const userContentDTO = z.object({email: z.string(), firstName: z.string().optional(), fullName: z.string().optional(), lastName: z.string().optional(), userId: z.string()}); export const userGroupContentBasicDTO = z.object({name: z.string(), userGroupId: z.string()}); export const datasetOwnerDTO = z.object({type: datasetOwnerTypeDTO, user: userContentDTO.optional(), userGroup: userGroupContentBasicDTO.optional()}); export const columnTypeDTO = z.enum(["unknown", "string", "integer", "float", "time", "boolean", "date", "dateTime"]); export const datasetPartitionColumnContentDTO = z.object({columnType: columnTypeDTO, name: z.string()}); export const datasetTimePartitionTypeDTO = z.enum(["disabled", "partitionColumn", "sqlExpression"]); export const datasetTimePartitionDTO = z.object({partitionColumn: datasetPartitionColumnContentDTO.optional(), sqlExpression: z.string().optional(), timePartitionType: datasetTimePartitionTypeDTO.optional()}); export const datasetsContentDTO = z.object({attributes: mapOfStringToObject.optional(), checks: z.number(), cloudUrl: z.string(), dataQualityStatus: dataQualityStatusDTO.optional(), datasetQualifiedName: z.string().optional(), datasource: datasourcePropertiesDTO.optional(), healthStatus: z.number().int(), id: z.string(), incidents: z.number(), label: z.string().optional(), lastUpdated: z.string().optional(), name: z.string(), owners: z.array(datasetOwnerDTO).optional(), partitionColumn: datasetPartitionColumnContentDTO.optional(), qualifiedName: z.string().optional(), tags: z.array(z.string()).optional(), timePartition: datasetTimePartitionDTO.optional()}); export const publicApiDatasetsResponse = z.object({content: z.array(datasetsContentDTO), first: z.boolean(), last: z.boolean(), number: z.number().int(), size: z.number().int(), totalElements: z.number().int(), totalPages: z.number().int()}); 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 }; /** * Dataset information * * GET /api/v1/datasets * * Also accepts auth: cookieToken */ export const getApiApiV1Datasets = async (args: GetApiApiV1DatasetsArgs, options?: GetApiApiV1DatasetsOptions) => { const {baseUrl = "https://cloud.soda.io", fetch: fetchFn = fetch, basicAuthApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.datasourceName != null) query.append("datasourceName", String(args.datasourceName)) if (args.from != null) query.append("from", String(args.from)) if (args.page != null) query.append("page", String(args.page)) if (args.search != null) query.append("search", String(args.search)) if (args.size != null) query.append("size", String(args.size)) if (basicAuthApiKey !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuthApiKey.username}:${basicAuthApiKey.password}`)}`) 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(publicApiDatasetsResponse, await res.json()) };