@dhruvp13/promo-code-core
v0.1.0
Published
Zero-dependency validation and calculation engine for coupon/promo-code systems. Framework-agnostic — bring your own database via the `CouponRepository` interface.
Downloads
98
Maintainers
Readme
@dhruvp13/promo-code-core
Zero-dependency validation and calculation engine for coupon/promo-code
systems. Framework-agnostic — bring your own database via the
CouponRepository interface.
Install
bun add @dhruvp13/promo-code-coreUsage
import { validateCoupon, type CouponRepository } from '@dhruvp13/promo-code-core'
declare const repo: CouponRepository<'coin' | 'subscription'>
const result = await validateCoupon(repo, {
code: 'SAVE20',
userId: 'user-1',
purchaseType: 'coin',
baseAmount: 5000,
})
if (result.valid) {
console.log(result.discountAmount)
await repo.recordSuccessfulRedemption({
couponId: result.coupon.id,
userId: 'user-1',
purchaseType: 'coin',
baseAmount: 5000,
idempotencyKey: 'stripe-evt-123',
})
}For a ready-made in-memory CouponRepository (testing/onboarding), see
@dhruvp13/promo-code-memory. For Payload CMS, see @dhruvp13/promo-code-adapters.
See the root README for full docs on validation rules, idempotency, and custom validators.
