medusa-storefront-data 1.0.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (109) hide show
  1. package/dist/cookies.d.ts +2 -2
  2. package/dist/cookies.d.ts.map +1 -1
  3. package/dist/edge.d.ts +3 -0
  4. package/dist/edge.d.ts.map +1 -0
  5. package/dist/edge.js +1 -0
  6. package/dist/middleware.d.ts +3 -0
  7. package/dist/middleware.d.ts.map +1 -0
  8. package/dist/middleware.js +1 -0
  9. package/dist/server/cart.d.ts +9 -5
  10. package/dist/server/cart.d.ts.map +1 -1
  11. package/dist/server/cart.js +164 -194
  12. package/dist/server/categories.d.ts +3 -2
  13. package/dist/server/categories.d.ts.map +1 -1
  14. package/dist/server/categories.js +14 -51
  15. package/dist/server/collections.d.ts.map +1 -1
  16. package/dist/server/collections.js +16 -61
  17. package/dist/server/contact.d.ts +34 -0
  18. package/dist/server/contact.d.ts.map +1 -0
  19. package/dist/server/contact.js +57 -0
  20. package/dist/server/customer.d.ts +7 -7
  21. package/dist/server/customer.d.ts.map +1 -1
  22. package/dist/server/customer.js +95 -145
  23. package/dist/server/dynamic-config.d.ts.map +1 -1
  24. package/dist/server/dynamic-config.js +5 -2
  25. package/dist/server/fulfillment.d.ts +4 -3
  26. package/dist/server/fulfillment.d.ts.map +1 -1
  27. package/dist/server/fulfillment.js +16 -41
  28. package/dist/server/guest.d.ts +35 -63
  29. package/dist/server/guest.d.ts.map +1 -1
  30. package/dist/server/guest.js +81 -202
  31. package/dist/server/home.d.ts +15 -0
  32. package/dist/server/home.d.ts.map +1 -0
  33. package/dist/server/home.js +45 -0
  34. package/dist/server/index.d.ts +2 -0
  35. package/dist/server/index.d.ts.map +1 -1
  36. package/dist/server/index.js +2 -0
  37. package/dist/server/locale-actions.d.ts +1 -1
  38. package/dist/server/locale-actions.d.ts.map +1 -1
  39. package/dist/server/locale-actions.js +8 -13
  40. package/dist/server/locales.d.ts +2 -4
  41. package/dist/server/locales.d.ts.map +1 -1
  42. package/dist/server/locales.js +5 -13
  43. package/dist/server/orders.d.ts +5 -11
  44. package/dist/server/orders.d.ts.map +1 -1
  45. package/dist/server/orders.js +126 -267
  46. package/dist/server/payment-details.d.ts +4 -4
  47. package/dist/server/payment-details.d.ts.map +1 -1
  48. package/dist/server/payment-details.js +17 -42
  49. package/dist/server/payment.d.ts +2 -1
  50. package/dist/server/payment.d.ts.map +1 -1
  51. package/dist/server/payment.js +9 -21
  52. package/dist/server/pincode.d.ts +7 -0
  53. package/dist/server/pincode.d.ts.map +1 -0
  54. package/dist/server/pincode.js +30 -0
  55. package/dist/server/products.d.ts +15 -19
  56. package/dist/server/products.d.ts.map +1 -1
  57. package/dist/server/products.js +47 -178
  58. package/dist/server/regions.d.ts +1 -1
  59. package/dist/server/regions.d.ts.map +1 -1
  60. package/dist/server/regions.js +6 -3
  61. package/dist/server/returns.d.ts +4 -4
  62. package/dist/server/returns.d.ts.map +1 -1
  63. package/dist/server/returns.js +50 -154
  64. package/dist/server/swaps.d.ts +3 -3
  65. package/dist/server/swaps.d.ts.map +1 -1
  66. package/dist/server/swaps.js +22 -56
  67. package/dist/server/variants.d.ts.map +1 -1
  68. package/dist/server/variants.js +11 -22
  69. package/dist/server/wishlist.d.ts +11 -0
  70. package/dist/server/wishlist.d.ts.map +1 -0
  71. package/dist/server/wishlist.js +49 -0
  72. package/dist/util/get-locale-header.d.ts +1 -1
  73. package/dist/util/revalidate-cart.d.ts +2 -0
  74. package/dist/util/revalidate-cart.d.ts.map +1 -0
  75. package/dist/util/revalidate-cart.js +8 -0
  76. package/dist/util/sort-products.d.ts +3 -0
  77. package/dist/util/sort-products.d.ts.map +1 -0
  78. package/dist/util/sort-products.js +1 -0
  79. package/dist/util/store-client.d.ts +13 -0
  80. package/dist/util/store-client.d.ts.map +1 -0
  81. package/dist/util/store-client.js +77 -0
  82. package/package.json +95 -37
  83. package/src/edge.ts +2 -0
  84. package/src/middleware.ts +2 -2
  85. package/src/server/cart.ts +214 -267
  86. package/src/server/categories.ts +19 -72
  87. package/src/server/collections.ts +25 -82
  88. package/src/server/contact.ts +92 -0
  89. package/src/server/customer.ts +140 -189
  90. package/src/server/dynamic-config.ts +6 -2
  91. package/src/server/fulfillment.ts +27 -53
  92. package/src/server/guest.ts +159 -276
  93. package/src/server/home.ts +68 -0
  94. package/src/server/index.ts +1 -0
  95. package/src/server/locale-actions.ts +8 -15
  96. package/src/server/locales.ts +6 -18
  97. package/src/server/orders.ts +167 -337
  98. package/src/server/payment-details.ts +24 -52
  99. package/src/server/payment.ts +8 -28
  100. package/src/server/pincode.ts +49 -0
  101. package/src/server/products.ts +72 -235
  102. package/src/server/regions.ts +10 -6
  103. package/src/server/returns.ts +75 -189
  104. package/src/server/swaps.ts +90 -121
  105. package/src/server/variants.ts +9 -28
  106. package/src/util/revalidate-cart.ts +10 -0
  107. package/src/util/sort-products.ts +2 -47
  108. package/src/util/store-client.ts +93 -0
  109. package/src/services/middleware.ts +0 -54
@@ -1,41 +1,14 @@
1
- import { sdk } from "../config"
2
1
  import { HttpTypes } from "@medusajs/types"
3
- import { getCacheOptions } from "../cookies"
2
+ import { medusaCategoryByHandle, medusaCategoryList } from "medusa-services/categories"
3
+ import { getStoreClientOptions } from "../util/store-client"
4
4
 
5
5
  export const listCategories = async (query?: Record<string, any>) => {
6
6
  try {
7
- const next = {
8
- ...(await getCacheOptions("categories")),
9
- }
10
-
11
- const limit = query?.limit || 100
12
-
13
- return sdk.client
14
- .fetch<{ product_categories: HttpTypes.StoreProductCategory[] }>(
15
- "/store/product-categories",
16
- {
17
- query: {
18
- fields:
19
- "*category_children, *products, *parent_category, *parent_category.parent_category",
20
- limit,
21
- ...query,
22
- },
23
- next: {
24
- ...next,
25
- revalidate: 0,
26
- },
27
- cache: "no-store",
28
- }
29
- )
30
- .then(({ product_categories }) => product_categories)
31
- .catch((error) => {
32
- if (process.env.NODE_ENV === 'production' || process.env.CI) {
33
- return []
34
- }
35
- throw error
36
- })
37
- } catch (error: any) {
38
- if (process.env.NODE_ENV === 'production' || process.env.CI) {
7
+ const options = await getStoreClientOptions()
8
+ const { product_categories } = await medusaCategoryList(options, query)
9
+ return product_categories as unknown as HttpTypes.StoreProductCategory[]
10
+ } catch (error) {
11
+ if (process.env.NODE_ENV === "production" || process.env.CI) {
39
12
  return []
40
13
  }
41
14
  throw error
@@ -46,49 +19,23 @@ export const getCategoryByHandle = async (categoryHandle: string[]) => {
46
19
  try {
47
20
  const handle = `${categoryHandle.join("/")}`
48
21
  const decodedHandle = decodeURIComponent(handle)
22
+ const options = await getStoreClientOptions()
49
23
 
50
-
51
- const next = {
52
- ...(await getCacheOptions("categories")),
53
- }
54
-
55
- const result = await sdk.client
56
- .fetch<HttpTypes.StoreProductCategoryListResponse>(
57
- `/store/product-categories`,
58
- {
59
- query: {
60
- handle: [decodedHandle],
61
- },
62
- next: {
63
- ...next,
64
- revalidate: 0,
65
- },
66
- cache: "no-store",
67
- }
68
- )
69
-
70
- if (result.product_categories && result.product_categories.length > 0) {
71
- return result.product_categories[0]
24
+ const matched = await medusaCategoryByHandle(decodedHandle, options)
25
+ if (matched) {
26
+ return matched as unknown as HttpTypes.StoreProductCategory
72
27
  }
73
28
 
74
- // Fallback: Fetch all categories and find manually
75
-
76
- const allCategories = await listCategories({ limit: "200" })
77
- const matchedCategory = allCategories.find(c =>
78
- c.handle === decodedHandle ||
79
- c.handle === handle ||
80
- c.handle?.replace(/-/g, " ") === decodedHandle
29
+ const allCategories = await listCategories({ limit: 200 })
30
+ const fallback = allCategories.find(
31
+ (c) =>
32
+ c.handle === decodedHandle ||
33
+ c.handle === handle ||
34
+ c.handle?.replace(/-/g, " ") === decodedHandle
81
35
  )
82
36
 
83
- if (matchedCategory) {
84
-
85
- return matchedCategory
86
- }
87
-
88
-
89
- return null
90
- } catch (error: any) {
91
-
37
+ return fallback ?? null
38
+ } catch {
92
39
  return null
93
40
  }
94
41
  }
@@ -1,60 +1,28 @@
1
1
  "use server"
2
2
 
3
- import { sdk } from "../config"
4
3
  import { HttpTypes } from "@medusajs/types"
5
- import { getCacheOptions } from "../cookies"
4
+ import {
5
+ medusaCollectionByHandle,
6
+ medusaCollectionList,
7
+ medusaCollectionRetrieve,
8
+ } from "medusa-services/collections"
9
+ import { getStoreClientOptions } from "../util/store-client"
6
10
 
7
11
  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
- ...next,
18
- revalidate: 0,
19
- },
20
- cache: "no-store",
21
- }
22
- )
23
- .then(({ collection }) => collection)
12
+ const options = await getStoreClientOptions()
13
+ const { collection } = await medusaCollectionRetrieve(id, options)
14
+ return collection as unknown as HttpTypes.StoreCollection
24
15
  }
25
16
 
26
17
  export const listCollections = async (
27
18
  queryParams: Record<string, string> = {}
28
19
  ): Promise<{ collections: HttpTypes.StoreCollection[]; count: number }> => {
29
20
  try {
30
- const next = {
31
- ...(await getCacheOptions("collections")),
32
- }
33
-
34
- queryParams.limit = queryParams.limit || "100"
35
- queryParams.offset = queryParams.offset || "0"
36
-
37
- return sdk.client
38
- .fetch<{ collections: HttpTypes.StoreCollection[]; count: number }>(
39
- "/store/collections",
40
- {
41
- query: queryParams,
42
- next: {
43
- ...next,
44
- revalidate: 0,
45
- },
46
- cache: "no-store",
47
- }
48
- )
49
- .then(({ collections }) => ({ collections, count: collections.length }))
50
- .catch((error) => {
51
- if (process.env.NODE_ENV === 'production' || process.env.CI) {
52
- return { collections: [], count: 0 }
53
- }
54
- throw error
55
- })
56
- } catch (error: any) {
57
- if (process.env.NODE_ENV === 'production' || process.env.CI) {
21
+ const options = await getStoreClientOptions()
22
+ const { collections } = await medusaCollectionList(options, queryParams)
23
+ return { collections: collections as unknown as HttpTypes.StoreCollection[], count: collections.length }
24
+ } catch (error) {
25
+ if (process.env.NODE_ENV === "production" || process.env.CI) {
58
26
  return { collections: [], count: 0 }
59
27
  }
60
28
  throw error
@@ -65,49 +33,24 @@ export const getCollectionByHandle = async (
65
33
  handle: string
66
34
  ): Promise<HttpTypes.StoreCollection | null> => {
67
35
  try {
68
- const next = {
69
- ...(await getCacheOptions("collections")),
70
- }
71
-
72
- // Ensure handle is decoded
73
36
  const decodedHandle = decodeURIComponent(handle)
37
+ const options = await getStoreClientOptions()
74
38
 
75
-
76
- // Try array-based handle filter (standard for v2)
77
- const result = await sdk.client
78
- .fetch<HttpTypes.StoreCollectionListResponse>(`/store/collections`, {
79
- query: {
80
- handle: [decodedHandle],
81
- },
82
- next: {
83
- ...next,
84
- revalidate: 0,
85
- },
86
- cache: "no-store",
87
- })
88
-
89
- if (result.collections && result.collections.length > 0) {
90
- return result.collections[0]
39
+ const matched = await medusaCollectionByHandle(decodedHandle, options)
40
+ if (matched) {
41
+ return matched as unknown as HttpTypes.StoreCollection
91
42
  }
92
43
 
93
- // Fallback: Fetch all and find manually (useful for handles with spaces/special chars)
94
-
95
44
  const { collections } = await listCollections({ limit: "100" })
96
- const matchedCollection = collections.find(c =>
97
- c.handle === decodedHandle ||
98
- c.handle === handle ||
99
- c.handle?.replace(/-/g, " ") === decodedHandle
45
+ const fallback = collections.find(
46
+ (c) =>
47
+ c.handle === decodedHandle ||
48
+ c.handle === handle ||
49
+ c.handle?.replace(/-/g, " ") === decodedHandle
100
50
  )
101
51
 
102
- if (matchedCollection) {
103
-
104
- return matchedCollection
105
- }
106
-
107
-
108
- return null
109
- } catch (error: any) {
110
-
52
+ return fallback ?? null
53
+ } catch {
111
54
  return null
112
55
  }
113
56
  }
@@ -0,0 +1,92 @@
1
+ "use server"
2
+
3
+ import {
4
+ sendContactRequest,
5
+ subscribeToNewsletter as subscribeNewsletterApi,
6
+ } from "medusa-services/contact-action"
7
+
8
+ function getStoreConfig() {
9
+ const backendUrl =
10
+ process.env.MEDUSA_BACKEND_URL ||
11
+ process.env.NEXT_PUBLIC_MEDUSA_BACKEND_URL ||
12
+ "http://localhost:9000"
13
+ const publishableApiKey = process.env.NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY || ""
14
+ return { backendUrl, publishableApiKey }
15
+ }
16
+
17
+ export async function submitContactRequest({
18
+ email,
19
+ payload,
20
+ metadata,
21
+ source = "storefront",
22
+ }: {
23
+ email: string
24
+ payload: {
25
+ full_name?: string
26
+ phone?: string
27
+ topic?: string
28
+ subject?: string
29
+ message: string
30
+ }
31
+ metadata?: Record<string, unknown>
32
+ source?: string
33
+ }) {
34
+ try {
35
+ const { backendUrl, publishableApiKey } = getStoreConfig()
36
+ if (!publishableApiKey) {
37
+ return {
38
+ success: false as const,
39
+ error: "Configuration error: Publishable API key is missing.",
40
+ }
41
+ }
42
+
43
+ const data = await sendContactRequest({
44
+ backendUrl,
45
+ publishableKey: publishableApiKey,
46
+ data: { email, payload, metadata, source },
47
+ })
48
+
49
+ return { success: true as const, data }
50
+ } catch (error: unknown) {
51
+ const message =
52
+ error instanceof Error ? error.message : "Failed to submit contact request"
53
+ return { success: false as const, error: message }
54
+ }
55
+ }
56
+
57
+ export async function subscribeToNewsletter({
58
+ email,
59
+ status = "subscribed",
60
+ source = "footer",
61
+ }: {
62
+ email: string
63
+ status?: "subscribed" | "unsubscribed"
64
+ source?: string
65
+ }) {
66
+ try {
67
+ const { backendUrl, publishableApiKey } = getStoreConfig()
68
+ if (!publishableApiKey) {
69
+ return {
70
+ success: false as const,
71
+ message: "Server configuration error (missing key)",
72
+ }
73
+ }
74
+
75
+ const data = await subscribeNewsletterApi({
76
+ backendUrl,
77
+ publishableApiKey,
78
+ email,
79
+ status,
80
+ source,
81
+ })
82
+
83
+ return {
84
+ success: true as const,
85
+ message: "Thank you for subscribing!",
86
+ data,
87
+ }
88
+ } catch (error: unknown) {
89
+ const message = error instanceof Error ? error.message : "Something went wrong"
90
+ return { success: false as const, message }
91
+ }
92
+ }