Skip to content

Commit 9fe557d

Browse files
authored
Merge pull request #165 from objectstack-ai/copilot/add-msw-integration-example
2 parents ff91f77 + db9541f commit 9fe557d

21 files changed

+2190
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Dependencies
2+
node_modules
3+
pnpm-lock.yaml
4+
5+
# Build outputs
6+
dist
7+
build
8+
*.tsbuildinfo
9+
10+
# Development
11+
.vite
12+
.cache
13+
14+
# Environment
15+
.env
16+
.env.local
17+
18+
# Editor
19+
.vscode/*
20+
!.vscode/extensions.json
21+
.idea
22+
*.swp
23+
*.swo
24+
*~
25+
26+
# OS
27+
.DS_Store
28+
Thumbs.db
29+
30+
# MSW
31+
public/mockServiceWorker.js
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# @objectstack/example-msw-react-crud
2+
3+
## 1.0.0
4+
5+
### Major Features
6+
7+
- Initial release of MSW + React CRUD example
8+
- Complete CRUD operations (Create, Read, Update, Delete) for tasks
9+
- Integration with @objectstack/client for all API calls
10+
- MSW browser worker setup for API mocking
11+
- React components demonstrating best practices:
12+
- TaskList component for displaying and managing tasks
13+
- TaskForm component for creating and editing tasks
14+
- TaskItem component for individual task display
15+
- Comprehensive README with setup instructions and usage examples
16+
- Full TypeScript support with proper type definitions
17+
- Vite development server and build configuration
18+
- Styled UI with modern CSS
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
# Quick Start Guide - Simplified MSW Integration
2+
3+
This is the **simplified** version using `@objectstack/plugin-msw` that auto-mocks all API endpoints. No manual handler code required!
4+
5+
## Prerequisites
6+
7+
- Node.js 18 or later
8+
- pnpm package manager
9+
10+
## Step 1: Install Dependencies
11+
12+
From the repository root:
13+
14+
```bash
15+
pnpm install
16+
```
17+
18+
## Step 2: Build Required Packages
19+
20+
Build the core packages:
21+
22+
```bash
23+
# Build all required packages
24+
pnpm --filter @objectstack/spec build
25+
pnpm --filter @objectstack/runtime build
26+
pnpm --filter @objectstack/plugin-msw build
27+
pnpm --filter @objectstack/client build
28+
```
29+
30+
## Step 3: Initialize MSW
31+
32+
The MSW service worker file should already be initialized:
33+
34+
```bash
35+
cd examples/ui/msw-react-crud
36+
npx msw init public/ --save
37+
```
38+
39+
## Step 4: Start the Development Server
40+
41+
```bash
42+
cd examples/ui/msw-react-crud
43+
pnpm dev
44+
```
45+
46+
The application will start on `http://localhost:3000`
47+
48+
## ✨ What's Different?
49+
50+
### Before (Manual Approach) ❌
51+
52+
You had to manually write 145+ lines of MSW handlers:
53+
54+
```typescript
55+
// src/mocks/browser.ts - OLD WAY
56+
const handlers = [
57+
http.get('/api/v1/data/task', ({ request }) => {
58+
// Manual pagination, filtering, sorting...
59+
}),
60+
http.post('/api/v1/data/task', async ({ request }) => {
61+
// Manual ID generation, validation...
62+
}),
63+
// ... more manual handlers
64+
];
65+
const worker = setupWorker(...handlers);
66+
await worker.start();
67+
```
68+
69+
### After (Plugin Approach) ✅
70+
71+
Now just **3 lines** with auto-mocking:
72+
73+
```typescript
74+
// src/mocks/browser.ts - NEW WAY
75+
const mswPlugin = new MSWPlugin({ baseUrl: '/api/v1' });
76+
const runtime = new ObjectStackKernel([appConfig, mswPlugin]);
77+
await runtime.start(); // Auto-mocks ALL endpoints!
78+
```
79+
80+
## 📦 How It Works
81+
82+
1. **Define Your Data Model** in `objectstack.config.ts`:
83+
```typescript
84+
const TaskObject = ObjectSchema.create({
85+
name: 'task',
86+
fields: {
87+
subject: Field.text({ required: true }),
88+
priority: Field.number()
89+
}
90+
});
91+
```
92+
93+
2. **Auto-Mock Everything**: The MSW plugin automatically mocks:
94+
- ✅ Discovery endpoints
95+
- ✅ Metadata endpoints
96+
- ✅ All CRUD operations
97+
- ✅ Query operations
98+
- ✅ Pagination, sorting, filtering
99+
100+
3. **Use ObjectStack Client** normally:
101+
```typescript
102+
const client = new ObjectStackClient({ baseUrl: '/api/v1' });
103+
await client.data.create('task', { subject: 'New task' });
104+
```
105+
106+
## 🎯 Test CRUD Operations
107+
108+
Once running, you can:
109+
110+
1. **Create** tasks using the form
111+
2. **Read** tasks in the list
112+
3. **Update** tasks by clicking "Edit"
113+
4. **Delete** tasks by clicking "Delete"
114+
5. **Toggle completion** status
115+
116+
## 🔍 What Gets Auto-Mocked?
117+
118+
The plugin automatically handles:
119+
120+
| Endpoint | Description |
121+
|----------|-------------|
122+
| `GET /api/v1` | Discovery |
123+
| `GET /api/v1/meta/*` | All metadata |
124+
| `GET /api/v1/data/:object` | Find records |
125+
| `GET /api/v1/data/:object/:id` | Get by ID |
126+
| `POST /api/v1/data/:object` | Create |
127+
| `PATCH /api/v1/data/:object/:id` | Update |
128+
| `DELETE /api/v1/data/:object/:id` | Delete |
129+
130+
**Zero manual code!** 🎉
131+
132+
## 🔧 Advanced: Custom Handlers
133+
134+
Need custom logic? Easy:
135+
136+
```typescript
137+
const customHandlers = [
138+
http.get('/api/custom/hello', () =>
139+
HttpResponse.json({ message: 'Hello!' })
140+
)
141+
];
142+
143+
const mswPlugin = new MSWPlugin({
144+
customHandlers, // Add your custom handlers
145+
baseUrl: '/api/v1'
146+
});
147+
```
148+
149+
## Troubleshooting
150+
151+
**MSW worker not starting?**
152+
```bash
153+
npx msw init public/ --save
154+
```
155+
156+
**TypeScript errors?**
157+
```bash
158+
pnpm --filter @objectstack/spec build
159+
pnpm --filter @objectstack/runtime build
160+
pnpm --filter @objectstack/plugin-msw build
161+
```
162+
163+
**404 errors?**
164+
Check browser console for MSW startup message
165+
166+
## 📚 Learn More
167+
168+
- Full documentation: [README.md](./README.md)
169+
- MSW Plugin: `packages/plugin-msw/README.md`
170+
- Runtime: `packages/runtime/README.md`
171+
- Client API: `packages/client/README.md`

0 commit comments

Comments
 (0)