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

@shopkit/discounts

v0.1.3

Published

Discount and coupon management for e-commerce storefronts

Readme

@shopkit/discounts

Discount and coupon management for e-commerce storefronts.

Installation

npm install @shopkit/discounts @shopkit/cart
# or
bun add @shopkit/discounts @shopkit/cart

Quick Start

1. Implement the discount service

import { IDiscountService, DiscountResult, DiscountValidationResult, OffersResult } from '@shopkit/discounts';

class MyDiscountService implements IDiscountService {
  async applyDiscount(cartId: string, code: string): Promise<DiscountResult> {
    const response = await fetch('/api/discount/apply', {
      method: 'POST',
      body: JSON.stringify({ cartId, code }),
    });
    return response.json();
  }

  async removeDiscount(cartId: string): Promise<void> {
    await fetch('/api/discount/remove', {
      method: 'POST',
      body: JSON.stringify({ cartId }),
    });
  }

  async validateDiscount(code: string, cartData: unknown): Promise<DiscountValidationResult> {
    const response = await fetch('/api/discount/validate', {
      method: 'POST',
      body: JSON.stringify({ code, cartData }),
    });
    return response.json();
  }

  async getOffers(cartId: string): Promise<OffersResult> {
    const response = await fetch(`/api/discount/offers?cartId=${cartId}`);
    return response.json();
  }

  async getCartData(cartId: string): Promise<unknown> {
    const response = await fetch(`/api/cart/${cartId}`);
    return response.json();
  }
}

export const discountService = new MyDiscountService();

2. Use the discount hook

import { useDiscount } from '@shopkit/discounts';
import { discountService } from './services/discount-service';

function CouponForm() {
  const {
    couponCode,
    setCouponCode,
    applyCoupon,
    removeCoupon,
    appliedDiscount,
    isDiscountLoading,
    availableOffers,
  } = useDiscount({
    discountService,
    onError: (error) => console.error(error.message),
  });

  return (
    <div>
      {appliedDiscount ? (
        <div>
          <p>Applied: {appliedDiscount.code}</p>
          <p>Discount: ${appliedDiscount.amount}</p>
          <button onClick={removeCoupon}>Remove</button>
        </div>
      ) : (
        <form onSubmit={(e) => { e.preventDefault(); applyCoupon(); }}>
          <input
            value={couponCode}
            onChange={(e) => setCouponCode(e.target.value)}
            placeholder="Enter coupon code"
          />
          <button type="submit" disabled={isDiscountLoading}>
            Apply
          </button>
        </form>
      )}

      {availableOffers.length > 0 && (
        <div>
          <h4>Available Offers</h4>
          {availableOffers.map((offer: any) => (
            <button key={offer.code} onClick={() => applyCoupon(offer.code)}>
              {offer.title}
            </button>
          ))}
        </div>
      )}
    </div>
  );
}

API Reference

Types

IDiscountService

Interface for discount service implementations.

interface IDiscountService {
  applyDiscount(cartId: string, code: string): Promise<DiscountResult>;
  removeDiscount(cartId: string): Promise<void>;
  validateDiscount(code: string, cartData: unknown): Promise<DiscountValidationResult>;
  getOffers?(cartId: string): Promise<OffersResult>;
  getCartData?(cartId: string): Promise<unknown>;
}

AppliedDiscount

Represents an applied discount.

interface AppliedDiscount {
  code: string;
  message: string;
  amount: number;
  totalPrice: number;
}

Hooks

useDiscount(options)

Main hook for managing discounts.

Options:

  • discountService: IDiscountService - Required discount service implementation
  • onError?: (error: Error) => void - Error callback

Returns:

  • appliedDiscount: AppliedDiscount | null - Currently applied discount
  • couponCode: string - Current coupon code input
  • setCouponCode: (code: string) => void - Set coupon code
  • availableOffers: unknown[] - Available discount offers
  • unavailableOffers: unknown[] - Unavailable discount offers
  • isDiscountLoading: boolean - Loading state
  • isRemovingDiscount: boolean - Removing discount state
  • applyCoupon: (code?: string) => Promise<void> - Apply a coupon
  • removeCoupon: () => Promise<void> - Remove applied coupon
  • validateAfterCartChange: () => Promise<void> - Re-validate after cart changes
  • refreshOffers: () => Promise<void> - Refresh available offers
  • refreshCartData: () => Promise<unknown> - Refresh cart data

Requirements

  • @shopkit/cart must be configured and initialized
  • React 18+

License

MIT