import {z} from 'zod' export type CreateApiExplorationMapArgs = {body: {sources: Array<{type: 'channel', channelId: string} | {type: 'query', query: string} | {type: 'brand', brandId: string} | {type: 'audience', audienceId: string, preset?: boolean | undefined} | {type: 'channels', channelIds: Array, label: string, of?: Array | undefined} | {type: 'topic', topic: string, kind: 'conversation' | 'content'} | {type: 'campaign', campaignId: string} | {type: 'link', url: string} | {type: 'inclusion_list', listId: string} | {type: 'project', projectId: string, selection?: string | undefined, channelIds?: Array | undefined}>, size?: number | undefined}}; export type CreateApiExplorationMapOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiExplorationMapResponse = z.object({title: z.string(), url: z.string(), builtAt: z.string(), canExpand: z.boolean(), notes: z.array(z.string()), audience: z.object({corePeople: z.number(), totalPeople: z.number(), totalComments: z.number()}).nullable(), groups: z.array(z.object({id: z.number(), name: z.string(), kind: z.enum(["core", "adjacent", "unconnected"]), description: z.string(), themes: z.array(z.string()), relevance: z.number().nullable(), channelIds: z.array(z.string()), expand: z.object({type: z.literal("channels"), channelIds: z.array(z.string()), label: z.string()}), people: z.number().nullable(), reach: z.number().nullable(), concentration: z.number().nullable(), fanDensity: z.number().nullable()})), channels: z.array(z.object({channelId: z.string(), title: z.string(), handle: z.string().nullable(), imageUrl: z.string(), subscribers: z.number(), country: z.string().nullable(), groupId: z.number(), seed: z.boolean(), relevance: z.number().nullable(), sharedCommenters: z.number(), audienceThemes: z.array(z.string()), contentThemes: z.array(z.string()), people: z.number().nullable(), reach: z.number().nullable(), concentration: z.number().nullable(), fanDensity: z.number().nullable()}))}); 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 }; /** * Map an audience * * POST /exploration/map */ export const createApiExplorationMap = async (args: CreateApiExplorationMapArgs, options?: CreateApiExplorationMapOptions) => { const {baseUrl = "https://www.fandom.ai/api/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/exploration/map${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(createApiExplorationMapResponse, await res.json()) };