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

@baseworks/billing

v0.1.1

Published

Billing capability: typed HTTP client, event catalog, projections, and CLI plugin for billing-service (usage-based billing: metrics, plans, subscriptions, usage). Contract-first, versioned with the service.

Readme

@baseworks/billing

Typed client, event catalog, projections, and CLI plugin for billing-service — the per-org usage-based billing core (metrics, plans, subscriptions). Contract-first, versioned with the service.

pnpm add @baseworks/billing

Client

Everything is org-scoped (X-Org-Id + identity token). Money is integer cents throughout.

import { createBillingClient } from '@baseworks/billing'

const billing = createBillingClient({
  baseUrl: process.env.BILLING_SERVICE_URL!,
  token:   identityJwt,
  orgId:   activeOrgId,
})

// 1. a billable metric
const apiCalls = await billing.createMetric({
  code: 'api_calls', name: 'API calls', aggregation: 'sum', unit: 'request',
})

// 2. a plan — base fee + per-metric usage charges (all integer cents)
const pro = await billing.createPlan({
  code: 'pro', name: 'Pro', currency: 'USD',
  baseAmountCents: 4900,                               // $49.00 / month
  charges: [{ metricCode: 'api_calls', unitAmountCents: 2 }], // $0.02 / call
})

// 3. subscribe a customer (customerId is a customers-service ref — validated,
//    name snapshotted). Opens a one-month billing period.
const sub = await billing.createSubscription({ customerId: 'acct-42', planId: 'pro' })

await billing.listSubscriptions({ status: 'active' })
await billing.getSubscription(sub.id)

// 4. meter usage — idempotent by transactionId (re-sending is a no-op)
await billing.recordUsage({ subscriptionId: sub.id, metricCode: 'api_calls', quantity: 100, transactionId: 'req-1' })
await billing.recordUsage({ subscriptionId: sub.id, metricCode: 'api_calls', quantity: 250, transactionId: 'req-2' })
await billing.listUsage({ subscriptionId: sub.id })

// 5. rate the current period — base fee + Σ(aggregated usage × charge). No invoice.
const rating = await billing.rateSubscription(sub.id)
// → { baseAmountCents: 4900, lines: [{ metricCode: 'api_calls', quantity: 350, amountCents: 700 }], totalCents: 5600 }

// 6. close the period → finalized invoice (idempotent + resumable + locked).
//    Re-running is safe: one invoice per subscription-period. --force closes the
//    current period even before it ends (a real cron omits it and closes on due).
const report = await billing.closePeriods({ subscriptionId: sub.id, force: true })
// → { locked: true, closed: [{ subscriptionShortId, invoiceNumber, totalCents, status: 'closed' }], skipped: [] }

await billing.cancelSubscription(sub.id, 'downgraded')

Errors throw BillingError (.status, .code): 409 code_taken, 400 unknown_metric (charge/usage references a missing metric), 404 customer_not_found, 404 plan_not_found, 404 subscription_not_found, 409 already_canceled, 409 subscription_canceled (usage on a canceled sub).

CLI plugin

import { buildBillingCommand } from '@baseworks/billing/cli'

program.addCommand(buildBillingCommand({ http, cliName: 'dtab' }))
// dtab billing | bill
//   metrics|metric        ls · create · get · update · archive
//   plans|plan            ls · create (--base, --charge metric=cents) · get · update · archive
//   subscriptions|subs    ls (--status) · create (--customer <ref> --plan <code>) · get · cancel
//   usage                 record (--subscription --metric --quantity --txn) · ls · rate
//   close                 [--subscription <ref>] [--force] [--due-in <days>]

Service integration

import { PROJECTIONS, POLICIES } from '@baseworks/billing/projections'
import { MetricEvents, PlanEvents, SubscriptionEvents, UsageEvents, CloseEvents } from '@baseworks/billing/events'

Projections metricsread_metrics, plansread_plans, subscriptionsread_subscriptions, usageread_usage, closesread_closes. Tenant = org id. Usage streams are keyed by sha256(subscriptionId:transactionId) and close markers by sha256(close:subscriptionId:period) — the read-model row collapses duplicates, so dedup (and one-invoice-per-period) holds even if two runs race.

See also

Service: projects/billing-service · ADR-010 · ADR-011.