@xcript-dev/next 0.1.1 → 0.1.2

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 +129 -0
  2. package/package.json +1 -1
package/README.md ADDED
@@ -0,0 +1,129 @@
1
+ # @xcript-dev/next
2
+
3
+ > Next.js integration for [Xcript](https://xcript.dev) — middleware, React hooks, and server utilities for license validation.
4
+
5
+ [![npm](https://img.shields.io/npm/v/@xcript-dev/next)](https://www.npmjs.com/package/@xcript-dev/next)
6
+ [![bundle size](https://img.shields.io/bundlephobia/minzip/@xcript-dev/next)](https://bundlephobia.com/package/@xcript-dev/next)
7
+ [![license](https://img.shields.io/npm/l/@xcript-dev/next)](https://github.com/xcript/xcript-js/blob/main/LICENSE)
8
+
9
+ ## Install
10
+
11
+ ```bash
12
+ npm install @xcript-dev/next
13
+ ```
14
+
15
+ ## 1. Middleware — Protect Routes
16
+
17
+ ```typescript
18
+ // middleware.ts
19
+ import { withXcript } from '@xcript-dev/next'
20
+
21
+ export default withXcript({
22
+ apiKey: process.env.XCRIPT_API_KEY!,
23
+ licenseKey: process.env.XCRIPT_LICENSE_KEY!,
24
+ protectedRoutes: ['/dashboard/*', '/api/*'],
25
+ onInvalid: '/license-expired',
26
+ })
27
+
28
+ export const config = {
29
+ matcher: ['/dashboard/:path*', '/api/:path*'],
30
+ }
31
+ ```
32
+
33
+ Validates once, caches in a cookie, re-validates every 5 minutes. If invalid → redirects to `onInvalid`.
34
+
35
+ ## 2. React Hook — Conditional UI
36
+
37
+ ```tsx
38
+ 'use client'
39
+ import { useLicense } from '@xcript-dev/next/client'
40
+
41
+ function PremiumFeature() {
42
+ const { isValid, config, isLoading } = useLicense()
43
+
44
+ if (isLoading) return <Spinner />
45
+ if (!isValid) return <UpgradePrompt />
46
+
47
+ return <PremiumContent maxUsers={config.max_users} />
48
+ }
49
+ ```
50
+
51
+ ## 3. Provider — Wrap Your Layout
52
+
53
+ ```tsx
54
+ // app/layout.tsx
55
+ import { XcriptProvider } from '@xcript-dev/next/client'
56
+
57
+ export default function Layout({ children }) {
58
+ return <XcriptProvider>{children}</XcriptProvider>
59
+ }
60
+ ```
61
+
62
+ ## 4. Server Utilities — Protect Actions
63
+
64
+ ```typescript
65
+ // Server Action or Route Handler
66
+ import { requireLicense } from '@xcript-dev/next/server'
67
+
68
+ export async function POST() {
69
+ const license = await requireLicense() // Throws if invalid
70
+ const maxReports = license.config['max_reports']
71
+ return Response.json({ ok: true })
72
+ }
73
+ ```
74
+
75
+ ### Other server utilities
76
+
77
+ ```typescript
78
+ import { getLicense, getLicenseConfig } from '@xcript-dev/next/server'
79
+
80
+ // Returns null if invalid (no throw)
81
+ const license = await getLicense()
82
+
83
+ // Get a single config value
84
+ const maxUsers = await getLicenseConfig('max_users')
85
+ ```
86
+
87
+ ## Middleware Options
88
+
89
+ | Option | Type | Required | Default | Description |
90
+ |--------|------|----------|---------|-------------|
91
+ | `apiKey` | `string` | ✅ | — | Your API key (`xk_...`) |
92
+ | `licenseKey` | `string` | ✅ | — | License key to validate |
93
+ | `publicKey` | `string` | — | — | Ed25519 public key (hex) |
94
+ | `protectedRoutes` | `string[]` | — | All routes | Glob patterns (`*` wildcard) |
95
+ | `onInvalid` | `string` | — | Returns 403 | Redirect path on failure |
96
+ | `revalidateInterval` | `number` | — | `300` (5 min) | Re-validation interval in seconds |
97
+
98
+ ## Environment Variables
99
+
100
+ ```env
101
+ XCRIPT_API_KEY=xk_your_api_key
102
+ XCRIPT_LICENSE_KEY=XCR-XXXX-XXXX-XXXX
103
+ ```
104
+
105
+ ## How It Works
106
+
107
+ ```
108
+ Request → Middleware checks cookie
109
+ ├── Cookie valid + fresh → Pass through ✅
110
+ ├── Cookie expired → Re-validate against API
111
+ │ ├── Valid → Update cookie, pass through ✅
112
+ │ └── Invalid → Redirect to onInvalid ❌
113
+ └── No cookie → Validate against API
114
+ ├── Valid → Set cookie, pass through ✅
115
+ └── Invalid → Redirect to onInvalid ❌
116
+ ```
117
+
118
+ ## Requirements
119
+
120
+ - Next.js ≥ 14
121
+ - React ≥ 18
122
+
123
+ ## Core SDK
124
+
125
+ This package uses [`@xcript-dev/sdk`](https://www.npmjs.com/package/@xcript-dev/sdk) under the hood. For non-Next.js projects, install the SDK directly.
126
+
127
+ ## License
128
+
129
+ MIT — [xcript.dev](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.2",
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",