import {z} from 'zod' export type CatalogType = 'table' | 'field'; export type CatalogFilter = 'tables' | 'dimensions' | 'metrics'; export type GetApiApiV1ProjectsProjectUuidDataCatalogArgs = {projectUuid: string, search?: string | undefined, type?: CatalogType | undefined, filter?: CatalogFilter | undefined}; export type GetApiApiV1ProjectsProjectUuidDataCatalogOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const fieldType = z.enum(["metric", "dimension"]); export const recordStringStringOrStringArray = z.record(z.string(), z.union([z.string(), z.array(z.string())])); export const catalogOwner = z.object({email: z.string(), lastName: z.string(), firstName: z.string(), userUuid: z.string()}); export const anyType = z.unknown(); export const filterOperator = z.enum(["isNull", "notNull", "equals", "notEquals", "startsWith", "endsWith", "include", "doesNotInclude", "lessThan", "lessThanOrEqual", "greaterThan", "greaterThanOrEqual", "inThePast", "notInThePast", "inTheNext", "inTheCurrent", "notInTheCurrent", "inBetween", "notInBetween", "inPeriodToDate"]); export const metricFilterRule = z.object({includeNull: z.boolean().optional(), values: z.array(anyType).optional(), operator: filterOperator, id: z.string(), target: z.object({fieldRef: z.string()}), settings: anyType.optional(), disabled: z.boolean().optional(), required: z.boolean().optional(), caseSensitive: z.boolean().optional()}).and(z.record(z.string(), z.unknown())); export const emojiIcon = z.object({unicode: z.string()}); export const customIcon = z.object({url: z.string()}); export const catalogItemIcon = z.union([emojiIcon, customIcon]); export const pickTagNameOrColorOrTagUuidOrYamlReference = z.object({name: z.string(), color: z.string(), tagUuid: z.string(), yamlReference: z.string().nullable()}); export const metricType = z.enum(["percentile", "average", "count", "count_distinct", "sum", "sum_distinct", "average_distinct", "min", "max", "percent_of_previous", "percent_of_total", "running_total", "number", "median", "string", "date", "timestamp", "boolean"]); export const dimensionType = z.enum(["string", "number", "timestamp", "date", "boolean"]); export const catalogTypeField = z.literal("field"); export const catalogField = z.object({name: z.string(), description: z.string().optional(), label: z.string(), fieldType: fieldType, tableLabel: z.string(), requiredAttributes: recordStringStringOrStringArray.optional(), anyAttributes: recordStringStringOrStringArray.optional(), owner: catalogOwner.nullable(), spotlightDefaultFilter: metricFilterRule.optional(), spotlightDefaultSegment: z.string().optional(), spotlightSegmentBy: z.array(z.string()).optional(), spotlightFilterBy: z.array(z.string()).optional(), searchRank: z.number().optional(), aiHints: z.array(z.string()).nullable(), icon: catalogItemIcon.nullable(), verifiedChartUsage: z.number().optional(), chartUsage: z.number().optional(), categories: z.array(pickTagNameOrColorOrTagUuidOrYamlReference), tags: z.array(z.string()).optional(), tableGroupLabel: z.string().optional(), tableName: z.string(), fieldValueType: z.union([metricType, dimensionType]), basicType: z.enum(["string", "number", "date", "timestamp", "boolean"]), type: catalogTypeField, catalogSearchUuid: z.string()}); export const catalogTypeTable = z.literal("table"); export const inlineErrorType = z.enum(["METADATA_PARSE_ERROR", "NO_DIMENSIONS_FOUND", "SKIPPED_JOIN", "MISSING_TABLE", "FIELD_ERROR", "SET_VALIDATION_ERROR", "INVALID_PARAMETER", "DUPLICATE_FIELD_NAME", "WAREHOUSE_COLUMN_ERROR"]); export const inlineError = z.object({message: z.string(), type: inlineErrorType}); export const catalogTable = z.object({name: z.string(), description: z.string().optional(), label: z.string(), groupLabel: z.string().optional(), requiredAttributes: recordStringStringOrStringArray.optional(), anyAttributes: recordStringStringOrStringArray.optional(), searchRank: z.number().optional(), joinedTables: z.array(z.string()).nullable(), aiHints: z.array(z.string()).nullable(), icon: catalogItemIcon.nullable(), chartUsage: z.number().optional(), categories: z.array(pickTagNameOrColorOrTagUuidOrYamlReference), tags: z.array(z.string()).optional(), type: catalogTypeTable, errors: z.array(inlineError).optional(), catalogSearchUuid: z.string()}); export const catalogItem = z.union([catalogField, catalogTable]); export const apiCatalogResults = z.array(catalogItem); export const getApiApiV1ProjectsProjectUuidDataCatalogResponse = z.object({results: apiCatalogResults, status: z.literal("ok")}); 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 catalog * * GET /api/v1/projects/{projectUuid}/dataCatalog */ export const getApiApiV1ProjectsProjectUuidDataCatalog = async (args: GetApiApiV1ProjectsProjectUuidDataCatalogArgs, options: GetApiApiV1ProjectsProjectUuidDataCatalogOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.search != null) query.append("search", String(args.search)) if (args.type != null) query.append("type", String(args.type)) if (args.filter != null) query.append("filter", String(args.filter)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/projects/${encodeURIComponent(String(args.projectUuid))}/dataCatalog${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(getApiApiV1ProjectsProjectUuidDataCatalogResponse, await res.json()) };