collections.ts
1 "use server" 2 3 import { sdk } from "@lib/config" 4 import { HttpTypes } from "@medusajs/types" 5 import { getCacheOptions } from "./cookies" 6 7 export const retrieveCollection = async (id: string) => { 8 const next = { 9 ...(await getCacheOptions("collections")), 10 } 11 12 return sdk.client 13 .fetch<{ collection: HttpTypes.StoreCollection }>( 14 `/store/collections/${id}`, 15 { 16 next, 17 cache: "force-cache", 18 } 19 ) 20 .then(({ collection }) => collection) 21 } 22 23 export const listCollections = async ( 24 queryParams: Record<string, string> = {} 25 ): Promise<{ collections: HttpTypes.StoreCollection[]; count: number }> => { 26 const next = { 27 ...(await getCacheOptions("collections")), 28 } 29 30 queryParams.limit = queryParams.limit || "100" 31 queryParams.offset = queryParams.offset || "0" 32 33 return sdk.client 34 .fetch<{ collections: HttpTypes.StoreCollection[]; count: number }>( 35 "/store/collections", 36 { 37 query: queryParams, 38 next, 39 cache: "force-cache", 40 } 41 ) 42 .then(({ collections }) => ({ collections, count: collections.length })) 43 } 44 45 export const getCollectionByHandle = async ( 46 handle: string 47 ): Promise<HttpTypes.StoreCollection> => { 48 const next = { 49 ...(await getCacheOptions("collections")), 50 } 51 52 return sdk.client 53 .fetch<HttpTypes.StoreCollectionListResponse>(`/store/collections`, { 54 query: { handle, fields: "*products" }, 55 next, 56 cache: "force-cache", 57 }) 58 .then(({ collections }) => collections[0]) 59 }