Skip to content

Commit bb8e7c6

Browse files
committed
improve mutation key, add operation type
1 parent 5ae905f commit bb8e7c6

File tree

10 files changed

+211
-177
lines changed

10 files changed

+211
-177
lines changed

examples/vite/src/App.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,14 @@ const deletePostMutation = builder.withMethod('delete').withPath('/posts/:id').w
8585
function App() {
8686
const [postId, setPostId] = useState<number | null>(null);
8787

88-
const posts = postsQuery.useInfiniteQuery({}, { enabled: !postId });
88+
const posts = postsQuery.useInfiniteQuery({}, { enabled: postId != null });
8989
const reset = resetMutation.useMutation();
9090

9191
const deleteErrors = deletePostMutation.useMutationState(undefined, { status: 'error' }, (x) => x.state.variables?.params.id);
9292

9393
const refresh = () => builder.client.operateTags({ tags: 'refreshable', operation: 'reset' });
9494

95-
if (postId) return <PostPage postId={postId} onBack={() => setPostId(null)} />;
95+
if (postId != null) return <PostPage postId={postId} onBack={() => setPostId(null)} />;
9696

9797
return (
9898
<>
Lines changed: 48 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,50 @@
11
export type CommentData = { postId: number; id: number; name: string; email: string; body: string };
22

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

examples/vite/src/mocks/mocks.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { http, HttpResponse, delay } from 'msw';
22
import { setupWorker } from 'msw/browser';
3-
import { mockComments } from './comments';
4-
import { PostData, mockPosts } from './posts';
3+
import { createMockComments } from './comments';
4+
import { PostData, createMockPosts } from './posts';
55

66
export type UserData = { id: number; name: string; email: string; username: string; website: string };
77

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

21-
return { users, posts: mockPosts, comments: mockComments };
21+
const posts = createMockPosts();
22+
const comments = createMockComments();
23+
24+
return { users, posts, comments };
2225
}
2326

2427
function saveMockData(saveData = { users, posts, comments }) {

examples/vite/src/mocks/posts.ts

Lines changed: 96 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,98 @@
11
export type PostData = { id: number; title: string; body: string; userId: number };
22

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

src/builder/HttpQueryBuilder.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { WithOptional } from '../type-utils';
44
import { QueryBuilder } from './QueryBuilder';
55
import { BuilderConfig } from './types';
66
import { HttpBaseHeaders, HttpBaseParams, HttpBaseSearch, HttpBuilderVars } from './types';
7-
import { createHttpMergeVarsFn, createHttpQueryFn, createHttpQueryHashFn } from './utils';
7+
import { createHttpMergeVarsFn, createHttpQueryFn, createHttpQueryKeySanitizer } from './utils';
88

99
export class HttpQueryBuilder<
1010
TParam = unknown,
@@ -22,8 +22,8 @@ export class HttpQueryBuilder<
2222
) {
2323
const mergeVars = config?.mergeVars || createHttpMergeVarsFn();
2424
const queryFn = config?.queryFn || createHttpQueryFn(mergeVars);
25-
const queryKeyHashFn = config?.queryKeyHashFn || createHttpQueryHashFn();
26-
super({ mergeVars, queryFn, queryKeyHashFn, ...config });
25+
const queryKeySanitizer = config?.queryKeySanitizer || createHttpQueryKeySanitizer();
26+
super({ mergeVars, queryFn, queryKeySanitizer, ...config });
2727
}
2828

2929
withBody<TBody$>(body?: TBody$): HttpQueryBuilder<TParam, TSearch, TBody$, THeader, TMeta, TData, TError, TTags> {

0 commit comments

Comments
 (0)