Skip to content

Commit fd54d48

Browse files
committed
improve example mocks
1 parent 8539b63 commit fd54d48

File tree

4 files changed

+147
-82
lines changed

4 files changed

+147
-82
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
export type CommentData = { postId: number; id: number; name: string; email: string; body: string };
2+
3+
export const mockComments: CommentData[] = [
4+
{
5+
id: 1,
6+
postId: 3,
7+
name: 'Mark Rivera',
8+
9+
body: "Very informative! I'd love to read more about crypto security practices.",
10+
},
11+
{
12+
id: 2,
13+
postId: 3,
14+
name: 'Ella Johnson',
15+
16+
body: 'Crypto can be tricky for beginners. Thanks for breaking it down!',
17+
},
18+
{
19+
id: 3,
20+
postId: 3,
21+
name: 'Ryan Clark',
22+
23+
body: 'Is there a specific exchange you recommend for beginners?',
24+
},
25+
{
26+
id: 4,
27+
postId: 0,
28+
name: 'Jane Doe',
29+
30+
body: 'Great insights! I wonder how AI will shape education in the next decade.',
31+
},
32+
{
33+
id: 5,
34+
postId: 0,
35+
name: 'Liam Smith',
36+
37+
body: 'AI in healthcare is fascinating. The potential for early diagnosis is promising.',
38+
},
39+
{
40+
id: 6,
41+
postId: 2,
42+
name: 'Sophia Lee',
43+
44+
body: 'Thanks for the tips! Do you recommend any beginner-friendly plants for small spaces?',
45+
},
46+
];

examples/vite/src/mocks/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from './mocks';
2+
export * from './posts';
3+
export * from './comments';
Lines changed: 4 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { http, HttpResponse, delay } from 'msw';
22
import { setupWorker } from 'msw/browser';
3+
import { mockComments } from './comments';
4+
import { PostData, mockPosts } from './posts';
35

46
export type UserData = { id: number; name: string; email: string; username: string; website: string };
5-
export type PostData = { id: number; title: string; body: string; userId: number };
6-
export type CommentData = { postId: number; id: number; name: string; email: string; body: string };
77

88
export const baseUrl = 'https://mock.blog.example.com';
99

@@ -18,85 +18,7 @@ function createMockData() {
1818
},
1919
];
2020

21-
const posts: PostData[] = [
22-
{
23-
id: 7,
24-
userId: 0,
25-
title: 'Exploring the Future of AI',
26-
body: 'Artificial intelligence is transforming industries, from healthcare to finance. This post explores the latest advancements and future possibilities.',
27-
},
28-
{
29-
id: 1,
30-
userId: 0,
31-
title: '10 Tips for Better Time Management',
32-
body: 'Managing your time effectively can boost productivity and reduce stress. Here are ten practical tips to help you stay on top of your tasks.',
33-
},
34-
{
35-
id: 2,
36-
userId: 0,
37-
title: "A Beginner's Guide to Gardening",
38-
body: 'Starting a garden can be both fun and rewarding. This guide covers the basics, from choosing plants to maintaining healthy soil.',
39-
},
40-
{
41-
id: 3,
42-
userId: 0,
43-
title: 'Understanding Cryptocurrency',
44-
body: 'Cryptocurrency has gained popularity as an alternative form of investment. Learn how it works, its risks, and potential rewards.',
45-
},
46-
{
47-
id: 4,
48-
userId: 0,
49-
title: 'Top 5 Travel Destinations for 2025',
50-
body: 'Looking for your next adventure? Here are five must-visit destinations for 2025, each offering unique experiences and breathtaking views.',
51-
},
52-
];
53-
54-
const comments: CommentData[] = [
55-
{
56-
id: 1,
57-
postId: 3,
58-
name: 'Mark Rivera',
59-
60-
body: "Very informative! I'd love to read more about crypto security practices.",
61-
},
62-
{
63-
id: 2,
64-
postId: 3,
65-
name: 'Ella Johnson',
66-
67-
body: 'Crypto can be tricky for beginners. Thanks for breaking it down!',
68-
},
69-
{
70-
id: 3,
71-
postId: 3,
72-
name: 'Ryan Clark',
73-
74-
body: 'Is there a specific exchange you recommend for beginners?',
75-
},
76-
{
77-
id: 4,
78-
postId: 0,
79-
name: 'Jane Doe',
80-
81-
body: 'Great insights! I wonder how AI will shape education in the next decade.',
82-
},
83-
{
84-
id: 5,
85-
postId: 0,
86-
name: 'Liam Smith',
87-
88-
body: 'AI in healthcare is fascinating. The potential for early diagnosis is promising.',
89-
},
90-
{
91-
id: 6,
92-
postId: 2,
93-
name: 'Sophia Lee',
94-
95-
body: 'Thanks for the tips! Do you recommend any beginner-friendly plants for small spaces?',
96-
},
97-
];
98-
99-
return { users, posts, comments };
21+
return { users, posts: mockPosts, comments: mockComments };
10022
}
10123

10224
function saveMockData(saveData = { users, posts, comments }) {
@@ -167,7 +89,7 @@ const handlers = [
16789
http.delete(`${baseUrl}/posts/:id`, async (req) => {
16890
await delay(1000);
16991
const { id } = req.params;
170-
if (id === '7' && Math.random() > 0.1) return HttpResponse.json({ error: '[Mock] Failed to delete' }, { status: 500 });
92+
if (id === '0') return HttpResponse.json({ error: '[Mock] Failed to delete' }, { status: 500 });
17193

17294
const postIndex = posts.findIndex((post) => post.id === Number(id));
17395
if (postIndex === -1) return HttpResponse.json({ error: 'Not found' }, { status: 404 });

examples/vite/src/mocks/posts.ts

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
export type PostData = { id: number; title: string; body: string; userId: number };
2+
3+
export const mockPosts: PostData[] = [
4+
{
5+
id: 0,
6+
userId: 0,
7+
title: 'Exploring the Future of AI',
8+
body: 'Artificial intelligence is transforming industries, from healthcare to finance. This post explores the latest advancements and future possibilities.',
9+
},
10+
{
11+
id: 1,
12+
userId: 0,
13+
title: '10 Tips for Better Time Management',
14+
body: 'Managing your time effectively can boost productivity and reduce stress. Here are ten practical tips to help you stay on top of your tasks.',
15+
},
16+
{
17+
id: 2,
18+
userId: 0,
19+
title: "A Beginner's Guide to Gardening",
20+
body: 'Starting a garden can be both fun and rewarding. This guide covers the basics, from choosing plants to maintaining healthy soil.',
21+
},
22+
{
23+
id: 3,
24+
userId: 0,
25+
title: 'Understanding Cryptocurrency',
26+
body: 'Cryptocurrency has gained popularity as an alternative form of investment. Learn how it works, its risks, and potential rewards.',
27+
},
28+
{
29+
id: 4,
30+
userId: 0,
31+
title: 'Top 5 Travel Destinations for 2025',
32+
body: 'Looking for your next adventure? Here are five must-visit destinations for 2025, each offering unique experiences and breathtaking views.',
33+
},
34+
{
35+
id: 5,
36+
userId: 0,
37+
title: 'The Benefits of a Plant-Based Diet',
38+
body: 'Discover the health benefits of a plant-based diet and how to get started.',
39+
},
40+
{
41+
id: 6,
42+
userId: 0,
43+
title: 'How to Start a Successful Blog',
44+
body: 'Learn the steps to create and grow a successful blog from scratch.',
45+
},
46+
{
47+
id: 7,
48+
userId: 0,
49+
title: 'The Future of Remote Work',
50+
body: 'Explore the trends and predictions for the future of remote work.',
51+
},
52+
{
53+
id: 8,
54+
userId: 0,
55+
title: 'Tips for Staying Productive While Working from Home',
56+
body: 'Boost your productivity with these tips for working from home.',
57+
},
58+
{
59+
id: 9,
60+
userId: 0,
61+
title: 'The Importance of Mental Health Awareness',
62+
body: 'Understand the importance of mental health awareness and how to support it.',
63+
},
64+
{
65+
id: 10,
66+
userId: 0,
67+
title: 'A Guide to Sustainable Living',
68+
body: 'Learn how to live a more sustainable and eco-friendly lifestyle.',
69+
},
70+
{
71+
id: 11,
72+
userId: 0,
73+
title: 'The Basics of Personal Finance',
74+
body: 'Get started with personal finance with these basic tips and strategies.',
75+
},
76+
{
77+
id: 12,
78+
userId: 0,
79+
title: 'How to Improve Your Public Speaking Skills',
80+
body: 'Enhance your public speaking skills with these practical tips.',
81+
},
82+
{
83+
id: 13,
84+
userId: 0,
85+
title: 'The Benefits of Regular Exercise',
86+
body: 'Discover the physical and mental benefits of regular exercise.',
87+
},
88+
{
89+
id: 14,
90+
userId: 0,
91+
title: 'How to Build a Strong Personal Brand',
92+
body: 'Learn the steps to create and maintain a strong personal brand.',
93+
},
94+
];

0 commit comments

Comments
 (0)