mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-21 22:46:32 +00:00
5fcb307270
contractHandler() parses the request body against the shared request schema, answering 400 like the server's Zod pipe on a body it would refuse, and the mock's answer against the response schema, failing on a factory that drifted. The trips, budget, places, todo, tags and categories, day notes, packing, reservations and assignments handlers and the vacay plan and colour updates use it wherever shared has a schema; the offline bundle and the other vacay routes have none yet and stay plain. The typed bodies showed where the old handlers echoed request shapes back as stored rows: payers and members are no longer echoed into a budget item, and checked and is_private come back as 0/1 the way the server stores them.
52 lines
1.5 KiB
TypeScript
52 lines
1.5 KiB
TypeScript
import {
|
|
dayNoteCreateRequestSchema,
|
|
dayNoteSchema,
|
|
dayNoteUpdateRequestSchema,
|
|
daySchema,
|
|
dayUpdateRequestSchema,
|
|
} from '@trek/shared';
|
|
import { HttpResponse } from 'msw';
|
|
import { z } from 'zod';
|
|
import { buildDayNote } from '../../factories';
|
|
import { contractHandler } from '../contract';
|
|
|
|
const noteResponse = z.object({ note: dayNoteSchema });
|
|
|
|
export const dayNotesHandlers = [
|
|
contractHandler(
|
|
'get',
|
|
'/api/trips/:id/days/:dayId/notes',
|
|
{ response: z.object({ notes: z.array(dayNoteSchema) }) },
|
|
({ params }) => ({
|
|
notes: [buildDayNote({ day_id: Number(params.dayId) })],
|
|
})
|
|
),
|
|
|
|
contractHandler(
|
|
'post',
|
|
'/api/trips/:id/days/:dayId/notes',
|
|
{ request: dayNoteCreateRequestSchema, response: noteResponse },
|
|
({ params, body }) => ({
|
|
note: buildDayNote({ day_id: Number(params.dayId), ...body }),
|
|
})
|
|
),
|
|
|
|
contractHandler(
|
|
'put',
|
|
'/api/trips/:id/days/:dayId/notes/:noteId',
|
|
{ request: dayNoteUpdateRequestSchema, response: noteResponse },
|
|
({ params, body }) => ({ note: buildDayNote({ id: Number(params.noteId), day_id: Number(params.dayId), ...body }) })
|
|
),
|
|
|
|
contractHandler('delete', '/api/trips/:id/days/:dayId/notes/:noteId', {}, () => HttpResponse.json({ success: true })),
|
|
|
|
contractHandler(
|
|
'put',
|
|
'/api/trips/:id/days/:dayId',
|
|
{ request: dayUpdateRequestSchema, response: z.object({ day: daySchema }) },
|
|
({ params, body }) => ({
|
|
day: { id: Number(params.dayId), trip_id: Number(params.id), ...body },
|
|
})
|
|
),
|
|
];
|