/
api.ts
123 lines (114 loc) · 3.34 KB
/
api.ts
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
import type { CategoryModel } from "./models/Category"
import { PriceHistoryModel, SearchResponseModel } from "./models/Product"
import type { ProductWithPriceModel } from "./models/Product"
const apiRoot = process.env.NEXT_PUBLIC_API_ROOT ?? ""
export enum SortType {
name = "name",
price = "price",
unitPrice = "unitPrice",
}
export enum SortOrder {
asc = "asc",
desc = "desc",
}
export function getProductById(id: string, signal?: AbortSignal): Promise<ProductWithPriceModel | null> {
return fetch(`${apiRoot}/products/${id}`, { signal })
.then(async (res) => {
if (res.status === 404) {
return null
}
if (res.status > 400) {
throw new Error() // todo
}
return await res.json() as ProductWithPriceModel
})
}
export function getProductHistoryById(id: string, signal?: AbortSignal): Promise<PriceHistoryModel | null> {
return fetch(`${apiRoot}/products/${id}/history`, { signal })
.then(async (res) => {
if (res.status === 404) {
return null
}
if (res.status > 400) {
throw new Error() // todo
}
return PriceHistoryModel.parse(await res.json())
})
}
export function getProductByEan(ean: string, signal?: AbortSignal): Promise<ProductWithPriceModel | null> {
return fetch(`${apiRoot}/product/ean/${ean}`, { signal })
.then(async (res) => {
if (res.status === 404) {
return null
}
if (res.status > 400) {
throw new Error() // todo
}
return await res.json() as ProductWithPriceModel
})
}
export function searchProducts(
{
query,
offset = 0,
sort,
sortOrder,
}: {
query: string
offset?: number
sort?: SortType
sortOrder?: SortOrder
},
signal?: AbortSignal,
): Promise<SearchResponseModel> {
const limit = 50
const queryString = new URLSearchParams({
query,
offset: offset.toString(),
limit: limit.toString(),
})
if (sort) queryString.set("sort", sort)
if (sortOrder) queryString.set("sortOrder", sortOrder)
return fetch(`${apiRoot}/v2/products/search?${queryString}`, { signal })
.then(async (res) => {
if (res.status > 400) {
throw new Error() // todo
}
return await res.json() as SearchResponseModel
})
}
export function getCategories(parentId: number | null, signal?: AbortSignal): Promise<CategoryModel[]> {
const params = parentId !== null
? new URLSearchParams({ parentId: parentId.toString() })
: new URLSearchParams()
return fetch(`${apiRoot}/categories?${params}`, { signal })
.then(async (res) => {
if (res.status > 400) {
throw new Error() // todo
}
return await res.json() as CategoryModel[]
})
}
export function getProductsByCategory(categoryId: number, {
sortType,
sortOrder,
limit,
signal,
}: {
sortType?: SortType
sortOrder?: SortOrder
limit?: number
signal?: AbortSignal
} = {}): Promise<ProductWithPriceModel[]> {
const params = new URLSearchParams()
if (sortType) params.append("sort", sortType)
if (sortOrder) params.append("sortOrder", sortOrder)
if (limit) params.append("limit", limit.toString())
return fetch(`${apiRoot}/categories/${categoryId}/products?${params}`, { signal })
.then(async (res) => {
if (res.status > 400) {
throw new Error() // todo
}
return await res.json() as ProductWithPriceModel[]
})
}