export type CreatePayload = { // TODO: define fields // example: // name: string; }; export type ReplacePayload = { // PUT: typically full replace. // Define required fields here. }; export type UpdatePayload = Partial; // PATCH: partial update function isObject(v: unknown): v is Record { return typeof v === "object" && v !== null && !Array.isArray(v); } export function parseCreatePayload(body: unknown): CreatePayload { if (!isObject(body)) throw badReq("Body must be a JSON object"); // TODO: validate required fields return body as CreatePayload; } export function parseReplacePayload(body: unknown): ReplacePayload { if (!isObject(body)) throw badReq("Body must be a JSON object"); // TODO: validate required fields for PUT return body as ReplacePayload; } export function parseUpdatePayload(body: unknown): UpdatePayload { if (!isObject(body)) throw badReq("Body must be a JSON object"); return body as UpdatePayload; } function badReq(message: string) { const err = new Error(message); (err as any).status = 400; return err; }