import {z} from 'zod' export type CreateApiV1DomainsIdDuplicateArgs = {id: string}; export type CreateApiV1DomainsIdDuplicateOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const taxonomyClass = z.object({group: z.string(), subgroup: z.string(), description: z.string(), aliases: z.array(z.string()).optional()}); export const taxonomy = z.object({classes: z.array(taxonomyClass), allow_other: z.literal(true).optional()}); export const domain = z.object({domain_id: z.string(), slug: z.string(), display_name: z.string(), owner: z.enum(["platform", "tenant"]), version: z.number().int().gte(1), status: z.enum(["draft", "published"]), parent_slug: z.string().nullable(), class_count: z.number().int(), selectable: z.boolean(), taxonomy: taxonomy, published_at: z.string().nullable()}).and(z.record(z.string(), z.unknown())); export const domainValidation = z.object({valid: z.boolean(), errors: z.array(z.object({code: z.string(), path: z.string(), message: z.string()})), effective_class_count: z.number().int()}); export const domainWrite = z.object({domain: domain, validation: domainValidation}); 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 }; /** * Duplicate a domain * * POST /v1/domains/{id}/duplicate */ export const createApiV1DomainsIdDuplicate = async (args: CreateApiV1DomainsIdDuplicateArgs, options?: CreateApiV1DomainsIdDuplicateOptions) => { const {baseUrl = "https://console.nace.ai", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/domains/${encodeURIComponent(String(args.id))}/duplicate${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(domainWrite, await res.json()) };