Spaces:
Sleeping
Sleeping
Download frontend/src/api.ts from Creatiste/taste-engine: direct link, hf CLI and curl.
- Browser
- Download file 4.12 kB
-
https://huggingface.co/spaces/Creatiste/taste-engine/resolve/main/frontend/src/api.ts
- Command line
-
hf download hf://spaces/Creatiste/taste-engine/frontend/src/api.ts
-
curl -L -o api.ts https://huggingface.co/spaces/Creatiste/taste-engine/resolve/main/frontend/src/api.ts
4.12 kB
| 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)}`); | |
| } | |