npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

medusa-cart-logic

v1.0.0

Published

React hook and helpers for Medusa Store cart (line items, promotions, cookie credentials).

Readme

medusa-cart-logic

Reusable cart hook logic for Medusa storefronts.

This package exposes useCart and Medusa cart helpers used by cart flows:

  • cart create/retrieve/update
  • line items (add/update/remove)
  • promotions/coupons (apply/remove)
  • totals + items state for cart UI
  • guest/customer cookie session support via credentials: "include"

Installation

npm install

For local development in this monorepo:

npm run build

Exports

From src/index.ts:

  • useCart hook
  • cart service helpers from medusa-services/cart

Quick Usage

import { useCart } from "medusa-cart-logic"

const cart = useCart({
  backendUrl: "https://your-medusa-server.com",
  publishableApiKey: "pk_...",
  cartId, // from localStorage/session/app state
  onCartIdChange: (nextId) => {
    setCartId(nextId)
    if (nextId) localStorage.setItem("cart_id", nextId)
  },
  credentials: "include",
})

await cart.ensureCart()
await cart.addLineItem({ variant_id: "variant_123", quantity: 1 })
await cart.applyPromotions(["SUMMER10"])

useCart Input

  • backendUrl: Medusa backend URL
  • publishableApiKey: Medusa publishable key
  • cartId: current cart id (nullable)
  • onCartIdChange: callback when createCart returns a new id
  • authorization: optional customer JWT (Bearer prefix optional)
  • credentials: request credentials mode (defaults to "include")
  • fields: optional Medusa Store fields query override
  • enabled: controls auto refresh behavior (default true)

Returned API

  • State:
    • cart
    • loading
    • mutating
    • error
  • Actions:
    • refresh
    • createCart
    • ensureCart
    • updateCart
    • addLineItem
    • updateLineItem
    • removeLineItem
    • applyPromotions
    • removePromotions
    • clearError

Medusa Guest Cart Notes

  • Browser JavaScript cannot read HttpOnly cookies like _medusa_cart_id.
  • Keep your own cartId reference (for example in localStorage, session state, or server session).
  • Always send cart requests with credentials: "include" so cookie-backed session/cart behavior works across requests.

Notes

  • This package is UI-agnostic and focuses only on cart logic/state transitions.
  • Use your own UI components for list rendering, quantity controls, coupon input, and checkout CTA.