Skip to content

Commit 9179031

Browse files
committed
simplify overriden types
1 parent 688ede7 commit 9179031

File tree

5 files changed

+97
-38
lines changed

5 files changed

+97
-38
lines changed

src/builder/HttpMutationBuilder.ts

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
import { ExtractPathParams } from '../http/types';
2-
import { Prettify, WithOptional } from '../types/utils';
2+
import { WithOptional } from '../types/utils';
3+
import { HttpQueryBuilder } from './HttpQueryBuilder';
34
import { MutationBuilder, MutationBuilderConfig } from './MutationBuilder';
4-
import { HttpBaseHeaders, HttpBaseParams, HttpBaseSearch, HttpBuilderTypeTemplate } from './types';
5-
import { PrettifyWithVars } from './types';
5+
import { QueryBuilder } from './QueryBuilder';
6+
import {
7+
HttpBaseHeaders,
8+
HttpBaseParams,
9+
HttpBaseSearch,
10+
HttpBuilderTypeTemplate,
11+
SetDataType,
12+
SetErrorType,
13+
} from './types';
14+
import { AppendVarsType } from './types';
615
import { createHttpQueryFn, mergeHttpVars } from './utils';
716

817
export class HttpMutationBuilder<
@@ -22,28 +31,28 @@ export class HttpMutationBuilder<
2231
});
2332
}
2433

25-
withBody<TBody>(body?: TBody): HttpMutationBuilder<PrettifyWithVars<T, { body: TBody }>> {
34+
withBody<TBody>(body?: TBody): HttpMutationBuilder<AppendVarsType<T, { body: TBody }>> {
2635
if (!body) return this as any;
2736
return this.withVars({ body }) as any;
2837
}
2938

3039
withHeaders<THeaders extends HttpBaseHeaders>(
3140
headers?: THeaders,
32-
): HttpMutationBuilder<PrettifyWithVars<T, { headers: THeaders }>> {
41+
): HttpMutationBuilder<AppendVarsType<T, { headers: THeaders }>> {
3342
if (!headers) return this as any;
3443
return this.withVars({ headers }) as any;
3544
}
3645

3746
withParams<TParams extends HttpBaseParams>(
3847
params?: TParams,
39-
): HttpMutationBuilder<PrettifyWithVars<T, { params: TParams }>> {
48+
): HttpMutationBuilder<AppendVarsType<T, { params: TParams }>> {
4049
if (!params) return this as any;
4150
return this.withVars({ params }) as any;
4251
}
4352

4453
withSearch<TSearch extends HttpBaseSearch>(
4554
search?: TSearch,
46-
): HttpMutationBuilder<PrettifyWithVars<T, { search: TSearch }>> {
55+
): HttpMutationBuilder<AppendVarsType<T, { search: TSearch }>> {
4756
if (!search) return this as any;
4857
return this.withVars({ search }) as any;
4958
}
@@ -52,15 +61,20 @@ export class HttpMutationBuilder<
5261
path: TPath,
5362
): ExtractPathParams<TPath> extends void
5463
? this
55-
: HttpMutationBuilder<PrettifyWithVars<T, { params: ExtractPathParams<TPath> }>> {
64+
: HttpMutationBuilder<AppendVarsType<T, { params: ExtractPathParams<TPath> }>> {
5665
return this.withVars({ path }) as any;
5766
}
5867

5968
withBaseUrl(baseUrl: string): this {
6069
return this.withVars({ baseUrl }) as any;
6170
}
6271

63-
declare withData: <TData>() => HttpMutationBuilder<Prettify<T & { data: TData }>>;
64-
declare withError: <TError>() => HttpMutationBuilder<Prettify<T & { error: TError }>>;
65-
declare withVars: <TVars = T['vars']>(vars?: TVars) => HttpMutationBuilder<PrettifyWithVars<T, Partial<TVars>>>;
72+
declare withData: <TData>() => HttpMutationBuilder<SetDataType<T, TData>>;
73+
declare withError: <TError>() => HttpMutationBuilder<SetErrorType<T, TError>>;
74+
declare withVars: <TVars = T['vars'], const TReset extends boolean = false>(
75+
vars?: TVars,
76+
reset?: TReset,
77+
) => HttpMutationBuilder<AppendVarsType<T, Partial<TVars>, TReset>>;
78+
declare asQueryBuilder: () => HttpQueryBuilder<T>;
79+
protected override QueryBuilderConstructor: typeof QueryBuilder = HttpQueryBuilder as typeof QueryBuilder;
6680
}

src/builder/HttpQueryBuilder.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
import { ExtractPathParams } from '../http/types';
2-
import { Prettify, WithOptional } from '../types/utils';
2+
import { WithOptional } from '../types/utils';
33
import { HttpMutationBuilder } from './HttpMutationBuilder';
44
import { MutationBuilder } from './MutationBuilder';
55
import { QueryBuilder, QueryBuilderConfig } from './QueryBuilder';
6-
import { HttpBaseHeaders, HttpBaseParams, HttpBaseSearch, HttpBuilderTypeTemplate } from './types';
7-
import { PrettifyWithVars } from './types';
6+
import {
7+
HttpBaseHeaders,
8+
HttpBaseParams,
9+
HttpBaseSearch,
10+
HttpBuilderTypeTemplate,
11+
SetDataType,
12+
SetErrorType,
13+
} from './types';
14+
import { AppendVarsType } from './types';
815
import { createHttpQueryFn, mergeHttpVars } from './utils';
916

1017
export class HttpQueryBuilder<T extends HttpBuilderTypeTemplate = HttpBuilderTypeTemplate> extends QueryBuilder<T> {
@@ -14,28 +21,28 @@ export class HttpQueryBuilder<T extends HttpBuilderTypeTemplate = HttpBuilderTyp
1421
super({ mergeVars, queryFn, ...config });
1522
}
1623

17-
withBody<TBody>(body?: TBody): HttpQueryBuilder<PrettifyWithVars<T, { body: TBody }>> {
24+
withBody<TBody>(body?: TBody): HttpQueryBuilder<AppendVarsType<T, { body: TBody }>> {
1825
if (!body) return this as any;
1926
return this.withVars({ body }) as any;
2027
}
2128

2229
withHeaders<THeaders extends HttpBaseHeaders>(
2330
headers?: THeaders,
24-
): HttpQueryBuilder<PrettifyWithVars<T, { headers: THeaders }>> {
31+
): HttpQueryBuilder<AppendVarsType<T, { headers: THeaders }>> {
2532
if (!headers) return this as any;
2633
return this.withVars({ headers }) as any;
2734
}
2835

2936
withParams<TParams extends HttpBaseParams>(
3037
params?: TParams,
31-
): HttpQueryBuilder<PrettifyWithVars<T, { params: TParams }>> {
38+
): HttpQueryBuilder<AppendVarsType<T, { params: TParams }>> {
3239
if (!params) return this as any;
3340
return this.withVars({ params }) as any;
3441
}
3542

3643
withSearch<TSearch extends HttpBaseSearch>(
3744
search?: TSearch,
38-
): HttpQueryBuilder<PrettifyWithVars<T, { search: TSearch }>> {
45+
): HttpQueryBuilder<AppendVarsType<T, { search: TSearch }>> {
3946
if (!search) return this as any;
4047
return this.withVars({ search }) as any;
4148
}
@@ -44,17 +51,20 @@ export class HttpQueryBuilder<T extends HttpBuilderTypeTemplate = HttpBuilderTyp
4451
path: TPath,
4552
): ExtractPathParams<TPath> extends void
4653
? this
47-
: HttpQueryBuilder<PrettifyWithVars<T, { params: ExtractPathParams<TPath> }>> {
54+
: HttpQueryBuilder<AppendVarsType<T, { params: ExtractPathParams<TPath> }>> {
4855
return this.withVars({ path }) as any;
4956
}
5057

5158
withBaseUrl(baseUrl: string): this {
5259
return this.withVars({ baseUrl }) as any;
5360
}
5461

55-
declare withData: <TData>() => HttpQueryBuilder<Prettify<T & { data: TData }>>;
56-
declare withError: <TError>() => HttpQueryBuilder<Prettify<T & { error: TError }>>;
57-
declare withVars: <TVars = T['vars']>(vars?: TVars) => HttpQueryBuilder<PrettifyWithVars<T, Partial<TVars>>>;
62+
declare withData: <TData>() => HttpQueryBuilder<SetDataType<T, TData>>;
63+
declare withError: <TError>() => HttpQueryBuilder<SetErrorType<T, TError>>;
64+
declare withVars: <TVars = T['vars'], const TReset extends boolean = false>(
65+
vars?: TVars,
66+
reset?: TReset,
67+
) => HttpQueryBuilder<AppendVarsType<T, Partial<TVars>, TReset>>;
5868
declare asMutationBuilder: () => HttpMutationBuilder<T>;
5969
protected override MutationBuilderConstructor: typeof MutationBuilder = HttpMutationBuilder as typeof MutationBuilder;
6070
}

src/builder/MutationBuilder.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import {
1010
} from '@tanstack/react-query';
1111
import { mergeTagOptions } from '../tags/mergeTagOptions';
1212
import { QueryInvalidatesMetadata } from '../tags/types';
13-
import { Prettify } from '../types/utils';
14-
import { BuilderMergeVarsFn, BuilderQueryFn } from './types';
15-
import { BuilderTypeTemplate, PrettifyWithVars } from './types';
13+
import { QueryBuilder } from './QueryBuilder';
14+
import { BuilderMergeVarsFn, BuilderQueryFn, SetDataType, SetErrorType } from './types';
15+
import { AppendVarsType, BuilderTypeTemplate } from './types';
1616
import { mergeMutationOptions, mergeVars } from './utils';
1717

1818
function getRandomKey() {
@@ -112,19 +112,22 @@ class MutationBuilderClient<
112112
}
113113

114114
export class MutationBuilder<T extends BuilderTypeTemplate = BuilderTypeTemplate> extends MutationBuilderFrozen<T> {
115-
withVars<TVars = T['vars']>(vars?: TVars): MutationBuilder<PrettifyWithVars<T, Partial<TVars>>> {
115+
withVars<TVars = T['vars'], const TReset extends boolean = false>(
116+
vars?: TVars,
117+
resetVars = false as TReset,
118+
): MutationBuilder<AppendVarsType<T, Partial<TVars>, TReset>> {
116119
if (!vars) return this as any;
117120

118121
return this.withConfig({
119-
vars: this.mergeVars([this.config.vars, vars]),
122+
vars: resetVars ? vars : mergeVars([this.config.vars, vars], this.config.mergeVars),
120123
}) as any;
121124
}
122125

123-
withData<TData>(): MutationBuilder<Prettify<T & { data: TData }>> {
126+
withData<TData>(): MutationBuilder<SetDataType<T, TData>> {
124127
return this as any;
125128
}
126129

127-
withError<TError>(): MutationBuilder<Prettify<T & { error: TError }>> {
130+
withError<TError>(): MutationBuilder<SetErrorType<T, TError>> {
128131
return this as any;
129132
}
130133

@@ -137,6 +140,17 @@ export class MutationBuilder<T extends BuilderTypeTemplate = BuilderTypeTemplate
137140
freeze(): MutationBuilderFrozen<T> {
138141
return this;
139142
}
143+
144+
protected QueryBuilderConstructor: typeof QueryBuilder = QueryBuilder;
145+
146+
asQueryBuilder(): QueryBuilder<T> {
147+
return new this.QueryBuilderConstructor({
148+
queryFn: this.config.queryFn,
149+
queryClient: this.config.queryClient,
150+
mergeVars: this.config.mergeVars,
151+
vars: this.config.vars,
152+
});
153+
}
140154
}
141155

142156
export type MutationBuilderConfig<T extends BuilderTypeTemplate> = QueryInvalidatesMetadata<

src/builder/QueryBuilder.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ import {
1818
} from '@tanstack/react-query';
1919
import { mergeTagOptions } from '../tags/mergeTagOptions';
2020
import { QueryTagOption } from '../tags/types';
21-
import { FunctionType, Prettify } from '../types/utils';
21+
import { FunctionType } from '../types/utils';
2222
import { MutationBuilder } from './MutationBuilder';
23-
import { BuilderMergeVarsFn, BuilderQueriesResult, BuilderQueryFn } from './types';
24-
import { BuilderTypeTemplate, PrettifyWithVars } from './types';
23+
import { BuilderMergeVarsFn, BuilderQueriesResult, BuilderQueryFn, SetDataType, SetErrorType } from './types';
24+
import { AppendVarsType, BuilderTypeTemplate } from './types';
2525
import { mergeQueryOptions, mergeVars } from './utils';
2626

2727
export class QueryBuilderFrozen<T extends BuilderTypeTemplate> {
@@ -182,19 +182,22 @@ class QueryBuilderClient<
182182
}
183183

184184
export class QueryBuilder<T extends BuilderTypeTemplate = BuilderTypeTemplate> extends QueryBuilderFrozen<T> {
185-
withVars<TVars = T['vars']>(vars?: TVars): QueryBuilder<PrettifyWithVars<T, Partial<TVars>>> {
185+
withVars<TVars = T['vars'], const TReset extends boolean = false>(
186+
vars?: TVars,
187+
resetVars = false as TReset,
188+
): QueryBuilder<AppendVarsType<T, Partial<TVars>, TReset>> {
186189
if (!vars) return this as any;
187190

188191
return this.withConfig({
189-
vars: mergeVars([this.config.vars, vars], this.config.mergeVars),
192+
vars: resetVars ? vars : mergeVars([this.config.vars, vars], this.config.mergeVars),
190193
}) as any;
191194
}
192195

193-
withData<TData>(): QueryBuilder<Prettify<T & { data: TData }>> {
196+
withData<TData>(): QueryBuilder<SetDataType<T, TData>> {
194197
return this as any;
195198
}
196199

197-
withError<TError>(): QueryBuilder<Prettify<T & { error: TError }>> {
200+
withError<TError>(): QueryBuilder<SetErrorType<T, TError>> {
198201
return this as any;
199202
}
200203

src/builder/types.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,26 @@ import { RequestError } from '../http/errors';
33
import { HttpRequestOptions } from '../http/types';
44
import { Prettify } from '../types/utils';
55

6-
export type PrettifyWithVars<T extends BuilderTypeTemplate, TVars> = Prettify<
7-
Omit<T, 'vars'> & { vars: Prettify<T['vars'] & TVars> }
6+
export type SetDataType<T extends BuilderTypeTemplate, TData> = Prettify<Omit<T, 'data'> & { data: TData }>;
7+
8+
export type SetErrorType<T extends BuilderTypeTemplate, TError> = Prettify<Omit<T, 'error'> & { error: TError }>;
9+
10+
export type AppendVarsType<T extends BuilderTypeTemplate, TVars, TResetVars extends boolean = false> = Prettify<
11+
Omit<T, 'vars'> & { vars: Prettify<(TResetVars extends true ? unknown : T['vars']) & TVars> }
12+
>;
13+
14+
export type SetAllTypes<
15+
T extends BuilderTypeTemplate,
16+
TData,
17+
TError,
18+
TVars,
19+
TResetVars extends boolean = false,
20+
> = Prettify<
21+
Omit<T, 'data' | 'error' | 'vars'> & {
22+
data: TData;
23+
error: TError;
24+
vars: (TResetVars extends true ? unknown : T['vars']) & TVars;
25+
}
826
>;
927

1028
export interface BuilderTypeTemplate {

0 commit comments

Comments
 (0)