-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcategory.ts
52 lines (41 loc) · 1.38 KB
/
category.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { http } from 'msw';
import { API_URL } from '@/api';
import categories from '@/mocks/fixtures/categoryList.json';
import { END_POINTS } from '@/routes';
import { Category } from '@/types';
import { mockResponse } from '@/utils/mockResponse';
const mockCategoryList = [...categories.categories];
export const categoryHandlers = [
http.get(`${API_URL}${END_POINTS.CATEGORIES}`, () =>
mockResponse({ status: 200, body: { categories: mockCategoryList } }),
),
http.post(`${API_URL}${END_POINTS.CATEGORIES}`, async (req) => {
const newCategory = await req.request.json();
if (typeof newCategory === 'object' && newCategory !== null) {
const newId = mockCategoryList.length + 1;
const category = { id: newId, ...newCategory } as Category;
mockCategoryList.push(category);
return mockResponse({ status: 201, body: { category } });
}
return mockResponse({
status: 400,
body: {
message: 'Invalid category data',
},
});
}),
http.put(`${API_URL}${END_POINTS.CATEGORIES}`, async (req) => {
const updatedCategory = await req.request.json();
if (typeof updatedCategory === 'object' && updatedCategory !== null) {
return mockResponse({
status: 200,
});
}
return mockResponse({
status: 404,
body: {
message: 'Category not found or invalid data',
},
});
}),
];