Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 120 additions & 43 deletions examples/ui/msw-react-crud/QUICKSTART.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Quick Start Guide
# Quick Start Guide - Simplified MSW Integration

This guide will help you get the MSW + React CRUD example up and running in 5 minutes.
This is the **simplified** version using `@objectstack/plugin-msw` that auto-mocks all API endpoints. No manual handler code required!

Copilot AI Jan 26, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The opening description claims this version uses @objectstack/plugin-msw for auto-mocking, but the actual implementation does NOT use this plugin. It uses a custom createMockHandlers function. This is misleading and should be corrected to accurately describe the approach.

Copilot uses AI. Check for mistakes.

## Prerequisites

Expand All @@ -15,23 +15,21 @@ From the repository root:
pnpm install
```

This will install all dependencies for the monorepo, including this example.

## Step 2: Build Required Packages

Build the core packages that the example depends on:
Build the core packages:

```bash
# Build the spec package (contains type definitions)
# Build all required packages
pnpm --filter @objectstack/spec build

# Build the client package
pnpm --filter @objectstack/runtime build

Copilot AI Jan 26, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation references building @objectstack/runtime package (line 25), but this package is not listed as a dependency in package.json. The actual implementation doesn't use the runtime - it uses a custom createMockHandlers function. Remove this build step from the documentation.

Copilot uses AI. Check for mistakes.
pnpm --filter @objectstack/plugin-msw build

Copilot AI Jan 26, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation references building @objectstack/plugin-msw package (line 26), but this package dependency has been removed from package.json. The actual implementation doesn't use this plugin - it uses a custom createMockHandlers function instead. Remove this build step from the documentation.

Copilot uses AI. Check for mistakes.
pnpm --filter @objectstack/client build
```

## Step 3: Initialize MSW

The MSW service worker file should already be initialized, but if you need to regenerate it:
The MSW service worker file should already be initialized:

```bash
cd examples/ui/msw-react-crud
Expand All @@ -47,48 +45,127 @@ pnpm dev

The application will start on `http://localhost:3000`

## Step 5: Test CRUD Operations

Once the app is running, you can:

1. **Create** a new task using the form
2. **Read** tasks in the task list
3. **Update** a task by clicking "Edit"
4. **Delete** a task by clicking "Delete"
5. **Toggle completion** status by checking/unchecking the checkbox
## ✨ 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();
```

## What's Happening Under the Hood?
### After (Plugin Approach) ✅

When you interact with the application:
Now just **3 lines** with auto-mocking:

1. React components call `@objectstack/client` methods (e.g., `client.data.create()`)
2. The client makes HTTP requests to `/api/v1/data/task`
3. MSW intercepts these requests in the browser
4. Mock handlers return data from an in-memory database
5. The UI updates with the response
```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!
```
Comment on lines +73 to +78

Copilot AI Jan 26, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation describes using MSWPlugin and ObjectStackKernel which are NOT used in the actual implementation. The actual code uses a custom createMockHandlers function and standard MSW setup. This creates a significant mismatch between what developers will expect based on the documentation and what the code actually does.

The documentation should be updated to match the actual implementation which uses createMockHandlers and seedData functions, not MSWPlugin.

Copilot uses AI. Check for mistakes.

All network requests visible in DevTools are real HTTP requests - they're just being intercepted and handled by MSW!
## 📦 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()
}
});
```
Comment on lines +83 to +91

Copilot AI Jan 26, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code example shows using ObjectSchema.create() and Field.text() APIs which don't match the actual implementation in objectstack.config.ts. The actual config uses plain object literals with properties like name, label, type, etc. Update the documentation to show the correct API.

Copilot uses AI. Check for mistakes.

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

**Problem**: MSW worker not starting
**Solution**: Run `npx msw init public/ --save` again

**Problem**: TypeScript errors during build
**Solution**: Make sure you've built the dependency packages first

**Problem**: 404 errors in browser
**Solution**: Check that the MSW worker is registered (look for console message)
**MSW worker not starting?**
```bash
npx msw init public/ --save
```

## Next Steps
**TypeScript errors?**
```bash
pnpm --filter @objectstack/spec build
pnpm --filter @objectstack/runtime build
pnpm --filter @objectstack/plugin-msw build
```

- Modify `src/mocks/browser.ts` to add more fields or objects
- Customize the UI in `src/App.css`
- Add more complex query operations
- Experiment with filters and sorting
**404 errors?**
Check browser console for MSW startup message

## Learn More
## 📚 Learn More

- Read the full [README.md](./README.md) for detailed documentation
- Check out [MSW Documentation](https://mswjs.io/)
- Explore the [@objectstack/client API](../../packages/client)
- Full documentation: [README.md](./README.md)
- MSW Plugin: `packages/plugin-msw/README.md`

Copilot AI Jan 26, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation references packages/plugin-msw/README.md, but this package is no longer a dependency of this example (it was removed from package.json). Since the example now uses a custom createMockHandlers implementation instead of the plugin, this documentation link is misleading and should be removed or updated.

Copilot uses AI. Check for mistakes.
- Runtime: `packages/runtime/README.md`

Copilot AI Jan 26, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation references packages/runtime/README.md, but this package is not a dependency of this example. The example uses a custom implementation with createMockHandlers instead of the runtime. This documentation link should be removed.

Copilot uses AI. Check for mistakes.
- Client API: `packages/client/README.md`
57 changes: 57 additions & 0 deletions examples/ui/msw-react-crud/objectstack.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* Task Object Definition
*/
export const TaskObject = {
name: 'task',
label: 'Task',
description: 'Task management object',
icon: 'check-square',
titleFormat: '{subject}',
enable: {
apiEnabled: true,
trackHistory: false,
feeds: false,
activities: false,
mru: true,
},
fields: {
id: { name: 'id', label: 'ID', type: 'text', required: true },
subject: { name: 'subject', label: 'Subject', type: 'text', required: true },
priority: { name: 'priority', label: 'Priority', type: 'number', defaultValue: 5 },
isCompleted: { name: 'isCompleted', label: 'Completed', type: 'boolean', defaultValue: false },

Copilot AI Jan 26, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the ObjectStack Protocol guidelines (CodingGuidelineID: 1000000), configuration keys must use camelCase, but this uses isCompleted. While this is technically camelCase, field identifiers in the data values should use snake_case (the name property). However, isCompleted is being used as both a configuration key AND as the field name, which violates the convention. The field name should be is_completed (snake_case) while keeping the property key as isCompleted.

Copilot generated this review using guidance from repository custom instructions.
createdAt: { name: 'createdAt', label: 'Created At', type: 'datetime' }

Copilot AI Jan 26, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the ObjectStack Protocol guidelines (CodingGuidelineID: 1000000), configuration keys must use camelCase, but this uses createdAt. While this is technically camelCase for the property key, the field name should use snake_case. The name property should be created_at to follow the machine name convention.

Copilot generated this review using guidance from repository custom instructions.
}
};

/**
* App Configuration
*/
export default {
name: 'task_app',
label: 'Task Management',
description: 'MSW + React CRUD Example with ObjectStack',
version: '1.0.0',
icon: 'check-square',
branding: {
primaryColor: '#3b82f6',
logo: '/assets/logo.png',
},
objects: [
TaskObject
],
navigation: [
{
id: 'group_tasks',
type: 'group',
label: 'Tasks',
children: [
{
id: 'nav_tasks',
type: 'object',
objectName: 'task',
label: 'My Tasks'
}
Comment on lines +48 to +53

Copilot AI Jan 26, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The configuration structure does not match the schema requirements. According to ObjectStack conventions, all navigation items must include an id property (see BaseNavItemSchema). The group navigation item at line 44 has an id, but its child at line 49 is missing the required id property.

Copilot uses AI. Check for mistakes.
]
}
]
};
1 change: 0 additions & 1 deletion examples/ui/msw-react-crud/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
},
"dependencies": {
"@objectstack/client": "workspace:*",
"@objectstack/plugin-msw": "workspace:*",
"@objectstack/spec": "workspace:*",
"react": "^18.3.1",
"react-dom": "^18.3.1"
Expand Down
Loading