@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 +21 -0
- package/README.md +51 -0
- package/dist/index-CL94UDoW.d.ts +107 -0
- package/dist/index-CRFRDPsJ.d.cts +107 -0
- package/dist/index.cjs +1170 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +144 -0
- package/dist/index.d.ts +144 -0
- package/dist/index.js +1120 -0
- package/dist/index.js.map +1 -0
- package/dist/middlewares/index.cjs +254 -0
- package/dist/middlewares/index.cjs.map +1 -0
- package/dist/middlewares/index.d.cts +4 -0
- package/dist/middlewares/index.d.ts +4 -0
- package/dist/middlewares/index.js +226 -0
- package/dist/middlewares/index.js.map +1 -0
- package/dist/migrations/index.js +7 -0
- package/dist/migrations/index.js.map +1 -0
- package/dist/migrations/sql/001_billing.sql +38 -0
- package/dist/migrations/sql/002_billing_plan_fields.sql +5 -0
- package/dist/migrations/sql/003_drop_limits.sql +1 -0
- package/dist/migrations/sql/004_polymorphic_subscribers.sql +49 -0
- package/dist/migrations/sql/005_billing_notifications.sql +13 -0
- package/dist/types.cjs +19 -0
- package/dist/types.cjs.map +1 -0
- package/dist/types.d.cts +76 -0
- package/dist/types.d.ts +76 -0
- package/dist/types.js +1 -0
- package/dist/types.js.map +1 -0
- package/package.json +86 -0
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 };
|