medusa-storefront-data 1.0.0 → 2.1.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 (110) 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 +96 -145
  23. package/dist/server/dynamic-config.d.ts.map +1 -1
  24. package/dist/server/dynamic-config.js +38 -12
  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 +7 -6
  65. package/dist/server/swaps.d.ts.map +1 -1
  66. package/dist/server/swaps.js +23 -57
  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 +146 -190
  90. package/src/server/dynamic-config.ts +38 -12
  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 +94 -123
  105. package/src/server/variants.ts +9 -28
  106. package/src/server/wishlist.ts +1 -1
  107. package/src/util/revalidate-cart.ts +10 -0
  108. package/src/util/sort-products.ts +2 -47
  109. package/src/util/store-client.ts +93 -0
  110. package/src/services/middleware.ts +0 -54
@@ -1,47 +1,2 @@
1
- import { HttpTypes } from "@medusajs/types";
2
-
3
- export type SortOptions =
4
- | "price_asc"
5
- | "price_desc"
6
- | "created_at_desc"
7
- | "created_at_asc"
8
- | "bestsellers"
9
- | "created_at";
10
-
11
- interface MinPricedProduct extends HttpTypes.StoreProduct {
12
- _minPrice?: number;
13
- }
14
-
15
- export function sortProducts(
16
- products: HttpTypes.StoreProduct[],
17
- sortBy: SortOptions
18
- ): HttpTypes.StoreProduct[] {
19
- const sortedProducts = products as MinPricedProduct[];
20
-
21
- if (["price_asc", "price_desc"].includes(sortBy)) {
22
- sortedProducts.forEach((product) => {
23
- if (product.variants && product.variants.length > 0) {
24
- product._minPrice = Math.min(
25
- ...product.variants.map(
26
- (variant) => variant?.calculated_price?.calculated_amount || 0
27
- )
28
- );
29
- } else {
30
- product._minPrice = Infinity;
31
- }
32
- });
33
-
34
- sortedProducts.sort((a, b) => {
35
- const diff = a._minPrice! - b._minPrice!;
36
- return sortBy === "price_asc" ? diff : -diff;
37
- });
38
- }
39
-
40
- if (sortBy === "created_at") {
41
- sortedProducts.sort((a, b) => {
42
- return new Date(b.created_at!).getTime() - new Date(a.created_at!).getTime();
43
- });
44
- }
45
-
46
- return sortedProducts;
47
- }
1
+ export type { SortOptions } from "medusa-services/product-listing";
2
+ export { sortProducts } from "medusa-services/product-listing";
@@ -0,0 +1,93 @@
1
+ import type { MedusaStoreClientOptions } from "medusa-services/store-api"
2
+ import { getStoreAuthOptions, type MedusaAuthClientOptions } from "medusa-services/auth"
3
+ import { getAuthHeaders, getGuestAuthHeaders } from "../cookies"
4
+ import { getLocaleHeader } from "./get-locale-header"
5
+ import { cookies as nextCookies } from "next/headers"
6
+
7
+ function getBackendUrl(): string {
8
+ return (
9
+ process.env.MEDUSA_BACKEND_URL?.replace(/\/$/, "") ||
10
+ process.env.NEXT_PUBLIC_MEDUSA_BACKEND_URL?.replace(/\/$/, "") ||
11
+ "http://localhost:9000"
12
+ )
13
+ }
14
+
15
+ function getPublishableApiKey(): string {
16
+ const key = process.env.NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY
17
+ if (!key) {
18
+ throw new Error("NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY is not configured")
19
+ }
20
+ return key
21
+ }
22
+
23
+ /**
24
+ * Builds portable Medusa store client options from Next.js cookies and env.
25
+ */
26
+ export async function getStoreClientOptions(): Promise<MedusaStoreClientOptions> {
27
+ const auth = await getAuthHeaders()
28
+ const localeHeader = await getLocaleHeader().catch(() => ({} as Record<string, string>))
29
+
30
+ return {
31
+ backendUrl: getBackendUrl(),
32
+ publishableApiKey: getPublishableApiKey(),
33
+ authorization: "authorization" in auth ? auth.authorization : undefined,
34
+ headers: localeHeader["x-medusa-locale"]
35
+ ? { "x-medusa-locale": localeHeader["x-medusa-locale"] }
36
+ : undefined,
37
+ }
38
+ }
39
+
40
+ /** @deprecated Use getStoreClientOptions */
41
+ export const getStoreCartClientOptions = getStoreClientOptions
42
+
43
+ export function getMedusaBackendUrl(): string {
44
+ return getBackendUrl()
45
+ }
46
+
47
+ export async function getGuestStoreClientOptions(): Promise<MedusaStoreClientOptions | null> {
48
+ const guestAuth = await getGuestAuthHeaders()
49
+ let authorization = "authorization" in guestAuth ? guestAuth.authorization : undefined
50
+
51
+ if (!authorization) {
52
+ try {
53
+ const cookieStore = await nextCookies()
54
+ const legacyToken = cookieStore.get("_medusa_guest_token")?.value
55
+ if (legacyToken) {
56
+ authorization = `Bearer ${legacyToken}`
57
+ }
58
+ } catch {
59
+ return null
60
+ }
61
+ }
62
+
63
+ if (!authorization) return null
64
+
65
+ return {
66
+ backendUrl: getBackendUrl(),
67
+ publishableApiKey: getPublishableApiKey(),
68
+ authorization,
69
+ }
70
+ }
71
+
72
+ export async function getStoreClientOptionsWithToken(
73
+ token: string
74
+ ): Promise<MedusaStoreClientOptions> {
75
+ return {
76
+ backendUrl: getBackendUrl(),
77
+ publishableApiKey: getPublishableApiKey(),
78
+ authorization: token.startsWith("Bearer ") ? token : `Bearer ${token}`,
79
+ }
80
+ }
81
+
82
+ export async function getAuthClientOptions(authorization?: string): Promise<MedusaAuthClientOptions> {
83
+ if (authorization) {
84
+ return getStoreAuthOptions({
85
+ backendUrl: getBackendUrl(),
86
+ publishableApiKey: getPublishableApiKey(),
87
+ authorization,
88
+ })
89
+ }
90
+
91
+ const storeOptions = await getStoreClientOptions()
92
+ return getStoreAuthOptions(storeOptions)
93
+ }
@@ -1,54 +0,0 @@
1
- /** Edge-safe Medusa store fetches (no Node SDK). */
2
-
3
- export interface MedusaMiddlewareClientOptions {
4
- backendUrl: string;
5
- publishableApiKey: string;
6
- }
7
-
8
- function normalizeBackendUrl(backendUrl: string): string {
9
- return backendUrl.replace(/\/$/, "");
10
- }
11
-
12
- export async function medusaListRegions(
13
- options: MedusaMiddlewareClientOptions,
14
- init?: RequestInit
15
- ): Promise<{ regions: unknown[] }> {
16
- const url = `${normalizeBackendUrl(options.backendUrl)}/store/regions`;
17
- const response = await fetch(url, {
18
- ...init,
19
- headers: {
20
- "x-publishable-api-key": options.publishableApiKey,
21
- ...(init?.headers as Record<string, string> | undefined),
22
- },
23
- });
24
-
25
- const json = (await response.json()) as { regions?: unknown[]; message?: string };
26
- if (!response.ok) {
27
- throw new Error(json.message || "Failed to fetch regions");
28
- }
29
- return { regions: json.regions ?? [] };
30
- }
31
-
32
- export async function medusaMergeCarts(
33
- options: MedusaMiddlewareClientOptions & {
34
- authorization: string;
35
- cartId?: string;
36
- },
37
- init?: RequestInit
38
- ): Promise<Response> {
39
- const url = `${normalizeBackendUrl(options.backendUrl)}/store/carts/merge`;
40
- return fetch(url, {
41
- method: "POST",
42
- ...init,
43
- headers: {
44
- "Content-Type": "application/json",
45
- "x-publishable-api-key": options.publishableApiKey,
46
- Authorization: options.authorization.startsWith("Bearer ")
47
- ? options.authorization
48
- : `Bearer ${options.authorization}`,
49
- ...(init?.headers as Record<string, string> | undefined),
50
- },
51
- body: options.cartId ? JSON.stringify({ cart_id: options.cartId }) : init?.body,
52
- cache: init?.cache ?? "no-store",
53
- });
54
- }