import {z} from 'zod' export type TermUrn = string; export type RulesOperator = string; export type DetectionRuleUrn = string; export type DetectionRuleAssignment = {detectionRuleUrn: DetectionRuleUrn, threshold?: number | undefined}; export type TermAssignment = {termUrn: TermUrn}; export type AttributeToCatalogItemTermRule = {operator?: RulesOperator | undefined, terms?: Array | undefined}; export type Urn = string; export type DynamicPropertyValue = string | number | boolean | Urn | Array | Array | Array | Array | null; export type ExtraProperties = {[key: string]: DynamicPropertyValue}; export type TermPatch = {name?: (string | null) | undefined, description?: (string | null) | undefined, detectionRules?: ({operator?: (RulesOperator | null) | undefined, ruleAssignments?: (Array | null) | undefined} | null) | undefined, attributeToCatalogItemTermRules?: ({operator?: (RulesOperator | null) | undefined, rules?: (Array | null) | undefined} | null) | undefined, extraProperties?: (ExtraProperties | null) | undefined}; export type PatchApiCatalogV1TermsUrnArgs = {urn: TermUrn, body: TermPatch}; export type PatchApiCatalogV1TermsUrnOptions = {baseUrl?: string | undefined, JWT?: string | undefined, fetch?: typeof fetch | undefined}; export const termUrn = z.string(); export const groupUrn = z.string(); export const urn = z.string(); export const effectiveStewardship = z.object({groupUrn: groupUrn, originUrn: urn}); export const effectiveCriticality = z.object({isCritical: z.boolean()}); export const dynamicPropertyValue = z.union([z.string(), z.number(), z.boolean(), urn, z.array(z.string()), z.array(z.number()), z.array(z.boolean()), z.array(urn)]).nullable(); export const extraProperties = z.record(z.string(), dynamicPropertyValue); export const rulesOperator = z.string(); export const detectionRuleUrn = z.string(); export const detectionRuleAssignment = z.object({detectionRuleUrn: detectionRuleUrn, threshold: z.number().gte(0).lte(100).optional()}); export const termDetectionRules = z.object({operator: rulesOperator.optional(), ruleAssignments: z.array(detectionRuleAssignment).optional()}); export const termAssignment = z.object({termUrn: termUrn}); export const attributeToCatalogItemTermRule = z.object({operator: rulesOperator.optional(), terms: z.array(termAssignment).optional()}); export const attributeToCatalogItemTermRules = z.object({operator: rulesOperator.optional(), rules: z.array(attributeToCatalogItemTermRule).optional()}); export const term = z.object({urn: termUrn, effectiveStewardship: effectiveStewardship.nullable().optional(), effectiveCriticality: effectiveCriticality.nullable().optional(), extraProperties: extraProperties.optional(), type: z.string(), name: z.string(), description: z.string().optional(), detectionRules: termDetectionRules.optional(), attributeToCatalogItemTermRules: attributeToCatalogItemTermRules.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 }; /** * Update term * * PATCH /catalog/v1/terms/{urn} */ export const patchApiCatalogV1TermsUrn = async (args: PatchApiCatalogV1TermsUrnArgs, options?: PatchApiCatalogV1TermsUrnOptions) => { 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(/\/+$/, '')}/catalog/v1/terms/${encodeURIComponent(String(args.urn))}${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(term, await res.json()) };