Skip to content

Commit c4e1833

Browse files
committed
move more methods
1 parent 473d69b commit c4e1833

File tree

5 files changed

+77
-75
lines changed

5 files changed

+77
-75
lines changed

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

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { QueryClient } from '@tanstack/react-query';
22
import { operateOnTags } from '../tags/operateOnTags';
33
import type { QueryTagObject, QueryTagOption, QueryUpdateTagObject } from '../tags/types';
4+
import { TODO } from '../type-utils';
45
import { QueryBuilderFrozen } from './QueryBuilderFrozen';
56
import { type MiddlewareFn, createMiddlewareFunction } from './createMiddlewareFunction';
67
import { type PreprocessorFn, createPreprocessorFunction, identityPreprocessor } from './createPreprocessorFunction';
@@ -19,6 +20,7 @@ export class QueryBuilder<
1920
TFlags extends BuilderFlags = '',
2021
> extends QueryBuilderFrozen<TVars, TData, TError, TKey, TTags, TFlags> {
2122
withVars<TVars$ = TVars, const TReset extends boolean = false>(
23+
this: this,
2224
vars?: TVars$,
2325
resetVars = false as TReset,
2426
): QueryBuilder<TVars$, TData, TError, TKey, TTags, TFlags> {
@@ -29,37 +31,45 @@ export class QueryBuilder<
2931
}) as any;
3032
}
3133

32-
withData<TData$>(): QueryBuilder<TVars, TData$, TError, TKey, TTags, TFlags> {
34+
withData<TData$>(this: this): QueryBuilder<TVars, TData$, TError, TKey, TTags, TFlags> {
3335
return this as any;
3436
}
3537

36-
withError<TError$>(): QueryBuilder<TVars, TData, TError$, TKey, TTags, TFlags> {
38+
withError<TError$>(this: this): QueryBuilder<TVars, TData, TError$, TKey, TTags, TFlags> {
3739
return this as any;
3840
}
3941

40-
withConfig(config: Partial<typeof this.config>): this {
42+
withConfig(this: this, config: Partial<typeof this.config>): this {
4143
const ctor = this.constructor as typeof QueryBuilder;
4244
const newConfig = this.mergeConfigs(this.config, config);
4345
return new ctor(newConfig) as this;
4446
}
4547

46-
withPreprocessor(preprocessor: PreprocessorFn<TVars, TVars>): this;
47-
withPreprocessor<TVars$ = TVars>(preprocessor: PreprocessorFn<TVars$, TVars>): QueryBuilder<TVars$, TData, TError, TKey, TTags, TFlags>;
48+
withPreprocessor(this: this, preprocessor: PreprocessorFn<TVars, TVars>): this;
49+
withPreprocessor<TVars$ = TVars>(
50+
this: this,
51+
preprocessor: PreprocessorFn<TVars$, TVars>,
52+
): QueryBuilder<TVars$, TData, TError, TKey, TTags, TFlags>;
4853

49-
withPreprocessor<TVars$ = TVars>(preprocessor: PreprocessorFn<TVars$, TVars>): QueryBuilder<TVars$, TData, TError, TKey, TTags, TFlags> {
54+
withPreprocessor<TVars$ = TVars>(
55+
this: this,
56+
preprocessor: PreprocessorFn<TVars$, TVars>,
57+
): QueryBuilder<TVars$, TData, TError, TKey, TTags, TFlags> {
5058
const newBuilder = this as unknown as QueryBuilder<TVars$, TData, TError, TKey, TTags, TFlags>;
5159

5260
return newBuilder.withConfig({
5361
preprocessorFn: createPreprocessorFunction(preprocessor, this.config.preprocessorFn || identityPreprocessor),
5462
});
5563
}
5664

57-
withMiddleware(middleware: MiddlewareFn<TVars, TData, TError, TKey>): this;
65+
withMiddleware(this: this, middleware: MiddlewareFn<TVars, TData, TError, TKey>): this;
5866
withMiddleware<TVars$ = TVars, TData$ = TData, TError$ = TError>(
67+
this: this,
5968
middleware: MiddlewareFn<TVars$, TData$, TError$, TKey>,
6069
): QueryBuilder<TVars$, TData$, TError$, TKey, TTags, TFlags>;
6170

6271
withMiddleware<TVars$ = TVars, TData$ = TData, TError$ = TError>(
72+
this: this,
6373
middleware: MiddlewareFn<TVars$, TData$, TError$, TKey>,
6474
config?: Partial<BuilderConfig<TVars$, TData$, TError$, TKey>>,
6575
): QueryBuilder<TVars$, TData$, TError$, TKey, TTags, TFlags> {
@@ -68,26 +78,27 @@ export class QueryBuilder<
6878
return newBuilder.withConfig({
6979
...config,
7080
queryFn: createMiddlewareFunction(this.config.queryFn, middleware, newBuilder.config),
71-
});
81+
} as TODO);
7282
}
7383

7484
static tagCacheId = 0;
7585

76-
withTags(...tags: QueryTagOption<TVars, TData, TError, QueryTagObject<TTags>>[]): this {
86+
withTags(this: this, ...tags: QueryTagOption<TVars, TData, TError, QueryTagObject<TTags>>[]): this {
7787
return this.withMiddleware(createTagMiddleware(tags.flat(), QueryBuilder.tagCacheId++)) as unknown as this;
7888
}
7989

80-
withUpdates(...tags: QueryTagOption<TVars, TData, TError, QueryUpdateTagObject<TVars, TData, TError, TTags>>[]): this {
90+
withUpdates(this: this, ...tags: QueryTagOption<TVars, TData, TError, QueryUpdateTagObject<TVars, TData, TError, TTags>>[]): this {
8191
return this.withMiddleware(createUpdateMiddleware<TVars, TData, TError, TKey, TTags>(tags)) as unknown as this;
8292
}
8393

84-
withTagTypes<TTag extends string, T = unknown>(): QueryBuilder<TVars, TData, TError, TKey, TTags & Record<TTag, T>, TFlags>;
85-
withTagTypes<TTags$ extends Record<string, unknown>>(): QueryBuilder<TVars, TData, TError, TKey, TTags$, TFlags>;
86-
withTagTypes(): this {
94+
withTagTypes<TTag extends string, T = unknown>(this: this): QueryBuilder<TVars, TData, TError, TKey, TTags & Record<TTag, T>, TFlags>;
95+
withTagTypes<TTags$ extends Record<string, unknown>>(this: this): QueryBuilder<TVars, TData, TError, TKey, TTags$, TFlags>;
96+
withTagTypes(this: this): this {
8797
return this as any;
8898
}
8999

90100
withClient(
101+
this: this,
91102
queryClient: QueryClient,
92103
{ syncTagsWithOtherTabs = true }: { syncTagsWithOtherTabs?: boolean } = {},
93104
): QueryBuilder<TVars, TData, TError, TKey, TTags, TFlags | 'withClient'> {
@@ -105,16 +116,17 @@ export class QueryBuilder<
105116
}
106117

107118
withPagination(
119+
this: this,
108120
paginationOptions: BuilderPaginationOptions<TVars, TData, TError, TKey>,
109121
): QueryBuilder<TVars, TData, TError, TKey, TTags, TFlags | 'withPagination'> {
110-
return this.withConfig({ paginationOptions }) as any;
122+
return this.withConfig({ paginationOptions } as TODO);
111123
}
112124

113-
asFrozen(): QueryBuilderFrozen<TVars, TData, TError, TKey, TTags, TFlags> {
114-
return this;
125+
asFrozen(this: this): QueryBuilderFrozen<TVars, TData, TError, TKey, TTags, TFlags> {
126+
return this as any;
115127
}
116128

117-
asBound(): QueryBuilder<TVars, TData, TError, TKey, TTags, TFlags | 'bound'> {
129+
asBound(this: this): QueryBuilder<TVars, TData, TError, TKey, TTags, TFlags | 'bound'> {
118130
return this.withConfig({ bound: true });
119131
}
120132
}

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

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ import { type BuilderOptions, BuilderPaginationOptions, mergeBuilderOptions, mer
3535
import type { BuilderConfig, BuilderFlags, BuilderQueriesResult, HasClient, HasPagination, IsBound } from './types';
3636
import { areKeysEqual, assertBound, getRandomKey, mergeMutationOptions, mergeVars } from './utils';
3737

38-
type AnyBuilder<TFlags extends BuilderFlags> = QueryBuilderFrozen<any, any, any, any, any, TFlags>;
39-
type IsBoundThis<TFlags extends BuilderFlags> = IsBound<TFlags, AnyBuilder<TFlags>>;
38+
type IsBoundThis<TFlags extends BuilderFlags, TB extends QueryBuilderFrozen<any, any, any, any, any, TFlags>> = IsBound<TFlags, TB>;
4039

4140
type UseQueriesArgs<TVars, TData, TError, TKey extends unknown[]> = [
4241
queries: {
@@ -113,7 +112,7 @@ export class QueryBuilderFrozen<
113112
//#region Query
114113

115114
getQueryFn(
116-
this: IsBoundThis<TFlags>,
115+
this: IsBoundThis<TFlags, this>,
117116
operationType: 'query' | 'queries' | 'infiniteQuery' = 'query',
118117
): QueryFunction<TData, TKey, Partial<TVars>> {
119118
assertBound(this);
@@ -130,21 +129,21 @@ export class QueryBuilderFrozen<
130129
};
131130
}
132131

133-
getQueryKeyHashFn(this: IsBoundThis<TFlags>): (key: TKey) => string {
132+
getQueryKeyHashFn(this: IsBoundThis<TFlags, this>): (key: TKey) => string {
134133
assertBound(this);
135134
return (key) => {
136135
const sanitized = this.config.queryKeySanitizer ? this.config.queryKeySanitizer(key) : key;
137136
return hashKey(sanitized);
138137
};
139138
}
140139

141-
getQueryKey(this: IsBoundThis<TFlags>, vars: TVars): DataTag<TKey, TData, TError> {
140+
getQueryKey(this: IsBoundThis<TFlags, this>, vars: TVars): DataTag<TKey, TData, TError> {
142141
assertBound(this);
143142
return [this.preprocessVars(this.mergeVars([this.config.vars, vars as TODO]))] as DataTag<TKey, TData, TError>;
144143
}
145144

146145
getQueryOptions(
147-
this: IsBoundThis<TFlags>,
146+
this: IsBoundThis<TFlags, this>,
148147
vars: TVars,
149148
opts?: BuilderOptions<TVars, TData, TError, TKey>,
150149
operationType?: 'query' | 'queries' | 'infiniteQuery',
@@ -162,26 +161,26 @@ export class QueryBuilderFrozen<
162161
]) as TODO;
163162
}
164163

165-
useQuery(this: IsBoundThis<TFlags>, vars: TVars, opts?: BuilderOptions<TVars, TData, TError, TKey>): UseQueryResult<TData, TError> {
164+
useQuery(this: IsBoundThis<TFlags, this>, vars: TVars, opts?: BuilderOptions<TVars, TData, TError, TKey>): UseQueryResult<TData, TError> {
166165
assertBound(this);
167166
return useQuery(this.getQueryOptions(vars, opts), this.config.queryClient);
168167
}
169168

170169
useSuspenseQuery(
171-
this: IsBoundThis<TFlags>,
170+
this: IsBoundThis<TFlags, this>,
172171
vars: TVars,
173172
opts?: BuilderOptions<TVars, TData, TError, TKey>,
174173
): UseSuspenseQueryResult<TData, TError> {
175174
assertBound(this);
176175
return useSuspenseQuery(this.getQueryOptions(vars, opts), this.config.queryClient);
177176
}
178177

179-
usePrefetchQuery(this: IsBoundThis<TFlags>, vars: TVars, opts?: BuilderOptions<TVars, TData, TError, TKey>): void {
178+
usePrefetchQuery(this: IsBoundThis<TFlags, this>, vars: TVars, opts?: BuilderOptions<TVars, TData, TError, TKey>): void {
180179
assertBound(this);
181180
usePrefetchQuery(this.getQueryOptions(vars, opts), this.config.queryClient);
182181
}
183182

184-
useIsFetching(this: IsBoundThis<TFlags>, vars: TVars, filters?: QueryFilters): number {
183+
useIsFetching(this: IsBoundThis<TFlags, this>, vars: TVars, filters?: QueryFilters): number {
185184
assertBound(this);
186185
return useIsFetching({ queryKey: this.getQueryKey(vars), ...filters }, this.config.queryClient);
187186
}
@@ -191,7 +190,7 @@ export class QueryBuilderFrozen<
191190
//#region Queries
192191

193192
private useQueriesInternal(
194-
this: IsBoundThis<TFlags>,
193+
this: IsBoundThis<TFlags, this>,
195194
useHook: typeof useQueries | typeof useSuspenseQueries,
196195
...[queries, sharedVars, sharedOpts]: UseQueriesArgs<TVars, TData, TError, TKey>
197196
): BuilderQueriesResult<TVars, TData, TError, TKey> {
@@ -219,15 +218,15 @@ export class QueryBuilderFrozen<
219218
}
220219

221220
useQueries(
222-
this: IsBoundThis<TFlags>,
221+
this: IsBoundThis<TFlags, this>,
223222
...args: UseQueriesArgs<TVars, TData, TError, TKey>
224223
): BuilderQueriesResult<TVars, TData, TError, TKey> {
225224
assertBound(this);
226225
return this.useQueriesInternal(useQueries, ...args);
227226
}
228227

229228
useSuspenseQueries(
230-
this: IsBoundThis<TFlags>,
229+
this: IsBoundThis<TFlags, this>,
231230
...args: UseQueriesArgs<TVars, TData, TError, TKey>
232231
): BuilderQueriesResult<TVars, TData, TError, TKey> {
233232
assertBound(this);
@@ -241,7 +240,7 @@ export class QueryBuilderFrozen<
241240
declare getInfiniteQueryOptions: HasPagination<
242241
TFlags,
243242
(
244-
this: IsBoundThis<TFlags>,
243+
this: IsBoundThis<TFlags, this>,
245244
vars: TVars,
246245
opts?: Partial<BuilderPaginationOptions<TVars, TData, TError, TKey>>,
247246
) => WithRequired<BuilderPaginationOptions<TVars, TData, TError, TKey>, 'queryFn' | 'queryKey' | 'initialPageParam'>
@@ -250,21 +249,21 @@ export class QueryBuilderFrozen<
250249
declare useInfiniteQuery: HasPagination<
251250
TFlags,
252251
(
253-
this: IsBoundThis<TFlags>,
252+
this: IsBoundThis<TFlags, this>,
254253
vars: TVars,
255254
opts?: Partial<BuilderPaginationOptions<TVars, TData, TError, TKey>>,
256255
) => UseInfiniteQueryResult<InfiniteData<TData, Partial<TVars>>, TError>
257256
>;
258257

259258
declare usePrefetchInfiniteQuery: HasPagination<
260259
TFlags,
261-
(this: IsBoundThis<TFlags>, vars: TVars, opts?: Partial<BuilderPaginationOptions<TVars, TData, TError, TKey>>) => void
260+
(this: IsBoundThis<TFlags, this>, vars: TVars, opts?: Partial<BuilderPaginationOptions<TVars, TData, TError, TKey>>) => void
262261
>;
263262

264263
declare useSuspenseInfiniteQuery: HasPagination<
265264
TFlags,
266265
(
267-
this: IsBoundThis<TFlags>,
266+
this: IsBoundThis<TFlags, this>,
268267
vars: TVars,
269268
opts?: Partial<BuilderPaginationOptions<TVars, TData, TError, TKey>>,
270269
) => UseSuspenseInfiniteQueryResult<InfiniteData<TData, Partial<TVars>>, TError>
@@ -274,7 +273,7 @@ export class QueryBuilderFrozen<
274273

275274
//#region Mutation
276275

277-
getMutationFn(this: IsBoundThis<TFlags>, queryClient: QueryClient, meta?: any): MutationFunction<TData, TVars> {
276+
getMutationFn(this: IsBoundThis<TFlags, this>, queryClient: QueryClient, meta?: any): MutationFunction<TData, TVars> {
278277
assertBound(this);
279278

280279
return async (vars) => {
@@ -291,7 +290,7 @@ export class QueryBuilderFrozen<
291290
}
292291

293292
#randomKey?: string;
294-
getMutationKey(this: IsBoundThis<TFlags>): MutationKey {
293+
getMutationKey(this: IsBoundThis<TFlags, this>): MutationKey {
295294
assertBound(this);
296295

297296
if (this.config.options?.mutationKey) return this.config.options.mutationKey;
@@ -302,7 +301,7 @@ export class QueryBuilderFrozen<
302301
}
303302

304303
getMutationOptions(
305-
this: IsBoundThis<TFlags>,
304+
this: IsBoundThis<TFlags, this>,
306305
queryClient: QueryClient,
307306
opts?: BuilderOptions<TVars, TData, TError, TKey>,
308307
): UseMutationOptions<TData, TError, TVars> {
@@ -319,7 +318,7 @@ export class QueryBuilderFrozen<
319318
}
320319

321320
getMutationFilters(
322-
this: IsBoundThis<TFlags>,
321+
this: IsBoundThis<TFlags, this>,
323322
vars?: TVars,
324323
filters?: MutationFilters<TData, TError, TVars>,
325324
): MutationFilters<any, any, any> {
@@ -342,21 +341,21 @@ export class QueryBuilderFrozen<
342341
}
343342

344343
useMutation(
345-
this: IsBoundThis<TFlags>,
344+
this: IsBoundThis<TFlags, this>,
346345
opts?: BuilderOptions<TVars, TData, TError, TKey>,
347346
): ReturnType<typeof useMutation<TData, TError, TVars>> {
348347
assertBound(this);
349348
const queryClient = useQueryClient(this.config.queryClient);
350349
return useMutation(this.getMutationOptions(queryClient, opts), this.config.queryClient);
351350
}
352351

353-
useIsMutating(this: IsBoundThis<TFlags>, vars: TVars, filters?: MutationFilters<TData, TError, TVars>): number {
352+
useIsMutating(this: IsBoundThis<TFlags, this>, vars: TVars, filters?: MutationFilters<TData, TError, TVars>): number {
354353
assertBound(this);
355354
return useIsMutating(this.getMutationFilters(vars, filters), this.config.queryClient);
356355
}
357356

358357
useMutationState<TSelect = Mutation<TData, TError, TVars>>(
359-
this: IsBoundThis<TFlags>,
358+
this: IsBoundThis<TFlags, this>,
360359
vars?: TVars,
361360
filters?: MutationFilters<TData, TError, TVars>,
362361
select?: (mt: Mutation<TData, TError, TVars>) => TSelect,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@ export function getRandomKey() {
5353
return Math.random().toString(36).substring(7);
5454
}
5555

56-
export function assertBound<T extends IsBound<any, unknown>>(t: T): asserts t is Exclude<T, void> {
56+
export function assertBound<T extends IsBound<any, unknown>>(t: T): asserts t is T & Exclude<T, void> {
5757
if (t === undefined) throw new Error('Method called on unbound instance');
5858
}

0 commit comments

Comments
 (0)