import {z} from 'zod' export type AssetIdentifier = {id?: string | undefined, assetName?: string | undefined, domainId?: string | undefined, communityName?: string | undefined, domainName?: string | undefined}; export type Counts = {rowCount?: number | undefined, emptyValuesCount?: number | undefined, distinctValuesCount?: number | undefined, mode?: string | undefined}; export type Samples = {samples?: Array | undefined}; export type Statistics = {mean?: number | undefined, variance?: number | undefined, standardDeviation?: number | undefined, minimum?: string | undefined, maximum?: string | undefined, minimumTextLength?: number | undefined, maximumTextLength?: number | undefined}; export type DatabaseMetadata = {defaultValue?: string | undefined, numberOfDecimalDigits?: number | undefined, charOctetLength?: number | undefined, columnSize?: number | undefined, primaryKeyName?: string | undefined, isNullable?: boolean | undefined, isAutoIncremented?: boolean | undefined, isGenerated?: boolean | undefined, isPrimaryKey?: boolean | undefined}; export type CategoricalMetadata = {categorical?: boolean | undefined, categoriesFrequencies?: {[key: string]: number} | undefined}; export type Quantiles = {percentile1?: string | undefined, percentile5?: string | undefined, decile1?: string | undefined, quartile1?: string | undefined, median?: string | undefined, quartile3?: string | undefined, decile9?: string | undefined, percentile95?: string | undefined, percentile99?: string | undefined}; export type Point = {x?: number | undefined, y?: number | undefined}; export type HistogramBin = {lowerBound?: string | undefined, upperBound?: string | undefined, frequency?: number | undefined, lowerBoundInISODateFormat?: string | undefined, upperBoundInISODateFormat?: string | undefined}; export type Distributions = {distributionDensityEstimation?: Array | undefined, histogram?: Array | undefined}; export type ColumnProfile = {assetIdentifier: AssetIdentifier, columnName?: string | undefined, technicalDataType?: string | undefined, dataType?: string | undefined, columnPosition?: number | undefined, counts?: Counts | undefined, samples?: Samples | undefined, statistics?: Statistics | undefined, databaseMetadata?: DatabaseMetadata | undefined, categoricalMetadata?: CategoricalMetadata | undefined, quantiles?: Quantiles | undefined, distributions?: Distributions | undefined}; export type DataProfilingRequest = {columnProfiles?: Array | undefined}; export type PatchApiProfilingColumnsArgs = {body: DataProfilingRequest}; export type PatchApiProfilingColumnsOptions = {baseUrl: string, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const dataProfilingResponse = z.object({updatedColumnsCount: z.number().int().optional(), errors: z.array(z.string()).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 }; /** * Updates column profiles. * * PATCH /profiling/columns * * Also accepts auth: jwtAuth, oauth2, oauth2 */ export const patchApiProfilingColumns = async (args: PatchApiProfilingColumnsArgs, options: PatchApiProfilingColumnsOptions) => { const {baseUrl, fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/profiling/columns${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(dataProfilingResponse, await res.json()) };