import {z} from 'zod' export type EntitySynonymStoreRequest = {entity_type: 'brand' | 'property' | 'league' | 'agency' | 'venue' | 'market' | 'category' | 'subcategory' | 'asset', entity_id: number, synonym: string}; export type CreateApiApiEntitySynonymsArgs = {body: EntitySynonymStoreRequest}; export type CreateApiApiEntitySynonymsOptions = {baseUrl: string, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const entitySynonymResource = z.object({id: z.number().int().optional(), entity_type: z.enum(["brand", "property", "league", "agency", "venue", "market", "category", "subcategory", "asset"]).optional(), entity_id: z.number().int().optional(), synonym: z.string().optional(), normalized_synonym: z.string().nullable().optional(), source: z.enum(["manual", "admin_link", "queue_approval", "auto_link", "edgar", "podcast_scouting", "logo_detection", "surface"]).optional(), confidence: z.enum(["high", "medium", "low"]).nullable().optional(), matched_at: z.string().nullable().optional()}); export const createApiApiEntitySynonymsResponse = z.object({outcome: z.string().optional(), data: entitySynonymResource.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 }; /** * Add a synonym to a brand, property, league, agency, venue, market, category, subcategory or asset * * POST /api/entity-synonyms */ export const createApiApiEntitySynonyms = async (args: CreateApiApiEntitySynonymsArgs, options: CreateApiApiEntitySynonymsOptions) => { const {baseUrl, fetch: fetchFn = fetch, apiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKeyAuth !== undefined) headers.set("X-API-Key", apiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/entity-synonyms${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(createApiApiEntitySynonymsResponse, await res.json()) };