@xcript-dev/next 0.1.1 → 0.1.3

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.
Files changed (2) hide show
  1. package/README.md +201 -0
  2. package/package.json +1 -1
package/README.md ADDED
@@ -0,0 +1,201 @@
1
+ # @xcript-dev/next
2
+
3
+ **Protect your Next.js app with license validation in 2 minutes.**
4
+
5
+ Edge middleware · React hooks · Server utilities · Zero boilerplate.
6
+
7
+ [![npm](https://img.shields.io/npm/v/@xcript-dev/next)](https://www.npmjs.com/package/@xcript-dev/next)
8
+ [![bundle size](https://img.shields.io/bundlephobia/minzip/@xcript-dev/next)](https://bundlephobia.com/package/@xcript-dev/next)
9
+
10
+ ---
11
+
12
+ ## Why This Instead of the Core SDK?
13
+
14
+ | | `@xcript-dev/sdk` | `@xcript-dev/next` |
15
+ |--|---|---|
16
+ | Validate a license | ✅ Manual call | ✅ **Automatic** at the edge |
17
+ | Cache results | ✅ Disk file | ✅ **httpOnly cookie** (no disk, no state) |
18
+ | Protect routes | ❌ Build it yourself | ✅ **One line** — `protectedRoutes: ['/dashboard/*']` |
19
+ | React UI gating | ❌ Build it yourself | ✅ **`useLicense()` hook** |
20
+ | Server Actions | ❌ Build it yourself | ✅ **`requireLicense()`** |
21
+ | Works with App Router | — | ✅ Built for it |
22
+
23
+ **If you use Next.js, use this package.** It saves you hours of glue code.
24
+
25
+ ---
26
+
27
+ ## Quick Start — 2 Minutes
28
+
29
+ ### 1. Install
30
+
31
+ ```bash
32
+ npm install @xcript-dev/next
33
+ ```
34
+
35
+ ### 2. Add Middleware
36
+
37
+ ```typescript
38
+ // middleware.ts
39
+ import { withXcript } from '@xcript-dev/next'
40
+
41
+ export default withXcript({
42
+ apiKey: process.env.XCRIPT_API_KEY!,
43
+ licenseKey: process.env.XCRIPT_LICENSE_KEY!,
44
+ protectedRoutes: ['/dashboard/*', '/api/*'],
45
+ onInvalid: '/license-expired',
46
+ })
47
+
48
+ export const config = {
49
+ matcher: ['/dashboard/:path*', '/api/:path*'],
50
+ }
51
+ ```
52
+
53
+ ### 3. Done.
54
+
55
+ Every request to `/dashboard/*` now validates the license automatically. Invalid? Redirected. Valid? Passes through. Cached for 5 minutes.
56
+
57
+ ---
58
+
59
+ ## What You Get
60
+
61
+ - **🛡️ Edge validation** — License checked before your page even renders
62
+ - **🍪 Automatic caching** — Result stored in httpOnly cookie, re-validates every 5 min
63
+ - **⚛️ React hook** — `useLicense()` for conditional UI in client components
64
+ - **🔒 Server utilities** — `requireLicense()` for protected Server Actions
65
+ - **🎛️ Feature flags** — `config['max_users']` from your dashboard, no redeploy
66
+ - **🚫 Kill switch** — Revoke access instantly, middleware blocks on next request
67
+ - **📦 5 KB** — Tiny. No bloat.
68
+
69
+ ---
70
+
71
+ ## Full Example: Protected SaaS Dashboard
72
+
73
+ ### `middleware.ts` — Gate all dashboard routes
74
+
75
+ ```typescript
76
+ import { withXcript } from '@xcript-dev/next'
77
+
78
+ export default withXcript({
79
+ apiKey: process.env.XCRIPT_API_KEY!,
80
+ licenseKey: process.env.XCRIPT_LICENSE_KEY!,
81
+ protectedRoutes: ['/dashboard/*'],
82
+ onInvalid: '/license-expired',
83
+ revalidateInterval: 300, // Re-check every 5 min
84
+ })
85
+
86
+ export const config = { matcher: ['/dashboard/:path*'] }
87
+ ```
88
+
89
+ ### `app/layout.tsx` — Provide license context
90
+
91
+ ```tsx
92
+ import { XcriptProvider } from '@xcript-dev/next/client'
93
+
94
+ export default function DashboardLayout({ children }) {
95
+ return <XcriptProvider>{children}</XcriptProvider>
96
+ }
97
+ ```
98
+
99
+ ### `app/dashboard/reports/page.tsx` — Gate UI by features
100
+
101
+ ```tsx
102
+ 'use client'
103
+ import { useLicense } from '@xcript-dev/next/client'
104
+
105
+ export default function ReportsPage() {
106
+ const { isValid, config, isLoading } = useLicense()
107
+
108
+ if (isLoading) return <p>Loading...</p>
109
+ if (!isValid) return <p>License required. <a href="/pricing">Upgrade</a></p>
110
+
111
+ const canExport = config['features']?.includes('export')
112
+
113
+ return (
114
+ <div>
115
+ <h1>Reports</h1>
116
+ <p>Max users: {config['max_users'] || '∞'}</p>
117
+ {canExport && <button>Export CSV</button>}
118
+ </div>
119
+ )
120
+ }
121
+ ```
122
+
123
+ ### `app/actions/generate.ts` — Protect server logic
124
+
125
+ ```typescript
126
+ 'use server'
127
+ import { requireLicense } from '@xcript-dev/next/server'
128
+
129
+ export async function generateReport(data: FormData) {
130
+ const license = await requireLicense() // Throws 403 if invalid
131
+
132
+ const maxReports = parseInt(license.config['max_reports'] || '10')
133
+ // ... generate report
134
+ }
135
+ ```
136
+
137
+ ---
138
+
139
+ ## How It Works
140
+
141
+ ```
142
+ Request hits your app
143
+
144
+
145
+ Middleware checks cookie (__xcript_status)
146
+
147
+ ├── ✅ Cookie valid + fresh (<5 min) → Pass through
148
+
149
+ ├── ⏰ Cookie expired → Re-validate against Xcript API
150
+ │ ├── ✅ Valid → Update cookie → Pass through
151
+ │ └── ❌ Invalid → Redirect to /license-expired
152
+
153
+ └── 🆕 No cookie → Validate against Xcript API
154
+ ├── ✅ Valid → Set cookie → Pass through
155
+ └── ❌ Invalid → Redirect to /license-expired
156
+ ```
157
+
158
+ ---
159
+
160
+ ## Configuration
161
+
162
+ ### Middleware Options
163
+
164
+ | Option | Type | Default | Description |
165
+ |--------|------|---------|-------------|
166
+ | `apiKey` | `string` | Required | Your API key (`xk_...`) |
167
+ | `licenseKey` | `string` | Required | License key to validate |
168
+ | `publicKey` | `string` | — | Ed25519 public key for signature verification |
169
+ | `protectedRoutes` | `string[]` | All routes | Glob patterns to protect |
170
+ | `onInvalid` | `string` | Returns 403 | Redirect path on invalid license |
171
+ | `revalidateInterval` | `number` | `300` (5 min) | Seconds between re-validations |
172
+ | `baseUrl` | `string` | `https://api.xcript.dev` | API base URL |
173
+
174
+ ### Environment Variables
175
+
176
+ ```env
177
+ XCRIPT_API_KEY=xk_your_api_key
178
+ XCRIPT_LICENSE_KEY=XCR-XXXX-XXXX-XXXX
179
+ XCRIPT_PUBLIC_KEY=ed25519_hex_optional
180
+ ```
181
+
182
+ ---
183
+
184
+ ## Requirements
185
+
186
+ - Next.js ≥ 14 (App Router)
187
+ - React ≥ 18
188
+
189
+ ## Core SDK
190
+
191
+ For non-Next.js projects → [`@xcript-dev/sdk`](https://www.npmjs.com/package/@xcript-dev/sdk)
192
+
193
+ ## Links
194
+
195
+ - 🌐 [xcript.dev](https://xcript.dev)
196
+ - 📖 [Documentation](https://xcript.dev/docs)
197
+ - 🐛 [Issues](https://github.com/xcript/xcript-js/issues)
198
+
199
+ ---
200
+
201
+ MIT © [Xcript](https://xcript.dev)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xcript-dev/next",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "Next.js integration for Xcript — middleware, hooks, and server utilities for license validation",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",