-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalls.ts
26 lines (20 loc) · 990 Bytes
/
calls.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import { http, HttpResponse } from "msw";
import { api } from "../api";
import { createNoteMock } from "../api/note/mocks";
import { CreateNoteSchema } from "../api/note/schema";
// biome-ignore lint/suspicious/noExplicitAny: Generic
type MockOpts<T extends (args: any) => any> = Parameters<T>[0];
type CreateNoteMockInput = MockOpts<typeof createNoteMock>;
export const getNote = (input: CreateNoteMockInput) =>
http.get(api.notes.get.url, () => HttpResponse.json(createNoteMock(input)));
export const getAllNotes = (input: CreateNoteMockInput[]) =>
http.get(api.notes.get.url, () => HttpResponse.json(input.map(createNoteMock)));
export const createNote = (input: CreateNoteMockInput) =>
http.post(api.notes.create.url, async ({ request }) => {
const body = await request.json();
const data = CreateNoteSchema.safeParse(body);
if (data.error) {
return HttpResponse.json(data.error.flatten(), { status: 400 });
}
return HttpResponse.json(createNoteMock(input));
});