mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-21 19:40:34 +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.
50 lines
1.6 KiB
TypeScript
50 lines
1.6 KiB
TypeScript
import { packingCreateItemRequestSchema, packingItemSchema, packingUpdateItemRequestSchema } from '@trek/shared';
|
|
import { HttpResponse } from 'msw';
|
|
import { z } from 'zod';
|
|
import { buildPackingItem } from '../../factories';
|
|
import { contractHandler } from '../contract';
|
|
|
|
const itemResponse = z.object({ item: packingItemSchema });
|
|
|
|
/** The server stores checked and is_private as 0/1 and answers them that way, whichever form the request used. */
|
|
const asStored = <T extends { checked?: boolean | number; is_private?: boolean }>({
|
|
checked,
|
|
is_private,
|
|
...rest
|
|
}: T) => ({
|
|
...rest,
|
|
...(checked === undefined ? {} : { checked: Number(checked) }),
|
|
...(is_private === undefined ? {} : { is_private: Number(is_private) }),
|
|
});
|
|
|
|
export const packingHandlers = [
|
|
contractHandler(
|
|
'get',
|
|
'/api/trips/:id/packing',
|
|
{ response: z.object({ items: z.array(packingItemSchema) }) },
|
|
({ params }) => ({
|
|
items: [buildPackingItem({ trip_id: Number(params.id) })],
|
|
})
|
|
),
|
|
|
|
contractHandler(
|
|
'post',
|
|
'/api/trips/:id/packing',
|
|
{ request: packingCreateItemRequestSchema, response: itemResponse },
|
|
({ params, body }) => ({
|
|
item: buildPackingItem({ trip_id: Number(params.id), ...asStored(body) }),
|
|
})
|
|
),
|
|
|
|
contractHandler(
|
|
'put',
|
|
'/api/trips/:id/packing/:itemId',
|
|
{ request: packingUpdateItemRequestSchema, response: itemResponse },
|
|
({ params, body }) => ({
|
|
item: buildPackingItem({ id: Number(params.itemId), trip_id: Number(params.id), ...asStored(body) }),
|
|
})
|
|
),
|
|
|
|
contractHandler('delete', '/api/trips/:id/packing/:itemId', {}, () => HttpResponse.json({ success: true })),
|
|
];
|