@demokit-ai/next 0.0.2 → 0.2.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/README.md +193 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
# @demokit-ai/next
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+

|
|
5
|
+
|
|
6
|
+
Next.js adapter for DemoKit - App Router, middleware, and API route mocking for demo mode.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install @demokit-ai/next @demokit-ai/core @demokit-ai/react
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Features
|
|
15
|
+
|
|
16
|
+
- Demo-aware middleware for URL parameter and cookie handling
|
|
17
|
+
- Server-side demo context with AsyncLocalStorage
|
|
18
|
+
- Client-side hooks for demo mode state
|
|
19
|
+
- API route interceptors for fixture responses
|
|
20
|
+
- Scenario-based demo configurations
|
|
21
|
+
- Full TypeScript support
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
### Middleware Setup
|
|
26
|
+
|
|
27
|
+
Add demo mode detection to your middleware:
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
// middleware.ts
|
|
31
|
+
import { createDemoMiddleware } from '@demokit-ai/next/middleware'
|
|
32
|
+
|
|
33
|
+
export const middleware = createDemoMiddleware({
|
|
34
|
+
cookieName: 'demo-mode',
|
|
35
|
+
paramName: 'demo',
|
|
36
|
+
scenarios: {
|
|
37
|
+
default: { users: 'standard' },
|
|
38
|
+
enterprise: { users: 'enterprise', features: 'all' },
|
|
39
|
+
},
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
export const config = {
|
|
43
|
+
matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'],
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Server Components
|
|
48
|
+
|
|
49
|
+
Access demo mode in Server Components:
|
|
50
|
+
|
|
51
|
+
```tsx
|
|
52
|
+
// app/users/page.tsx
|
|
53
|
+
import { getServerDemoContext, isServerDemoMode } from '@demokit-ai/next/server'
|
|
54
|
+
|
|
55
|
+
export default async function UsersPage() {
|
|
56
|
+
const isDemo = isServerDemoMode()
|
|
57
|
+
const context = getServerDemoContext()
|
|
58
|
+
|
|
59
|
+
if (isDemo) {
|
|
60
|
+
return <UserList users={context.fixtures.users} />
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const users = await fetchRealUsers()
|
|
64
|
+
return <UserList users={users} />
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### Client Components
|
|
69
|
+
|
|
70
|
+
Use the client-side hook:
|
|
71
|
+
|
|
72
|
+
```tsx
|
|
73
|
+
'use client'
|
|
74
|
+
import { useNextDemoMode } from '@demokit-ai/next/client'
|
|
75
|
+
|
|
76
|
+
export function DemoToggle() {
|
|
77
|
+
const { enabled, toggle, scenario, setScenario } = useNextDemoMode()
|
|
78
|
+
|
|
79
|
+
return (
|
|
80
|
+
<div>
|
|
81
|
+
<button onClick={toggle}>
|
|
82
|
+
Demo: {enabled ? 'ON' : 'OFF'}
|
|
83
|
+
</button>
|
|
84
|
+
{enabled && (
|
|
85
|
+
<select value={scenario} onChange={(e) => setScenario(e.target.value)}>
|
|
86
|
+
<option value="default">Default</option>
|
|
87
|
+
<option value="enterprise">Enterprise</option>
|
|
88
|
+
</select>
|
|
89
|
+
)}
|
|
90
|
+
</div>
|
|
91
|
+
)
|
|
92
|
+
}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### API Route Interception
|
|
96
|
+
|
|
97
|
+
Create demo-aware API routes:
|
|
98
|
+
|
|
99
|
+
```ts
|
|
100
|
+
// app/api/users/route.ts
|
|
101
|
+
import { createServerInterceptor } from '@demokit-ai/next/server'
|
|
102
|
+
|
|
103
|
+
const interceptor = createServerInterceptor({
|
|
104
|
+
fixtures: {
|
|
105
|
+
GET: [
|
|
106
|
+
{ id: '1', name: 'Demo User' },
|
|
107
|
+
{ id: '2', name: 'Another User' },
|
|
108
|
+
],
|
|
109
|
+
POST: (request) => ({
|
|
110
|
+
id: '3',
|
|
111
|
+
name: request.body.name,
|
|
112
|
+
created: true,
|
|
113
|
+
}),
|
|
114
|
+
},
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
export async function GET(request: Request) {
|
|
118
|
+
return interceptor(request, async () => {
|
|
119
|
+
// Real implementation
|
|
120
|
+
const users = await db.users.findMany()
|
|
121
|
+
return Response.json(users)
|
|
122
|
+
})
|
|
123
|
+
}
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### Configuration
|
|
127
|
+
|
|
128
|
+
Define demo scenarios and fixtures:
|
|
129
|
+
|
|
130
|
+
```ts
|
|
131
|
+
// demo.config.ts
|
|
132
|
+
import { createDemoConfig, defineFixtures, defineScenarios } from '@demokit-ai/next'
|
|
133
|
+
|
|
134
|
+
export const demoConfig = createDemoConfig({
|
|
135
|
+
fixtures: defineFixtures({
|
|
136
|
+
users: [
|
|
137
|
+
{ id: '1', name: 'Demo User', plan: 'free' },
|
|
138
|
+
{ id: '2', name: 'Pro User', plan: 'pro' },
|
|
139
|
+
],
|
|
140
|
+
products: [
|
|
141
|
+
{ id: '1', name: 'Widget', price: 9.99 },
|
|
142
|
+
],
|
|
143
|
+
}),
|
|
144
|
+
scenarios: defineScenarios({
|
|
145
|
+
default: {},
|
|
146
|
+
enterprise: {
|
|
147
|
+
users: [
|
|
148
|
+
{ id: '1', name: 'Enterprise User', plan: 'enterprise' },
|
|
149
|
+
],
|
|
150
|
+
},
|
|
151
|
+
}),
|
|
152
|
+
})
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
## API Reference
|
|
156
|
+
|
|
157
|
+
### Middleware
|
|
158
|
+
|
|
159
|
+
#### `createDemoMiddleware(options)`
|
|
160
|
+
|
|
161
|
+
Options:
|
|
162
|
+
- `cookieName` - Cookie name for demo state (default: 'demo-mode')
|
|
163
|
+
- `paramName` - URL parameter for demo activation (default: 'demo')
|
|
164
|
+
- `scenarios` - Scenario configurations
|
|
165
|
+
- `defaultScenario` - Default scenario name
|
|
166
|
+
|
|
167
|
+
### Server
|
|
168
|
+
|
|
169
|
+
#### `isServerDemoMode()`
|
|
170
|
+
|
|
171
|
+
Returns `true` if demo mode is active in the current request.
|
|
172
|
+
|
|
173
|
+
#### `getServerDemoContext()`
|
|
174
|
+
|
|
175
|
+
Returns the demo context including fixtures and scenario.
|
|
176
|
+
|
|
177
|
+
#### `createServerInterceptor(options)`
|
|
178
|
+
|
|
179
|
+
Creates an interceptor for API routes.
|
|
180
|
+
|
|
181
|
+
### Client
|
|
182
|
+
|
|
183
|
+
#### `useNextDemoMode()`
|
|
184
|
+
|
|
185
|
+
Returns:
|
|
186
|
+
- `enabled` - Demo mode state
|
|
187
|
+
- `toggle()` - Toggle demo mode
|
|
188
|
+
- `scenario` - Current scenario
|
|
189
|
+
- `setScenario(name)` - Switch scenario
|
|
190
|
+
|
|
191
|
+
## License
|
|
192
|
+
|
|
193
|
+
MIT
|