Skip to content

Commit b2d5001

Browse files
committed
improve docs
1 parent 9773472 commit b2d5001

File tree

16 files changed

+469
-33
lines changed

16 files changed

+469
-33
lines changed

examples/vite/src/client.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import { QueryClient } from '@tanstack/react-query';
2+
import { getMockHandlers } from 'tanstack-query-builder-example-mocks';
3+
import { setupMSW } from 'tanstack-query-builder-example-mocks/setup-msw';
4+
5+
await setupMSW(...getMockHandlers()).start({ onUnhandledRequest: 'bypass', quiet: true, waitUntilReady: true });
26

37
export const queryClient = new QueryClient({
48
defaultOptions: {

examples/vite/src/examples/main/example.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import { queryClient } from 'src/client';
2-
import { ArticleData, CommentData, baseUrl, getMockHandlers } from 'tanstack-query-builder-example-mocks';
3-
import { setupMSW } from 'tanstack-query-builder-example-mocks/setup-msw';
2+
import { ArticleData, CommentData, baseUrl } from 'tanstack-query-builder-example-mocks';
43
import { HttpQueryBuilder } from 'tanstack-query-builder/http';
54

6-
await setupMSW(...getMockHandlers()).start({ onUnhandledRequest: 'bypass', quiet: true, waitUntilReady: true });
7-
85
export const builder = new HttpQueryBuilder().withClient(queryClient).withBaseUrl(baseUrl).withTagTypes<{
96
article: ArticleData;
107
articles: ArticleData[];

examples/vite/src/examples/main/list.tsx

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
import { QueryClientProvider } from '@tanstack/react-query';
2-
import { ReactQueryDevtools } from '@tanstack/react-query-devtools';
31
import { useNavigate } from '@tanstack/react-router';
42
import { useState } from 'react';
5-
import { queryClient } from '../../client';
63
import { articleQuery, articlesQuery, builder, commentsQuery, deleteArticleMutation, resetMutation } from './example';
74

8-
function AppCore() {
5+
export function MainExample() {
96
const [enablePrefetch, setEnablePrefetch] = useState(false);
107
const nav = useNavigate();
118

@@ -82,12 +79,3 @@ function AppCore() {
8279
</>
8380
);
8481
}
85-
86-
export function MainExample() {
87-
return (
88-
<QueryClientProvider client={queryClient}>
89-
<AppCore />
90-
<ReactQueryDevtools initialIsOpen={false} />
91-
</QueryClientProvider>
92-
);
93-
}

examples/vite/src/main.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
1+
import './index.css';
2+
3+
import { QueryClientProvider } from '@tanstack/react-query';
4+
import { ReactQueryDevtools } from '@tanstack/react-query-devtools';
15
import { StrictMode } from 'react';
26
import { createRoot } from 'react-dom/client';
3-
import './index.css';
7+
import { queryClient } from './client';
48
import { AppRouter } from './router';
59

610
const rootElement = document.getElementById('root')!;
711
if (!rootElement.innerHTML) {
812
const root = createRoot(rootElement);
9-
root.render(<StrictMode children={<AppRouter />} />);
13+
root.render(
14+
<StrictMode>
15+
<QueryClientProvider client={queryClient}>
16+
<AppRouter />
17+
<ReactQueryDevtools initialIsOpen={false} />
18+
</QueryClientProvider>
19+
</StrictMode>,
20+
);
1021
}

examples/vite/tests/Render.test.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { QueryClientProvider } from '@tanstack/react-query';
12
import { createMemoryHistory } from '@tanstack/react-router';
23
import { queryClient } from 'src/client';
34
import { AppRouter } from 'src/router';
@@ -9,7 +10,11 @@ import '../src/index.css';
910
import 'src/examples/main/example';
1011

1112
const history = createMemoryHistory({ initialEntries: ['/main'] });
12-
const TestRenderer = () => <AppRouter history={history} />;
13+
const TestRenderer = () => (
14+
<QueryClientProvider client={queryClient}>
15+
<AppRouter history={history} />
16+
</QueryClientProvider>
17+
);
1318

1419
beforeEach(async () => {
1520
await fetch(`${baseUrl}/reset`, { method: 'POST' });

packages/tanstack-query-builder/src/builder/QueryBuilder.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ export class QueryBuilder<
1919
TTags extends Record<string, unknown>,
2020
TFlags extends BuilderFlags = '',
2121
> extends QueryBuilderFrozen<TVars, TData, TError, TKey, TTags, TFlags> {
22+
/**
23+
* Declares the type of variables of the query.
24+
* Optionally, you can pass the first argument to set the default variables,
25+
* which will be merged with the variables passed to the query.
26+
*/
2227
withVars<TVars$ = TVars, const TReset extends boolean = false>(
2328
vars?: TVars$,
2429
resetVars = false as TReset,
@@ -30,21 +35,39 @@ export class QueryBuilder<
3035
}) as any;
3136
}
3237

38+
/**
39+
* Declares the type of returned data of the query.
40+
*/
3341
withData<TData$>(): QueryBuilder<TVars, TData$, TError, TKey, TTags, TFlags> {
3442
return this as any;
3543
}
3644

45+
/**
46+
* Declares the type of error of the query.
47+
*/
3748
withError<TError$>(): QueryBuilder<TVars, TData, TError$, TKey, TTags, TFlags> {
3849
return this as any;
3950
}
4051

52+
/**
53+
* Creates a new query builder with additional config.
54+
*/
4155
withConfig(config: Partial<typeof this.config>): this {
4256
const ctor = this.constructor as typeof QueryBuilder;
4357
const newConfig = this.mergeConfigs(this.config, config);
4458
return new ctor(newConfig) as this;
4559
}
4660

61+
/**
62+
* Adds a preprocessor function to the query.
63+
* The preprocessor function is called before the query function is called.
64+
*/
4765
withPreprocessor(preprocessor: PreprocessorFn<TVars, TVars>): this;
66+
67+
/**
68+
* Adds a preprocessor function to the query with a different type of input variables.
69+
* The preprocessor function is called before the query function is called.
70+
*/
4871
withPreprocessor<TVars$ = TVars>(preprocessor: PreprocessorFn<TVars$, TVars>): QueryBuilder<TVars$, TData, TError, TKey, TTags, TFlags>;
4972

5073
withPreprocessor<TVars$ = TVars>(preprocessor: PreprocessorFn<TVars$, TVars>): QueryBuilder<TVars$, TData, TError, TKey, TTags, TFlags> {
@@ -55,7 +78,18 @@ export class QueryBuilder<
5578
});
5679
}
5780

81+
/**
82+
* Adds a middleware function to the query.
83+
* The middleware function wraps the query function and
84+
* can be used to modify the input variables, the output data, or the error.
85+
*/
5886
withMiddleware(middleware: MiddlewareFn<TVars, TData, TError, TKey>): this;
87+
88+
/**
89+
* Adds a middleware function to the query with overloaded types.
90+
* The middleware function wraps the query function and
91+
* can be used to modify the input variables, the output data, or the error.
92+
*/
5993
withMiddleware<TVars$ = TVars, TData$ = TData, TError$ = TError>(
6094
middleware: MiddlewareFn<TVars$, TData$, TError$, TKey>,
6195
): QueryBuilder<TVars$, TData$, TError$, TKey, TTags, TFlags>;
@@ -74,20 +108,39 @@ export class QueryBuilder<
74108

75109
static tagCacheId = 0;
76110

111+
/**
112+
* Adds a tag to the query.
113+
* The tag is used to invalidate or update the query when the tag is updated.
114+
*/
77115
withTags(...tags: QueryTagOption<TVars, TData, TError, QueryTagObject<TTags>>[]): this {
78116
return this.withMiddleware(createTagMiddleware(tags.flat(), QueryBuilder.tagCacheId++)) as unknown as this;
79117
}
80118

119+
/**
120+
* Adds a declarative update to the mutation.
121+
* This is used to invalidate or update the queries that were marked with {@link withTags}.
122+
*/
81123
withUpdates(...tags: QueryTagOption<TVars, TData, TError, QueryUpdateTagObject<TVars, TData, TError, TTags>>[]): this {
82124
return this.withMiddleware(createUpdateMiddleware<TVars, TData, TError, TKey, TTags>(tags)) as unknown as this;
83125
}
84126

85127
withTagTypes<TTag extends string, T = unknown>(): QueryBuilder<TVars, TData, TError, TKey, TTags & Record<TTag, T>, TFlags>;
86128
withTagTypes<TTags$ extends Record<string, unknown>>(): QueryBuilder<TVars, TData, TError, TKey, TTags$, TFlags>;
129+
130+
/**
131+
* Declares the type of tags of the query.
132+
* This is used to strongly type the tags which can be helpful when using {@link withUpdates}.
133+
*/
87134
withTagTypes(): this {
88135
return this as any;
89136
}
90137

138+
/**
139+
* Sets the query client for this builder.
140+
* This is required in order to enable {@link client} and {@link tags} interfaces.
141+
* Imperative operations done through those interfaces will be done with the provided query client.
142+
* This method also enables the ability to sync tag invalidation with other browser tabs.
143+
*/
91144
withClient(
92145
queryClient: QueryClient,
93146
{ syncTagsWithOtherTabs = true }: { syncTagsWithOtherTabs?: boolean } = {},
@@ -105,16 +158,26 @@ export class QueryBuilder<
105158
return this.withConfig({ queryClient, syncChannel }) as any;
106159
}
107160

161+
/**
162+
* Adds pagination to the query. This is required for methods like {@link useInfiniteQuery} to be available.
163+
*/
108164
withPagination(
109165
paginationOptions: BuilderPaginationOptions<TVars, TData, TError, TKey>,
110166
): QueryBuilder<TVars, TData, TError, TKey, TTags, TFlags | 'withPagination'> {
111167
return this.withConfig({ paginationOptions } as TODO);
112168
}
113169

170+
/**
171+
* Returns a frozen version of this builder.
172+
* The frozen version cannot be modified using `with*` methods.
173+
*/
114174
asFrozen(): QueryBuilderFrozen<TVars, TData, TError, TKey, TTags, TFlags> {
115175
return this as any;
116176
}
117177

178+
/**
179+
* Binds all query methods to this class instance, so that they can be called after object destructuring.
180+
*/
118181
asBound(): QueryBuilder<TVars, TData, TError, TKey, TTags, TFlags | 'bound'> {
119182
return this.withConfig({ bound: true });
120183
}

packages/tanstack-query-builder/src/http/HttpQueryBuilder.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ import type { HttpBaseHeaders, HttpBaseParams, HttpBaseSearch, HttpBuilderVars }
88
import { createHttpMergeVarsFn, createHttpQueryFn, createHttpQueryKeySanitizer } from './builder-utils';
99
import type { ExtractPathParams, HttpMethod } from './request-types';
1010

11+
/**
12+
* A builder for creating HTTP queries.
13+
* It allows you to define the path parameters, search parameters, body, headers, and metadata.
14+
* It also provides methods for setting the base URL, HTTP method, and other options.
15+
*/
1116
export class HttpQueryBuilder<
1217
TParam = unknown,
1318
TSearch = unknown,
@@ -29,37 +34,64 @@ export class HttpQueryBuilder<
2934
super({ mergeVars, queryFn, queryKeySanitizer, ...config });
3035
}
3136

37+
/**
38+
* Defines the type of body of the request.
39+
* Optionally, you can pass the first argument to set the default body.
40+
*/
3241
withBody<TBody$>(body?: TBody$): HttpQueryBuilder<TParam, TSearch, TBody$, THeader, TMeta, TData, TError, TTags, TFlags> {
3342
if (!body) return this as any;
3443
return this.withVars({ body }) as any;
3544
}
3645

46+
/**
47+
* Defines the type of headers of the request.
48+
* Optionally, you can pass the first argument to set the default headers.
49+
*/
3750
withHeaders<THeaders$ extends HttpBaseHeaders>(
3851
headers?: THeaders$,
3952
): HttpQueryBuilder<TParam, TSearch, TBody, THeaders$, TMeta, TData, TError, TTags, TFlags> {
4053
if (!headers) return this as any;
4154
return this.withVars({ headers }) as any;
4255
}
4356

57+
/**
58+
* Defines the type of path parameters of the request.
59+
* Optionally, you can pass the first argument to set the default parameters.
60+
* Note that when {@link withPath} is used, the path parameters are automatically extracted.
61+
* This method doesn't have to be used in that case.
62+
*/
4463
withParams<TParams$ extends HttpBaseParams>(
4564
params?: TParams$,
4665
): HttpQueryBuilder<TParams$, TSearch, TBody, THeader, TMeta, TData, TError, TTags, TFlags> {
4766
if (!params) return this as any;
4867
return this.withVars({ params }) as any;
4968
}
5069

70+
/**
71+
* Defines the type of search parameters of the request.
72+
* Optionally, you can pass the first argument to set the default search parameters.
73+
*/
5174
withSearch<TSearch$ extends HttpBaseSearch>(
5275
search?: TSearch$,
5376
): HttpQueryBuilder<TParam, TSearch$, TBody, THeader, TMeta, TData, TError, TTags, TFlags> {
5477
if (!search) return this as any;
5578
return this.withVars({ search }) as any;
5679
}
5780

81+
/**
82+
* Defines the type of metadata of the request.
83+
* Optionally, you can pass the first argument to set the default metadata.
84+
*/
5885
withMeta<TMeta$>(meta?: TMeta$): HttpQueryBuilder<TParam, TSearch, TBody, THeader, TMeta$, TData, TError, TTags, TFlags> {
5986
if (!meta) return this as any;
6087
return this.withVars({ meta }) as any;
6188
}
6289

90+
/**
91+
* Sets the path for the HTTP request.
92+
* The path can contain path parameters which are defined with the format `/path/:param1/:param2`.
93+
* The path parameters are automatically extracted and typed strongly as if declared with {@link withParams}.
94+
*/
6395
withPath<const TPath$ extends string>(
6496
path: TPath$,
6597
): ExtractPathParams<TPath$> extends void
@@ -68,10 +100,19 @@ export class HttpQueryBuilder<
68100
return this.withVars({ path }) as any;
69101
}
70102

103+
/**
104+
* Sets the base URL for the HTTP request.
105+
* The base URL will be prepended to relative URLs.
106+
*/
71107
withBaseUrl(baseUrl: string): this {
72108
return this.withVars({ baseUrl }) as any;
73109
}
74110

111+
/**
112+
* SSets the HTTP method for the request.
113+
* The method can be one of the following: `get`, `post`, `put`, `patch`, `delete`, `options`, `head`.
114+
* The default method is `get`.
115+
*/
75116
withMethod(method: HttpMethod): this {
76117
return this.withVars({ method }) as any;
77118
}

website/docs/api/_category_.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"position": 100,
3+
"label": "API Reference",
4+
"collapsible": false,
5+
"collapsed": false
6+
}

0 commit comments

Comments
 (0)