geo/frontend/lib/api/competitor-analysis.ts

62 lines
1.8 KiB
TypeScript

import { fetchWithAuth } from "./client";
function buildQuery(params: Record<string, string | number | boolean | undefined>): string {
const qs = Object.entries(params)
.filter(([, v]) => v !== undefined)
.map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(String(v))}`)
.join("&");
return qs ? `?${qs}` : "";
}
export interface CompetitorAnalysisRequest {
brand_id: string;
analysis_types?: string[];
period_days?: number;
}
export interface CompetitorInsight {
id: string;
brand_id: string;
insight_type: string;
competitor_name: string | null;
data: Record<string, unknown>;
recommendations: string[] | null;
period_start: string | null;
period_end: string | null;
created_at: string;
}
export interface CompetitorInsightList {
items: CompetitorInsight[];
total: number;
}
export interface CompetitorInsightResponse extends CompetitorInsight {}
export interface CompetitorGapSummary {
competitor_name: string;
gap_score: number;
dimensions: Record<string, unknown>;
}
export const competitorAnalysisApi = {
analyze: (token: string, data: CompetitorAnalysisRequest) =>
fetchWithAuth("/api/v1/competitor/analyze", {
method: "POST",
body: JSON.stringify(data),
}, token) as Promise<CompetitorInsightList>,
getBrandInsights: (token: string, brandId: string, params?: { skip?: number; limit?: number }) =>
fetchWithAuth(
`/api/v1/competitor/brand/${brandId}${buildQuery(params || {})}`,
{},
token
) as Promise<CompetitorInsightList>,
getInsight: (token: string, insightId: string) =>
fetchWithAuth(`/api/v1/competitor/${insightId}`, {}, token) as Promise<CompetitorInsightResponse>,
getGapSummary: (token: string, brandId: string) =>
fetchWithAuth(`/api/v1/competitor/brand/${brandId}/gap-summary`, {}, token) as Promise<CompetitorGapSummary[]>,
};