File size: 4,124 Bytes
8d87873
a9fc7b2
6241e51
8d87873
 
 
 
 
6241e51
8d87873
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a74100
7dcb597
8d87873
 
1a74100
 
8d87873
 
 
 
 
 
1a74100
7dcb597
8d87873
 
1a74100
 
8d87873
 
 
 
 
 
 
 
1a74100
 
 
 
7dcb597
 
 
 
1a74100
 
 
 
8d87873
 
 
 
34727e8
8d87873
 
 
34727e8
8d87873
 
 
34727e8
8d87873
 
 
34727e8
8d87873
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6241e51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1607a5a
 
 
 
 
 
 
 
6241e51
 
 
a9fc7b2
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import type {
  Outfit,
  Product,
  ProductsResponse,
  StoreInfo,
  RatingsResponse,
  Stats,
  ScanStatus,
  DiscoverStats,
} from "./types";

const BASE = "/api";

async function request<T>(path: string, init?: RequestInit): Promise<T> {
  const res = await fetch(`${BASE}${path}`, init);
  if (!res.ok) {
    const text = await res.text().catch(() => res.statusText);
    throw new Error(`API ${res.status}: ${text}`);
  }
  return res.json() as Promise<T>;
}

export async function getProducts(params: {
  page?: number;
  page_size?: number;
  store?: string;
  brand?: string;
  category?: string;
  sort?: string;
  min_score?: number;
  min_price?: number;
  max_price?: number;
  search?: string;
}): Promise<ProductsResponse> {
  const sp = new URLSearchParams();
  if (params.page) sp.set("page", String(params.page));
  if (params.page_size) sp.set("page_size", String(params.page_size));
  if (params.store) sp.set("store", params.store);
  if (params.brand) sp.set("brand", params.brand);
  if (params.category) sp.set("category", params.category);
  if (params.sort) sp.set("sort", params.sort);
  if (params.min_score !== undefined) sp.set("min_score", String(params.min_score));
  if (params.min_price !== undefined) sp.set("min_price", String(params.min_price));
  if (params.max_price !== undefined) sp.set("max_price", String(params.max_price));
  if (params.search) sp.set("search", params.search);
  return request<ProductsResponse>(`/products?${sp}`);
}

export async function getStores(): Promise<StoreInfo[]> {
  return request<StoreInfo[]>("/products/stores");
}

export async function getBrands(): Promise<{ name: string; count: number }[]> {
  return request("/products/brands");
}

export async function getCategories(): Promise<{ name: string; count: number }[]> {
  return request("/products/categories");
}

export async function getPriceRange(): Promise<{ min: number; max: number }> {
  return request("/products/price-range");
}

export async function getRatings(): Promise<RatingsResponse> {
  return request<RatingsResponse>("/ratings");
}

export async function setRating(filename: string, rating: number): Promise<void> {
  await request<unknown>("/ratings", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ filename, rating }),
  });
}

export async function deleteRating(filenames: string[]): Promise<void> {
  await request<unknown>("/ratings", {
    method: "DELETE",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ filenames }),
  });
}

export async function uploadImage(file: File): Promise<unknown> {
  const form = new FormData();
  form.append("file", file);
  return request<unknown>("/ratings/upload", {
    method: "POST",
    body: form,
  });
}

export async function getStats(): Promise<Stats> {
  return request<Stats>("/stats");
}

export async function startScan(stores?: string[]): Promise<void> {
  await request<unknown>("/scan", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(stores ? { stores } : {}),
  });
}

export async function getScanStatus(): Promise<ScanStatus> {
  return request<ScanStatus>("/scan/status");
}

export async function getDiscoverProducts(count = 10): Promise<Product[]> {
  return request<Product[]>(`/discover/next?count=${count}`);
}

export async function rateDiscoverProduct(
  url: string,
  rating: "like" | "dislike" | "skip"
): Promise<{ ok: boolean; stats: DiscoverStats }> {
  return request(`/discover/rate`, {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ url, rating }),
  });
}

export async function undoDiscoverRating(): Promise<{
  ok: boolean;
  product: Product | null;
  stats: DiscoverStats;
}> {
  return request("/discover/undo", { method: "POST" });
}

export async function getDiscoverStats(): Promise<DiscoverStats> {
  return request<DiscoverStats>("/discover/stats");
}

export async function getOutfit(url: string): Promise<Outfit> {
  return request<Outfit>(`/products/outfit?url=${encodeURIComponent(url)}`);
}