@lime-bundles/react 0.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/hooks/useCart.cjs +84 -0
- package/dist/hooks/useCart.cjs.map +1 -0
- package/dist/hooks/useCart.d.cts +19 -0
- package/dist/hooks/useCart.d.ts +19 -0
- package/dist/hooks/useCart.js +64 -0
- package/dist/hooks/useCart.js.map +1 -0
- package/dist/index.cjs +689 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +60 -0
- package/dist/index.d.ts +60 -0
- package/dist/index.js +677 -0
- package/dist/index.js.map +1 -0
- package/package.json +68 -0
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/hooks/useCart.ts
|
|
21
|
+
var useCart_exports = {};
|
|
22
|
+
__export(useCart_exports, {
|
|
23
|
+
useCart: () => useCart
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(useCart_exports);
|
|
26
|
+
var import_react = require("react");
|
|
27
|
+
var import_core = require("@lime-bundles/core");
|
|
28
|
+
function useCart(options) {
|
|
29
|
+
const [loading, setLoading] = (0, import_react.useState)(false);
|
|
30
|
+
const [error, setError] = (0, import_react.useState)(null);
|
|
31
|
+
const [lastResult, setLastResult] = (0, import_react.useState)(null);
|
|
32
|
+
const cart = (0, import_react.useMemo)(() => {
|
|
33
|
+
const apiType = options.cartApi ?? (0, import_core.detectCartApi)();
|
|
34
|
+
if (apiType === "ajax") {
|
|
35
|
+
return (0, import_core.createAjaxCartApi)(options.bundleGid, options.bundleType);
|
|
36
|
+
}
|
|
37
|
+
const client = (0, import_core.createStorefrontClient)({
|
|
38
|
+
shopDomain: options.shopDomain,
|
|
39
|
+
accessToken: options.storefrontAccessToken
|
|
40
|
+
});
|
|
41
|
+
return (0, import_core.createStorefrontCartApi)(
|
|
42
|
+
client,
|
|
43
|
+
options.bundleGid,
|
|
44
|
+
options.bundleType,
|
|
45
|
+
options.cartId
|
|
46
|
+
);
|
|
47
|
+
}, [
|
|
48
|
+
options.shopDomain,
|
|
49
|
+
options.storefrontAccessToken,
|
|
50
|
+
options.bundleGid,
|
|
51
|
+
options.bundleType,
|
|
52
|
+
options.cartId,
|
|
53
|
+
options.cartApi
|
|
54
|
+
]);
|
|
55
|
+
const addToCart = (0, import_react.useCallback)(
|
|
56
|
+
async (items) => {
|
|
57
|
+
setLoading(true);
|
|
58
|
+
setError(null);
|
|
59
|
+
try {
|
|
60
|
+
const result = await cart.addLines(items);
|
|
61
|
+
setLastResult(result);
|
|
62
|
+
if (!result.success) {
|
|
63
|
+
setError(result.error ?? "Failed to add to cart");
|
|
64
|
+
}
|
|
65
|
+
return result;
|
|
66
|
+
} catch (err) {
|
|
67
|
+
const message = err instanceof Error ? err.message : "Failed to add to cart";
|
|
68
|
+
setError(message);
|
|
69
|
+
const result = { success: false, error: message };
|
|
70
|
+
setLastResult(result);
|
|
71
|
+
return result;
|
|
72
|
+
} finally {
|
|
73
|
+
setLoading(false);
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
[cart]
|
|
77
|
+
);
|
|
78
|
+
return { addToCart, loading, error, lastResult };
|
|
79
|
+
}
|
|
80
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
81
|
+
0 && (module.exports = {
|
|
82
|
+
useCart
|
|
83
|
+
});
|
|
84
|
+
//# sourceMappingURL=useCart.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/hooks/useCart.ts"],"sourcesContent":["/**\n * Hook for cart API operations.\n * Auto-detects AJAX vs Storefront Cart API.\n */\nimport { useState, useCallback, useMemo } from \"react\";\nimport {\n detectCartApi,\n createAjaxCartApi,\n createStorefrontCartApi,\n createStorefrontClient,\n type CartApi,\n type CartLineItem,\n type AddToCartResult,\n} from \"@lime-bundles/core\";\n\nexport interface UseCartOptions {\n shopDomain: string;\n storefrontAccessToken: string;\n bundleGid: string;\n bundleType: string;\n cartId?: string;\n cartApi?: \"ajax\" | \"storefront\"; // Manual override\n}\n\nexport interface UseCartResult {\n addToCart: (items: CartLineItem[]) => Promise<AddToCartResult>;\n loading: boolean;\n error: string | null;\n lastResult: AddToCartResult | null;\n}\n\nexport function useCart(options: UseCartOptions): UseCartResult {\n const [loading, setLoading] = useState(false);\n const [error, setError] = useState<string | null>(null);\n const [lastResult, setLastResult] = useState<AddToCartResult | null>(null);\n\n const cart: CartApi = useMemo(() => {\n const apiType = options.cartApi ?? detectCartApi();\n\n if (apiType === \"ajax\") {\n return createAjaxCartApi(options.bundleGid, options.bundleType);\n }\n\n const client = createStorefrontClient({\n shopDomain: options.shopDomain,\n accessToken: options.storefrontAccessToken,\n });\n return createStorefrontCartApi(\n client,\n options.bundleGid,\n options.bundleType,\n options.cartId,\n );\n }, [\n options.shopDomain,\n options.storefrontAccessToken,\n options.bundleGid,\n options.bundleType,\n options.cartId,\n options.cartApi,\n ]);\n\n const addToCart = useCallback(\n async (items: CartLineItem[]): Promise<AddToCartResult> => {\n setLoading(true);\n setError(null);\n try {\n const result = await cart.addLines(items);\n setLastResult(result);\n if (!result.success) {\n setError(result.error ?? \"Failed to add to cart\");\n }\n return result;\n } catch (err) {\n const message =\n err instanceof Error ? err.message : \"Failed to add to cart\";\n setError(message);\n const result: AddToCartResult = { success: false, error: message };\n setLastResult(result);\n return result;\n } finally {\n setLoading(false);\n }\n },\n [cart],\n );\n\n return { addToCart, loading, error, lastResult };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAIA,mBAA+C;AAC/C,kBAQO;AAkBA,SAAS,QAAQ,SAAwC;AAC9D,QAAM,CAAC,SAAS,UAAU,QAAI,uBAAS,KAAK;AAC5C,QAAM,CAAC,OAAO,QAAQ,QAAI,uBAAwB,IAAI;AACtD,QAAM,CAAC,YAAY,aAAa,QAAI,uBAAiC,IAAI;AAEzE,QAAM,WAAgB,sBAAQ,MAAM;AAClC,UAAM,UAAU,QAAQ,eAAW,2BAAc;AAEjD,QAAI,YAAY,QAAQ;AACtB,iBAAO,+BAAkB,QAAQ,WAAW,QAAQ,UAAU;AAAA,IAChE;AAEA,UAAM,aAAS,oCAAuB;AAAA,MACpC,YAAY,QAAQ;AAAA,MACpB,aAAa,QAAQ;AAAA,IACvB,CAAC;AACD,eAAO;AAAA,MACL;AAAA,MACA,QAAQ;AAAA,MACR,QAAQ;AAAA,MACR,QAAQ;AAAA,IACV;AAAA,EACF,GAAG;AAAA,IACD,QAAQ;AAAA,IACR,QAAQ;AAAA,IACR,QAAQ;AAAA,IACR,QAAQ;AAAA,IACR,QAAQ;AAAA,IACR,QAAQ;AAAA,EACV,CAAC;AAED,QAAM,gBAAY;AAAA,IAChB,OAAO,UAAoD;AACzD,iBAAW,IAAI;AACf,eAAS,IAAI;AACb,UAAI;AACF,cAAM,SAAS,MAAM,KAAK,SAAS,KAAK;AACxC,sBAAc,MAAM;AACpB,YAAI,CAAC,OAAO,SAAS;AACnB,mBAAS,OAAO,SAAS,uBAAuB;AAAA,QAClD;AACA,eAAO;AAAA,MACT,SAAS,KAAK;AACZ,cAAM,UACJ,eAAe,QAAQ,IAAI,UAAU;AACvC,iBAAS,OAAO;AAChB,cAAM,SAA0B,EAAE,SAAS,OAAO,OAAO,QAAQ;AACjE,sBAAc,MAAM;AACpB,eAAO;AAAA,MACT,UAAE;AACA,mBAAW,KAAK;AAAA,MAClB;AAAA,IACF;AAAA,IACA,CAAC,IAAI;AAAA,EACP;AAEA,SAAO,EAAE,WAAW,SAAS,OAAO,WAAW;AACjD;","names":[]}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { CartLineItem, AddToCartResult } from '@lime-bundles/core';
|
|
2
|
+
|
|
3
|
+
interface UseCartOptions {
|
|
4
|
+
shopDomain: string;
|
|
5
|
+
storefrontAccessToken: string;
|
|
6
|
+
bundleGid: string;
|
|
7
|
+
bundleType: string;
|
|
8
|
+
cartId?: string;
|
|
9
|
+
cartApi?: "ajax" | "storefront";
|
|
10
|
+
}
|
|
11
|
+
interface UseCartResult {
|
|
12
|
+
addToCart: (items: CartLineItem[]) => Promise<AddToCartResult>;
|
|
13
|
+
loading: boolean;
|
|
14
|
+
error: string | null;
|
|
15
|
+
lastResult: AddToCartResult | null;
|
|
16
|
+
}
|
|
17
|
+
declare function useCart(options: UseCartOptions): UseCartResult;
|
|
18
|
+
|
|
19
|
+
export { type UseCartOptions, type UseCartResult, useCart };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { CartLineItem, AddToCartResult } from '@lime-bundles/core';
|
|
2
|
+
|
|
3
|
+
interface UseCartOptions {
|
|
4
|
+
shopDomain: string;
|
|
5
|
+
storefrontAccessToken: string;
|
|
6
|
+
bundleGid: string;
|
|
7
|
+
bundleType: string;
|
|
8
|
+
cartId?: string;
|
|
9
|
+
cartApi?: "ajax" | "storefront";
|
|
10
|
+
}
|
|
11
|
+
interface UseCartResult {
|
|
12
|
+
addToCart: (items: CartLineItem[]) => Promise<AddToCartResult>;
|
|
13
|
+
loading: boolean;
|
|
14
|
+
error: string | null;
|
|
15
|
+
lastResult: AddToCartResult | null;
|
|
16
|
+
}
|
|
17
|
+
declare function useCart(options: UseCartOptions): UseCartResult;
|
|
18
|
+
|
|
19
|
+
export { type UseCartOptions, type UseCartResult, useCart };
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// src/hooks/useCart.ts
|
|
2
|
+
import { useState, useCallback, useMemo } from "react";
|
|
3
|
+
import {
|
|
4
|
+
detectCartApi,
|
|
5
|
+
createAjaxCartApi,
|
|
6
|
+
createStorefrontCartApi,
|
|
7
|
+
createStorefrontClient
|
|
8
|
+
} from "@lime-bundles/core";
|
|
9
|
+
function useCart(options) {
|
|
10
|
+
const [loading, setLoading] = useState(false);
|
|
11
|
+
const [error, setError] = useState(null);
|
|
12
|
+
const [lastResult, setLastResult] = useState(null);
|
|
13
|
+
const cart = useMemo(() => {
|
|
14
|
+
const apiType = options.cartApi ?? detectCartApi();
|
|
15
|
+
if (apiType === "ajax") {
|
|
16
|
+
return createAjaxCartApi(options.bundleGid, options.bundleType);
|
|
17
|
+
}
|
|
18
|
+
const client = createStorefrontClient({
|
|
19
|
+
shopDomain: options.shopDomain,
|
|
20
|
+
accessToken: options.storefrontAccessToken
|
|
21
|
+
});
|
|
22
|
+
return createStorefrontCartApi(
|
|
23
|
+
client,
|
|
24
|
+
options.bundleGid,
|
|
25
|
+
options.bundleType,
|
|
26
|
+
options.cartId
|
|
27
|
+
);
|
|
28
|
+
}, [
|
|
29
|
+
options.shopDomain,
|
|
30
|
+
options.storefrontAccessToken,
|
|
31
|
+
options.bundleGid,
|
|
32
|
+
options.bundleType,
|
|
33
|
+
options.cartId,
|
|
34
|
+
options.cartApi
|
|
35
|
+
]);
|
|
36
|
+
const addToCart = useCallback(
|
|
37
|
+
async (items) => {
|
|
38
|
+
setLoading(true);
|
|
39
|
+
setError(null);
|
|
40
|
+
try {
|
|
41
|
+
const result = await cart.addLines(items);
|
|
42
|
+
setLastResult(result);
|
|
43
|
+
if (!result.success) {
|
|
44
|
+
setError(result.error ?? "Failed to add to cart");
|
|
45
|
+
}
|
|
46
|
+
return result;
|
|
47
|
+
} catch (err) {
|
|
48
|
+
const message = err instanceof Error ? err.message : "Failed to add to cart";
|
|
49
|
+
setError(message);
|
|
50
|
+
const result = { success: false, error: message };
|
|
51
|
+
setLastResult(result);
|
|
52
|
+
return result;
|
|
53
|
+
} finally {
|
|
54
|
+
setLoading(false);
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
[cart]
|
|
58
|
+
);
|
|
59
|
+
return { addToCart, loading, error, lastResult };
|
|
60
|
+
}
|
|
61
|
+
export {
|
|
62
|
+
useCart
|
|
63
|
+
};
|
|
64
|
+
//# sourceMappingURL=useCart.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/hooks/useCart.ts"],"sourcesContent":["/**\n * Hook for cart API operations.\n * Auto-detects AJAX vs Storefront Cart API.\n */\nimport { useState, useCallback, useMemo } from \"react\";\nimport {\n detectCartApi,\n createAjaxCartApi,\n createStorefrontCartApi,\n createStorefrontClient,\n type CartApi,\n type CartLineItem,\n type AddToCartResult,\n} from \"@lime-bundles/core\";\n\nexport interface UseCartOptions {\n shopDomain: string;\n storefrontAccessToken: string;\n bundleGid: string;\n bundleType: string;\n cartId?: string;\n cartApi?: \"ajax\" | \"storefront\"; // Manual override\n}\n\nexport interface UseCartResult {\n addToCart: (items: CartLineItem[]) => Promise<AddToCartResult>;\n loading: boolean;\n error: string | null;\n lastResult: AddToCartResult | null;\n}\n\nexport function useCart(options: UseCartOptions): UseCartResult {\n const [loading, setLoading] = useState(false);\n const [error, setError] = useState<string | null>(null);\n const [lastResult, setLastResult] = useState<AddToCartResult | null>(null);\n\n const cart: CartApi = useMemo(() => {\n const apiType = options.cartApi ?? detectCartApi();\n\n if (apiType === \"ajax\") {\n return createAjaxCartApi(options.bundleGid, options.bundleType);\n }\n\n const client = createStorefrontClient({\n shopDomain: options.shopDomain,\n accessToken: options.storefrontAccessToken,\n });\n return createStorefrontCartApi(\n client,\n options.bundleGid,\n options.bundleType,\n options.cartId,\n );\n }, [\n options.shopDomain,\n options.storefrontAccessToken,\n options.bundleGid,\n options.bundleType,\n options.cartId,\n options.cartApi,\n ]);\n\n const addToCart = useCallback(\n async (items: CartLineItem[]): Promise<AddToCartResult> => {\n setLoading(true);\n setError(null);\n try {\n const result = await cart.addLines(items);\n setLastResult(result);\n if (!result.success) {\n setError(result.error ?? \"Failed to add to cart\");\n }\n return result;\n } catch (err) {\n const message =\n err instanceof Error ? err.message : \"Failed to add to cart\";\n setError(message);\n const result: AddToCartResult = { success: false, error: message };\n setLastResult(result);\n return result;\n } finally {\n setLoading(false);\n }\n },\n [cart],\n );\n\n return { addToCart, loading, error, lastResult };\n}\n"],"mappings":";AAIA,SAAS,UAAU,aAAa,eAAe;AAC/C;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAIK;AAkBA,SAAS,QAAQ,SAAwC;AAC9D,QAAM,CAAC,SAAS,UAAU,IAAI,SAAS,KAAK;AAC5C,QAAM,CAAC,OAAO,QAAQ,IAAI,SAAwB,IAAI;AACtD,QAAM,CAAC,YAAY,aAAa,IAAI,SAAiC,IAAI;AAEzE,QAAM,OAAgB,QAAQ,MAAM;AAClC,UAAM,UAAU,QAAQ,WAAW,cAAc;AAEjD,QAAI,YAAY,QAAQ;AACtB,aAAO,kBAAkB,QAAQ,WAAW,QAAQ,UAAU;AAAA,IAChE;AAEA,UAAM,SAAS,uBAAuB;AAAA,MACpC,YAAY,QAAQ;AAAA,MACpB,aAAa,QAAQ;AAAA,IACvB,CAAC;AACD,WAAO;AAAA,MACL;AAAA,MACA,QAAQ;AAAA,MACR,QAAQ;AAAA,MACR,QAAQ;AAAA,IACV;AAAA,EACF,GAAG;AAAA,IACD,QAAQ;AAAA,IACR,QAAQ;AAAA,IACR,QAAQ;AAAA,IACR,QAAQ;AAAA,IACR,QAAQ;AAAA,IACR,QAAQ;AAAA,EACV,CAAC;AAED,QAAM,YAAY;AAAA,IAChB,OAAO,UAAoD;AACzD,iBAAW,IAAI;AACf,eAAS,IAAI;AACb,UAAI;AACF,cAAM,SAAS,MAAM,KAAK,SAAS,KAAK;AACxC,sBAAc,MAAM;AACpB,YAAI,CAAC,OAAO,SAAS;AACnB,mBAAS,OAAO,SAAS,uBAAuB;AAAA,QAClD;AACA,eAAO;AAAA,MACT,SAAS,KAAK;AACZ,cAAM,UACJ,eAAe,QAAQ,IAAI,UAAU;AACvC,iBAAS,OAAO;AAChB,cAAM,SAA0B,EAAE,SAAS,OAAO,OAAO,QAAQ;AACjE,sBAAc,MAAM;AACpB,eAAO;AAAA,MACT,UAAE;AACA,mBAAW,KAAK;AAAA,MAClB;AAAA,IACF;AAAA,IACA,CAAC,IAAI;AAAA,EACP;AAEA,SAAO,EAAE,WAAW,SAAS,OAAO,WAAW;AACjD;","names":[]}
|