|
| 1 | +import medusaRequest from "../medusa-fetch" |
1 | 2 | import { StoreGetProductsParams } from "@medusajs/medusa"
|
2 | 3 |
|
3 |
| -const API_BASE_URL = process.env.NEXT_PUBLIC_BASE_URL || "http://localhost:8000" |
4 |
| - |
5 |
| -type FetchProductListParams = { |
| 4 | +type GetProductListParams = { |
6 | 5 | pageParam?: number
|
7 | 6 | queryParams: StoreGetProductsParams
|
8 | 7 | }
|
9 | 8 |
|
10 |
| -export const fetchProductsList = async ({ |
| 9 | +/** |
| 10 | + * This file contains functions for fetching products and collections from the Medusa API or the Medusa Product Module, |
| 11 | + * depending on the feature flag. By default, the standard Medusa API is used. To use the Medusa Product Module, set the feature flag to true. |
| 12 | + */ |
| 13 | + |
| 14 | +// The feature flag is set in the store.config.json file. Restart the server after changing the flag for the changes to take effect. |
| 15 | +const PRODUCT_MODULE_ENABLED = |
| 16 | + process.env.FEATURE_PRODUCTMODULE_ENABLED || false |
| 17 | + |
| 18 | +// The API_BASE_URL is set in the .env file. It is the base URL of your Next.js app. |
| 19 | +const API_BASE_URL = process.env.NEXT_PUBLIC_BASE_URL || "http://localhost:8000" |
| 20 | + |
| 21 | +// Set DEBUG to true to console.log where the data is coming from. |
| 22 | +const DEBUG = true |
| 23 | + |
| 24 | +/** |
| 25 | + * Fetches a product by handle, using the Medusa API or the Medusa Product Module, depending on the feature flag. |
| 26 | + * @param handle (string) - The handle of the product to retrieve |
| 27 | + * @returns (array) - An array of products (should only be one) |
| 28 | + */ |
| 29 | +export async function getProductByHandle(handle: string) { |
| 30 | + if (PRODUCT_MODULE_ENABLED) { |
| 31 | + DEBUG && console.log("PRODUCT_MODULE_ENABLED") |
| 32 | + const data = await fetch(`${API_BASE_URL}/api/products/${handle}`) |
| 33 | + .then((res) => res.json()) |
| 34 | + .catch((err) => { |
| 35 | + throw err |
| 36 | + }) |
| 37 | + |
| 38 | + return data |
| 39 | + } |
| 40 | + |
| 41 | + DEBUG && console.log("PRODUCT_MODULE_DISABLED") |
| 42 | + const { products } = await medusaRequest("GET", "/products", { |
| 43 | + query: { |
| 44 | + handle, |
| 45 | + }, |
| 46 | + }) |
| 47 | + .then((res) => res.body) |
| 48 | + .catch((err) => { |
| 49 | + throw err |
| 50 | + }) |
| 51 | + |
| 52 | + return { |
| 53 | + products, |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * Fetches a list of products, using the Medusa API or the Medusa Product Module, depending on the feature flag. |
| 59 | + * @param pageParam (number) - The offset of the products to retrieve |
| 60 | + * @param queryParams (object) - The query parameters to pass to the API |
| 61 | + * @returns 'response' (object) - An object containing the products and the next page offset |
| 62 | + * @returns 'nextPage' (number) - The offset of the next page of products |
| 63 | + */ |
| 64 | +export async function getProductsList({ |
11 | 65 | pageParam = 0,
|
12 | 66 | queryParams,
|
13 |
| -}: FetchProductListParams) => { |
14 |
| - const params = new URLSearchParams(queryParams as Record<string, string>) |
| 67 | +}: GetProductListParams) { |
| 68 | + const limit = queryParams.limit || 12 |
| 69 | + |
| 70 | + if (PRODUCT_MODULE_ENABLED) { |
| 71 | + console.log("PRODUCT_MODULE_ENABLED") |
| 72 | + const params = new URLSearchParams(queryParams as Record<string, string>) |
| 73 | + |
| 74 | + const { products, count, nextPage } = await fetch( |
| 75 | + `${API_BASE_URL}/api/products?limit=${limit}&offset=${pageParam}&${params.toString()}`, |
| 76 | + { |
| 77 | + next: { |
| 78 | + tags: ["products"], |
| 79 | + }, |
| 80 | + } |
| 81 | + ).then((res) => res.json()) |
| 82 | + |
| 83 | + return { |
| 84 | + response: { products, count }, |
| 85 | + nextPage, |
| 86 | + } |
| 87 | + } |
15 | 88 |
|
16 |
| - const { products, count, nextPage } = await fetch( |
17 |
| - `${API_BASE_URL}/api/products?limit=12&offset=${pageParam}&${params.toString()}`, |
| 89 | + DEBUG && console.log("PRODUCT_MODULE_DISABLED") |
| 90 | + const { products, count, nextPage } = await medusaRequest( |
| 91 | + "GET", |
| 92 | + "/products", |
18 | 93 | {
|
19 |
| - next: { |
20 |
| - tags: ["products"], |
| 94 | + query: { |
| 95 | + limit, |
| 96 | + offset: pageParam, |
| 97 | + ...queryParams, |
21 | 98 | },
|
22 | 99 | }
|
23 |
| - ).then((res) => res.json()) |
| 100 | + ) |
| 101 | + .then((res) => res.body) |
| 102 | + .catch((err) => { |
| 103 | + throw err |
| 104 | + }) |
24 | 105 |
|
25 | 106 | return {
|
26 | 107 | response: { products, count },
|
27 | 108 | nextPage,
|
28 | 109 | }
|
29 | 110 | }
|
| 111 | + |
| 112 | +/** |
| 113 | + * Fetches a list of collections, using the Medusa API or the Medusa Product Module, depending on the feature flag. |
| 114 | + * @param offset (number) - The offset of the collections to retrieve (default: 0 |
| 115 | + * @returns collections (array) - An array of collections |
| 116 | + * @returns count (number) - The total number of collections |
| 117 | + */ |
| 118 | +export async function getCollectionsList(offset: number = 0) { |
| 119 | + if (PRODUCT_MODULE_ENABLED) { |
| 120 | + DEBUG && console.log("PRODUCT_MODULE_ENABLED") |
| 121 | + const { collections, count } = await fetch( |
| 122 | + `${API_BASE_URL}/api/collections?offset=${offset}`, |
| 123 | + { |
| 124 | + next: { |
| 125 | + tags: ["collections"], |
| 126 | + }, |
| 127 | + } |
| 128 | + ) |
| 129 | + .then((res) => res.json()) |
| 130 | + .catch((err) => { |
| 131 | + throw err |
| 132 | + }) |
| 133 | + |
| 134 | + return { |
| 135 | + collections, |
| 136 | + count, |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + DEBUG && console.log("PRODUCT_MODULE_DISABLED") |
| 141 | + const { collections, count } = await medusaRequest("GET", "/collections", { |
| 142 | + query: { |
| 143 | + offset, |
| 144 | + }, |
| 145 | + }) |
| 146 | + .then((res) => res.body) |
| 147 | + .catch((err) => { |
| 148 | + throw err |
| 149 | + }) |
| 150 | + |
| 151 | + return { |
| 152 | + collections, |
| 153 | + count, |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +/** |
| 158 | + * Fetches a collection by handle, using the Medusa API or the Medusa Product Module, depending on the feature flag. |
| 159 | + * @param handle (string) - The handle of the collection to retrieve |
| 160 | + * @returns (array) - An array of collections (should only be one) |
| 161 | + */ |
| 162 | +export async function getCollectionByHandle(handle: string) { |
| 163 | + if (PRODUCT_MODULE_ENABLED) { |
| 164 | + DEBUG && console.log("PRODUCT_MODULE_ENABLED") |
| 165 | + const data = await fetch(`${API_BASE_URL}/api/collections/${handle}`) |
| 166 | + .then((res) => res.json()) |
| 167 | + .catch((err) => { |
| 168 | + throw err |
| 169 | + }) |
| 170 | + |
| 171 | + return data |
| 172 | + } |
| 173 | + |
| 174 | + DEBUG && console.log("PRODUCT_MODULE_DISABLED") |
| 175 | + const data = await medusaRequest("GET", "/collections", { |
| 176 | + query: { |
| 177 | + handle: [handle], |
| 178 | + }, |
| 179 | + }) |
| 180 | + .then((res) => res.body) |
| 181 | + .catch((err) => { |
| 182 | + throw err |
| 183 | + }) |
| 184 | + |
| 185 | + return data |
| 186 | +} |
| 187 | + |
| 188 | +export async function getProductsByCollectionHandle({ |
| 189 | + pageParam = 0, |
| 190 | + handle, |
| 191 | + cartId, |
| 192 | +}: { |
| 193 | + pageParam?: number |
| 194 | + handle: string |
| 195 | + cartId?: string |
| 196 | +}) { |
| 197 | + if (PRODUCT_MODULE_ENABLED) { |
| 198 | + DEBUG && console.log("PRODUCT_MODULE_ENABLED") |
| 199 | + const { response, nextPage } = await fetch( |
| 200 | + `${API_BASE_URL}/api/collections/${handle}?cart_id=${cartId}&page=${pageParam.toString()}` |
| 201 | + ) |
| 202 | + .then((res) => res.json()) |
| 203 | + .catch((err) => { |
| 204 | + throw err |
| 205 | + }) |
| 206 | + |
| 207 | + return { |
| 208 | + response, |
| 209 | + nextPage, |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + DEBUG && console.log("PRODUCT_MODULE_DISABLED") |
| 214 | + const { id } = await getCollectionByHandle(handle).then( |
| 215 | + (res) => res.collections[0] |
| 216 | + ) |
| 217 | + |
| 218 | + const { response, nextPage } = await getProductsList({ |
| 219 | + pageParam, |
| 220 | + queryParams: { collection_id: [id], cart_id: cartId }, |
| 221 | + }) |
| 222 | + .then((res) => res) |
| 223 | + .catch((err) => { |
| 224 | + throw err |
| 225 | + }) |
| 226 | + |
| 227 | + return { |
| 228 | + response, |
| 229 | + nextPage, |
| 230 | + } |
| 231 | +} |
0 commit comments