-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathproductApi.ts
More file actions
52 lines (45 loc) · 1.41 KB
/
productApi.ts
File metadata and controls
52 lines (45 loc) · 1.41 KB
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
// 상품 목록 조회
import type { Categories, Product } from "../entities";
import type { StringRecord } from "../types.ts";
import { BASE_URL } from "../constants.ts";
interface ProductsResponse {
products: Product[];
pagination: {
page: number;
limit: number;
total: number;
totalPages: number;
hasNext: boolean;
hasPrev: boolean;
};
filters: {
search: string;
category1: string;
category2: string;
sort: string;
};
}
export async function getProducts(params: StringRecord = {}): Promise<ProductsResponse> {
const { limit = 20, search = "", category1 = "", category2 = "", sort = "price_asc" } = params;
const page = params.current ?? params.page ?? 1;
const searchParams = new URLSearchParams({
page: page.toString(),
limit: limit.toString(),
...(search && { search }),
...(category1 && { category1 }),
...(category2 && { category2 }),
sort,
});
const response = await fetch(`${BASE_URL}api/products?${searchParams}`);
return await response.json();
}
// 상품 상세 조회
export async function getProduct(productId: string): Promise<Product> {
const response = await fetch(`${BASE_URL}api/products/${productId}`);
return await response.json();
}
// 카테고리 목록 조회
export async function getCategories(): Promise<Categories> {
const response = await fetch(`${BASE_URL}api/categories`);
return await response.json();
}