{"_id":"@ashishbarthwal1999/cart-checkout-plugin","_rev":"6-3cc71dac71ff317c84f6bb4e39da1987","name":"@ashishbarthwal1999/cart-checkout-plugin","dist-tags":{"latest":"2.3.0"},"versions":{"1.0.0":{"name":"@ashishbarthwal1999/cart-checkout-plugin","version":"1.0.0","_id":"@ashishbarthwal1999/cart-checkout-plugin@1.0.0","maintainers":[{"name":"ashishbarthwal1999","email":"Ashishbartwal1999@gmail.com"}],"dist":{"shasum":"d0ac5e7d41efbdff18239c7d5b89e118be4b1086","tarball":"https://registry.npmjs.org/@ashishbarthwal1999/cart-checkout-plugin/-/cart-checkout-plugin-1.0.0.tgz","fileCount":8,"integrity":"sha512-7Ej0A8M7yD1xz7+ZWREq8xUYwq+mZe86Fj2gBANo8/5RaCV2fVRIIDblB+9FuUY6xVjKvrxg3b5LSMxS3sz7Dw==","signatures":[{"sig":"MEUCIDkCVB+ov35KEynyt62HEXDNq2LHOjNiLHwBEN61APkqAiEAtpGQ5nK8pVApRiMegzInD15WqEKNGu1TaGogn/BXnPY=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":25930},"main":"dist/cart-checkout-plugin.umd.cjs","type":"module","types":"dist/index.d.ts","module":"dist/cart-checkout-plugin.js","exports":{".":{"types":"./dist/index.d.ts","import":"./dist/cart-checkout-plugin.js","require":"./dist/cart-checkout-plugin.umd.cjs"}},"scripts":{"dev":"vite","lint":"eslint .","build":"tsc -b && vite build && tsc -p tsconfig.declaration.json","preview":"vite preview"},"_npmUser":{"name":"ashishbarthwal1999","email":"Ashishbartwal1999@gmail.com"},"_npmVersion":"10.9.0","description":"Reusable cart and checkout SDK for multi-tenant React storefront applications","directories":{},"_nodeVersion":"22.11.0","_hasShrinkwrap":false,"devDependencies":{"vite":"^7.3.1","eslint":"^9.39.1","globals":"^16.5.0","@eslint/js":"^9.39.1","typescript":"~5.9.3","@types/node":"^24.10.1","@types/react":"^19.2.7","@types/react-dom":"^19.2.3","typescript-eslint":"^8.48.0","@vitejs/plugin-react":"^5.1.1","eslint-plugin-react-hooks":"^7.0.1","eslint-plugin-react-refresh":"^0.4.24"},"peerDependencies":{"react":">=18","react-dom":">=18"},"_npmOperationalInternal":{"tmp":"tmp/cart-checkout-plugin_1.0.0_1773730256089_0.40150936128339354","host":"s3://npm-registry-packages-npm-production"}},"2.0.0":{"name":"@ashishbarthwal1999/cart-checkout-plugin","version":"2.0.0","_id":"@ashishbarthwal1999/cart-checkout-plugin@2.0.0","maintainers":[{"name":"ashishbarthwal1999","email":"Ashishbartwal1999@gmail.com"}],"dist":{"shasum":"6b877f1d6ab030f52e7299d7099fd1141edd3bbd","tarball":"https://registry.npmjs.org/@ashishbarthwal1999/cart-checkout-plugin/-/cart-checkout-plugin-2.0.0.tgz","fileCount":10,"integrity":"sha512-mp3241fbcmWeECWzW1Q4MyfEsqb++atRoa+InYQNNLO0vZADXSDtbqCmcfNGrXi3jvhMgQrGEm90eeAXTu30Yg==","signatures":[{"sig":"MEUCIQCEbLe/MYiCFoAgh2u6UwLStUiBcGBgbcSwgE1xjG28KgIgN5zhh9NyDd7PTmbstQ3Jn1sBPTr9sciHU/6N6RZ+esU=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":47250},"main":"dist/cart-checkout-plugin.umd.cjs","type":"module","types":"dist/index.d.ts","module":"dist/cart-checkout-plugin.js","exports":{".":{"types":"./dist/index.d.ts","import":"./dist/cart-checkout-plugin.js","require":"./dist/cart-checkout-plugin.umd.cjs"}},"gitHead":"fb9d6e88e70257cf02a1e74677125e80223d4e28","scripts":{"dev":"vite","lint":"eslint .","build":"tsc -b && vite build && tsc -p tsconfig.declaration.json","preview":"vite preview"},"_npmUser":{"name":"ashishbarthwal1999","email":"Ashishbartwal1999@gmail.com"},"_npmVersion":"10.9.0","description":"Reusable cart and checkout SDK for multi-tenant React storefront applications","directories":{},"_nodeVersion":"22.11.0","_hasShrinkwrap":false,"devDependencies":{"vite":"^7.3.1","eslint":"^9.39.1","globals":"^16.5.0","@eslint/js":"^9.39.1","typescript":"~5.9.3","@types/node":"^24.10.1","@types/react":"^19.2.7","@types/react-dom":"^19.2.3","typescript-eslint":"^8.48.0","@vitejs/plugin-react":"^5.1.1","eslint-plugin-react-hooks":"^7.0.1","eslint-plugin-react-refresh":"^0.4.24"},"peerDependencies":{"react":">=18","react-dom":">=18"},"_npmOperationalInternal":{"tmp":"tmp/cart-checkout-plugin_2.0.0_1773746854205_0.30375907781862965","host":"s3://npm-registry-packages-npm-production"}},"2.0.1":{"name":"@ashishbarthwal1999/cart-checkout-plugin","version":"2.0.1","_id":"@ashishbarthwal1999/cart-checkout-plugin@2.0.1","maintainers":[{"name":"ashishbarthwal1999","email":"Ashishbartwal1999@gmail.com"}],"dist":{"shasum":"f725e43a515209e7382ec10c6a4f505a440622aa","tarball":"https://registry.npmjs.org/@ashishbarthwal1999/cart-checkout-plugin/-/cart-checkout-plugin-2.0.1.tgz","fileCount":11,"integrity":"sha512-MzMp7J16gWxfvEgmhl7S2efk5CPaSHUMP/ymqNthy161G2ckecSZB50O8r9HxNfqAovGv4vZS5BKe+gZQbTerA==","signatures":[{"sig":"MEYCIQCFKNQ27R2K+1ojfGErIZk1k/bojZBeOsIzTqkOkItefgIhAIvWIMoPvkU7FRsryrTKKNMHUCku7eyIRvsmxjyAxWoN","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":50239},"main":"dist/cart-checkout-plugin.umd.cjs","type":"module","types":"dist/index.d.ts","module":"dist/cart-checkout-plugin.js","exports":{".":{"types":"./dist/index.d.ts","import":"./dist/cart-checkout-plugin.js","require":"./dist/cart-checkout-plugin.umd.cjs"}},"gitHead":"f03817e3ff6d4cdfa851a3145426ddbf962ad168","scripts":{"dev":"vite","lint":"eslint .","build":"tsc -b && vite build && tsc -p tsconfig.declaration.json","preview":"vite preview"},"_npmUser":{"name":"ashishbarthwal1999","email":"Ashishbartwal1999@gmail.com"},"_npmVersion":"10.9.0","description":"Reusable cart and checkout SDK for multi-tenant React storefront applications","directories":{},"_nodeVersion":"22.11.0","_hasShrinkwrap":false,"devDependencies":{"vite":"^7.3.1","eslint":"^9.39.1","globals":"^16.5.0","@eslint/js":"^9.39.1","typescript":"~5.9.3","@types/node":"^24.10.1","@types/react":"^19.2.7","@types/react-dom":"^19.2.3","typescript-eslint":"^8.48.0","@vitejs/plugin-react":"^5.1.1","eslint-plugin-react-hooks":"^7.0.1","eslint-plugin-react-refresh":"^0.4.24"},"peerDependencies":{"react":">=18","react-dom":">=18"},"_npmOperationalInternal":{"tmp":"tmp/cart-checkout-plugin_2.0.1_1773750568199_0.1480226687235049","host":"s3://npm-registry-packages-npm-production"}},"2.0.2":{"name":"@ashishbarthwal1999/cart-checkout-plugin","version":"2.0.2","_id":"@ashishbarthwal1999/cart-checkout-plugin@2.0.2","maintainers":[{"name":"ashishbarthwal1999","email":"Ashishbartwal1999@gmail.com"}],"dist":{"shasum":"aea7f550c837e533180615ba6fd424ffec5f8307","tarball":"https://registry.npmjs.org/@ashishbarthwal1999/cart-checkout-plugin/-/cart-checkout-plugin-2.0.2.tgz","fileCount":11,"integrity":"sha512-WvPsa6p1a0rVD/x3Bxl3sGfir1LUzS78m6ph6BnsHecuwoLKW+nECM6p5SMYYRaN6afUm9KFyYsDaDrH4Q0Qwg==","signatures":[{"sig":"MEUCIBqs7L5H7fDqaUSK38yfwCccykES0pB7p9VB+N4uYpN4AiEA2OR1jq7YJuEEpVgu4iQ2Q7udHom80vQTiZEzOp1QwXQ=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":50239},"main":"dist/cart-checkout-plugin.umd.cjs","type":"module","types":"dist/index.d.ts","module":"dist/cart-checkout-plugin.js","exports":{".":{"types":"./dist/index.d.ts","import":"./dist/cart-checkout-plugin.js","require":"./dist/cart-checkout-plugin.umd.cjs"}},"gitHead":"9ef48c0844bd56c6008ec5ddb6a2b8c7b9deae79","scripts":{"dev":"vite","lint":"eslint .","build":"tsc -b && vite build && tsc -p tsconfig.declaration.json","preview":"vite preview"},"_npmUser":{"name":"ashishbarthwal1999","email":"Ashishbartwal1999@gmail.com"},"_npmVersion":"10.9.0","description":"Reusable cart and checkout SDK for multi-tenant React storefront applications","directories":{},"_nodeVersion":"22.11.0","_hasShrinkwrap":false,"devDependencies":{"vite":"^7.3.1","eslint":"^9.39.1","globals":"^16.5.0","@eslint/js":"^9.39.1","typescript":"~5.9.3","@types/node":"^24.10.1","@types/react":"^19.2.7","@types/react-dom":"^19.2.3","typescript-eslint":"^8.48.0","@vitejs/plugin-react":"^5.1.1","eslint-plugin-react-hooks":"^7.0.1","eslint-plugin-react-refresh":"^0.4.24"},"peerDependencies":{"react":">=18","react-dom":">=18"},"_npmOperationalInternal":{"tmp":"tmp/cart-checkout-plugin_2.0.2_1773750650733_0.15616390027979943","host":"s3://npm-registry-packages-npm-production"}},"2.3.0":{"name":"@ashishbarthwal1999/cart-checkout-plugin","version":"2.3.0","_id":"@ashishbarthwal1999/cart-checkout-plugin@2.3.0","maintainers":[{"name":"ashishbarthwal1999","email":"Ashishbartwal1999@gmail.com"}],"dist":{"shasum":"3e1a660896e8a4781cc270273b26b0813e886ab5","tarball":"https://registry.npmjs.org/@ashishbarthwal1999/cart-checkout-plugin/-/cart-checkout-plugin-2.3.0.tgz","fileCount":11,"integrity":"sha512-uf1mghjUWPZvssNZiOEf7zzXRJDtD2w68QDkzx5269KEPbG4k6gOiCq2olGoSHGufe5XGw0pmg4lUrkKBbO7HA==","signatures":[{"sig":"MEYCIQDea3BuHtt0J3mDPVoSHx64HSJR7IR1ed76xS6c+A01pgIhAOe+dD85h9uKBTOJDJx+li+0J3YI8CvjSU9Ya41P5i0S","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":52643},"main":"dist/cart-checkout-plugin.umd.cjs","type":"module","types":"dist/index.d.ts","module":"dist/cart-checkout-plugin.js","exports":{".":{"types":"./dist/index.d.ts","import":"./dist/cart-checkout-plugin.js","require":"./dist/cart-checkout-plugin.umd.cjs"}},"gitHead":"d00176475c58fcbd1e167019777256095c726872","scripts":{"dev":"vite","lint":"eslint .","build":"tsc -b && vite build && tsc -p tsconfig.declaration.json","preview":"vite preview"},"_npmUser":{"name":"ashishbarthwal1999","email":"Ashishbartwal1999@gmail.com"},"_npmVersion":"10.9.0","description":"Reusable cart and checkout SDK for multi-tenant React storefront applications","directories":{},"_nodeVersion":"22.11.0","_hasShrinkwrap":false,"devDependencies":{"vite":"^7.3.1","eslint":"^9.39.1","globals":"^16.5.0","@eslint/js":"^9.39.1","typescript":"~5.9.3","@types/node":"^24.10.1","@types/react":"^19.2.7","@types/react-dom":"^19.2.3","typescript-eslint":"^8.48.0","@vitejs/plugin-react":"^5.1.1","eslint-plugin-react-hooks":"^7.0.1","eslint-plugin-react-refresh":"^0.4.24"},"peerDependencies":{"react":">=18","react-dom":">=18"},"_npmOperationalInternal":{"tmp":"tmp/cart-checkout-plugin_2.3.0_1774266263557_0.9971403494864168","host":"s3://npm-registry-packages-npm-production"}}},"time":{"created":"2026-03-17T06:50:55.958Z","modified":"2026-03-23T11:54:25.480Z","1.0.0":"2026-03-17T06:50:56.259Z","2.0.0":"2026-03-17T11:27:34.380Z","2.0.1":"2026-03-17T12:29:28.352Z","2.0.2":"2026-03-17T12:30:50.907Z","2.3.0":"2026-03-23T11:44:23.717Z"},"description":"Reusable cart and checkout SDK for multi-tenant React storefront applications","maintainers":[{"email":"Ashishbartwal1999@gmail.com","name":"store-management"}],"readme":"# @ecommerce-store/cart-checkout-plugin\n\nA small React SDK for cart state and persistence in multi-tenant storefronts. Supports **local-only mode** (localStorage per tenant) or **API mode** (sync with your backend: guest session, add/view/update/remove/clear, checkout, merge guest cart on login).\n\n**Requirements:** React 18+\n\nFor **flow, requirements checklist, and where to debug**, see [ARCHITECTURE.md](./ARCHITECTURE.md).\n\n---\n\n## Install\n\n```bash\nnpm install @ecommerce-store/cart-checkout-plugin\n```\n\n---\n\n## Quick start\n\n1. Wrap your app with `CartProvider` and pass a `tenantId` (e.g. store or site id):\n\n```jsx\nimport { CartProvider } from \"@ecommerce-store/cart-checkout-plugin\";\n\nfunction App() {\n  return (\n    <CartProvider tenantId=\"my-store-id\">\n      <YourApp />\n    </CartProvider>\n  );\n}\n```\n\n2. Use `useCart` in any child to read the cart and call actions:\n\n```jsx\nimport { useCart } from \"@ecommerce-store/cart-checkout-plugin\";\n\nfunction ProductCard({ product }) {\n  const { addItem } = useCart();\n\n  return (\n    <button\n      onClick={() =>\n        addItem({\n          id: product.id,\n          name: product.name,\n          price: product.price,\n          quantity: 1,\n        })\n      }\n    >\n      Add to cart\n    </button>\n  );\n}\n\nfunction CartPage() {\n  const { items, summary, removeItem, updateQuantity, clearCart } = useCart();\n\n  return (\n    <div>\n      {items.map((item) => (\n        <div key={item.id}>\n          {item.name} × {item.quantity} — ${(item.price ?? 0) * item.quantity}\n          <button onClick={() => updateQuantity(item.id, item.quantity + 1)}>+</button>\n          <button onClick={() => updateQuantity(item.id, item.quantity - 1)}>-</button>\n          <button onClick={() => removeItem(item.id)}>Remove</button>\n        </div>\n      ))}\n      <p>Subtotal: ${summary.subtotal}</p>\n      <p>Items: {summary.itemCount}</p>\n      <button onClick={clearCart}>Clear cart</button>\n    </div>\n  );\n}\n```\n\n---\n\n## API\n\n### `CartProvider`\n\n| Prop                | Type     | Default   | Description                                      |\n|---------------------|----------|-----------|--------------------------------------------------|\n| `tenantId`          | `string` | required  | Tenant/store id. Cart is stored per tenant.       |\n| `storageKeyPrefix`  | `string` | `\"cart\"`  | Prefix for localStorage key (local-only mode).   |\n| `children`         | `ReactNode` | required | Your app or subtree.                             |\n| `apiBaseUrl`       | `string` | —         | When set, enables **API mode** (see below).      |\n| `storeId`          | `string` | —         | Required when `apiBaseUrl` is set.                |\n| `getHeaders`       | `() => Record<string, string>` | — | Optional. Return headers for each request (e.g. `{ \"X-User-Id\": userId }` for logged-in users). |\n\n### `useCart()`\n\nReturns:\n\n| Property        | Type                    | Description |\n|-----------------|-------------------------|-------------|\n| `items`         | `CartItem[]`            | Current line items. |\n| `summary`       | `CartSummary`           | `{ subtotal, total, itemCount }`. |\n| `tenantId`      | `string`                | Current tenant id. |\n| `isEmpty`       | `boolean`               | `items.length === 0`. |\n| `addItem`       | `(item, quantity?) => void` | Add or merge by `id`. |\n| `removeItem`    | `(id) => void`          | Remove line by `id`. |\n| `updateQuantity`| `(id, quantity) => void`| Set quantity; remove if ≤ 0. |\n| `clearCart`     | `() => void`             | Remove all items. |\n| `isSyncing`     | `boolean`               | **API mode only.** True while loading or mutating. |\n| `lastError`     | `Error \\| null`          | **API mode only.** Last API error, clear on next success. |\n| `mergeGuestCart`| `() => Promise<void>`   | **API mode only.** Call after login to merge guest cart into user cart. |\n\n### `useCheckout()`\n\n**API mode only.** Returns:\n\n| Property        | Type                    | Description |\n|-----------------|-------------------------|-------------|\n| `startCheckout` | `(body?) => Promise<CheckoutResponse \\| null>` | `POST /checkout/?tenant_id&store_id` (query when set); empty body unless you pass a JSON `body`. |\n| `isPending`     | `boolean`               | True while checkout request is in flight. |\n| `error`         | `Error \\| null`          | Checkout request error. |\n| `result`        | `CheckoutResponse \\| null` | Last successful response (`checkout_url`, `order_id`, etc.). |\n\n### Types\n\n- **`CartItem`** — `id` (string or number), optional `name`, `price`, `quantity` (defaults to 1 when adding), and any extra fields.\n- **`CartSummary`** — `{ subtotal, total, itemCount }`.\n- **`CheckoutResponse`** — Typed response from checkout (e.g. `checkout_url`, `order_id`). Import when using `useCheckout`.\n- **`CartContextValue`** — Full context type if you need it.\n\n```ts\nimport type { CartItem, CartSummary, CheckoutResponse } from \"@ecommerce-store/cart-checkout-plugin\";\n```\n\n---\n\n## API mode (backend sync)\n\nWhen you pass `apiBaseUrl` and `storeId`, the plugin switches to **API mode**:\n\n- **Guest session:** The plugin calls `POST /cart/guest/session` to get a guest cart id, stores it in localStorage (key `cart_guest_id:{tenantId}:{storeId}`), and sends it as the `X-Guest-Cart-Id` header on all cart requests.\n- **Logged-in users:** Provide `getHeaders={() => ({ \"X-User-Id\": currentUserId })}`. The plugin sends `X-User-Id` instead of (or with) the guest id. After login, call **`mergeGuestCart()`** so the backend merges the guest cart into the user cart; the plugin then stops using the stored guest id and refetches the cart.\n- **Endpoints used:** `GET /cart/view`, `POST /cart/add`, `PUT /cart/item/{item_id}`, `DELETE /cart/item/{item_id}`, `DELETE /cart/clear?tenant_id&store_id`, `POST /cart/merge-guest-cart`, `POST /checkout/?tenant_id&store_id` (checkout body empty by default). Request bodies and query params use `tenant_id` and `store_id` as required by your API.\n\nExample:\n\n```jsx\n<CartProvider\n  tenantId={tenantId}\n  storeId={storeId}\n  apiBaseUrl=\"https://api.example.com\"\n  getHeaders={() => (user ? { \"X-User-Id\": user.id } : {})}\n>\n  <App />\n</CartProvider>\n\n// After user logs in:\nconst { mergeGuestCart } = useCart();\nawait mergeGuestCart();\n\n// Checkout:\nconst { startCheckout, isPending, result } = useCheckout();\nconst res = await startCheckout();\nif (res?.checkout_url) window.location.href = res.checkout_url;\n```\n\n---\n\n## Multi-tenant behavior\n\n- **Local-only mode:** Each `tenantId` gets its own cart in localStorage under the key `{storageKeyPrefix}:{tenantId}`. Persistence runs only after hydration so the cart is not overwritten on first paint.\n- **API mode:** Cart is stored on the server. Guest cart id is stored per tenant+store and reused across refreshes. Use `mergeGuestCart()` after login to merge guest cart into the user cart.\n\n---\n\n## Building from source\n\n```bash\nnpm install\nnpm run build\n```\n\nOutput is in `dist/`: ESM and CJS bundles plus TypeScript declarations.\n","readmeFilename":"README.md"}