Skip to content

Commit a4665cb

Browse files
Copilothuangyiirene
andcommitted
Add dev handler for browser-based code editing and API testing
Co-authored-by: huangyiirene <7665279+huangyiirene@users.noreply.github.com>
1 parent b4a014b commit a4665cb

11 files changed

Lines changed: 1914 additions & 130 deletions

File tree

Lines changed: 271 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
1+
# ObjectQL Development Playground
2+
3+
A browser-based development environment for ObjectQL that enables code browsing, editing, and API testing in the browser - similar to shadcn's approach.
4+
5+
## Features
6+
7+
**File Browser**
8+
- Tree view of your ObjectQL project structure
9+
- Filter by file type (`.object.yml`, `.validation.yml`, `.action.ts`, etc.)
10+
- Real-time file list refresh
11+
12+
📝 **Code Editor**
13+
- Multi-tab editing interface
14+
- Syntax highlighting for YAML, TypeScript, and JSON
15+
- Auto-save detection (Ctrl+S / Cmd+S)
16+
- Modified file indicators
17+
18+
🧪 **API Testing Playground**
19+
- Interactive API testing panel
20+
- Test CRUD operations (find, findOne, create, update, delete)
21+
- JSON request/response viewer
22+
- Real-time execution
23+
24+
🔒 **Security**
25+
- Only enabled in development mode
26+
- File operation restrictions to `src/` directory
27+
- Whitelisted file extensions
28+
- Path traversal protection
29+
30+
## Quick Start
31+
32+
### 1. Start the Backend Server
33+
34+
```bash
35+
cd examples/integrations/dev-playground
36+
pnpm install
37+
pnpm run server
38+
```
39+
40+
The server will start on `http://localhost:3000` with:
41+
- Dev API: `http://localhost:3000/api/dev/`
42+
- REST API: `http://localhost:3000/api/data/`
43+
44+
### 2. Start the Frontend (in another terminal)
45+
46+
```bash
47+
pnpm run dev
48+
```
49+
50+
The UI will open on `http://localhost:5173`
51+
52+
## API Endpoints
53+
54+
### Development API
55+
56+
| Method | Endpoint | Description |
57+
|--------|----------|-------------|
58+
| GET | `/api/dev/files` | List all files in src directory |
59+
| GET | `/api/dev/files/:path` | Read file content |
60+
| PUT | `/api/dev/files/:path` | Update file content |
61+
| POST | `/api/dev/files` | Create new file |
62+
| DELETE | `/api/dev/files/:path` | Delete file |
63+
| GET | `/api/dev/metadata` | Get ObjectQL metadata |
64+
65+
### Example: List Files
66+
67+
```bash
68+
curl http://localhost:3000/api/dev/files
69+
```
70+
71+
Response:
72+
```json
73+
{
74+
"baseDir": "/path/to/project/src",
75+
"tree": [
76+
{
77+
"name": "objects",
78+
"path": "objects",
79+
"type": "directory",
80+
"children": [
81+
{
82+
"name": "project.object.yml",
83+
"path": "objects/project.object.yml",
84+
"type": "file",
85+
"size": 1234,
86+
"modified": "2024-01-17T10:30:00.000Z"
87+
}
88+
]
89+
}
90+
]
91+
}
92+
```
93+
94+
### Example: Read File
95+
96+
```bash
97+
curl http://localhost:3000/api/dev/files/objects/project.object.yml
98+
```
99+
100+
Response:
101+
```json
102+
{
103+
"path": "objects/project.object.yml",
104+
"content": "name: project\nlabel: Project\n...",
105+
"size": 1234,
106+
"modified": "2024-01-17T10:30:00.000Z",
107+
"extension": ".yml"
108+
}
109+
```
110+
111+
### Example: Update File
112+
113+
```bash
114+
curl -X PUT http://localhost:3000/api/dev/files/objects/project.object.yml \
115+
-H "Content-Type: application/json" \
116+
-d '{"content": "name: project\nlabel: Updated Project\n..."}'
117+
```
118+
119+
### Example: Create File
120+
121+
```bash
122+
curl -X POST http://localhost:3000/api/dev/files \
123+
-H "Content-Type: application/json" \
124+
-d '{
125+
"path": "objects/task.object.yml",
126+
"content": "name: task\nlabel: Task\n..."
127+
}'
128+
```
129+
130+
## Integration with Your Project
131+
132+
To add the dev playground to your existing ObjectQL project:
133+
134+
### 1. Install Dependencies
135+
136+
```bash
137+
pnpm add @objectql/server
138+
```
139+
140+
### 2. Add Dev Handler to Your Server
141+
142+
```typescript
143+
import { createDevHandler } from '@objectql/server';
144+
145+
const devHandler = createDevHandler({
146+
enabled: process.env.NODE_ENV !== 'production',
147+
baseDir: process.cwd()
148+
});
149+
150+
// In your HTTP server handler:
151+
if (req.url?.startsWith('/api/dev/')) {
152+
await devHandler(req, res);
153+
}
154+
```
155+
156+
### 3. Copy the HTML UI
157+
158+
Copy `index.html` to your project's static directory and serve it.
159+
160+
## Configuration
161+
162+
### DevHandlerOptions
163+
164+
```typescript
165+
interface DevHandlerOptions {
166+
/** Base directory for file operations (defaults to process.cwd()) */
167+
baseDir?: string;
168+
169+
/** Enable dev mode (must be explicitly enabled) */
170+
enabled?: boolean;
171+
172+
/** Allowed file extensions for editing */
173+
allowedExtensions?: string[];
174+
}
175+
```
176+
177+
### Example Configuration
178+
179+
```typescript
180+
const devHandler = createDevHandler({
181+
baseDir: '/path/to/your/project',
182+
enabled: process.env.NODE_ENV === 'development',
183+
allowedExtensions: [
184+
'.yml', '.yaml', '.ts', '.js', '.json', '.md',
185+
'.object.yml', '.validation.yml', '.permission.yml',
186+
'.hook.ts', '.action.ts', '.app.yml'
187+
]
188+
});
189+
```
190+
191+
## Security Considerations
192+
193+
⚠️ **IMPORTANT**: This dev handler should ONLY be enabled in development environments.
194+
195+
The handler includes several security measures:
196+
- Only enabled when `enabled: true` is explicitly set
197+
- Defaults to production-safe (disabled) mode
198+
- Restricts file operations to the `src/` directory
199+
- Validates file extensions against a whitelist
200+
- Protects against path traversal attacks
201+
- Requires explicit file paths (no wildcards)
202+
203+
**Never enable this in production!**
204+
205+
## Use Cases
206+
207+
### 1. Remote Development
208+
Work on ObjectQL projects from any device with just a browser - perfect for cloud IDEs, Chromebooks, or tablets.
209+
210+
### 2. Live Workshops & Training
211+
Teach ObjectQL concepts with live code editing and immediate API testing feedback.
212+
213+
### 3. Rapid Prototyping
214+
Quickly iterate on object definitions and test them immediately without switching contexts.
215+
216+
### 4. Code Review
217+
Review and comment on ObjectQL metadata files in a collaborative environment.
218+
219+
### 5. Documentation
220+
Embed live, editable examples in documentation websites.
221+
222+
## Keyboard Shortcuts
223+
224+
| Shortcut | Action |
225+
|----------|--------|
226+
| `Ctrl+S` / `Cmd+S` | Save current file |
227+
| `Ctrl+W` / `Cmd+W` | Close current tab |
228+
229+
## Architecture
230+
231+
```
232+
┌─────────────────────────────────────────┐
233+
│ Browser UI (Vite + HTML) │
234+
│ ┌────────────┬────────────┬──────────┐ │
235+
│ │ File Tree │ Editor │ API Test │ │
236+
│ └────────────┴────────────┴──────────┘ │
237+
└─────────────────────────────────────────┘
238+
↕ HTTP
239+
┌─────────────────────────────────────────┐
240+
│ Node.js Server (HTTP) │
241+
│ ┌────────────┬────────────────────────┐│
242+
│ │ Dev Handler│ REST Handler (ObjectQL││
243+
│ │ (Files) │ CRUD API) ││
244+
│ └────────────┴────────────────────────┘│
245+
└─────────────────────────────────────────┘
246+
247+
┌─────────────────────────────────────────┐
248+
│ File System (src/ directory) │
249+
└─────────────────────────────────────────┘
250+
```
251+
252+
## Troubleshooting
253+
254+
### Files not loading
255+
- Check that the server is running on port 3000
256+
- Verify the `baseDir` points to your project root
257+
- Ensure the `src/` directory exists
258+
259+
### Cannot save files
260+
- Verify file extension is in `allowedExtensions`
261+
- Check file permissions on the file system
262+
- Ensure the path is within the `src/` directory
263+
264+
### API tests failing
265+
- Verify your ObjectQL app is initialized
266+
- Check that objects are registered in the app
267+
- Ensure the REST handler is configured correctly
268+
269+
## License
270+
271+
MIT - Part of the ObjectQL project

0 commit comments

Comments
 (0)