import {z} from 'zod' export type GetApiApiMcpProjectsProjectIdShareOfVoiceArgs = {projectId: string, days?: number | undefined, platform?: string | undefined, topicId?: string | undefined, unbranded?: 'true' | undefined, branded?: 'true' | undefined, competitorType?: ('all' | 'direct' | 'serp') | undefined, country?: string | undefined, locationId?: string | undefined, compare?: ('previous_period' | 'previous_year') | undefined, from?: string | undefined, to?: string | undefined}; export type GetApiApiMcpProjectsProjectIdShareOfVoiceOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const projectRefWithBrand = z.object({id: z.string(), name: z.string(), domain: z.string().nullable(), brandName: z.string().optional()}); export const dateRange = z.object({from: z.string(), to: z.string()}); export const shareOfVoiceResponse = z.object({project: projectRefWithBrand, brand: z.object({name: z.string().optional(), sov: z.number().optional(), rank: z.number().int().nullable().optional(), delta: z.number().nullable().optional()}), competitors: z.array(z.object({name: z.string().optional(), domain: z.string().nullable().optional(), competitorType: z.enum(["direct", "serp"]).optional(), sov: z.number().optional(), rank: z.number().int().optional(), delta: z.number().nullable().optional(), mentions: z.unknown(), citations: z.unknown(), previousSov: z.number().optional(), sovDelta: z.number().optional()})), totalCompetitors: z.number().int().optional(), dateRange: dateRange, comparison: z.object({mode: z.enum(["previous_period", "previous_year"]).optional(), dateRange: dateRange.optional(), brand: z.object({previousSov: z.number().optional(), sovDelta: z.number().optional(), previousRank: z.number().int().nullable().optional()}).optional()}).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 }; /** * Get share of voice * * GET /api/mcp/projects/{projectId}/share-of-voice */ export const getApiApiMcpProjectsProjectIdShareOfVoice = async (args: GetApiApiMcpProjectsProjectIdShareOfVoiceArgs, options?: GetApiApiMcpProjectsProjectIdShareOfVoiceOptions) => { const {baseUrl = "https://app.searchable.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.days != null) query.append("days", String(args.days)) if (args.platform != null) query.append("platform", String(args.platform)) if (args.topicId != null) query.append("topicId", String(args.topicId)) if (args.unbranded != null) query.append("unbranded", String(args.unbranded)) if (args.branded != null) query.append("branded", String(args.branded)) if (args.competitorType != null) query.append("competitorType", String(args.competitorType)) if (args.country != null) query.append("country", String(args.country)) if (args.locationId != null) query.append("locationId", String(args.locationId)) if (args.compare != null) query.append("compare", String(args.compare)) if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/mcp/projects/${encodeURIComponent(String(args.projectId))}/share-of-voice${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(shareOfVoiceResponse, await res.json()) };