1
+ import { Product , ProductCategory , ProductCollection } from "@medusajs/product"
1
2
import medusaRequest from "../medusa-fetch"
2
3
import { StoreGetProductsParams } from "@medusajs/medusa"
3
4
@@ -21,7 +22,9 @@ const DEBUG = false
21
22
* @param handle (string) - The handle of the product to retrieve
22
23
* @returns (array) - An array of products (should only be one)
23
24
*/
24
- export async function getProductByHandle ( handle : string ) {
25
+ export async function getProductByHandle (
26
+ handle : string
27
+ ) : Promise < { products : Product [ ] } > {
25
28
if ( PRODUCT_MODULE_ENABLED ) {
26
29
DEBUG && console . log ( "PRODUCT_MODULE_ENABLED" )
27
30
const data = await fetch ( `${ API_BASE_URL } /api/products/${ handle } ` )
@@ -62,7 +65,10 @@ export async function getProductsList({
62
65
} : {
63
66
pageParam ?: number
64
67
queryParams : StoreGetProductsParams
65
- } ) {
68
+ } ) : Promise < {
69
+ response : { products : Product [ ] ; count : number }
70
+ nextPage : number
71
+ } > {
66
72
const limit = queryParams . limit || 12
67
73
68
74
if ( PRODUCT_MODULE_ENABLED ) {
@@ -113,7 +119,9 @@ export async function getProductsList({
113
119
* @returns collections (array) - An array of collections
114
120
* @returns count (number) - The total number of collections
115
121
*/
116
- export async function getCollectionsList ( offset : number = 0 ) {
122
+ export async function getCollectionsList (
123
+ offset : number = 0
124
+ ) : Promise < { collections : ProductCollection [ ] ; count : number } > {
117
125
if ( PRODUCT_MODULE_ENABLED ) {
118
126
DEBUG && console . log ( "PRODUCT_MODULE_ENABLED" )
119
127
const { collections, count } = await fetch (
@@ -159,7 +167,11 @@ export async function getCollectionsList(offset: number = 0) {
159
167
* @returns response (object) - An object containing the products and the number of products in the collection
160
168
* @returns nextPage (number) - The offset of the next page of products
161
169
*/
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
+ } > {
163
175
if ( PRODUCT_MODULE_ENABLED ) {
164
176
DEBUG && console . log ( "PRODUCT_MODULE_ENABLED" )
165
177
const data = await fetch ( `${ API_BASE_URL } /api/collections/${ handle } ` )
@@ -197,15 +209,20 @@ export async function getProductsByCollectionHandle({
197
209
pageParam = 0 ,
198
210
handle,
199
211
cartId,
212
+ currency_code,
200
213
} : {
201
214
pageParam ?: number
202
215
handle : string
203
216
cartId ?: string
204
- } ) {
217
+ currency_code ?: string
218
+ } ) : Promise < {
219
+ response : { products : Product [ ] ; count : number }
220
+ nextPage : number
221
+ } > {
205
222
if ( PRODUCT_MODULE_ENABLED ) {
206
223
DEBUG && console . log ( "PRODUCT_MODULE_ENABLED" )
207
224
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 ( ) } `
209
226
)
210
227
. then ( ( res ) => res . json ( ) )
211
228
. catch ( ( err ) => {
@@ -238,14 +255,72 @@ export async function getProductsByCollectionHandle({
238
255
}
239
256
}
240
257
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
+
241
313
/**
242
314
* Fetches a category by handle, using the Medusa API or the Medusa Product Module, depending on the feature flag.
243
315
* @param handle (string) - The handle of the category to retrieve
244
316
* @returns collections (array) - An array of categories (should only be one)
245
317
* @returns response (object) - An object containing the products and the number of products in the category
246
318
* @returns nextPage (number) - The offset of the next page of products
247
319
*/
248
- export async function getCategoryByHandle ( handle : string ) {
320
+ export async function getCategoryByHandle ( handle : string ) : Promise < {
321
+ product_categories : ProductCategory [ ]
322
+ parent : ProductCategory
323
+ } > {
249
324
if ( PRODUCT_MODULE_ENABLED ) {
250
325
DEBUG && console . log ( "PRODUCT_MODULE_ENABLED" )
251
326
const data = await fetch ( `${ API_BASE_URL } /api/categories/${ handle } ` )
@@ -290,7 +365,10 @@ export async function getProductsByCategoryHandle({
290
365
pageParam ?: number
291
366
handle : string
292
367
cartId ?: string
293
- } ) {
368
+ } ) : Promise < {
369
+ response : { products : Product [ ] ; count : number }
370
+ nextPage : number
371
+ } > {
294
372
if ( PRODUCT_MODULE_ENABLED ) {
295
373
DEBUG && console . log ( "PRODUCT_MODULE_ENABLED" )
296
374
const { response, nextPage } = await fetch (
0 commit comments