@ai-billing/nextjs 0.0.0 → 0.0.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
CHANGED
|
@@ -6,24 +6,52 @@ Next.js UI components for displaying billing usage and managing top-ups.
|
|
|
6
6
|
npm install @ai-billing/nextjs
|
|
7
7
|
```
|
|
8
8
|
|
|
9
|
-
##
|
|
9
|
+
## Polar
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
Display usage for Polar meters and let users purchase credit bundles.
|
|
12
12
|
|
|
13
13
|
```tsx
|
|
14
|
-
import { CreditUsagePolar,
|
|
14
|
+
import { CreditUsagePolar, CreditTopUpPolar } from '@ai-billing/nextjs';
|
|
15
15
|
|
|
16
16
|
<CreditUsagePolar userId="user_123" budget={50} />
|
|
17
|
-
<CreditUsageStripe stripeCustomerId="cus_123" budget={100} />
|
|
18
17
|
<CreditTopUpPolar userId="user_123" />
|
|
19
18
|
```
|
|
20
19
|
|
|
21
20
|
### budget vs top-up
|
|
22
21
|
|
|
23
|
-
- **`budget`** — use when billing is usage-based (e.g. monthly invoice per consumption). Represents a spending cap.
|
|
24
|
-
- **No `budget`** — use together with `CreditTopUpPolar` when users pre-purchase credits. Omit `budget` and the cap is automatically set to the account's `creditedUnits
|
|
22
|
+
- **`budget`** — use when billing is usage-based (e.g. monthly invoice per consumption). Represents a spending cap.
|
|
23
|
+
- **No `budget`** — use together with `CreditTopUpPolar` when users pre-purchase credits. Omit `budget` and the cap is automatically set to the account's `creditedUnits`.
|
|
25
24
|
|
|
26
|
-
|
|
25
|
+
### Environment Variables
|
|
26
|
+
|
|
27
|
+
| Variable | Required |
|
|
28
|
+
|----------|----------|
|
|
29
|
+
| `NAREV_API_KEY` | Config fetch |
|
|
30
|
+
| `POLAR_ACCESS_TOKEN` | Meter usage + top-up |
|
|
31
|
+
| `POLAR_SERVER` | `sandbox` or `production` |
|
|
32
|
+
|
|
33
|
+
## Stripe
|
|
34
|
+
|
|
35
|
+
Display usage for Stripe billing meters.
|
|
36
|
+
|
|
37
|
+
```tsx
|
|
38
|
+
import { CreditUsageStripe } from '@ai-billing/nextjs';
|
|
39
|
+
|
|
40
|
+
<CreditUsageStripe stripeCustomerId="cus_123" budget={100} unit="$" />
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Stripe meters report values in nano-units. The component converts them to dollars and displays them with `$` formatting by default. Use the `unit` prop to override.
|
|
44
|
+
|
|
45
|
+
### Environment Variables
|
|
46
|
+
|
|
47
|
+
| Variable | Required |
|
|
48
|
+
|----------|----------|
|
|
49
|
+
| `NAREV_API_KEY` | Config fetch |
|
|
50
|
+
| `STRIPE_SECRET_KEY` | Meter usage |
|
|
51
|
+
|
|
52
|
+
## Server Actions
|
|
53
|
+
|
|
54
|
+
Server actions are available for advanced use cases:
|
|
27
55
|
|
|
28
56
|
```tsx
|
|
29
57
|
import { fetchPolarUsage, fetchStripeUsage, createCheckout } from '@ai-billing/nextjs/server';
|
|
@@ -42,9 +42,11 @@ async function fetchTopUpConfig() {
|
|
|
42
42
|
if (tb === "inclusive" || tb === "exclusive" || tb === "location") {
|
|
43
43
|
config.taxBehavior = tb;
|
|
44
44
|
}
|
|
45
|
-
} catch {
|
|
45
|
+
} catch (error) {
|
|
46
|
+
console.error("fetchTopUpConfig: failed to fetch tax behavior", error);
|
|
46
47
|
}
|
|
47
|
-
} catch {
|
|
48
|
+
} catch (error) {
|
|
49
|
+
console.error("fetchTopUpConfig: config fetch failed", error);
|
|
48
50
|
}
|
|
49
51
|
return config;
|
|
50
52
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/polar/fetchTopUpConfig.ts"],"sourcesContent":["'use server';\n\nimport { Polar } from '@polar-sh/sdk';\nimport { fetchPolarConfig } from './fetchPolarConfig.js';\nimport type { CreditPackage } from './types.js';\n\ninterface TopUpConfig {\n packages: CreditPackage[];\n taxBehavior?: 'inclusive' | 'exclusive' | 'location';\n}\n\n/** Fetches available top-up packages and optional tax behavior from Polar */\nexport async function fetchTopUpConfig(): Promise<TopUpConfig> {\n const config: TopUpConfig = { packages: [] };\n\n try {\n const polarConfig = await fetchPolarConfig();\n if (!polarConfig) return config;\n\n config.packages = polarConfig.topup ?? [];\n\n const env = polarConfig.environment ?? 'sandbox';\n try {\n const polar = new Polar({\n accessToken: process.env.POLAR_ACCESS_TOKEN,\n server: env,\n });\n const orgs = await polar.organizations.list({ limit: 1 });\n const org = orgs.result?.items?.[0];\n const tb = org?.defaultTaxBehavior;\n if (tb === 'inclusive' || tb === 'exclusive' || tb === 'location') {\n config.taxBehavior = tb as typeof config.taxBehavior;\n }\n } catch {\n
|
|
1
|
+
{"version":3,"sources":["../../src/polar/fetchTopUpConfig.ts"],"sourcesContent":["'use server';\n\nimport { Polar } from '@polar-sh/sdk';\nimport { fetchPolarConfig } from './fetchPolarConfig.js';\nimport type { CreditPackage } from './types.js';\n\ninterface TopUpConfig {\n packages: CreditPackage[];\n taxBehavior?: 'inclusive' | 'exclusive' | 'location';\n}\n\n/** Fetches available top-up packages and optional tax behavior from Polar */\nexport async function fetchTopUpConfig(): Promise<TopUpConfig> {\n const config: TopUpConfig = { packages: [] };\n\n try {\n const polarConfig = await fetchPolarConfig();\n if (!polarConfig) return config;\n\n config.packages = polarConfig.topup ?? [];\n\n const env = polarConfig.environment ?? 'sandbox';\n try {\n const polar = new Polar({\n accessToken: process.env.POLAR_ACCESS_TOKEN,\n server: env,\n });\n const orgs = await polar.organizations.list({ limit: 1 });\n const org = orgs.result?.items?.[0];\n const tb = org?.defaultTaxBehavior;\n if (tb === 'inclusive' || tb === 'exclusive' || tb === 'location') {\n config.taxBehavior = tb as typeof config.taxBehavior;\n }\n } catch (error) {\n console.error('fetchTopUpConfig: failed to fetch tax behavior', error);\n }\n } catch (error) {\n console.error('fetchTopUpConfig: config fetch failed', error);\n }\n\n return config;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAEA,iBAAsB;AACtB,8BAAiC;AASjC,eAAsB,mBAAyC;AAC7D,QAAM,SAAsB,EAAE,UAAU,CAAC,EAAE;AAE3C,MAAI;AACF,UAAM,cAAc,UAAM,0CAAiB;AAC3C,QAAI,CAAC,YAAa,QAAO;AAEzB,WAAO,WAAW,YAAY,SAAS,CAAC;AAExC,UAAM,MAAM,YAAY,eAAe;AACvC,QAAI;AACF,YAAM,QAAQ,IAAI,iBAAM;AAAA,QACtB,aAAa,QAAQ,IAAI;AAAA,QACzB,QAAQ;AAAA,MACV,CAAC;AACD,YAAM,OAAO,MAAM,MAAM,cAAc,KAAK,EAAE,OAAO,EAAE,CAAC;AACxD,YAAM,MAAM,KAAK,QAAQ,QAAQ,CAAC;AAClC,YAAM,KAAK,KAAK;AAChB,UAAI,OAAO,eAAe,OAAO,eAAe,OAAO,YAAY;AACjE,eAAO,cAAc;AAAA,MACvB;AAAA,IACF,SAAS,OAAO;AACd,cAAQ,MAAM,kDAAkD,KAAK;AAAA,IACvE;AAAA,EACF,SAAS,OAAO;AACd,YAAQ,MAAM,yCAAyC,KAAK;AAAA,EAC9D;AAEA,SAAO;AACT;","names":[]}
|
|
@@ -19,9 +19,11 @@ async function fetchTopUpConfig() {
|
|
|
19
19
|
if (tb === "inclusive" || tb === "exclusive" || tb === "location") {
|
|
20
20
|
config.taxBehavior = tb;
|
|
21
21
|
}
|
|
22
|
-
} catch {
|
|
22
|
+
} catch (error) {
|
|
23
|
+
console.error("fetchTopUpConfig: failed to fetch tax behavior", error);
|
|
23
24
|
}
|
|
24
|
-
} catch {
|
|
25
|
+
} catch (error) {
|
|
26
|
+
console.error("fetchTopUpConfig: config fetch failed", error);
|
|
25
27
|
}
|
|
26
28
|
return config;
|
|
27
29
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/polar/fetchTopUpConfig.ts"],"sourcesContent":["'use server';\n\nimport { Polar } from '@polar-sh/sdk';\nimport { fetchPolarConfig } from './fetchPolarConfig.js';\nimport type { CreditPackage } from './types.js';\n\ninterface TopUpConfig {\n packages: CreditPackage[];\n taxBehavior?: 'inclusive' | 'exclusive' | 'location';\n}\n\n/** Fetches available top-up packages and optional tax behavior from Polar */\nexport async function fetchTopUpConfig(): Promise<TopUpConfig> {\n const config: TopUpConfig = { packages: [] };\n\n try {\n const polarConfig = await fetchPolarConfig();\n if (!polarConfig) return config;\n\n config.packages = polarConfig.topup ?? [];\n\n const env = polarConfig.environment ?? 'sandbox';\n try {\n const polar = new Polar({\n accessToken: process.env.POLAR_ACCESS_TOKEN,\n server: env,\n });\n const orgs = await polar.organizations.list({ limit: 1 });\n const org = orgs.result?.items?.[0];\n const tb = org?.defaultTaxBehavior;\n if (tb === 'inclusive' || tb === 'exclusive' || tb === 'location') {\n config.taxBehavior = tb as typeof config.taxBehavior;\n }\n } catch {\n
|
|
1
|
+
{"version":3,"sources":["../../src/polar/fetchTopUpConfig.ts"],"sourcesContent":["'use server';\n\nimport { Polar } from '@polar-sh/sdk';\nimport { fetchPolarConfig } from './fetchPolarConfig.js';\nimport type { CreditPackage } from './types.js';\n\ninterface TopUpConfig {\n packages: CreditPackage[];\n taxBehavior?: 'inclusive' | 'exclusive' | 'location';\n}\n\n/** Fetches available top-up packages and optional tax behavior from Polar */\nexport async function fetchTopUpConfig(): Promise<TopUpConfig> {\n const config: TopUpConfig = { packages: [] };\n\n try {\n const polarConfig = await fetchPolarConfig();\n if (!polarConfig) return config;\n\n config.packages = polarConfig.topup ?? [];\n\n const env = polarConfig.environment ?? 'sandbox';\n try {\n const polar = new Polar({\n accessToken: process.env.POLAR_ACCESS_TOKEN,\n server: env,\n });\n const orgs = await polar.organizations.list({ limit: 1 });\n const org = orgs.result?.items?.[0];\n const tb = org?.defaultTaxBehavior;\n if (tb === 'inclusive' || tb === 'exclusive' || tb === 'location') {\n config.taxBehavior = tb as typeof config.taxBehavior;\n }\n } catch (error) {\n console.error('fetchTopUpConfig: failed to fetch tax behavior', error);\n }\n } catch (error) {\n console.error('fetchTopUpConfig: config fetch failed', error);\n }\n\n return config;\n}\n"],"mappings":";AAEA,SAAS,aAAa;AACtB,SAAS,wBAAwB;AASjC,eAAsB,mBAAyC;AAC7D,QAAM,SAAsB,EAAE,UAAU,CAAC,EAAE;AAE3C,MAAI;AACF,UAAM,cAAc,MAAM,iBAAiB;AAC3C,QAAI,CAAC,YAAa,QAAO;AAEzB,WAAO,WAAW,YAAY,SAAS,CAAC;AAExC,UAAM,MAAM,YAAY,eAAe;AACvC,QAAI;AACF,YAAM,QAAQ,IAAI,MAAM;AAAA,QACtB,aAAa,QAAQ,IAAI;AAAA,QACzB,QAAQ;AAAA,MACV,CAAC;AACD,YAAM,OAAO,MAAM,MAAM,cAAc,KAAK,EAAE,OAAO,EAAE,CAAC;AACxD,YAAM,MAAM,KAAK,QAAQ,QAAQ,CAAC;AAClC,YAAM,KAAK,KAAK;AAChB,UAAI,OAAO,eAAe,OAAO,eAAe,OAAO,YAAY;AACjE,eAAO,cAAc;AAAA,MACvB;AAAA,IACF,SAAS,OAAO;AACd,cAAQ,MAAM,kDAAkD,KAAK;AAAA,IACvE;AAAA,EACF,SAAS,OAAO;AACd,YAAQ,MAAM,yCAAyC,KAAK;AAAA,EAC9D;AAEA,SAAO;AACT;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-billing/nextjs",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.1",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"repository": {
|
|
@@ -33,39 +33,26 @@
|
|
|
33
33
|
}
|
|
34
34
|
},
|
|
35
35
|
"publishConfig": {
|
|
36
|
-
"access": "public"
|
|
36
|
+
"access": "public",
|
|
37
|
+
"provenance": true
|
|
37
38
|
},
|
|
38
39
|
"files": [
|
|
39
40
|
"dist"
|
|
40
41
|
],
|
|
41
|
-
"scripts": {
|
|
42
|
-
"build": "tsup",
|
|
43
|
-
"check-types": "tsc --noEmit",
|
|
44
|
-
"lint": "oxlint",
|
|
45
|
-
"dev": "tsup --watch",
|
|
46
|
-
"test": "vitest run",
|
|
47
|
-
"test:watch": "vitest",
|
|
48
|
-
"test:coverage": "vitest run --coverage",
|
|
49
|
-
"docs:generate": "typedoc --options typedoc.json",
|
|
50
|
-
"prepack": "node -e \"require('fs').copyFileSync('../../LICENSE', 'LICENSE')\"",
|
|
51
|
-
"postpack": "node -e \"require('fs').rmSync('LICENSE', { force: true })\""
|
|
52
|
-
},
|
|
53
42
|
"dependencies": {},
|
|
54
43
|
"devDependencies": {
|
|
55
|
-
"@
|
|
56
|
-
"@types/
|
|
57
|
-
"
|
|
58
|
-
"
|
|
59
|
-
"
|
|
60
|
-
"
|
|
61
|
-
"
|
|
62
|
-
"
|
|
44
|
+
"@types/node": "^22.15.3",
|
|
45
|
+
"@types/react": "19.2.2",
|
|
46
|
+
"tsup": "^8.5.1",
|
|
47
|
+
"typescript": "5.9.2",
|
|
48
|
+
"oxlint": "^1.57.0",
|
|
49
|
+
"typedoc": "^0.28.19",
|
|
50
|
+
"vitest": "4.1.1",
|
|
51
|
+
"@ai-billing/typescript-config": "0.0.1"
|
|
63
52
|
},
|
|
64
53
|
"peerDependencies": {
|
|
65
54
|
"@polar-sh/sdk": "^0.46.7",
|
|
66
|
-
"next": ">=14",
|
|
67
55
|
"react": "^18 || ^19",
|
|
68
|
-
"react-dom": "^18 || ^19",
|
|
69
56
|
"stripe": "^21.0.0"
|
|
70
57
|
},
|
|
71
58
|
"peerDependenciesMeta": {
|
|
@@ -78,5 +65,15 @@
|
|
|
78
65
|
},
|
|
79
66
|
"engines": {
|
|
80
67
|
"node": ">=20.0.0"
|
|
68
|
+
},
|
|
69
|
+
"scripts": {
|
|
70
|
+
"build": "tsup",
|
|
71
|
+
"check-types": "tsc --noEmit",
|
|
72
|
+
"lint": "oxlint",
|
|
73
|
+
"dev": "tsup --watch",
|
|
74
|
+
"test": "vitest run",
|
|
75
|
+
"test:watch": "vitest",
|
|
76
|
+
"test:coverage": "vitest run --coverage",
|
|
77
|
+
"docs:generate": "typedoc --options typedoc.json"
|
|
81
78
|
}
|
|
82
|
-
}
|
|
79
|
+
}
|