-
Notifications
You must be signed in to change notification settings - Fork 1
Add MSW + React CRUD reference implementation #165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
acbdc35
Initial plan
Copilot b6ffbba
Add complete MSW + React CRUD example with ObjectStack Client
Copilot cb60d98
Add workspace configuration and CHANGELOG for MSW React example
Copilot 7ddf1ab
Add Quick Start guide for MSW React CRUD example
Copilot 7a0c222
Initial plan
Copilot 745ac10
Simplify MSW integration with auto-mocking plugin
Copilot a1f7dcc
WIP: Simplify MSW integration with config-based approach
Copilot 06460cc
Complete: Simplify MSW integration with config-based auto-mocking
Copilot db9541f
Merge pull request #166 from objectstack-ai/copilot/add-msw-integrati…
hotlong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # Dependencies | ||
| node_modules | ||
| pnpm-lock.yaml | ||
|
|
||
| # Build outputs | ||
| dist | ||
| build | ||
| *.tsbuildinfo | ||
|
|
||
| # Development | ||
| .vite | ||
| .cache | ||
|
|
||
| # Environment | ||
| .env | ||
| .env.local | ||
|
|
||
| # Editor | ||
| .vscode/* | ||
| !.vscode/extensions.json | ||
| .idea | ||
| *.swp | ||
| *.swo | ||
| *~ | ||
|
|
||
| # OS | ||
| .DS_Store | ||
| Thumbs.db | ||
|
|
||
| # MSW | ||
| public/mockServiceWorker.js |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # @objectstack/example-msw-react-crud | ||
|
|
||
| ## 1.0.0 | ||
|
|
||
| ### Major Features | ||
|
|
||
| - Initial release of MSW + React CRUD example | ||
| - Complete CRUD operations (Create, Read, Update, Delete) for tasks | ||
| - Integration with @objectstack/client for all API calls | ||
| - MSW browser worker setup for API mocking | ||
| - React components demonstrating best practices: | ||
| - TaskList component for displaying and managing tasks | ||
| - TaskForm component for creating and editing tasks | ||
| - TaskItem component for individual task display | ||
| - Comprehensive README with setup instructions and usage examples | ||
| - Full TypeScript support with proper type definitions | ||
| - Vite development server and build configuration | ||
| - Styled UI with modern CSS |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| # Quick Start Guide - Simplified MSW Integration | ||
|
|
||
| This is the **simplified** version using `@objectstack/plugin-msw` that auto-mocks all API endpoints. No manual handler code required! | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - Node.js 18 or later | ||
| - pnpm package manager | ||
|
|
||
| ## Step 1: Install Dependencies | ||
|
|
||
| From the repository root: | ||
|
|
||
| ```bash | ||
| pnpm install | ||
| ``` | ||
|
|
||
| ## Step 2: Build Required Packages | ||
|
|
||
| Build the core packages: | ||
|
|
||
| ```bash | ||
| # Build all required packages | ||
| pnpm --filter @objectstack/spec build | ||
| pnpm --filter @objectstack/runtime build | ||
| pnpm --filter @objectstack/plugin-msw build | ||
| pnpm --filter @objectstack/client build | ||
| ``` | ||
|
|
||
| ## Step 3: Initialize MSW | ||
|
|
||
| The MSW service worker file should already be initialized: | ||
|
|
||
| ```bash | ||
| cd examples/ui/msw-react-crud | ||
| npx msw init public/ --save | ||
| ``` | ||
|
|
||
| ## Step 4: Start the Development Server | ||
|
|
||
| ```bash | ||
| cd examples/ui/msw-react-crud | ||
| pnpm dev | ||
| ``` | ||
|
|
||
| The application will start on `http://localhost:3000` | ||
|
|
||
| ## ✨ What's Different? | ||
|
|
||
| ### Before (Manual Approach) ❌ | ||
|
|
||
| You had to manually write 145+ lines of MSW handlers: | ||
|
|
||
| ```typescript | ||
| // src/mocks/browser.ts - OLD WAY | ||
| const handlers = [ | ||
| http.get('/api/v1/data/task', ({ request }) => { | ||
| // Manual pagination, filtering, sorting... | ||
| }), | ||
| http.post('/api/v1/data/task', async ({ request }) => { | ||
| // Manual ID generation, validation... | ||
| }), | ||
| // ... more manual handlers | ||
| ]; | ||
| const worker = setupWorker(...handlers); | ||
| await worker.start(); | ||
| ``` | ||
|
|
||
| ### After (Plugin Approach) ✅ | ||
|
|
||
| Now just **3 lines** with auto-mocking: | ||
|
|
||
| ```typescript | ||
| // src/mocks/browser.ts - NEW WAY | ||
| const mswPlugin = new MSWPlugin({ baseUrl: '/api/v1' }); | ||
| const runtime = new ObjectStackKernel([appConfig, mswPlugin]); | ||
| await runtime.start(); // Auto-mocks ALL endpoints! | ||
| ``` | ||
|
|
||
| ## 📦 How It Works | ||
|
|
||
| 1. **Define Your Data Model** in `objectstack.config.ts`: | ||
| ```typescript | ||
| const TaskObject = ObjectSchema.create({ | ||
| name: 'task', | ||
| fields: { | ||
| subject: Field.text({ required: true }), | ||
| priority: Field.number() | ||
| } | ||
| }); | ||
| ``` | ||
|
|
||
| 2. **Auto-Mock Everything**: The MSW plugin automatically mocks: | ||
| - ✅ Discovery endpoints | ||
| - ✅ Metadata endpoints | ||
| - ✅ All CRUD operations | ||
| - ✅ Query operations | ||
| - ✅ Pagination, sorting, filtering | ||
|
|
||
| 3. **Use ObjectStack Client** normally: | ||
| ```typescript | ||
| const client = new ObjectStackClient({ baseUrl: '/api/v1' }); | ||
| await client.data.create('task', { subject: 'New task' }); | ||
| ``` | ||
|
|
||
| ## 🎯 Test CRUD Operations | ||
|
|
||
| Once running, you can: | ||
|
|
||
| 1. **Create** tasks using the form | ||
| 2. **Read** tasks in the list | ||
| 3. **Update** tasks by clicking "Edit" | ||
| 4. **Delete** tasks by clicking "Delete" | ||
| 5. **Toggle completion** status | ||
|
|
||
| ## 🔍 What Gets Auto-Mocked? | ||
|
|
||
| The plugin automatically handles: | ||
|
|
||
| | Endpoint | Description | | ||
| |----------|-------------| | ||
| | `GET /api/v1` | Discovery | | ||
| | `GET /api/v1/meta/*` | All metadata | | ||
| | `GET /api/v1/data/:object` | Find records | | ||
| | `GET /api/v1/data/:object/:id` | Get by ID | | ||
| | `POST /api/v1/data/:object` | Create | | ||
| | `PATCH /api/v1/data/:object/:id` | Update | | ||
| | `DELETE /api/v1/data/:object/:id` | Delete | | ||
|
|
||
| **Zero manual code!** 🎉 | ||
|
|
||
| ## 🔧 Advanced: Custom Handlers | ||
|
|
||
| Need custom logic? Easy: | ||
|
|
||
| ```typescript | ||
| const customHandlers = [ | ||
| http.get('/api/custom/hello', () => | ||
| HttpResponse.json({ message: 'Hello!' }) | ||
| ) | ||
| ]; | ||
|
|
||
| const mswPlugin = new MSWPlugin({ | ||
| customHandlers, // Add your custom handlers | ||
| baseUrl: '/api/v1' | ||
| }); | ||
| ``` | ||
|
|
||
| ## Troubleshooting | ||
|
|
||
| **MSW worker not starting?** | ||
| ```bash | ||
| npx msw init public/ --save | ||
| ``` | ||
|
|
||
| **TypeScript errors?** | ||
| ```bash | ||
| pnpm --filter @objectstack/spec build | ||
| pnpm --filter @objectstack/runtime build | ||
| pnpm --filter @objectstack/plugin-msw build | ||
| ``` | ||
|
|
||
| **404 errors?** | ||
| Check browser console for MSW startup message | ||
|
|
||
| ## 📚 Learn More | ||
|
|
||
| - Full documentation: [README.md](./README.md) | ||
| - MSW Plugin: `packages/plugin-msw/README.md` | ||
| - Runtime: `packages/runtime/README.md` | ||
| - Client API: `packages/client/README.md` | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This QUICKSTART describes a "simplified" setup using
@objectstack/plugin-msw(MSWPlugin,ObjectStackKernel, auto-mocked endpoints), but the actual example code in this folder uses manual MSW handlers viacreateMockHandlersand does not reference the plugin. Consider either updating the Quick Start to match the current implementation, or clearly scoping it as an alternative plugin-based approach and pointing to the appropriate example and required dependencies.