@gfed-medusa/sf-lib-common 2.0.0 → 2.2.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 +2 -2
- 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.d.ts.map +1 -1
- package/dist/components/cart-mismatch-banner/index.js +4 -2
- package/dist/components/cart-mismatch-banner/index.js.map +1 -1
- package/dist/components/delete-button/index.d.ts +2 -2
- package/dist/components/delete-button/index.d.ts.map +1 -1
- package/dist/components/delete-button/index.js +4 -2
- package/dist/components/delete-button/index.js.map +1 -1
- package/dist/components/error-message/index.d.ts +2 -2
- 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.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/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 +16 -10
- package/dist/lib/data/cookies.d.ts.map +1 -1
- package/dist/lib/data/cookies.js +46 -30
- 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 +42 -0
- package/dist/lib/data/next-context.js.map +1 -0
- package/dist/lib/data/orders.d.ts +2 -1
- package/dist/lib/data/orders.d.ts.map +1 -1
- package/dist/lib/data/orders.js +4 -4
- package/dist/lib/data/orders.js.map +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/cart.d.ts +9 -9
- package/dist/lib/gql/fragments/customer.d.ts +3 -3
- 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/hooks/use-apollo.d.ts +2 -2
- package/dist/lib/utils/env.js +1 -1
- package/dist/lib/utils/env.js.map +1 -1
- package/package.json +3 -2
- 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
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"modal-context.d.ts","names":[],"sources":["../../../src/lib/context/modal-context.tsx"],"sourcesContent":[],"mappings":";;;;UAIU,YAAA;;;AAF+C,cAMnD,YAJgB,EAIJ,KAAA,CAAA,OAJI,CAIJ,YAJI,GAAA,IAAA,CAAA;AAAA,UAMZ,kBAAA,CAFmD;EAEnD,QAAA,CAAA,EACG,KAAA,CAAM,SADS;EAKtB,KAAA,EAAA,GAAA,GAAA,IAUL;;cAVK,aAAiB,EAAA,CAAA;EAAA,QAAA;EAAA;AAAA,CAAA,EAAqB,kBAArB,EAAA,GAAuC,
|
|
1
|
+
{"version":3,"file":"modal-context.d.ts","names":[],"sources":["../../../src/lib/context/modal-context.tsx"],"sourcesContent":[],"mappings":";;;;UAIU,YAAA;;;AAF+C,cAMnD,YAJgB,EAIJ,KAAA,CAAA,OAJI,CAIJ,YAJI,GAAA,IAAA,CAAA;AAAA,UAMZ,kBAAA,CAFmD;EAEnD,QAAA,CAAA,EACG,KAAA,CAAM,SADS;EAKtB,KAAA,EAAA,GAAA,GAAA,IAUL;;cAVK,aAAiB,EAAA,CAAA;EAAA,QAAA;EAAA;AAAA,CAAA,EAAqB,kBAArB,EAAA,GAAuC,kBAAA,CAAA,GAAA,CAAA,OAAvC;cAYjB,QAZsC,EAAA,GAAA,GAY9B,YAZ8B"}
|
package/dist/lib/data/cart.d.ts
CHANGED
|
@@ -1,24 +1,14 @@
|
|
|
1
|
-
import { Cart, DeleteLineItemMutation } from "../../types/graphql.js";
|
|
1
|
+
import { Cart, DeleteLineItemMutation, UpdateCartMutation, UpdateCartMutationVariables } from "../../types/graphql.js";
|
|
2
|
+
import { StorefrontContext } from "./context.js";
|
|
2
3
|
import { HttpTypes } from "@medusajs/types";
|
|
3
4
|
|
|
4
5
|
//#region src/lib/data/cart.d.ts
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
* @returns The cart object if found, or null if not found.
|
|
10
|
-
*/
|
|
11
|
-
declare const retrieveCart: (cartId?: string) => Promise<Cart | null>;
|
|
12
|
-
declare const deleteLineItem: (lineId: string) => Promise<DeleteLineItemMutation["deleteLineItem"] | null>;
|
|
13
|
-
/**
|
|
14
|
-
* Updates the countrycode param and revalidates the regions cache
|
|
15
|
-
* @param regionId
|
|
16
|
-
* @param countryCode
|
|
17
|
-
*/
|
|
18
|
-
declare function updateRegion(countryCode: string, currentPath: string): Promise<void>;
|
|
19
|
-
declare function listCartOptions(): Promise<{
|
|
6
|
+
declare const retrieveCart: (ctx: StorefrontContext) => Promise<Cart | null>;
|
|
7
|
+
declare const updateCart: (data: UpdateCartMutationVariables["data"], ctx: StorefrontContext) => Promise<UpdateCartMutation["updateCart"] | null>;
|
|
8
|
+
declare const deleteLineItem: (lineId: string, ctx: StorefrontContext) => Promise<DeleteLineItemMutation["deleteLineItem"] | null>;
|
|
9
|
+
declare function listCartOptions(ctx: StorefrontContext): Promise<{
|
|
20
10
|
shipping_options: HttpTypes.StoreCartShippingOption[];
|
|
21
11
|
}>;
|
|
22
12
|
//#endregion
|
|
23
|
-
export { deleteLineItem, listCartOptions, retrieveCart,
|
|
13
|
+
export { deleteLineItem, listCartOptions, retrieveCart, updateCart };
|
|
24
14
|
//# sourceMappingURL=cart.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cart.d.ts","names":[],"sources":["../../../src/lib/data/cart.ts"],"sourcesContent":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"cart.d.ts","names":[],"sources":["../../../src/lib/data/cart.ts"],"sourcesContent":[],"mappings":";;;;;cA2Ba,oBACN,sBACJ,QAAQ;cAmBE,mBACL,0CACD,sBACJ,QAAQ;AAxBE,cA6DA,cA1CZ,EAAA,CAAA,MAAA,EAAA,MAAA,EAAA,GAAA,EA4CM,iBA5CN,EAAA,GA6CE,OA7CF,CA6CU,sBA7CV,CAAA,gBAAA,CAAA,GAAA,IAAA,CAAA;AAlBM,iBAuGe,eAAA,CAvGf,GAAA,EAuGoC,iBAvGpC,CAAA,EAuGqD,OAvGrD,CAAA;EACI,gBAAA,EAgHW,SAAA,CAAU,uBAhHrB,EAAA;CAAR,CAAA"}
|
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_runtime1 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_runtime1.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,EAAA,MAAA;EAM6C,YAAA,EAAA,MAAA;EACjC,UAAA,EAAA,CAAA,GAAA,EAAA,MAAA,EAAA,GAAA,IAAA;EACP,YAAA,EAAA,CAAA,WAAA,EAAA,MAAA,EAAA,WAAA,EAAA,MAAA,EAAA,GAFwC,OAExC,CAAA,IAAA,CAAA;EACa,SAAA,EAAA,CAAA,EAAA,EAAA,MAAA,EAAA,GAFN,OAEM,CAAA,IAAA,CAAA;EACV,YAAA,EAAA,GAAA,GAFH,OAEG,CAAA,IAAA,CAAA;EAAO,YAAA,EAAA,CAAA,KAAA,EAAA,MAAA,EAAA,GADG,OACH,CAAA,IAAA,CAAA;EAOnB,eAAA,EAAA,GAAA,GAPY,OAmBxB,CAAA,IAAA,CAAA;;AAZkC,cAAtB,kBAAsB,EAAA,CAAA;EAAA,QAAA;EAAA;CAAA,EAAA;EAIvB,QAAA,EAAA,SAAA;EACH,KAAA,EAAA,iBAAA;CACR,EAAA,GAAA,kBAAA,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 {} as StorefrontContext\n);\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,cAC7B,EAAE,CACH;AAED,MAAa,sBAAsB,EACjC,UACA,YAII;AACJ,QACE,oBAAC,uBAAuB;EAAgB;EACrC;GAC+B;;AAItC,MAAa,6BAA6B,WAAW,uBAAuB"}
|
|
@@ -1,16 +1,22 @@
|
|
|
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
|
|
13
|
-
declare
|
|
10
|
+
} | {};
|
|
11
|
+
declare const getCartId: (ctx: StorefrontContext) => string;
|
|
12
|
+
declare function setCartIdAction(id: string): Promise<void>;
|
|
13
|
+
declare function removeCartIdAction(): Promise<void>;
|
|
14
|
+
declare function setAuthTokenAction(token: string): Promise<void>;
|
|
15
|
+
declare function removeAuthTokenAction(): Promise<void>;
|
|
16
|
+
declare const setCartId: (id: string, ctx: StorefrontContext) => Promise<void>;
|
|
17
|
+
declare const removeCartId: (ctx: StorefrontContext) => Promise<void>;
|
|
18
|
+
declare const setAuthToken: (token: string, ctx: StorefrontContext) => Promise<void>;
|
|
19
|
+
declare const removeAuthToken: (ctx: StorefrontContext) => Promise<void>;
|
|
14
20
|
//#endregion
|
|
15
|
-
export { getAuthHeaders, getCacheOptions, getCacheTag, getCartId, removeCartId, setCartId };
|
|
21
|
+
export { getAuthHeaders, getCacheOptions, getCacheTag, getCartId, removeAuthToken, removeAuthTokenAction, removeCartId, removeCartIdAction, setAuthToken, setAuthTokenAction, setCartId, setCartIdAction };
|
|
16
22
|
//# 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,GAAsB,CAAA,CAAA;AAWA,cAfT,SAe2B,EAAA,CAAA,GAAA,EAfT,iBAeS,EAAA,GAAA,MAAA;AAMlB,iBAjBA,eAAA,CAiBgC,EAAA,EAAA,MAAA,CAAA,EAjBN,OAiBM,CAAA,IAAA,CAAA;AAWhC,iBAjBA,kBAAA,CAAA,CAiBqB,EAjBH,OAiBG,CAAA,IAAA,CAAA;AAM9B,iBAjBS,kBAAA,CAiB2B,KAAiB,EAAA,MAAA,CAAA,EAjBZ,OAiBY,CAAA,IAAA,CAAA;AAMrD,iBAZS,qBAAA,CAAA,CAYkB,EAZG,OAYc,CAAA,IAAA,CAAA;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,62 @@
|
|
|
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;
|
|
31
20
|
};
|
|
32
|
-
|
|
33
|
-
|
|
21
|
+
async function setCartIdAction(id) {
|
|
22
|
+
const { cookies } = await import("next/headers");
|
|
23
|
+
(await cookies()).set("_medusa_cart_id", id, {
|
|
34
24
|
maxAge: 3600 * 24 * 7,
|
|
35
25
|
httpOnly: true,
|
|
36
26
|
sameSite: "strict",
|
|
37
27
|
secure: false
|
|
38
28
|
});
|
|
39
|
-
}
|
|
40
|
-
|
|
29
|
+
}
|
|
30
|
+
async function removeCartIdAction() {
|
|
31
|
+
const { cookies } = await import("next/headers");
|
|
41
32
|
(await cookies()).set("_medusa_cart_id", "", { maxAge: -1 });
|
|
33
|
+
}
|
|
34
|
+
async function setAuthTokenAction(token) {
|
|
35
|
+
const { cookies } = await import("next/headers");
|
|
36
|
+
(await cookies()).set("_medusa_jwt", token, {
|
|
37
|
+
maxAge: 3600 * 24 * 7,
|
|
38
|
+
httpOnly: true,
|
|
39
|
+
sameSite: "strict",
|
|
40
|
+
secure: false
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
async function removeAuthTokenAction() {
|
|
44
|
+
const { cookies } = await import("next/headers");
|
|
45
|
+
(await cookies()).set("_medusa_jwt", "", { maxAge: -1 });
|
|
46
|
+
}
|
|
47
|
+
const setCartId = async (id, ctx) => {
|
|
48
|
+
if (ctx.setCartId) await ctx.setCartId(id);
|
|
49
|
+
};
|
|
50
|
+
const removeCartId = async (ctx) => {
|
|
51
|
+
if (ctx.removeCartId) await ctx.removeCartId();
|
|
52
|
+
};
|
|
53
|
+
const setAuthToken = async (token, ctx) => {
|
|
54
|
+
if (ctx.setAuthToken) await ctx.setAuthToken(token);
|
|
55
|
+
};
|
|
56
|
+
const removeAuthToken = async (ctx) => {
|
|
57
|
+
if (ctx.removeAuthToken) await ctx.removeAuthToken();
|
|
42
58
|
};
|
|
43
59
|
|
|
44
60
|
//#endregion
|
|
45
|
-
export { getAuthHeaders, getCacheOptions, getCacheTag, getCartId, removeCartId, setCartId };
|
|
61
|
+
export { getAuthHeaders, getCacheOptions, getCacheTag, getCartId, removeAuthToken, removeAuthTokenAction, removeCartId, removeCartIdAction, setAuthToken, setAuthTokenAction, setCartId, setCartIdAction };
|
|
46
62
|
//# 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 async function setCartIdAction(id: string) {\n const { cookies } = await import('next/headers');\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\nexport async function removeCartIdAction() {\n const { cookies } = await import('next/headers');\n const c = await cookies();\n c.set('_medusa_cart_id', '', { maxAge: -1 });\n}\n\nexport async function setAuthTokenAction(token: string) {\n const { cookies } = await import('next/headers');\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\nexport async function removeAuthTokenAction() {\n const { cookies } = await import('next/headers');\n const c = await cookies();\n c.set('_medusa_jwt', '', { maxAge: -1 });\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,eAAsB,gBAAgB,IAAY;CAChD,MAAM,EAAE,YAAY,MAAM,OAAO;AAEjC,EADU,MAAM,SAAS,EACvB,IAAI,mBAAmB,IAAI;EAC3B,QAAQ,OAAU,KAAK;EACvB,UAAU;EACV,UAAU;EACV,QAAQ;EACT,CAAC;;AAGJ,eAAsB,qBAAqB;CACzC,MAAM,EAAE,YAAY,MAAM,OAAO;AAEjC,EADU,MAAM,SAAS,EACvB,IAAI,mBAAmB,IAAI,EAAE,QAAQ,IAAI,CAAC;;AAG9C,eAAsB,mBAAmB,OAAe;CACtD,MAAM,EAAE,YAAY,MAAM,OAAO;AAEjC,EADU,MAAM,SAAS,EACvB,IAAI,eAAe,OAAO;EAC1B,QAAQ,OAAU,KAAK;EACvB,UAAU;EACV,UAAU;EACV,QAAQ;EACT,CAAC;;AAGJ,eAAsB,wBAAwB;CAC5C,MAAM,EAAE,YAAY,MAAM,OAAO;AAEjC,EADU,MAAM,SAAS,EACvB,IAAI,eAAe,IAAI,EAAE,QAAQ,IAAI,CAAC;;AAG1C,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,oBACN,sBACJ,QAAQ;cAgCE,wBACN,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 type { 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,QACyD;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,QAC6B;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":";;;cAkBa,0BAA+B,QAAQ;iBAiB9B,kBAAA,4CAED"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
'use server';
|
|
2
|
+
|
|
3
|
+
import { getCacheTag, getCartId, removeAuthTokenAction, removeCartIdAction, setAuthTokenAction, setCartIdAction } from "./cookies.js";
|
|
4
|
+
import { updateCart } from "./cart.js";
|
|
5
|
+
import { getRegion } from "./regions.js";
|
|
6
|
+
import { redirect } from "next/navigation";
|
|
7
|
+
import { revalidateTag } from "next/cache";
|
|
8
|
+
import { cookies } from "next/headers";
|
|
9
|
+
|
|
10
|
+
//#region src/lib/data/next-context.ts
|
|
11
|
+
const resolveNextContext = async () => {
|
|
12
|
+
const cookieStore = await cookies();
|
|
13
|
+
return {
|
|
14
|
+
cartId: cookieStore.get("_medusa_cart_id")?.value ?? "",
|
|
15
|
+
customerToken: cookieStore.get("_medusa_jwt")?.value ?? "",
|
|
16
|
+
cacheId: cookieStore.get("_medusa_cache_id")?.value ?? "",
|
|
17
|
+
cookieHeader: cookieStore.toString(),
|
|
18
|
+
revalidate: (tag) => revalidateTag(tag),
|
|
19
|
+
updateRegion: updateRegionAction,
|
|
20
|
+
setCartId: setCartIdAction,
|
|
21
|
+
removeCartId: removeCartIdAction,
|
|
22
|
+
setAuthToken: setAuthTokenAction,
|
|
23
|
+
removeAuthToken: removeAuthTokenAction
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
async function updateRegionAction(countryCode, currentPath) {
|
|
27
|
+
const ctx = await resolveNextContext();
|
|
28
|
+
const cartId = getCartId(ctx);
|
|
29
|
+
const region = await getRegion(countryCode, ctx);
|
|
30
|
+
if (!region) throw new Error(`Region not found for country code: ${countryCode}`);
|
|
31
|
+
if (cartId) {
|
|
32
|
+
await updateCart({ regionId: region.id }, ctx);
|
|
33
|
+
revalidateTag(getCacheTag("carts", ctx));
|
|
34
|
+
}
|
|
35
|
+
revalidateTag(getCacheTag("regions", ctx));
|
|
36
|
+
revalidateTag(getCacheTag("products", ctx));
|
|
37
|
+
redirect(`/${countryCode}${currentPath}`);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
//#endregion
|
|
41
|
+
export { resolveNextContext, updateRegionAction };
|
|
42
|
+
//# sourceMappingURL=next-context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"next-context.js","names":[],"sources":["../../../src/lib/data/next-context.ts"],"sourcesContent":["'use server';\n\nimport { revalidateTag } from 'next/cache';\nimport { cookies } from 'next/headers';\nimport { redirect } from 'next/navigation';\n\nimport { updateCart } from './cart';\nimport { StorefrontContext } from './context';\nimport {\n getCacheTag,\n getCartId,\n removeAuthTokenAction,\n removeCartIdAction,\n setAuthTokenAction,\n setCartIdAction,\n} 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: updateRegionAction,\n setCartId: setCartIdAction,\n removeCartId: removeCartIdAction,\n setAuthToken: setAuthTokenAction,\n removeAuthToken: removeAuthTokenAction,\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":";;;;;;;;;;AAkBA,MAAa,qBAAqB,YAAwC;CACxE,MAAM,cAAc,MAAM,SAAS;AAEnC,QAAO;EACL,QAAQ,YAAY,IAAI,kBAAkB,EAAE,SAAS;EACrD,eAAe,YAAY,IAAI,cAAc,EAAE,SAAS;EACxD,SAAS,YAAY,IAAI,mBAAmB,EAAE,SAAS;EACvD,cAAc,YAAY,UAAU;EACpC,aAAa,QAAgB,cAAc,IAAI;EAC/C,cAAc;EACd,WAAW;EACX,cAAc;EACd,cAAc;EACd,iBAAiB;EAClB;;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"}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { Order } from "../../types/graphql.js";
|
|
2
|
+
import { StorefrontContext } from "./context.js";
|
|
2
3
|
|
|
3
4
|
//#region src/lib/data/orders.d.ts
|
|
4
|
-
declare const retrieveOrder: (id: string) => Promise<Order>;
|
|
5
|
+
declare const retrieveOrder: (id: string, ctx: StorefrontContext) => Promise<Order>;
|
|
5
6
|
//#endregion
|
|
6
7
|
export { retrieveOrder };
|
|
7
8
|
//# sourceMappingURL=orders.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"orders.d.ts","names":[],"sources":["../../../src/lib/data/orders.ts"],"sourcesContent":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"orders.d.ts","names":[],"sources":["../../../src/lib/data/orders.ts"],"sourcesContent":[],"mappings":";;;;cAQa,iCAAwC,sBAAiB,QAAA,KAAA"}
|
package/dist/lib/data/orders.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
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
|
|
7
|
-
const retrieveOrder = async (id) => {
|
|
8
|
-
const headers = { ...
|
|
9
|
-
const next = { ...
|
|
7
|
+
const retrieveOrder = async (id, ctx) => {
|
|
8
|
+
const headers = { ...getAuthHeaders(ctx) };
|
|
9
|
+
const next = { ...getCacheOptions("orders", ctx) };
|
|
10
10
|
return sdk.client.fetch(`/store/orders/${id}`, {
|
|
11
11
|
method: "GET",
|
|
12
12
|
query: { fields: "*payment_collections.payments,*items,*items.metadata,*items.variant,*items.product" },
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"orders.js","names":[],"sources":["../../../src/lib/data/orders.ts"],"sourcesContent":["import { HttpTypes } from '@medusajs/types';\n\nimport { sdk } from '../config/medusa';\nimport { medusaError } from '../utils/medusa-error';\nimport { normalizeOrder } from '../utils/normalize-functions';\nimport { getAuthHeaders, getCacheOptions } from './cookies';\n\nexport const retrieveOrder = async (id: string) => {\n const headers = {\n ...
|
|
1
|
+
{"version":3,"file":"orders.js","names":[],"sources":["../../../src/lib/data/orders.ts"],"sourcesContent":["import { HttpTypes } from '@medusajs/types';\n\nimport { sdk } from '../config/medusa';\nimport { medusaError } from '../utils/medusa-error';\nimport { normalizeOrder } from '../utils/normalize-functions';\nimport { StorefrontContext } from './context';\nimport { getAuthHeaders, getCacheOptions } from './cookies';\n\nexport const retrieveOrder = async (id: string, ctx: StorefrontContext) => {\n const headers = {\n ...getAuthHeaders(ctx),\n };\n\n const next = {\n ...getCacheOptions('orders', ctx),\n };\n\n return sdk.client\n .fetch<HttpTypes.StoreOrderResponse>(`/store/orders/${id}`, {\n method: 'GET',\n query: {\n fields:\n '*payment_collections.payments,*items,*items.metadata,*items.variant,*items.product',\n },\n headers,\n next,\n cache: 'force-cache',\n })\n .then(({ order }) => order && normalizeOrder(order))\n .catch((err) => medusaError(err));\n};\n"],"mappings":";;;;;;AAQA,MAAa,gBAAgB,OAAO,IAAY,QAA2B;CACzE,MAAM,UAAU,EACd,GAAG,eAAe,IAAI,EACvB;CAED,MAAM,OAAO,EACX,GAAG,gBAAgB,UAAU,IAAI,EAClC;AAED,QAAO,IAAI,OACR,MAAoC,iBAAiB,MAAM;EAC1D,QAAQ;EACR,OAAO,EACL,QACE,sFACH;EACD;EACA;EACA,OAAO;EACR,CAAC,CACD,MAAM,EAAE,YAAY,SAAS,eAAe,MAAM,CAAC,CACnD,OAAO,QAAQ,YAAY,IAAI,CAAC"}
|
|
@@ -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"}
|