Skip to content

Commit 584c189

Browse files
committed
feat: add return types and category list to lib/data
1 parent 80cf8ba commit 584c189

File tree

1 file changed

+86
-8
lines changed

1 file changed

+86
-8
lines changed

src/lib/data/index.ts

Lines changed: 86 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Product, ProductCategory, ProductCollection } from "@medusajs/product"
12
import medusaRequest from "../medusa-fetch"
23
import { StoreGetProductsParams } from "@medusajs/medusa"
34

@@ -21,7 +22,9 @@ const DEBUG = false
2122
* @param handle (string) - The handle of the product to retrieve
2223
* @returns (array) - An array of products (should only be one)
2324
*/
24-
export async function getProductByHandle(handle: string) {
25+
export async function getProductByHandle(
26+
handle: string
27+
): Promise<{ products: Product[] }> {
2528
if (PRODUCT_MODULE_ENABLED) {
2629
DEBUG && console.log("PRODUCT_MODULE_ENABLED")
2730
const data = await fetch(`${API_BASE_URL}/api/products/${handle}`)
@@ -62,7 +65,10 @@ export async function getProductsList({
6265
}: {
6366
pageParam?: number
6467
queryParams: StoreGetProductsParams
65-
}) {
68+
}): Promise<{
69+
response: { products: Product[]; count: number }
70+
nextPage: number
71+
}> {
6672
const limit = queryParams.limit || 12
6773

6874
if (PRODUCT_MODULE_ENABLED) {
@@ -113,7 +119,9 @@ export async function getProductsList({
113119
* @returns collections (array) - An array of collections
114120
* @returns count (number) - The total number of collections
115121
*/
116-
export async function getCollectionsList(offset: number = 0) {
122+
export async function getCollectionsList(
123+
offset: number = 0
124+
): Promise<{ collections: ProductCollection[]; count: number }> {
117125
if (PRODUCT_MODULE_ENABLED) {
118126
DEBUG && console.log("PRODUCT_MODULE_ENABLED")
119127
const { collections, count } = await fetch(
@@ -159,7 +167,11 @@ export async function getCollectionsList(offset: number = 0) {
159167
* @returns response (object) - An object containing the products and the number of products in the collection
160168
* @returns nextPage (number) - The offset of the next page of products
161169
*/
162-
export async function getCollectionByHandle(handle: string) {
170+
export async function getCollectionByHandle(handle: string): Promise<{
171+
collections: ProductCollection[]
172+
response: { products: Product[]; count: number }
173+
nextPage: number
174+
}> {
163175
if (PRODUCT_MODULE_ENABLED) {
164176
DEBUG && console.log("PRODUCT_MODULE_ENABLED")
165177
const data = await fetch(`${API_BASE_URL}/api/collections/${handle}`)
@@ -197,15 +209,20 @@ export async function getProductsByCollectionHandle({
197209
pageParam = 0,
198210
handle,
199211
cartId,
212+
currency_code,
200213
}: {
201214
pageParam?: number
202215
handle: string
203216
cartId?: string
204-
}) {
217+
currency_code?: string
218+
}): Promise<{
219+
response: { products: Product[]; count: number }
220+
nextPage: number
221+
}> {
205222
if (PRODUCT_MODULE_ENABLED) {
206223
DEBUG && console.log("PRODUCT_MODULE_ENABLED")
207224
const { response, nextPage } = await fetch(
208-
`${API_BASE_URL}/api/collections/${handle}?cart_id=${cartId}&page=${pageParam.toString()}`
225+
`${API_BASE_URL}/api/collections/${handle}?currency_code=${currency_code}&page=${pageParam.toString()}`
209226
)
210227
.then((res) => res.json())
211228
.catch((err) => {
@@ -238,14 +255,72 @@ export async function getProductsByCollectionHandle({
238255
}
239256
}
240257

258+
/**
259+
* Fetches a list of categories, using the Medusa API or the Medusa Product Module, depending on the feature flag.
260+
* @param offset (number) - The offset of the categories to retrieve (default: 0
261+
* @param limit (number) - The limit of the categories to retrieve (default: 100)
262+
* @returns product_categories (array) - An array of product_categories
263+
* @returns count (number) - The total number of categories
264+
* @returns nextPage (number) - The offset of the next page of categories
265+
*/
266+
export async function getCategoriesList(
267+
offset: number = 0,
268+
limit?: number
269+
): Promise<{ product_categories: ProductCategory[]; count: number }> {
270+
if (PRODUCT_MODULE_ENABLED) {
271+
DEBUG && console.log("PRODUCT_MODULE_ENABLED")
272+
const { product_categories, count } = await fetch(
273+
`${API_BASE_URL}/api/categories?offset=${offset}&limit=${limit}`,
274+
{
275+
next: {
276+
tags: ["categories"],
277+
},
278+
}
279+
)
280+
.then((res) => res.json())
281+
.catch((err) => {
282+
throw err
283+
})
284+
285+
return {
286+
product_categories,
287+
count,
288+
}
289+
}
290+
291+
DEBUG && console.log("PRODUCT_MODULE_DISABLED")
292+
const { product_categories, count } = await medusaRequest(
293+
"GET",
294+
"/product-categories",
295+
{
296+
query: {
297+
offset,
298+
limit,
299+
},
300+
}
301+
)
302+
.then((res) => res.body)
303+
.catch((err) => {
304+
throw err
305+
})
306+
307+
return {
308+
product_categories,
309+
count,
310+
}
311+
}
312+
241313
/**
242314
* Fetches a category by handle, using the Medusa API or the Medusa Product Module, depending on the feature flag.
243315
* @param handle (string) - The handle of the category to retrieve
244316
* @returns collections (array) - An array of categories (should only be one)
245317
* @returns response (object) - An object containing the products and the number of products in the category
246318
* @returns nextPage (number) - The offset of the next page of products
247319
*/
248-
export async function getCategoryByHandle(handle: string) {
320+
export async function getCategoryByHandle(handle: string): Promise<{
321+
product_categories: ProductCategory[]
322+
parent: ProductCategory
323+
}> {
249324
if (PRODUCT_MODULE_ENABLED) {
250325
DEBUG && console.log("PRODUCT_MODULE_ENABLED")
251326
const data = await fetch(`${API_BASE_URL}/api/categories/${handle}`)
@@ -290,7 +365,10 @@ export async function getProductsByCategoryHandle({
290365
pageParam?: number
291366
handle: string
292367
cartId?: string
293-
}) {
368+
}): Promise<{
369+
response: { products: Product[]; count: number }
370+
nextPage: number
371+
}> {
294372
if (PRODUCT_MODULE_ENABLED) {
295373
DEBUG && console.log("PRODUCT_MODULE_ENABLED")
296374
const { response, nextPage } = await fetch(

0 commit comments

Comments
 (0)