@fonderie/billing 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Fonderie, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # @fonderie/billing
2
+
3
+ SaaS billing as a brick: a config-driven plan catalogue, Stripe
4
+ subscriptions, feature gates, and usage limits — so "can this workspace do
5
+ that?" is one function call.
6
+
7
+ ## Install
8
+
9
+ ```sh
10
+ npm install @fonderie/billing
11
+ ```
12
+
13
+ ## Use
14
+
15
+ ```ts
16
+ import { FonderieApp, defineConfig } from '@fonderie/core';
17
+ import { BillingModule } from '@fonderie/billing';
18
+
19
+ const app = await new FonderieApp(defineConfig({}))
20
+ .register(new BillingModule())
21
+ .boot();
22
+ ```
23
+
24
+ Gate routes and features:
25
+
26
+ ```ts
27
+ import { requirePlan, requireFeature, hasFeature, getPlanLimit } from '@fonderie/billing';
28
+ ```
29
+
30
+ `StripeProvider` handles checkout and webhook events; usage counters run
31
+ on `MemoryCounterBackend` or `DBCounterBackend`.
32
+
33
+ ## Why this exists
34
+
35
+ You've shipped this plumbing before — auth, teams, billing, messaging —
36
+ and the next project will ask for it again. Fonderie packages it once:
37
+ plain TypeScript modules for
38
+ [`@fonderie/core`](https://github.com/fonderie-js/sdk/tree/main/packages/core),
39
+ PostgreSQL-backed, self-hosted, MIT. No external control plane, no
40
+ per-seat anything. Register the modules you need; skip the ones you don't.
41
+
42
+ **This package owns** what the caller pays for. Plans, subscriptions, feature gates, and
43
+ usage limits — the commercial rules the other bricks consult before acting.
44
+
45
+ Browse the whole set at
46
+ [fonderie-js/sdk](https://github.com/fonderie-js/sdk) · follow
47
+ [@fonderiejs](https://x.com/fonderiejs)
48
+
49
+ ## License
50
+
51
+ MIT © Fonderie, Inc.
@@ -0,0 +1,107 @@
1
+ import { Middleware, IFonderieContext } from '@fonderie/core';
2
+ import { IStoreAdapter } from '@fonderie/store';
3
+ import { SubscriberType, PolicyEntry } from './types.js';
4
+
5
+ interface IBillingEvent {
6
+ type: string;
7
+ subscription: INormalizedSubscription | null;
8
+ }
9
+ interface INormalizedSubscription {
10
+ subscriberType: SubscriberType;
11
+ subscriberId: string;
12
+ plan: string;
13
+ status: string;
14
+ providerCustomerId: string;
15
+ providerSubscriptionId: string;
16
+ currentPeriodStart: Date;
17
+ currentPeriodEnd: Date;
18
+ cancelAtPeriodEnd: boolean;
19
+ trialEndsAt: Date | null;
20
+ }
21
+ interface IBillingProvider {
22
+ name: string;
23
+ createCustomer(opts: {
24
+ email: string;
25
+ subscriberType: SubscriberType;
26
+ subscriberId: string;
27
+ userId: string;
28
+ }): Promise<{
29
+ customerId: string;
30
+ }>;
31
+ createCheckoutSession(opts: {
32
+ customerId: string;
33
+ priceId: string;
34
+ subscriberType: SubscriberType;
35
+ subscriberId: string;
36
+ trialDays?: number;
37
+ successUrl: string;
38
+ cancelUrl: string;
39
+ }): Promise<{
40
+ url: string;
41
+ }>;
42
+ createPortalSession(opts: {
43
+ customerId: string;
44
+ returnUrl: string;
45
+ }): Promise<{
46
+ url: string;
47
+ }>;
48
+ constructEvent(opts: {
49
+ payload: string;
50
+ signature: string;
51
+ secret: string;
52
+ }): Promise<IBillingEvent>;
53
+ }
54
+
55
+ interface ICounterBackend {
56
+ increment(key: string, windowMs: number | null, quantity?: number): Promise<number>;
57
+ get(key: string, windowMs: number | null): Promise<number>;
58
+ }
59
+
60
+ interface IBillingPlanPrice {
61
+ amount: number;
62
+ priceId?: string;
63
+ }
64
+ interface IBillingPlanDefaults {
65
+ warnAt?: number;
66
+ buffer?: number;
67
+ }
68
+ interface IBillingPlan {
69
+ name: string;
70
+ description?: string;
71
+ tier?: number;
72
+ trialDays?: number;
73
+ monthly?: IBillingPlanPrice;
74
+ yearly?: IBillingPlanPrice;
75
+ defaults?: IBillingPlanDefaults;
76
+ policy?: Record<string, PolicyEntry>;
77
+ metadata?: Record<string, unknown>;
78
+ }
79
+ type RateLimitBackendConfig = 'memory' | 'db' | ICounterBackend;
80
+ interface IBillingNotificationsConfig {
81
+ warnAt?: boolean;
82
+ softHit?: boolean;
83
+ }
84
+ interface IBillingConfig {
85
+ provider: IBillingProvider;
86
+ plans: IBillingPlan[];
87
+ successUrl: string;
88
+ cancelUrl: string;
89
+ webhookSecret?: string;
90
+ rateLimit?: {
91
+ backend?: RateLimitBackendConfig;
92
+ };
93
+ notifications?: IBillingNotificationsConfig;
94
+ }
95
+ declare const MESSAGE_KEYS: {
96
+ readonly limitWarning: "billing.limit-warning";
97
+ readonly limitReached: "billing.limit-reached";
98
+ readonly limitBlocked: "billing.limit-blocked";
99
+ };
100
+ type BillingMessageKey = (typeof MESSAGE_KEYS)[keyof typeof MESSAGE_KEYS];
101
+
102
+ declare function requirePlan(plans: string | string[], store: IStoreAdapter): Middleware;
103
+ declare function requirePlan(plans: string | string[], store: IStoreAdapter, ctx: IFonderieContext, next: () => Promise<Response>): Promise<Response>;
104
+
105
+ declare function withBilling(store: IStoreAdapter, config: IBillingConfig, backend: ICounterBackend): Middleware;
106
+
107
+ export { type BillingMessageKey as B, type IBillingConfig as I, MESSAGE_KEYS as M, type RateLimitBackendConfig as R, type IBillingProvider as a, type IBillingEvent as b, type ICounterBackend as c, type IBillingPlan as d, type IBillingNotificationsConfig as e, type IBillingPlanDefaults as f, requirePlan as r, withBilling as w };
@@ -0,0 +1,107 @@
1
+ import { Middleware, IFonderieContext } from '@fonderie/core';
2
+ import { IStoreAdapter } from '@fonderie/store';
3
+ import { SubscriberType, PolicyEntry } from './types.cjs';
4
+
5
+ interface IBillingEvent {
6
+ type: string;
7
+ subscription: INormalizedSubscription | null;
8
+ }
9
+ interface INormalizedSubscription {
10
+ subscriberType: SubscriberType;
11
+ subscriberId: string;
12
+ plan: string;
13
+ status: string;
14
+ providerCustomerId: string;
15
+ providerSubscriptionId: string;
16
+ currentPeriodStart: Date;
17
+ currentPeriodEnd: Date;
18
+ cancelAtPeriodEnd: boolean;
19
+ trialEndsAt: Date | null;
20
+ }
21
+ interface IBillingProvider {
22
+ name: string;
23
+ createCustomer(opts: {
24
+ email: string;
25
+ subscriberType: SubscriberType;
26
+ subscriberId: string;
27
+ userId: string;
28
+ }): Promise<{
29
+ customerId: string;
30
+ }>;
31
+ createCheckoutSession(opts: {
32
+ customerId: string;
33
+ priceId: string;
34
+ subscriberType: SubscriberType;
35
+ subscriberId: string;
36
+ trialDays?: number;
37
+ successUrl: string;
38
+ cancelUrl: string;
39
+ }): Promise<{
40
+ url: string;
41
+ }>;
42
+ createPortalSession(opts: {
43
+ customerId: string;
44
+ returnUrl: string;
45
+ }): Promise<{
46
+ url: string;
47
+ }>;
48
+ constructEvent(opts: {
49
+ payload: string;
50
+ signature: string;
51
+ secret: string;
52
+ }): Promise<IBillingEvent>;
53
+ }
54
+
55
+ interface ICounterBackend {
56
+ increment(key: string, windowMs: number | null, quantity?: number): Promise<number>;
57
+ get(key: string, windowMs: number | null): Promise<number>;
58
+ }
59
+
60
+ interface IBillingPlanPrice {
61
+ amount: number;
62
+ priceId?: string;
63
+ }
64
+ interface IBillingPlanDefaults {
65
+ warnAt?: number;
66
+ buffer?: number;
67
+ }
68
+ interface IBillingPlan {
69
+ name: string;
70
+ description?: string;
71
+ tier?: number;
72
+ trialDays?: number;
73
+ monthly?: IBillingPlanPrice;
74
+ yearly?: IBillingPlanPrice;
75
+ defaults?: IBillingPlanDefaults;
76
+ policy?: Record<string, PolicyEntry>;
77
+ metadata?: Record<string, unknown>;
78
+ }
79
+ type RateLimitBackendConfig = 'memory' | 'db' | ICounterBackend;
80
+ interface IBillingNotificationsConfig {
81
+ warnAt?: boolean;
82
+ softHit?: boolean;
83
+ }
84
+ interface IBillingConfig {
85
+ provider: IBillingProvider;
86
+ plans: IBillingPlan[];
87
+ successUrl: string;
88
+ cancelUrl: string;
89
+ webhookSecret?: string;
90
+ rateLimit?: {
91
+ backend?: RateLimitBackendConfig;
92
+ };
93
+ notifications?: IBillingNotificationsConfig;
94
+ }
95
+ declare const MESSAGE_KEYS: {
96
+ readonly limitWarning: "billing.limit-warning";
97
+ readonly limitReached: "billing.limit-reached";
98
+ readonly limitBlocked: "billing.limit-blocked";
99
+ };
100
+ type BillingMessageKey = (typeof MESSAGE_KEYS)[keyof typeof MESSAGE_KEYS];
101
+
102
+ declare function requirePlan(plans: string | string[], store: IStoreAdapter): Middleware;
103
+ declare function requirePlan(plans: string | string[], store: IStoreAdapter, ctx: IFonderieContext, next: () => Promise<Response>): Promise<Response>;
104
+
105
+ declare function withBilling(store: IStoreAdapter, config: IBillingConfig, backend: ICounterBackend): Middleware;
106
+
107
+ export { type BillingMessageKey as B, type IBillingConfig as I, MESSAGE_KEYS as M, type RateLimitBackendConfig as R, type IBillingProvider as a, type IBillingEvent as b, type ICounterBackend as c, type IBillingPlan as d, type IBillingNotificationsConfig as e, type IBillingPlanDefaults as f, requirePlan as r, withBilling as w };