@gfed-medusa/sf-lib-common 2.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.
- package/dist/components/breadcrumbs/index.d.ts.map +1 -1
- package/dist/components/breadcrumbs/index.js.map +1 -1
- package/dist/components/cart-mismatch-banner/index.d.ts +2 -2
- package/dist/components/cart-mismatch-banner/index.js +1 -1
- package/dist/components/delete-button/index.d.ts +2 -2
- package/dist/components/delete-button/index.js +1 -1
- package/dist/components/error-message/index.d.ts +2 -2
- package/dist/components/error-message/index.d.ts.map +1 -1
- package/dist/components/free-shipping-price-nudge/index.d.ts +2 -2
- package/dist/components/free-shipping-price-nudge/index.d.ts.map +1 -1
- package/dist/components/free-shipping-price-nudge/index.js +2 -2
- package/dist/components/free-shipping-price-nudge/index.js.map +1 -1
- package/dist/components/interactive-link/index.d.ts +2 -2
- package/dist/components/interactive-link/index.d.ts.map +1 -1
- package/dist/components/interactive-link/index.js +1 -1
- package/dist/components/line-item-options/index.d.ts +2 -2
- package/dist/components/line-item-price/index.d.ts +2 -2
- package/dist/components/localized-client-link/index.d.ts +2 -2
- package/dist/components/localized-client-link/index.d.ts.map +1 -1
- package/dist/components/localized-client-link/index.js +1 -5
- package/dist/components/localized-client-link/index.js.map +1 -1
- package/dist/components/modal/index.d.ts +2 -2
- package/dist/components/modal/index.d.ts.map +1 -1
- package/dist/components/modal/index.js +1 -1
- package/dist/components/preview-price/index.d.ts +2 -2
- package/dist/components/preview-price/index.d.ts.map +1 -1
- package/dist/components/product-card/index.d.ts +2 -2
- package/dist/components/product-card/index.d.ts.map +1 -1
- package/dist/components/product-card/index.js +2 -2
- package/dist/components/product-card/index.js.map +1 -1
- package/dist/components/product-preview/index.d.ts +2 -2
- package/dist/components/product-preview/index.js +1 -1
- package/dist/components/submit-button/index.d.ts +2 -2
- package/dist/components/submit-button/index.d.ts.map +1 -1
- package/dist/lib/context/apollo-context.d.ts +2 -2
- package/dist/lib/context/apollo-context.d.ts.map +1 -1
- package/dist/lib/context/modal-context.d.ts +2 -2
- package/dist/lib/context/modal-context.d.ts.map +1 -1
- package/dist/lib/data/cart.d.ts +7 -17
- package/dist/lib/data/cart.d.ts.map +1 -1
- package/dist/lib/data/cart.js +23 -46
- package/dist/lib/data/cart.js.map +1 -1
- package/dist/lib/data/context.d.ts +27 -0
- package/dist/lib/data/context.d.ts.map +1 -0
- package/dist/lib/data/context.js +18 -0
- package/dist/lib/data/context.js.map +1 -0
- package/dist/lib/data/cookies.d.ts +12 -10
- package/dist/lib/data/cookies.d.ts.map +1 -1
- package/dist/lib/data/cookies.js +25 -35
- package/dist/lib/data/cookies.js.map +1 -1
- package/dist/lib/data/customer.d.ts +3 -2
- package/dist/lib/data/customer.d.ts.map +1 -1
- package/dist/lib/data/customer.js +11 -10
- package/dist/lib/data/customer.js.map +1 -1
- package/dist/lib/data/next-context.d.ts +8 -0
- package/dist/lib/data/next-context.d.ts.map +1 -0
- package/dist/lib/data/next-context.js +65 -0
- package/dist/lib/data/next-context.js.map +1 -0
- package/dist/lib/data/orders.js +1 -1
- package/dist/lib/data/regions.d.ts +3 -2
- package/dist/lib/data/regions.d.ts.map +1 -1
- package/dist/lib/data/regions.js +5 -5
- package/dist/lib/data/regions.js.map +1 -1
- package/dist/lib/gql/fragments/customer.d.ts +3 -3
- package/dist/lib/gql/fragments/customer.d.ts.map +1 -1
- package/dist/lib/gql/fragments/product.d.ts +8 -8
- package/dist/lib/gql/mutations/cart.d.ts +4 -4
- package/dist/lib/gql/queries/cart.d.ts +2 -2
- package/dist/lib/gql/queries/collections.d.ts +2 -2
- package/dist/lib/gql/queries/customer.d.ts +2 -2
- package/dist/lib/gql/queries/footer.d.ts +2 -2
- package/dist/lib/gql/queries/product.d.ts +3 -3
- package/dist/lib/gql/queries/product.d.ts.map +1 -1
- package/dist/lib/utils/env.js +1 -1
- package/dist/lib/utils/env.js.map +1 -1
- package/package.json +1 -1
- package/dist/components/cart-button/index.d.ts +0 -7
- package/dist/components/cart-button/index.d.ts.map +0 -1
- package/dist/components/cart-button/index.js +0 -12
- package/dist/components/cart-button/index.js.map +0 -1
- package/dist/components/cart-dropdown/index.d.ts +0 -12
- package/dist/components/cart-dropdown/index.d.ts.map +0 -1
- package/dist/components/cart-dropdown/index.js +0 -196
- package/dist/components/cart-dropdown/index.js.map +0 -1
- package/dist/components/footer/index.d.ts +0 -7
- package/dist/components/footer/index.d.ts.map +0 -1
- package/dist/components/footer/index.js +0 -110
- package/dist/components/footer/index.js.map +0 -1
- package/dist/components/layout-country-select/index.d.ts +0 -16
- package/dist/components/layout-country-select/index.d.ts.map +0 -1
- package/dist/components/layout-country-select/index.js +0 -92
- package/dist/components/layout-country-select/index.js.map +0 -1
- package/dist/components/nav/index.d.ts +0 -7
- package/dist/components/nav/index.d.ts.map +0 -1
- package/dist/components/nav/index.js +0 -66
- package/dist/components/nav/index.js.map +0 -1
- package/dist/components/portable-text/index.d.ts +0 -13
- package/dist/components/portable-text/index.d.ts.map +0 -1
- package/dist/components/portable-text/index.js +0 -330
- package/dist/components/portable-text/index.js.map +0 -1
- package/dist/components/search-modal/index.d.ts +0 -12
- package/dist/components/search-modal/index.d.ts.map +0 -1
- package/dist/components/search-modal/index.js +0 -138
- package/dist/components/search-modal/index.js.map +0 -1
- package/dist/components/side-menu/index.d.ts +0 -12
- package/dist/components/side-menu/index.d.ts.map +0 -1
- package/dist/components/side-menu/index.js +0 -99
- package/dist/components/side-menu/index.js.map +0 -1
package/dist/lib/data/cart.js
CHANGED
|
@@ -1,23 +1,13 @@
|
|
|
1
|
-
'use server';
|
|
2
|
-
|
|
3
|
-
import { sdk } from "../config/medusa.js";
|
|
4
1
|
import { graphqlFetch, graphqlMutation } from "../gql/apollo-client.js";
|
|
5
2
|
import { DELETE_LINE_ITEM_MUTATION, UPDATE_CART_MUTATION } from "../gql/mutations/cart.js";
|
|
6
|
-
import { GET_CART_QUERY } from "../gql/queries/cart.js";
|
|
7
3
|
import { medusaError } from "../utils/medusa-error.js";
|
|
8
4
|
import { getAuthHeaders, getCacheOptions, getCacheTag, getCartId } from "./cookies.js";
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
11
|
-
import { revalidateTag } from "next/cache";
|
|
5
|
+
import { sdk } from "../config/medusa.js";
|
|
6
|
+
import { GET_CART_QUERY } from "../gql/queries/cart.js";
|
|
12
7
|
|
|
13
8
|
//#region src/lib/data/cart.ts
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
* @param cartId - optional - The ID of the cart to retrieve.
|
|
17
|
-
* @returns The cart object if found, or null if not found.
|
|
18
|
-
*/
|
|
19
|
-
const retrieveCart = async (cartId) => {
|
|
20
|
-
const id = cartId || await getCartId();
|
|
9
|
+
const retrieveCart = async (ctx) => {
|
|
10
|
+
const id = getCartId(ctx);
|
|
21
11
|
if (!id) return null;
|
|
22
12
|
try {
|
|
23
13
|
return (await graphqlFetch({
|
|
@@ -29,8 +19,8 @@ const retrieveCart = async (cartId) => {
|
|
|
29
19
|
return null;
|
|
30
20
|
}
|
|
31
21
|
};
|
|
32
|
-
const updateCart = async (data) => {
|
|
33
|
-
const cartId =
|
|
22
|
+
const updateCart = async (data, ctx) => {
|
|
23
|
+
const cartId = getCartId(ctx);
|
|
34
24
|
if (!cartId) throw new Error("No existing cart found, please create one before updating");
|
|
35
25
|
try {
|
|
36
26
|
const cart = (await graphqlMutation({
|
|
@@ -40,18 +30,20 @@ const updateCart = async (data) => {
|
|
|
40
30
|
data
|
|
41
31
|
}
|
|
42
32
|
}))?.updateCart ?? null;
|
|
43
|
-
if (cart) {
|
|
44
|
-
|
|
45
|
-
|
|
33
|
+
if (cart && ctx.revalidate) {
|
|
34
|
+
const cartCacheTag = getCacheTag("carts", ctx);
|
|
35
|
+
ctx.revalidate(cartCacheTag);
|
|
36
|
+
const fulfillmentCacheTag = getCacheTag("fulfillment", ctx);
|
|
37
|
+
ctx.revalidate(fulfillmentCacheTag);
|
|
46
38
|
}
|
|
47
39
|
return cart;
|
|
48
40
|
} catch (err) {
|
|
49
41
|
medusaError(err);
|
|
50
42
|
}
|
|
51
43
|
};
|
|
52
|
-
const deleteLineItem = async (lineId) => {
|
|
44
|
+
const deleteLineItem = async (lineId, ctx) => {
|
|
53
45
|
if (!lineId) throw new Error("Missing lineItem ID when deleting line item");
|
|
54
|
-
const cartId =
|
|
46
|
+
const cartId = getCartId(ctx);
|
|
55
47
|
if (!cartId) throw new Error("Missing cart ID when deleting line item");
|
|
56
48
|
try {
|
|
57
49
|
const deletedLineItem = (await graphqlMutation({
|
|
@@ -61,9 +53,11 @@ const deleteLineItem = async (lineId) => {
|
|
|
61
53
|
lineItemId: lineId
|
|
62
54
|
}
|
|
63
55
|
}))?.deleteLineItem ?? null;
|
|
64
|
-
if (deletedLineItem) {
|
|
65
|
-
|
|
66
|
-
|
|
56
|
+
if (deletedLineItem && ctx.revalidate) {
|
|
57
|
+
const cartCacheTag = getCacheTag("carts", ctx);
|
|
58
|
+
ctx.revalidate(cartCacheTag);
|
|
59
|
+
const fulfillmentCacheTag = getCacheTag("fulfillment", ctx);
|
|
60
|
+
ctx.revalidate(fulfillmentCacheTag);
|
|
67
61
|
}
|
|
68
62
|
return deletedLineItem;
|
|
69
63
|
} catch (error) {
|
|
@@ -71,27 +65,10 @@ const deleteLineItem = async (lineId) => {
|
|
|
71
65
|
throw error;
|
|
72
66
|
}
|
|
73
67
|
};
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
*/
|
|
79
|
-
async function updateRegion(countryCode, currentPath) {
|
|
80
|
-
const cartId = await getCartId();
|
|
81
|
-
const region = await getRegion(countryCode);
|
|
82
|
-
if (!region) throw new Error(`Region not found for country code: ${countryCode}`);
|
|
83
|
-
if (cartId) {
|
|
84
|
-
await updateCart({ regionId: region.id });
|
|
85
|
-
revalidateTag(await getCacheTag("carts"));
|
|
86
|
-
}
|
|
87
|
-
revalidateTag(await getCacheTag("regions"));
|
|
88
|
-
revalidateTag(await getCacheTag("products"));
|
|
89
|
-
redirect(`/${countryCode}${currentPath}`);
|
|
90
|
-
}
|
|
91
|
-
async function listCartOptions() {
|
|
92
|
-
const cartId = await getCartId();
|
|
93
|
-
const headers = { ...await getAuthHeaders() };
|
|
94
|
-
const next = { ...await getCacheOptions("shippingOptions") };
|
|
68
|
+
async function listCartOptions(ctx) {
|
|
69
|
+
const cartId = getCartId(ctx);
|
|
70
|
+
const headers = { ...getAuthHeaders(ctx) };
|
|
71
|
+
const next = { ...getCacheOptions("shippingOptions", ctx) };
|
|
95
72
|
return await sdk.client.fetch("/store/shipping-options", {
|
|
96
73
|
query: { cart_id: cartId },
|
|
97
74
|
next,
|
|
@@ -101,5 +78,5 @@ async function listCartOptions() {
|
|
|
101
78
|
}
|
|
102
79
|
|
|
103
80
|
//#endregion
|
|
104
|
-
export { deleteLineItem, listCartOptions, retrieveCart,
|
|
81
|
+
export { deleteLineItem, listCartOptions, retrieveCart, updateCart };
|
|
105
82
|
//# sourceMappingURL=cart.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cart.js","names":["error: any"],"sources":["../../../src/lib/data/cart.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"file":"cart.js","names":["error: any"],"sources":["../../../src/lib/data/cart.ts"],"sourcesContent":["import { HttpTypes } from '@medusajs/types';\n\nimport {\n Cart,\n DeleteLineItemMutation,\n DeleteLineItemMutationVariables,\n GetCartQuery,\n GetCartQueryVariables,\n UpdateCartMutation,\n UpdateCartMutationVariables,\n} from '../../types/graphql';\nimport { sdk } from '../config/medusa';\nimport { graphqlFetch, graphqlMutation } from '../gql/apollo-client';\nimport {\n DELETE_LINE_ITEM_MUTATION,\n UPDATE_CART_MUTATION,\n} from '../gql/mutations/cart';\nimport { GET_CART_QUERY } from '../gql/queries/cart';\nimport { medusaError } from '../utils/medusa-error';\nimport type { StorefrontContext } from './context';\nimport {\n getAuthHeaders,\n getCacheOptions,\n getCacheTag,\n getCartId,\n} from './cookies';\n\nexport const retrieveCart = async (\n ctx: StorefrontContext\n): Promise<Cart | null> => {\n const id = getCartId(ctx);\n if (!id) {\n return null;\n }\n\n try {\n const data = await graphqlFetch<GetCartQuery, GetCartQueryVariables>({\n query: GET_CART_QUERY,\n variables: { id },\n });\n\n return data?.cart ?? null;\n } catch (error) {\n console.error('Failed to fetch cart:', error);\n return null;\n }\n};\n\nexport const updateCart = async (\n data: UpdateCartMutationVariables['data'],\n ctx: StorefrontContext\n): Promise<UpdateCartMutation['updateCart'] | null> => {\n const cartId = getCartId(ctx);\n\n if (!cartId) {\n throw new Error(\n 'No existing cart found, please create one before updating'\n );\n }\n\n try {\n const result = await graphqlMutation<\n UpdateCartMutation,\n UpdateCartMutationVariables\n >({\n mutation: UPDATE_CART_MUTATION,\n variables: {\n id: cartId,\n data,\n },\n });\n\n const cart = result?.updateCart ?? null;\n\n if (cart && ctx.revalidate) {\n const cartCacheTag = getCacheTag('carts', ctx);\n ctx.revalidate(cartCacheTag);\n\n const fulfillmentCacheTag = getCacheTag('fulfillment', ctx);\n ctx.revalidate(fulfillmentCacheTag);\n }\n\n return cart;\n } catch (err) {\n medusaError(err);\n }\n};\n\nexport const deleteLineItem = async (\n lineId: string,\n ctx: StorefrontContext\n): Promise<DeleteLineItemMutation['deleteLineItem'] | null> => {\n if (!lineId) {\n throw new Error('Missing lineItem ID when deleting line item');\n }\n\n const cartId = getCartId(ctx);\n\n if (!cartId) {\n throw new Error('Missing cart ID when deleting line item');\n }\n\n try {\n const result = await graphqlMutation<\n DeleteLineItemMutation,\n DeleteLineItemMutationVariables\n >({\n mutation: DELETE_LINE_ITEM_MUTATION,\n variables: {\n cartId,\n lineItemId: lineId,\n },\n });\n\n const deletedLineItem = result?.deleteLineItem ?? null;\n\n if (deletedLineItem && ctx.revalidate) {\n const cartCacheTag = getCacheTag('carts', ctx);\n ctx.revalidate(cartCacheTag);\n\n const fulfillmentCacheTag = getCacheTag('fulfillment', ctx);\n ctx.revalidate(fulfillmentCacheTag);\n }\n\n return deletedLineItem;\n } catch (error: any) {\n console.error('GraphQL deleteLineItem error:', error.message);\n throw error;\n }\n};\n\nexport async function listCartOptions(ctx: StorefrontContext) {\n const cartId = getCartId(ctx);\n const headers = {\n ...getAuthHeaders(ctx),\n };\n const next = {\n ...getCacheOptions('shippingOptions', ctx),\n };\n\n return await sdk.client.fetch<{\n shipping_options: HttpTypes.StoreCartShippingOption[];\n }>('/store/shipping-options', {\n query: { cart_id: cartId },\n next,\n headers,\n cache: 'force-cache',\n });\n}\n"],"mappings":";;;;;;;;AA2BA,MAAa,eAAe,OAC1B,QACyB;CACzB,MAAM,KAAK,UAAU,IAAI;AACzB,KAAI,CAAC,GACH,QAAO;AAGT,KAAI;AAMF,UALa,MAAM,aAAkD;GACnE,OAAO;GACP,WAAW,EAAE,IAAI;GAClB,CAAC,GAEW,QAAQ;UACd,OAAO;AACd,UAAQ,MAAM,yBAAyB,MAAM;AAC7C,SAAO;;;AAIX,MAAa,aAAa,OACxB,MACA,QACqD;CACrD,MAAM,SAAS,UAAU,IAAI;AAE7B,KAAI,CAAC,OACH,OAAM,IAAI,MACR,4DACD;AAGH,KAAI;EAYF,MAAM,QAXS,MAAM,gBAGnB;GACA,UAAU;GACV,WAAW;IACT,IAAI;IACJ;IACD;GACF,CAAC,GAEmB,cAAc;AAEnC,MAAI,QAAQ,IAAI,YAAY;GAC1B,MAAM,eAAe,YAAY,SAAS,IAAI;AAC9C,OAAI,WAAW,aAAa;GAE5B,MAAM,sBAAsB,YAAY,eAAe,IAAI;AAC3D,OAAI,WAAW,oBAAoB;;AAGrC,SAAO;UACA,KAAK;AACZ,cAAY,IAAI;;;AAIpB,MAAa,iBAAiB,OAC5B,QACA,QAC6D;AAC7D,KAAI,CAAC,OACH,OAAM,IAAI,MAAM,8CAA8C;CAGhE,MAAM,SAAS,UAAU,IAAI;AAE7B,KAAI,CAAC,OACH,OAAM,IAAI,MAAM,0CAA0C;AAG5D,KAAI;EAYF,MAAM,mBAXS,MAAM,gBAGnB;GACA,UAAU;GACV,WAAW;IACT;IACA,YAAY;IACb;GACF,CAAC,GAE8B,kBAAkB;AAElD,MAAI,mBAAmB,IAAI,YAAY;GACrC,MAAM,eAAe,YAAY,SAAS,IAAI;AAC9C,OAAI,WAAW,aAAa;GAE5B,MAAM,sBAAsB,YAAY,eAAe,IAAI;AAC3D,OAAI,WAAW,oBAAoB;;AAGrC,SAAO;UACAA,OAAY;AACnB,UAAQ,MAAM,iCAAiC,MAAM,QAAQ;AAC7D,QAAM;;;AAIV,eAAsB,gBAAgB,KAAwB;CAC5D,MAAM,SAAS,UAAU,IAAI;CAC7B,MAAM,UAAU,EACd,GAAG,eAAe,IAAI,EACvB;CACD,MAAM,OAAO,EACX,GAAG,gBAAgB,mBAAmB,IAAI,EAC3C;AAED,QAAO,MAAM,IAAI,OAAO,MAErB,2BAA2B;EAC5B,OAAO,EAAE,SAAS,QAAQ;EAC1B;EACA;EACA,OAAO;EACR,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { ReactNode } from "react";
|
|
2
|
+
import * as react_jsx_runtime13 from "react/jsx-runtime";
|
|
3
|
+
|
|
4
|
+
//#region src/lib/data/context.d.ts
|
|
5
|
+
interface StorefrontContext {
|
|
6
|
+
cartId?: string;
|
|
7
|
+
customerToken?: string;
|
|
8
|
+
cacheId?: string;
|
|
9
|
+
cookieHeader?: string;
|
|
10
|
+
revalidate?: (tag: string) => void;
|
|
11
|
+
updateRegion?: (countryCode: string, currentPath: string) => Promise<void>;
|
|
12
|
+
setCartId?: (id: string) => Promise<void>;
|
|
13
|
+
removeCartId?: () => Promise<void>;
|
|
14
|
+
setAuthToken?: (token: string) => Promise<void>;
|
|
15
|
+
removeAuthToken?: () => Promise<void>;
|
|
16
|
+
}
|
|
17
|
+
declare const StorefrontProvider: ({
|
|
18
|
+
children,
|
|
19
|
+
value
|
|
20
|
+
}: {
|
|
21
|
+
children: ReactNode;
|
|
22
|
+
value: StorefrontContext;
|
|
23
|
+
}) => react_jsx_runtime13.JSX.Element;
|
|
24
|
+
declare const useStorefrontContext: () => StorefrontContext;
|
|
25
|
+
//#endregion
|
|
26
|
+
export { StorefrontContext, StorefrontProvider, useStorefrontContext };
|
|
27
|
+
//# sourceMappingURL=context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.d.ts","names":[],"sources":["../../../src/lib/data/context.tsx"],"sourcesContent":[],"mappings":";;;;UAIiB,iBAAA;;;EAAA,OAAA,CAAA,EAAA,MAAA;EAM8C,YAAA,CAAA,EAAA,MAAA;EACjC,UAAA,CAAA,EAAA,CAAA,GAAA,EAAA,MAAA,EAAA,GAAA,IAAA;EACP,YAAA,CAAA,EAAA,CAAA,WAAA,EAAA,MAAA,EAAA,WAAA,EAAA,MAAA,EAAA,GAFwC,OAExC,CAAA,IAAA,CAAA;EACa,SAAA,CAAA,EAAA,CAAA,EAAA,EAAA,MAAA,EAAA,GAFN,OAEM,CAAA,IAAA,CAAA;EACV,YAAA,CAAA,EAAA,GAAA,GAFH,OAEG,CAAA,IAAA,CAAA;EAAO,YAAA,CAAA,EAAA,CAAA,KAAA,EAAA,MAAA,EAAA,GADG,OACH,CAAA,IAAA,CAAA;EAKpB,eAAA,CAAA,EAAA,GAYZ,GAjByB,OAiBzB,CAAA,IAAA,CAAA;;AAZkC,cAAtB,kBAAsB,EAAA,CAAA;EAAA,QAAA;EAAA;CAAA,EAAA;EAIvB,QAAA,EAAA,SAAA;EACH,KAAA,EAAA,iBAAA;CACR,EAAA,GAAA,mBAAA,CAAA,GAAA,CAAA,OAAA;AAMA,cAEY,oBAFZ,EAAA,GAAA,GAEgC,iBAFhC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
import { createContext, useContext } from "react";
|
|
4
|
+
import { jsx } from "react/jsx-runtime";
|
|
5
|
+
|
|
6
|
+
//#region src/lib/data/context.tsx
|
|
7
|
+
const StorefrontReactContext = createContext({});
|
|
8
|
+
const StorefrontProvider = ({ children, value }) => {
|
|
9
|
+
return /* @__PURE__ */ jsx(StorefrontReactContext.Provider, {
|
|
10
|
+
value,
|
|
11
|
+
children
|
|
12
|
+
});
|
|
13
|
+
};
|
|
14
|
+
const useStorefrontContext = () => useContext(StorefrontReactContext);
|
|
15
|
+
|
|
16
|
+
//#endregion
|
|
17
|
+
export { StorefrontProvider, useStorefrontContext };
|
|
18
|
+
//# sourceMappingURL=context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.js","names":[],"sources":["../../../src/lib/data/context.tsx"],"sourcesContent":["'use client';\n\nimport { type ReactNode, createContext, useContext } from 'react';\n\nexport interface StorefrontContext {\n cartId?: string;\n customerToken?: string;\n cacheId?: string;\n cookieHeader?: string;\n revalidate?: (tag: string) => void;\n updateRegion?: (countryCode: string, currentPath: string) => Promise<void>;\n setCartId?: (id: string) => Promise<void>;\n removeCartId?: () => Promise<void>;\n setAuthToken?: (token: string) => Promise<void>;\n removeAuthToken?: () => Promise<void>;\n}\n\nconst StorefrontReactContext = createContext<StorefrontContext>({});\n\nexport const StorefrontProvider = ({\n children,\n value,\n}: {\n children: ReactNode;\n value: StorefrontContext;\n}) => {\n return (\n <StorefrontReactContext.Provider value={value}>\n {children}\n </StorefrontReactContext.Provider>\n );\n};\n\nexport const useStorefrontContext = () => useContext(StorefrontReactContext);\n"],"mappings":";;;;;;AAiBA,MAAM,yBAAyB,cAAiC,EAAE,CAAC;AAEnE,MAAa,sBAAsB,EACjC,UACA,YAII;AACJ,QACE,oBAAC,uBAAuB;EAAgB;EACrC;GAC+B;;AAItC,MAAa,6BAA6B,WAAW,uBAAuB"}
|
|
@@ -1,16 +1,18 @@
|
|
|
1
|
-
import "
|
|
1
|
+
import { StorefrontContext } from "./context.js";
|
|
2
2
|
|
|
3
3
|
//#region src/lib/data/cookies.d.ts
|
|
4
|
-
declare const getAuthHeaders: () =>
|
|
4
|
+
declare const getAuthHeaders: (ctx: StorefrontContext) => {
|
|
5
5
|
authorization: string;
|
|
6
|
-
} | {}
|
|
7
|
-
declare const getCacheTag: (tag: string) =>
|
|
8
|
-
declare const getCacheOptions: (tag: string) =>
|
|
6
|
+
} | {};
|
|
7
|
+
declare const getCacheTag: (tag: string, ctx: StorefrontContext) => string;
|
|
8
|
+
declare const getCacheOptions: (tag: string, ctx: StorefrontContext) => {
|
|
9
9
|
tags: string[];
|
|
10
|
-
} | {}
|
|
11
|
-
declare const getCartId: () =>
|
|
12
|
-
declare const setCartId: (
|
|
13
|
-
declare const removeCartId: () => Promise<void>;
|
|
10
|
+
} | {};
|
|
11
|
+
declare const getCartId: (ctx: StorefrontContext) => string | undefined;
|
|
12
|
+
declare const setCartId: (id: string, ctx: StorefrontContext) => Promise<void>;
|
|
13
|
+
declare const removeCartId: (ctx: StorefrontContext) => Promise<void>;
|
|
14
|
+
declare const setAuthToken: (token: string, ctx: StorefrontContext) => Promise<void>;
|
|
15
|
+
declare const removeAuthToken: (ctx: StorefrontContext) => Promise<void>;
|
|
14
16
|
//#endregion
|
|
15
|
-
export { getAuthHeaders, getCacheOptions, getCacheTag, getCartId, removeCartId, setCartId };
|
|
17
|
+
export { getAuthHeaders, getCacheOptions, getCacheTag, getCartId, removeAuthToken, removeCartId, setAuthToken, setCartId };
|
|
16
18
|
//# sourceMappingURL=cookies.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cookies.d.ts","names":[],"sources":["../../../src/lib/data/cookies.ts"],"sourcesContent":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"cookies.d.ts","names":[],"sources":["../../../src/lib/data/cookies.ts"],"sourcesContent":[],"mappings":";;;cAEa,sBACN;;AADP,CAAA,GAAa,CAAA,CAAA;AAYA,cAAA,WAAiC,EAAA,CAAA,GAAA,EAAA,MAAA,EAAA,GAAiB,EAAjB,iBAAiB,EAAA,GAAA,MAAA;AAUlD,cAAA,eAEN,EAAA,CAAA,GAAA,EAAA,MAAA,EAAA,GAAiB,EAAjB,iBAAiB,EAAA,GAAA;EAeX,IAAA,EAAA,MAEZ,EAAA;AAED,CAAA,GAAa,CAAA,CAAA;AAMA,cAVA,SAcZ,EAJuC,CAAA,GAAA,EAVT,iBAU0B,EAAA,GAAA,MAAA,GAAA,SAAA;AAM5C,cAZA,SAgBZ,EAJsD,CAAA,EAAA,EAAA,MAAA,EAAA,GAAA,EAZN,iBAYuB,EAAA,GAZN,OAYM,CAAA,IAAA,CAAA;AAM3D,cAZA,YAgBZ,EAJ0C,CAAA,GAAA,EAZH,iBAYoB,EAAA,GAZH,OAYG,CAAA,IAAA,CAAA;cAN/C,mCAA0C,sBAAiB;cAM3D,uBAA8B,sBAAiB"}
|
package/dist/lib/data/cookies.js
CHANGED
|
@@ -1,46 +1,36 @@
|
|
|
1
|
-
import { cookies } from "next/headers";
|
|
2
|
-
import "server-only";
|
|
3
|
-
|
|
4
1
|
//#region src/lib/data/cookies.ts
|
|
5
|
-
const getAuthHeaders =
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const cacheId = (await cookies()).get("_medusa_cache_id")?.value;
|
|
17
|
-
if (!cacheId) return "";
|
|
18
|
-
return `${tag}-${cacheId}`;
|
|
19
|
-
} catch (error) {
|
|
20
|
-
return "";
|
|
21
|
-
}
|
|
22
|
-
};
|
|
23
|
-
const getCacheOptions = async (tag) => {
|
|
2
|
+
const getAuthHeaders = (ctx) => {
|
|
3
|
+
const token = ctx.customerToken;
|
|
4
|
+
if (!token) return {};
|
|
5
|
+
return { authorization: `Bearer ${token}` };
|
|
6
|
+
};
|
|
7
|
+
const getCacheTag = (tag, ctx) => {
|
|
8
|
+
const cacheId = ctx.cacheId;
|
|
9
|
+
if (!cacheId) return "";
|
|
10
|
+
return `${tag}-${cacheId}`;
|
|
11
|
+
};
|
|
12
|
+
const getCacheOptions = (tag, ctx) => {
|
|
24
13
|
if (typeof window !== "undefined") return {};
|
|
25
|
-
const cacheTag =
|
|
14
|
+
const cacheTag = getCacheTag(tag, ctx);
|
|
26
15
|
if (!cacheTag) return {};
|
|
27
16
|
return { tags: [`${cacheTag}`] };
|
|
28
17
|
};
|
|
29
|
-
const getCartId =
|
|
30
|
-
return
|
|
18
|
+
const getCartId = (ctx) => {
|
|
19
|
+
return ctx.cartId;
|
|
20
|
+
};
|
|
21
|
+
const setCartId = async (id, ctx) => {
|
|
22
|
+
if (ctx.setCartId) await ctx.setCartId(id);
|
|
23
|
+
};
|
|
24
|
+
const removeCartId = async (ctx) => {
|
|
25
|
+
if (ctx.removeCartId) await ctx.removeCartId();
|
|
31
26
|
};
|
|
32
|
-
const
|
|
33
|
-
(await
|
|
34
|
-
maxAge: 3600 * 24 * 7,
|
|
35
|
-
httpOnly: true,
|
|
36
|
-
sameSite: "strict",
|
|
37
|
-
secure: false
|
|
38
|
-
});
|
|
27
|
+
const setAuthToken = async (token, ctx) => {
|
|
28
|
+
if (ctx.setAuthToken) await ctx.setAuthToken(token);
|
|
39
29
|
};
|
|
40
|
-
const
|
|
41
|
-
(await
|
|
30
|
+
const removeAuthToken = async (ctx) => {
|
|
31
|
+
if (ctx.removeAuthToken) await ctx.removeAuthToken();
|
|
42
32
|
};
|
|
43
33
|
|
|
44
34
|
//#endregion
|
|
45
|
-
export { getAuthHeaders, getCacheOptions, getCacheTag, getCartId, removeCartId, setCartId };
|
|
35
|
+
export { getAuthHeaders, getCacheOptions, getCacheTag, getCartId, removeAuthToken, removeCartId, setAuthToken, setCartId };
|
|
46
36
|
//# sourceMappingURL=cookies.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cookies.js","names":[
|
|
1
|
+
{"version":3,"file":"cookies.js","names":[],"sources":["../../../src/lib/data/cookies.ts"],"sourcesContent":["import type { StorefrontContext } from './context';\n\nexport const getAuthHeaders = (\n ctx: StorefrontContext\n): { authorization: string } | {} => {\n const token = ctx.customerToken;\n\n if (!token) {\n return {};\n }\n\n return { authorization: `Bearer ${token}` };\n};\n\nexport const getCacheTag = (tag: string, ctx: StorefrontContext): string => {\n const cacheId = ctx.cacheId;\n\n if (!cacheId) {\n return '';\n }\n\n return `${tag}-${cacheId}`;\n};\n\nexport const getCacheOptions = (\n tag: string,\n ctx: StorefrontContext\n): { tags: string[] } | {} => {\n if (typeof window !== 'undefined') {\n return {};\n }\n\n const cacheTag = getCacheTag(tag, ctx);\n\n if (!cacheTag) {\n return {};\n }\n\n return { tags: [`${cacheTag}`] };\n};\n\nexport const getCartId = (ctx: StorefrontContext) => {\n return ctx.cartId;\n};\n\nexport const setCartId = async (id: string, ctx: StorefrontContext) => {\n if (ctx.setCartId) {\n await ctx.setCartId(id);\n }\n};\n\nexport const removeCartId = async (ctx: StorefrontContext) => {\n if (ctx.removeCartId) {\n await ctx.removeCartId();\n }\n};\n\nexport const setAuthToken = async (token: string, ctx: StorefrontContext) => {\n if (ctx.setAuthToken) {\n await ctx.setAuthToken(token);\n }\n};\n\nexport const removeAuthToken = async (ctx: StorefrontContext) => {\n if (ctx.removeAuthToken) {\n await ctx.removeAuthToken();\n }\n};\n"],"mappings":";AAEA,MAAa,kBACX,QACmC;CACnC,MAAM,QAAQ,IAAI;AAElB,KAAI,CAAC,MACH,QAAO,EAAE;AAGX,QAAO,EAAE,eAAe,UAAU,SAAS;;AAG7C,MAAa,eAAe,KAAa,QAAmC;CAC1E,MAAM,UAAU,IAAI;AAEpB,KAAI,CAAC,QACH,QAAO;AAGT,QAAO,GAAG,IAAI,GAAG;;AAGnB,MAAa,mBACX,KACA,QAC4B;AAC5B,KAAI,OAAO,WAAW,YACpB,QAAO,EAAE;CAGX,MAAM,WAAW,YAAY,KAAK,IAAI;AAEtC,KAAI,CAAC,SACH,QAAO,EAAE;AAGX,QAAO,EAAE,MAAM,CAAC,GAAG,WAAW,EAAE;;AAGlC,MAAa,aAAa,QAA2B;AACnD,QAAO,IAAI;;AAGb,MAAa,YAAY,OAAO,IAAY,QAA2B;AACrE,KAAI,IAAI,UACN,OAAM,IAAI,UAAU,GAAG;;AAI3B,MAAa,eAAe,OAAO,QAA2B;AAC5D,KAAI,IAAI,aACN,OAAM,IAAI,cAAc;;AAI5B,MAAa,eAAe,OAAO,OAAe,QAA2B;AAC3E,KAAI,IAAI,aACN,OAAM,IAAI,aAAa,MAAM;;AAIjC,MAAa,kBAAkB,OAAO,QAA2B;AAC/D,KAAI,IAAI,gBACN,OAAM,IAAI,iBAAiB"}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { Customer, TransferCartMutation } from "../../types/graphql.js";
|
|
2
|
+
import { StorefrontContext } from "./context.js";
|
|
2
3
|
|
|
3
4
|
//#region src/lib/data/customer.d.ts
|
|
4
|
-
declare const transferCart: () => Promise<TransferCartMutation["transferCart"] | null>;
|
|
5
|
-
declare const retrieveCustomer: () => Promise<Customer | null>;
|
|
5
|
+
declare const transferCart: (ctx?: StorefrontContext) => Promise<TransferCartMutation["transferCart"] | null>;
|
|
6
|
+
declare const retrieveCustomer: (ctx?: StorefrontContext) => Promise<Customer | null>;
|
|
6
7
|
//#endregion
|
|
7
8
|
export { retrieveCustomer, transferCart };
|
|
8
9
|
//# sourceMappingURL=customer.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"customer.d.ts","names":[],"sources":["../../../src/lib/data/customer.ts"],"sourcesContent":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"customer.d.ts","names":[],"sources":["../../../src/lib/data/customer.ts"],"sourcesContent":[],"mappings":";;;;cAkBa,qBACN,sBACJ,QAAQ;cAgCE,yBACN,sBACJ,QAAQ"}
|
|
@@ -1,30 +1,31 @@
|
|
|
1
|
-
'use server';
|
|
2
|
-
|
|
3
1
|
import { createServerApolloClient, graphqlFetch, graphqlMutation } from "../gql/apollo-client.js";
|
|
4
2
|
import { TRANSFER_CART_MUTATION } from "../gql/mutations/cart.js";
|
|
3
|
+
import { GET_CUSTOMER_QUERY } from "../gql/queries/customer.js";
|
|
5
4
|
import { medusaError } from "../utils/medusa-error.js";
|
|
6
5
|
import { getCacheTag, getCartId } from "./cookies.js";
|
|
7
|
-
import { GET_CUSTOMER_QUERY } from "../gql/queries/customer.js";
|
|
8
|
-
import { revalidateTag } from "next/cache";
|
|
9
|
-
import { cookies } from "next/headers";
|
|
10
6
|
|
|
11
7
|
//#region src/lib/data/customer.ts
|
|
12
|
-
const transferCart = async () => {
|
|
13
|
-
const cartId =
|
|
8
|
+
const transferCart = async (ctx = {}) => {
|
|
9
|
+
const cartId = getCartId(ctx);
|
|
14
10
|
if (!cartId) return null;
|
|
15
11
|
try {
|
|
16
12
|
const cart = (await graphqlMutation({
|
|
17
13
|
mutation: TRANSFER_CART_MUTATION,
|
|
18
14
|
variables: { cartId }
|
|
19
15
|
}))?.transferCart ?? null;
|
|
20
|
-
if (cart
|
|
16
|
+
if (cart && ctx.revalidate) {
|
|
17
|
+
const cartCacheTag = getCacheTag("carts", ctx);
|
|
18
|
+
ctx.revalidate(cartCacheTag);
|
|
19
|
+
}
|
|
21
20
|
return cart;
|
|
22
21
|
} catch (err) {
|
|
23
22
|
medusaError(err);
|
|
23
|
+
return null;
|
|
24
24
|
}
|
|
25
25
|
};
|
|
26
|
-
const retrieveCustomer = async () => {
|
|
27
|
-
const
|
|
26
|
+
const retrieveCustomer = async (ctx = {}) => {
|
|
27
|
+
const cookieHeader = ctx.cookieHeader;
|
|
28
|
+
const apolloClient = createServerApolloClient(cookieHeader);
|
|
28
29
|
try {
|
|
29
30
|
return await graphqlFetch({
|
|
30
31
|
query: GET_CUSTOMER_QUERY,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"customer.js","names":[],"sources":["../../../src/lib/data/customer.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"file":"customer.js","names":[],"sources":["../../../src/lib/data/customer.ts"],"sourcesContent":["import {\n Customer,\n GetCustomerQuery,\n GetCustomerQueryVariables,\n TransferCartMutation,\n TransferCartMutationVariables,\n} from '../../types/graphql';\nimport {\n createServerApolloClient,\n graphqlFetch,\n graphqlMutation,\n} from '../gql/apollo-client';\nimport { TRANSFER_CART_MUTATION } from '../gql/mutations/cart';\nimport { GET_CUSTOMER_QUERY } from '../gql/queries/customer';\nimport { medusaError } from '../utils/medusa-error';\nimport { StorefrontContext } from './context';\nimport { getCacheTag, getCartId } from './cookies';\n\nexport const transferCart = async (\n ctx: StorefrontContext = {}\n): Promise<TransferCartMutation['transferCart'] | null> => {\n const cartId = getCartId(ctx);\n\n if (!cartId) {\n return null;\n }\n\n try {\n const result = await graphqlMutation<\n TransferCartMutation,\n TransferCartMutationVariables\n >({\n mutation: TRANSFER_CART_MUTATION,\n variables: {\n cartId,\n },\n });\n\n const cart = result?.transferCart ?? null;\n\n if (cart && ctx.revalidate) {\n const cartCacheTag = getCacheTag('carts', ctx);\n ctx.revalidate(cartCacheTag);\n }\n\n return cart;\n } catch (err) {\n medusaError(err);\n return null;\n }\n};\n\nexport const retrieveCustomer = async (\n ctx: StorefrontContext = {}\n): Promise<Customer | null> => {\n const cookieHeader = ctx.cookieHeader;\n const apolloClient = createServerApolloClient(cookieHeader);\n\n try {\n const customer = await graphqlFetch<\n GetCustomerQuery,\n GetCustomerQueryVariables\n >(\n {\n query: GET_CUSTOMER_QUERY,\n fetchPolicy: 'network-only',\n },\n apolloClient\n ).then((response) => response?.me ?? null);\n\n return customer;\n } catch {\n return null;\n }\n};\n"],"mappings":";;;;;;;AAkBA,MAAa,eAAe,OAC1B,MAAyB,EAAE,KAC8B;CACzD,MAAM,SAAS,UAAU,IAAI;AAE7B,KAAI,CAAC,OACH,QAAO;AAGT,KAAI;EAWF,MAAM,QAVS,MAAM,gBAGnB;GACA,UAAU;GACV,WAAW,EACT,QACD;GACF,CAAC,GAEmB,gBAAgB;AAErC,MAAI,QAAQ,IAAI,YAAY;GAC1B,MAAM,eAAe,YAAY,SAAS,IAAI;AAC9C,OAAI,WAAW,aAAa;;AAG9B,SAAO;UACA,KAAK;AACZ,cAAY,IAAI;AAChB,SAAO;;;AAIX,MAAa,mBAAmB,OAC9B,MAAyB,EAAE,KACE;CAC7B,MAAM,eAAe,IAAI;CACzB,MAAM,eAAe,yBAAyB,aAAa;AAE3D,KAAI;AAYF,SAXiB,MAAM,aAIrB;GACE,OAAO;GACP,aAAa;GACd,EACD,aACD,CAAC,MAAM,aAAa,UAAU,MAAM,KAAK;SAGpC;AACN,SAAO"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { StorefrontContext } from "./context.js";
|
|
2
|
+
|
|
3
|
+
//#region src/lib/data/next-context.d.ts
|
|
4
|
+
declare const resolveNextContext: () => Promise<StorefrontContext>;
|
|
5
|
+
declare function updateRegionAction(countryCode: string, currentPath: string): Promise<void>;
|
|
6
|
+
//#endregion
|
|
7
|
+
export { resolveNextContext, updateRegionAction };
|
|
8
|
+
//# sourceMappingURL=next-context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"next-context.d.ts","names":[],"sources":["../../../src/lib/data/next-context.ts"],"sourcesContent":[],"mappings":";;;cASa,0BAA+B,QAAQ;iBA8C9B,kBAAA,4CAED"}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { getCacheTag, getCartId } from "./cookies.js";
|
|
2
|
+
import { updateCart } from "./cart.js";
|
|
3
|
+
import { getRegion } from "./regions.js";
|
|
4
|
+
import { redirect } from "next/navigation";
|
|
5
|
+
import { revalidateTag } from "next/cache";
|
|
6
|
+
import { cookies } from "next/headers";
|
|
7
|
+
|
|
8
|
+
//#region src/lib/data/next-context.ts
|
|
9
|
+
const resolveNextContext = async () => {
|
|
10
|
+
const cookieStore = await cookies();
|
|
11
|
+
return {
|
|
12
|
+
cartId: cookieStore.get("_medusa_cart_id")?.value,
|
|
13
|
+
customerToken: cookieStore.get("_medusa_jwt")?.value,
|
|
14
|
+
cacheId: cookieStore.get("_medusa_cache_id")?.value,
|
|
15
|
+
cookieHeader: cookieStore.toString(),
|
|
16
|
+
revalidate: (tag) => revalidateTag(tag),
|
|
17
|
+
updateRegion: async (countryCode, currentPath) => {
|
|
18
|
+
"use server";
|
|
19
|
+
await updateRegionAction(countryCode, currentPath);
|
|
20
|
+
},
|
|
21
|
+
setCartId: async (id) => {
|
|
22
|
+
"use server";
|
|
23
|
+
(await cookies()).set("_medusa_cart_id", id, {
|
|
24
|
+
maxAge: 3600 * 24 * 7,
|
|
25
|
+
httpOnly: true,
|
|
26
|
+
sameSite: "strict",
|
|
27
|
+
secure: false
|
|
28
|
+
});
|
|
29
|
+
},
|
|
30
|
+
removeCartId: async () => {
|
|
31
|
+
"use server";
|
|
32
|
+
(await cookies()).set("_medusa_cart_id", "", { maxAge: -1 });
|
|
33
|
+
},
|
|
34
|
+
setAuthToken: async (token) => {
|
|
35
|
+
"use server";
|
|
36
|
+
(await cookies()).set("_medusa_jwt", token, {
|
|
37
|
+
maxAge: 3600 * 24 * 7,
|
|
38
|
+
httpOnly: true,
|
|
39
|
+
sameSite: "strict",
|
|
40
|
+
secure: false
|
|
41
|
+
});
|
|
42
|
+
},
|
|
43
|
+
removeAuthToken: async () => {
|
|
44
|
+
"use server";
|
|
45
|
+
(await cookies()).set("_medusa_jwt", "", { maxAge: -1 });
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
async function updateRegionAction(countryCode, currentPath) {
|
|
50
|
+
const ctx = await resolveNextContext();
|
|
51
|
+
const cartId = getCartId(ctx);
|
|
52
|
+
const region = await getRegion(countryCode, ctx);
|
|
53
|
+
if (!region) throw new Error(`Region not found for country code: ${countryCode}`);
|
|
54
|
+
if (cartId) {
|
|
55
|
+
await updateCart({ regionId: region.id }, ctx);
|
|
56
|
+
revalidateTag(getCacheTag("carts", ctx));
|
|
57
|
+
}
|
|
58
|
+
revalidateTag(getCacheTag("regions", ctx));
|
|
59
|
+
revalidateTag(getCacheTag("products", ctx));
|
|
60
|
+
redirect(`/${countryCode}${currentPath}`);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
//#endregion
|
|
64
|
+
export { resolveNextContext, updateRegionAction };
|
|
65
|
+
//# sourceMappingURL=next-context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"next-context.js","names":[],"sources":["../../../src/lib/data/next-context.ts"],"sourcesContent":["import { revalidateTag } from 'next/cache';\nimport { cookies } from 'next/headers';\nimport { redirect } from 'next/navigation';\n\nimport { updateCart } from './cart';\nimport { StorefrontContext } from './context';\nimport { getCacheTag, getCartId } from './cookies';\nimport { getRegion } from './regions';\n\nexport const resolveNextContext = async (): Promise<StorefrontContext> => {\n const cookieStore = await cookies();\n\n return {\n cartId: cookieStore.get('_medusa_cart_id')?.value,\n customerToken: cookieStore.get('_medusa_jwt')?.value,\n cacheId: cookieStore.get('_medusa_cache_id')?.value,\n cookieHeader: cookieStore.toString(),\n revalidate: (tag: string) => revalidateTag(tag),\n updateRegion: async (countryCode: string, currentPath: string) => {\n 'use server';\n await updateRegionAction(countryCode, currentPath);\n },\n setCartId: async (id: string) => {\n 'use server';\n const c = await cookies();\n c.set('_medusa_cart_id', id, {\n maxAge: 60 * 60 * 24 * 7,\n httpOnly: true,\n sameSite: 'strict',\n secure: process.env.NODE_ENV === 'production',\n });\n },\n removeCartId: async () => {\n 'use server';\n const c = await cookies();\n c.set('_medusa_cart_id', '', { maxAge: -1 });\n },\n setAuthToken: async (token: string) => {\n 'use server';\n const c = await cookies();\n c.set('_medusa_jwt', token, {\n maxAge: 60 * 60 * 24 * 7,\n httpOnly: true,\n sameSite: 'strict',\n secure: process.env.NODE_ENV === 'production',\n });\n },\n removeAuthToken: async () => {\n 'use server';\n const c = await cookies();\n c.set('_medusa_jwt', '', { maxAge: -1 });\n },\n };\n};\n\nexport async function updateRegionAction(\n countryCode: string,\n currentPath: string\n) {\n const ctx = await resolveNextContext();\n const cartId = getCartId(ctx);\n const region = await getRegion(countryCode, ctx);\n\n if (!region) {\n throw new Error(`Region not found for country code: ${countryCode}`);\n }\n\n if (cartId) {\n await updateCart({ regionId: region.id }, ctx);\n const cartCacheTag = getCacheTag('carts', ctx);\n revalidateTag(cartCacheTag);\n }\n\n const regionCacheTag = getCacheTag('regions', ctx);\n revalidateTag(regionCacheTag);\n\n const productsCacheTag = getCacheTag('products', ctx);\n revalidateTag(productsCacheTag);\n\n redirect(`/${countryCode}${currentPath}`);\n}\n"],"mappings":";;;;;;;;AASA,MAAa,qBAAqB,YAAwC;CACxE,MAAM,cAAc,MAAM,SAAS;AAEnC,QAAO;EACL,QAAQ,YAAY,IAAI,kBAAkB,EAAE;EAC5C,eAAe,YAAY,IAAI,cAAc,EAAE;EAC/C,SAAS,YAAY,IAAI,mBAAmB,EAAE;EAC9C,cAAc,YAAY,UAAU;EACpC,aAAa,QAAgB,cAAc,IAAI;EAC/C,cAAc,OAAO,aAAqB,gBAAwB;AAChE;AACA,SAAM,mBAAmB,aAAa,YAAY;;EAEpD,WAAW,OAAO,OAAe;AAC/B;AAEA,IADU,MAAM,SAAS,EACvB,IAAI,mBAAmB,IAAI;IAC3B,QAAQ,OAAU,KAAK;IACvB,UAAU;IACV,UAAU;IACV,QAAQ;IACT,CAAC;;EAEJ,cAAc,YAAY;AACxB;AAEA,IADU,MAAM,SAAS,EACvB,IAAI,mBAAmB,IAAI,EAAE,QAAQ,IAAI,CAAC;;EAE9C,cAAc,OAAO,UAAkB;AACrC;AAEA,IADU,MAAM,SAAS,EACvB,IAAI,eAAe,OAAO;IAC1B,QAAQ,OAAU,KAAK;IACvB,UAAU;IACV,UAAU;IACV,QAAQ;IACT,CAAC;;EAEJ,iBAAiB,YAAY;AAC3B;AAEA,IADU,MAAM,SAAS,EACvB,IAAI,eAAe,IAAI,EAAE,QAAQ,IAAI,CAAC;;EAE3C;;AAGH,eAAsB,mBACpB,aACA,aACA;CACA,MAAM,MAAM,MAAM,oBAAoB;CACtC,MAAM,SAAS,UAAU,IAAI;CAC7B,MAAM,SAAS,MAAM,UAAU,aAAa,IAAI;AAEhD,KAAI,CAAC,OACH,OAAM,IAAI,MAAM,sCAAsC,cAAc;AAGtE,KAAI,QAAQ;AACV,QAAM,WAAW,EAAE,UAAU,OAAO,IAAI,EAAE,IAAI;AAE9C,gBADqB,YAAY,SAAS,IAAI,CACnB;;AAI7B,eADuB,YAAY,WAAW,IAAI,CACrB;AAG7B,eADyB,YAAY,YAAY,IAAI,CACtB;AAE/B,UAAS,IAAI,cAAc,cAAc"}
|
package/dist/lib/data/orders.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { sdk } from "../config/medusa.js";
|
|
2
1
|
import { medusaError } from "../utils/medusa-error.js";
|
|
3
2
|
import { getAuthHeaders, getCacheOptions } from "./cookies.js";
|
|
3
|
+
import { sdk } from "../config/medusa.js";
|
|
4
4
|
import { normalizeOrder } from "../utils/normalize-functions.js";
|
|
5
5
|
|
|
6
6
|
//#region src/lib/data/orders.ts
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { Region } from "../../types/graphql.js";
|
|
2
|
+
import { StorefrontContext } from "./context.js";
|
|
2
3
|
|
|
3
4
|
//#region src/lib/data/regions.d.ts
|
|
4
|
-
declare const listRegions: () => Promise<Region[]>;
|
|
5
|
-
declare const getRegion: (countryCode: string) => Promise<Region | null>;
|
|
5
|
+
declare const listRegions: (ctx: StorefrontContext) => Promise<Region[]>;
|
|
6
|
+
declare const getRegion: (countryCode: string, ctx: StorefrontContext) => Promise<Region | null>;
|
|
6
7
|
//#endregion
|
|
7
8
|
export { getRegion, listRegions };
|
|
8
9
|
//# sourceMappingURL=regions.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"regions.d.ts","names":[],"sources":["../../../src/lib/data/regions.ts"],"sourcesContent":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"regions.d.ts","names":[],"sources":["../../../src/lib/data/regions.ts"],"sourcesContent":[],"mappings":";;;;cAWa,mBAA0B,sBAAiB,QAAA;cAe3C,sCAEN,sBACJ,QAAQ"}
|
package/dist/lib/data/regions.js
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
import { sdk } from "../config/medusa.js";
|
|
2
1
|
import { medusaError } from "../utils/medusa-error.js";
|
|
3
2
|
import { getCacheOptions } from "./cookies.js";
|
|
3
|
+
import { sdk } from "../config/medusa.js";
|
|
4
4
|
import { normalizeRegion } from "../utils/normalize-functions.js";
|
|
5
5
|
|
|
6
6
|
//#region src/lib/data/regions.ts
|
|
7
7
|
const regionMap = /* @__PURE__ */ new Map();
|
|
8
|
-
const listRegions = async () => {
|
|
9
|
-
const next = { ...
|
|
8
|
+
const listRegions = async (ctx) => {
|
|
9
|
+
const next = { ...getCacheOptions("regions", ctx) };
|
|
10
10
|
return sdk.client.fetch(`/store/regions`, {
|
|
11
11
|
method: "GET",
|
|
12
12
|
next,
|
|
13
13
|
cache: "force-cache"
|
|
14
14
|
}).then(({ regions }) => regions.map(normalizeRegion)).catch(medusaError);
|
|
15
15
|
};
|
|
16
|
-
const getRegion = async (countryCode) => {
|
|
16
|
+
const getRegion = async (countryCode, ctx) => {
|
|
17
17
|
try {
|
|
18
18
|
if (regionMap.has(countryCode)) return regionMap.get(countryCode) ?? null;
|
|
19
|
-
const regions = await listRegions();
|
|
19
|
+
const regions = await listRegions(ctx);
|
|
20
20
|
if (!regions) return null;
|
|
21
21
|
regions.forEach((region) => {
|
|
22
22
|
region.countries?.forEach((c) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"regions.js","names":["e: any"],"sources":["../../../src/lib/data/regions.ts"],"sourcesContent":["import { HttpTypes } from '@medusajs/types';\n\nimport { sdk } from '
|
|
1
|
+
{"version":3,"file":"regions.js","names":["e: any"],"sources":["../../../src/lib/data/regions.ts"],"sourcesContent":["import { HttpTypes } from '@medusajs/types';\n\nimport { Region } from '../../types/graphql';\nimport { sdk } from '../config/medusa';\nimport { medusaError } from '../utils/medusa-error';\nimport { normalizeRegion } from '../utils/normalize-functions';\nimport type { StorefrontContext } from './context';\nimport { getCacheOptions } from './cookies';\n\nconst regionMap = new Map<string, Region>();\n\nexport const listRegions = async (ctx: StorefrontContext) => {\n const next = {\n ...getCacheOptions('regions', ctx),\n };\n\n return sdk.client\n .fetch<{ regions: HttpTypes.StoreRegion[] }>(`/store/regions`, {\n method: 'GET',\n next,\n cache: 'force-cache',\n })\n .then(({ regions }) => regions.map(normalizeRegion))\n .catch(medusaError);\n};\n\nexport const getRegion = async (\n countryCode: string,\n ctx: StorefrontContext\n): Promise<Region | null> => {\n try {\n if (regionMap.has(countryCode)) {\n return regionMap.get(countryCode) ?? null;\n }\n\n const regions = await listRegions(ctx);\n\n if (!regions) return null;\n\n regions.forEach((region) => {\n region.countries?.forEach((c) => {\n regionMap.set(c?.iso2 ?? '', region);\n });\n });\n\n const region = countryCode\n ? (regionMap.get(countryCode) ?? null)\n : (regionMap.get('us') ?? null);\n\n return region;\n } catch (e: any) {\n return null;\n }\n};\n"],"mappings":";;;;;;AASA,MAAM,4BAAY,IAAI,KAAqB;AAE3C,MAAa,cAAc,OAAO,QAA2B;CAC3D,MAAM,OAAO,EACX,GAAG,gBAAgB,WAAW,IAAI,EACnC;AAED,QAAO,IAAI,OACR,MAA4C,kBAAkB;EAC7D,QAAQ;EACR;EACA,OAAO;EACR,CAAC,CACD,MAAM,EAAE,cAAc,QAAQ,IAAI,gBAAgB,CAAC,CACnD,MAAM,YAAY;;AAGvB,MAAa,YAAY,OACvB,aACA,QAC2B;AAC3B,KAAI;AACF,MAAI,UAAU,IAAI,YAAY,CAC5B,QAAO,UAAU,IAAI,YAAY,IAAI;EAGvC,MAAM,UAAU,MAAM,YAAY,IAAI;AAEtC,MAAI,CAAC,QAAS,QAAO;AAErB,UAAQ,SAAS,WAAW;AAC1B,UAAO,WAAW,SAAS,MAAM;AAC/B,cAAU,IAAI,GAAG,QAAQ,IAAI,OAAO;KACpC;IACF;AAMF,SAJe,cACV,UAAU,IAAI,YAAY,IAAI,OAC9B,UAAU,IAAI,KAAK,IAAI;UAGrBA,GAAQ;AACf,SAAO"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as _apollo_client11 from "@apollo/client";
|
|
2
2
|
|
|
3
3
|
//#region src/lib/gql/fragments/customer.d.ts
|
|
4
|
-
declare const CUSTOMER_ADDRESS_FRAGMENT:
|
|
5
|
-
declare const CUSTOMER_FRAGMENT:
|
|
4
|
+
declare const CUSTOMER_ADDRESS_FRAGMENT: _apollo_client11.DocumentNode;
|
|
5
|
+
declare const CUSTOMER_FRAGMENT: _apollo_client11.DocumentNode;
|
|
6
6
|
//#endregion
|
|
7
7
|
export { CUSTOMER_ADDRESS_FRAGMENT, CUSTOMER_FRAGMENT };
|
|
8
8
|
//# sourceMappingURL=customer.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"customer.d.ts","names":[],"sources":["../../../../src/lib/gql/fragments/customer.ts"],"sourcesContent":[],"mappings":";;;cAEa,2BAkBZ,
|
|
1
|
+
{"version":3,"file":"customer.d.ts","names":[],"sources":["../../../../src/lib/gql/fragments/customer.ts"],"sourcesContent":[],"mappings":";;;cAEa,2BAkBZ,gBAAA,CAlBqC;cAoBzB,mBAeZ,gBAAA,CAf6B"}
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as _apollo_client19 from "@apollo/client";
|
|
2
2
|
|
|
3
3
|
//#region src/lib/gql/fragments/product.d.ts
|
|
4
|
-
declare const PRICE_FRAGMENT:
|
|
5
|
-
declare const PRODUCT_VARIANT_FRAGMENT:
|
|
6
|
-
declare const PRODUCT_HIT_FRAGMENT:
|
|
7
|
-
declare const PRODUCT_CATEGORY_FRAGMENT:
|
|
8
|
-
declare const PRODUCT_COLLECTION_FRAGMENT:
|
|
9
|
-
declare const PRODUCT_IMAGE_FRAGMENT:
|
|
10
|
-
declare const COLLECTION_PRODUCTS_FRAGMENT:
|
|
4
|
+
declare const PRICE_FRAGMENT: _apollo_client19.DocumentNode;
|
|
5
|
+
declare const PRODUCT_VARIANT_FRAGMENT: _apollo_client19.DocumentNode;
|
|
6
|
+
declare const PRODUCT_HIT_FRAGMENT: _apollo_client19.DocumentNode;
|
|
7
|
+
declare const PRODUCT_CATEGORY_FRAGMENT: _apollo_client19.DocumentNode;
|
|
8
|
+
declare const PRODUCT_COLLECTION_FRAGMENT: _apollo_client19.DocumentNode;
|
|
9
|
+
declare const PRODUCT_IMAGE_FRAGMENT: _apollo_client19.DocumentNode;
|
|
10
|
+
declare const COLLECTION_PRODUCTS_FRAGMENT: _apollo_client19.DocumentNode;
|
|
11
11
|
//#endregion
|
|
12
12
|
export { COLLECTION_PRODUCTS_FRAGMENT, PRICE_FRAGMENT, PRODUCT_CATEGORY_FRAGMENT, PRODUCT_COLLECTION_FRAGMENT, PRODUCT_HIT_FRAGMENT, PRODUCT_IMAGE_FRAGMENT, PRODUCT_VARIANT_FRAGMENT };
|
|
13
13
|
//# sourceMappingURL=product.d.ts.map
|