web/src/utils/fetch-privacy-policy.ts

75 lines · 1634 bytes

 1import { error } from './logger';
 2import { safeFetch } from './safe-fetch';
 3import { apiBase, enrichHeaders } from './api-config';
 4
 5// Wrap the flat ToS;DR v3 record in the shape the app already consumes.
 6export const fetchTosdrPrivacy = async (
 7  serviceId: string,
 8): Promise<PrivacyPolicyResponse | null> => {
 9  const endpoint = `${apiBase}/v1/enrich/privacy/${serviceId}`;
10  try {
11    const res = await safeFetch(endpoint, { headers: enrichHeaders() });
12    if (!res.ok) {
13      error(
14        'ToS;DR',
15        `HTTP ${res.status} for service ${serviceId} (${endpoint})`,
16      );
17      return null;
18    }
19    return { error: 0, message: '', parameters: await res.json() };
20  } catch (err) {
21    error('ToS;DR', `Network error for service ${serviceId}: ${err}`);
22    return null;
23  }
24};
25
26interface Document {
27  id: number;
28  name: string;
29  url: string;
30  updated_at: string;
31  created_at: string;
32}
33
34interface Case {
35  id: number;
36  weight: number;
37  title: string;
38  description: string;
39  updated_at: string;
40  created_at: string;
41  topic_id: number;
42  classification: string;
43}
44
45interface Point {
46  id: number;
47  title: string;
48  source: string;
49  status: string;
50  analysis: string;
51  case: Case;
52  document_id: number | null;
53  updated_at: string;
54  created_at: string;
55}
56
57interface Params {
58  id: number;
59  is_comprehensively_reviewed: boolean;
60  name: string;
61  updated_at: string;
62  created_at: string;
63  slug: string;
64  rating: string;
65  urls: string[];
66  image: string;
67  documents: Document[];
68  points: Point[];
69}
70
71export interface PrivacyPolicyResponse {
72  error: number;
73  message: string;
74  parameters: Params;
75}