import {z} from 'zod' export type ParseResultSource = {type?: 'parse_result' | undefined, job_id: string}; export type GenerateDomainRequest = {description?: (string | null) | undefined, source?: (ParseResultSource | null) | undefined, sheet_name?: (string | null) | undefined, display_name?: (string | null) | undefined, slug?: (string | null) | undefined}; export type CreateApiV1DomainsGenerateArgs = {body: GenerateDomainRequest}; export type CreateApiV1DomainsGenerateOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const taxonomyClass = z.object({group: z.string().max(64), subgroup: z.string().max(64), description: z.string(), aliases: z.array(z.string()).optional()}); export const taxonomyOutput = z.object({classes: z.array(taxonomyClass).optional(), allow_other: z.literal(true).optional()}); export const domainValidationError = z.object({code: z.string(), path: z.string(), message: z.string()}); export const domainValidation = z.object({valid: z.boolean(), errors: z.array(domainValidationError).optional(), effective_class_count: z.number().int()}); export const generateDomainResponse = z.object({taxonomy: taxonomyOutput, suggested_slug: z.string().max(64), suggested_display_name: z.string().min(1).max(200), 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 }; /** * generate_domain * * POST /v1/domains/generate */ export const createApiV1DomainsGenerate = async (args: CreateApiV1DomainsGenerateArgs, options: CreateApiV1DomainsGenerateOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("X-API-Key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/domains/generate${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(generateDomainResponse, await res.json()) };