import {z} from 'zod' export type CorrelationIdRequest = string; export type DqThresholdRequest = {severity?: string | undefined, value?: number | undefined, correlationId?: CorrelationIdRequest | undefined}; export type GroupUrn = string; export type InputAttributeDataType = string; export type RuleInputAttributeRequest = {name?: string | undefined, description?: string | undefined, dataType?: InputAttributeDataType | undefined, order?: number | undefined, correlationId?: CorrelationIdRequest | undefined, hideDq?: boolean | undefined}; export type RuleInputGroupRequest = {name?: string | undefined, order?: number | undefined, correlationId?: CorrelationIdRequest | undefined, inputs?: {[key: string]: RuleInputAttributeRequest} | undefined, joined?: boolean | undefined}; export type DqDimensionUrn = string; export type DqDimensionResultUrn = string; export type RuleFallback = {resultUrn?: DqDimensionResultUrn | undefined, score?: number | undefined, explanation?: string | undefined}; export type RuleConditionExpression = {stringExpression?: string | undefined}; export type RuleConditionRequest = {order?: number | undefined, name?: string | undefined, resultUrn?: DqDimensionResultUrn | undefined, expression?: RuleConditionExpression | undefined, score?: number | undefined, correlationId?: CorrelationIdRequest | undefined, aggregated?: boolean | undefined, enabled?: boolean | undefined}; export type RuleInputAttributeRef = {inputGroupName?: string | undefined, attributeName: string}; export type GroupByInputRequest = {correlationId?: CorrelationIdRequest | undefined, attribute: RuleInputAttributeRef, variable?: string | undefined} | {correlationId?: CorrelationIdRequest | undefined, attribute?: RuleInputAttributeRef | undefined, variable: string}; export type RuleParameterRequest = {name?: string | undefined, order?: number | undefined, dataType?: InputAttributeDataType | undefined, extendedType?: string | undefined, correlationId?: CorrelationIdRequest | undefined}; export type DqTransformationUrn = string; export type DqTransformationInstanceRequest = {order?: number | undefined, transformationUrn: DqTransformationUrn, correlationId?: CorrelationIdRequest | undefined}; export type RuleVariableRequest = {name?: string | undefined, variableType?: string | undefined, input?: RuleInputAttributeRef | undefined, correlationId?: CorrelationIdRequest | undefined, expression?: string | undefined, transformations?: {[key: string]: DqTransformationInstanceRequest} | undefined}; export type RuleBatchCreateItemRequest = {name: string, description?: (string | null) | undefined, defaultDqThreshold?: (DqThresholdRequest | null) | undefined, stewardship?: ({groupUrn?: GroupUrn | undefined} | null) | undefined, inputGroups: {[key: string]: RuleInputGroupRequest} | null, implementation: {ruleType?: string | undefined, dqDimensionUrn?: DqDimensionUrn | undefined, fallback?: RuleFallback | undefined, conditions?: {[key: string]: RuleConditionRequest} | undefined, groupByInputs?: {[key: string]: GroupByInputRequest} | undefined} | null, parameters?: ({[key: string]: RuleParameterRequest} | null) | undefined, variables?: ({[key: string]: RuleVariableRequest} | null) | undefined, id?: string | undefined}; export type CreateApiDataQualityV1RulesBatchCreateArgs = {body: {data: Array}}; export type CreateApiDataQualityV1RulesBatchCreateOptions = {baseUrl?: string | undefined, JWT?: string | undefined, fetch?: typeof fetch | undefined}; export const batchOperationMeta = z.object({total: z.number().int(), success: z.number().int(), fail: z.number().int()}); export const ruleUrn = z.string(); export const subEntityKey = z.string(); export const problemDetail = z.object({type: z.string(), title: z.string(), detail: z.string()}); export const ruleBatchResultItemResponse = z.union([z.object({id: z.string().optional(), status: z.string(), data: z.object({urn: ruleUrn, correlationIds: z.record(z.string(), subEntityKey).optional()}), error: problemDetail.optional()}), z.object({id: z.string().optional(), status: z.string(), data: z.object({urn: ruleUrn, correlationIds: z.record(z.string(), subEntityKey).optional()}).optional(), error: problemDetail})]); export const createApiDataQualityV1RulesBatchCreateResponse = z.object({meta: batchOperationMeta, results: z.array(ruleBatchResultItemResponse)}); 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 }; /** * Create rules in batch * * POST /data-quality/v1/rules/batch/create */ export const createApiDataQualityV1RulesBatchCreate = async (args: CreateApiDataQualityV1RulesBatchCreateArgs, options?: CreateApiDataQualityV1RulesBatchCreateOptions) => { const {baseUrl = "https://your-environment.ataccama.one/api", fetch: fetchFn = fetch, JWT} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (JWT !== undefined) headers.set('authorization', `Bearer ${JWT}`) const url = `${baseUrl.replace(/\/+$/, '')}/data-quality/v1/rules/batch/create${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(createApiDataQualityV1RulesBatchCreateResponse, await res.json()) };