@farm.js/autumn 0.1.50 → 0.1.51-beta.1
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/README.md +114 -3
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,11 +1,122 @@
|
|
|
1
1
|
# @farm.js/autumn
|
|
2
2
|
|
|
3
|
-
Autumn billing
|
|
3
|
+
Autumn billing for Farm.js applications. The integration mounts typed routes for the plan catalog,
|
|
4
|
+
checkout, the customer portal, invoices, and billing status, entitlement, and usage checks. Autumn
|
|
5
|
+
keeps the billing state, and Farm reads it for each owner.
|
|
4
6
|
|
|
5
7
|
See [Stability and Support](https://farmjs.dev/docs/stability) for what this package guarantees.
|
|
6
8
|
|
|
9
|
+
## Install
|
|
10
|
+
|
|
7
11
|
```bash
|
|
8
|
-
|
|
12
|
+
pnpm add @farm.js/autumn
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
`autumn-js` and `standardwebhooks` are dependencies and install with the package. To scaffold the
|
|
16
|
+
registry entry and a pricing page at `/integrations/autumn` in an existing app, run
|
|
17
|
+
`farm add integration autumn --ui`.
|
|
18
|
+
|
|
19
|
+
## Configure
|
|
20
|
+
|
|
21
|
+
`billing` is required, and its `resolveOwner` decides who the signed-in caller bills as.
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
// src/lib/integrations.ts
|
|
25
|
+
import { autumn } from "@farm.js/autumn";
|
|
26
|
+
import { getUser } from "./session";
|
|
27
|
+
|
|
28
|
+
export const appIntegrations = {
|
|
29
|
+
billing: autumn({
|
|
30
|
+
billing: {
|
|
31
|
+
async resolveOwner(ctx) {
|
|
32
|
+
const user = await getUser(ctx.request);
|
|
33
|
+
return user ? { kind: "user", id: user.id, email: user.email } : null;
|
|
34
|
+
},
|
|
35
|
+
plans: { pro: { features: { analytics: true }, limits: { tokensMonthly: 1_000_000 } } },
|
|
36
|
+
products: {
|
|
37
|
+
proMonthly: { kind: "subscription", planId: "pro", autumn: { planId: "pro_monthly" } },
|
|
38
|
+
},
|
|
39
|
+
meters: {
|
|
40
|
+
tokensMonthly: { aggregation: "sum", eventName: "ai_usage", featureId: "tokens" },
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
}),
|
|
44
|
+
} as const;
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
| Variable | Purpose |
|
|
48
|
+
| ----------------------- | --------------------------------------------------------------------------- |
|
|
49
|
+
| `AUTUMN_SECRET_KEY` | Builds the Autumn client. Required unless `instance` or `secretKey` is set. |
|
|
50
|
+
| `AUTUMN_WEBHOOK_SECRET` | Default secret for the webhook route. |
|
|
51
|
+
| `APP_BASE_URL` | Origin for checkout and portal return URLs. Defaults to the request origin. |
|
|
52
|
+
|
|
53
|
+
`serverURL` points the client at another Autumn API. It is an option only, with no environment
|
|
54
|
+
variable. Pass an app-owned Autumn SDK client as `instance` to keep construction in app code.
|
|
55
|
+
|
|
56
|
+
## Catalog rules
|
|
57
|
+
|
|
58
|
+
`autumn()` throws at startup when the catalog is incomplete:
|
|
59
|
+
|
|
60
|
+
- every product needs the Autumn plan it sells, as `autumn.planId` or `autumnPlanId`;
|
|
61
|
+
- every meter needs the Autumn feature it reports to, as `featureId`.
|
|
62
|
+
|
|
63
|
+
A product's `autumn` block can also set `featureQuantities` and `items` to customize the attached
|
|
64
|
+
plan.
|
|
65
|
+
|
|
66
|
+
## Customers
|
|
67
|
+
|
|
68
|
+
Each owner is a `"user"` or an `"organization"` and maps to one Autumn customer. Its `name`,
|
|
69
|
+
`fingerprint`, and `metadata` are passed to Autumn when the customer is created. The customer id is
|
|
70
|
+
`<kind>_<id>`, such as `user_42`. An id with other characters, such as an email, is encoded as
|
|
71
|
+
`<kind>_-<base64url>` so two owners never share a customer. `billing.resolveExternalCustomerId`
|
|
72
|
+
replaces the scheme.
|
|
73
|
+
|
|
74
|
+
Customers created before this encoding used a lossy form of the id. Return
|
|
75
|
+
`legacyAutumnExternalCustomerId(owner)` from `resolveExternalCustomerId` only while migrating them.
|
|
76
|
+
|
|
77
|
+
## Routes
|
|
78
|
+
|
|
79
|
+
Routes mount under `/billing`: `products`, `status`, `current-charges`, `invoices`, `features`,
|
|
80
|
+
`limits`, `usage`, `meter-usage`, `report-usage`, `check`, `checkout`, and `portal`. Each has a
|
|
81
|
+
matching option, such as `invoicesPath`, to move it. Call them through `createIntegrations`:
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
const checkout = await apiClient.billing.checkout({
|
|
85
|
+
body: { productId: "proMonthly", successPath: "/billing/success" },
|
|
86
|
+
});
|
|
9
87
|
```
|
|
10
88
|
|
|
11
|
-
|
|
89
|
+
The routes act for the caller from its cookies, so requests from other origins are refused unless
|
|
90
|
+
listed in `allowedOrigins`. `hooks.onUsageReported` runs after each usage report.
|
|
91
|
+
|
|
92
|
+
## Webhooks
|
|
93
|
+
|
|
94
|
+
The webhook route mounts only when `webhooks` is set, at `/billing/webhook` unless it sets a `path`:
|
|
95
|
+
|
|
96
|
+
```ts
|
|
97
|
+
autumn({
|
|
98
|
+
webhooks: {
|
|
99
|
+
async onEvent(event) {
|
|
100
|
+
console.log(event.type, event.id);
|
|
101
|
+
},
|
|
102
|
+
},
|
|
103
|
+
billing,
|
|
104
|
+
});
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
The route verifies the Standard Webhooks signature with `AUTUMN_WEBHOOK_SECRET` or `secret`, then
|
|
108
|
+
calls `onEvent`. A bad signature gets `403`, and `onError` can return a response of its own.
|
|
109
|
+
|
|
110
|
+
## Client entry
|
|
111
|
+
|
|
112
|
+
`@farm.js/autumn/client` exports `autumnClient`, `createAutumnClientApi`, and the request and
|
|
113
|
+
response types without loading the Autumn SDK, so browser code can import it.
|
|
114
|
+
|
|
115
|
+
## Documentation
|
|
116
|
+
|
|
117
|
+
- [Autumn integration](https://farmjs.dev/docs/integrations/autumn)
|
|
118
|
+
- [`examples/autumn-integration`](https://github.com/farming-labs/farm.js/tree/main/examples/autumn-integration)
|
|
119
|
+
bills Better Auth organizations, with seat and token meters
|
|
120
|
+
|
|
121
|
+
Report issues and find examples in the
|
|
122
|
+
[Farm.js repository](https://github.com/farming-labs/farm.js).
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@farm.js/autumn",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.51-beta.1",
|
|
4
4
|
"description": "Autumn billing integration for Farm.js",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -31,8 +31,8 @@
|
|
|
31
31
|
"dependencies": {
|
|
32
32
|
"autumn-js": "^1.2.7",
|
|
33
33
|
"standardwebhooks": "^1.0.0",
|
|
34
|
-
"@farm.js/integration-utils": "0.1.
|
|
35
|
-
"@farm.js/core": "0.1.
|
|
34
|
+
"@farm.js/integration-utils": "0.1.51-beta.1",
|
|
35
|
+
"@farm.js/core": "0.1.51-beta.1"
|
|
36
36
|
},
|
|
37
37
|
"scripts": {
|
|
38
38
|
"build": "node -e \"require('node:fs').rmSync('dist', { recursive: true, force: true })\" && tsc",
|