|
| 1 | +// .storybook/mocks.ts |
| 2 | +import { http, HttpResponse } from 'msw'; |
| 3 | +import { ObjectStackServer } from '@objectstack/plugin-msw'; |
| 4 | + |
| 5 | +export const protocol = { |
| 6 | + objects: [ |
| 7 | + { |
| 8 | + name: "contact", |
| 9 | + fields: { |
| 10 | + name: { type: "text" }, |
| 11 | + email: { type: "email" }, |
| 12 | + title: { type: "text" }, |
| 13 | + company: { type: "text" }, |
| 14 | + status: { type: "select", options: ["Active", "Lead", "Customer"] } |
| 15 | + } |
| 16 | + }, |
| 17 | + { |
| 18 | + name: "opportunity", |
| 19 | + fields: { |
| 20 | + name: { type: "text" }, |
| 21 | + amount: { type: "currency" }, |
| 22 | + stage: { type: "select" }, |
| 23 | + closeDate: { type: "date" }, |
| 24 | + accountId: { type: "lookup", reference_to: "account" } |
| 25 | + } |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "account", |
| 29 | + fields: { |
| 30 | + name: { type: "text" }, |
| 31 | + industry: { type: "text" } |
| 32 | + } |
| 33 | + } |
| 34 | + ] |
| 35 | +}; |
| 36 | + |
| 37 | +// Initialize the mock server |
| 38 | +// @ts-ignore |
| 39 | +ObjectStackServer.init(protocol); |
| 40 | + |
| 41 | +// Seed basic data |
| 42 | +(async () => { |
| 43 | + await ObjectStackServer.createData('contact', { id: '1', name: 'John Doe', title: 'Developer', company: 'Tech', status: 'Active' }); |
| 44 | + await ObjectStackServer.createData('contact', { id: '2', name: 'Jane Smith', title: 'Manager', company: 'Corp', status: 'Customer' }); |
| 45 | + await ObjectStackServer.createData('account', { id: '1', name: 'Big Corp', industry: 'Finance' }); |
| 46 | + await ObjectStackServer.createData('opportunity', { id: '1', name: 'Big Deal', amount: 50000, stage: 'Negotiation', accountId: '1' }); |
| 47 | +})(); |
| 48 | + |
| 49 | +export const handlers = [ |
| 50 | + // Standard CRUD handlers using ObjectStackServer |
| 51 | + http.get('/api/v1/data/:object', async ({ params }) => { |
| 52 | + const { object } = params; |
| 53 | + const result = await ObjectStackServer.findData(object as string); |
| 54 | + return HttpResponse.json({ value: result.data }); |
| 55 | + }), |
| 56 | + |
| 57 | + http.get('/api/v1/data/:object/:id', async ({ params }) => { |
| 58 | + const { object, id } = params; |
| 59 | + const result = await ObjectStackServer.getData(object as string, id as string); |
| 60 | + return HttpResponse.json(result.data); |
| 61 | + }), |
| 62 | + |
| 63 | + http.post('/api/v1/data/:object', async ({ params, request }) => { |
| 64 | + const { object } = params; |
| 65 | + const body = await request.json(); |
| 66 | + const result = await ObjectStackServer.createData(object as string, body); |
| 67 | + return HttpResponse.json(result.data); |
| 68 | + }), |
| 69 | + |
| 70 | + // Custom bootstrap if needed |
| 71 | + http.get('/api/bootstrap', async () => { |
| 72 | + const contacts = (await ObjectStackServer.findData('contact')).data; |
| 73 | + return HttpResponse.json({ contacts }); |
| 74 | + }) |
| 75 | +]; |
0 commit comments