Skip to content

Commit 929b29b

Browse files
committed
spread types for better TS performance
1 parent 663144d commit 929b29b

File tree

15 files changed

+378
-366
lines changed

15 files changed

+378
-366
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ It uses the builder pattern, the best pattern that works with complex Typescript
3434

3535
- Strong typed customizable tags
3636
- Infinite queries
37-
- OOTB Query Hash function
37+
- Single interface for mutations and queries
3838

3939
## Examples
4040

examples/vite/src/App.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@ import './mocks';
22
import { useRef, useState } from 'react';
33
import { CommentData, PostData, baseUrl } from './mocks';
44
import './App.css';
5-
import { HttpQueryBuilder, useOperateOnTags } from 'react-query-builder';
5+
import { HttpMutationBuilder, HttpQueryBuilder, useOperateOnTags } from 'react-query-builder';
66
import { queryClient } from './client';
77

88
const baseQuery = new HttpQueryBuilder({
99
queryClient,
1010
syncChannel: new BroadcastChannel('react-query-builder'),
1111
}).withBaseUrl(baseUrl);
12-
const baseMutation = baseQuery.asMutationBuilder();
12+
const baseMutation = new HttpMutationBuilder({
13+
queryClient,
14+
syncChannel: new BroadcastChannel('react-query-builder'),
15+
}).withBaseUrl(baseUrl);
1316

1417
const resetMutation = baseMutation.withPath('/reset').withUpdates('*');
1518

src/builder/HttpMutationBuilder.ts

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,81 @@
1+
import { RequestError } from '../http/errors';
12
import { ExtractPathParams, HttpMethod } from '../http/types';
23
import { WithOptional } from '../types/utils';
34
import { MutationBuilder, MutationBuilderConfig } from './MutationBuilder';
4-
import { MiddlewareFn } from './createMiddlewareFunction';
5-
import {
6-
HttpBaseHeaders,
7-
HttpBaseParams,
8-
HttpBaseSearch,
9-
HttpBuilderTypeTemplate,
10-
SetAllTypes,
11-
SetDataType,
12-
SetErrorType,
13-
} from './types';
14-
import { AppendVarsType } from './types';
15-
import { createHttpQueryFn, mergeHttpVars } from './utils';
5+
import { HttpBaseHeaders, HttpBaseParams, HttpBaseSearch, HttpBuilderVars } from './types';
6+
import { createHttpQueryFn, createHttpQueryHashFn } from './utils';
7+
import { createHttpMergeVarsFn } from './utils';
168

179
export class HttpMutationBuilder<
18-
T extends HttpBuilderTypeTemplate = HttpBuilderTypeTemplate,
19-
> extends MutationBuilder<T> {
20-
constructor(config?: WithOptional<MutationBuilderConfig<T>, 'queryFn'>) {
21-
const mergeVars = config?.mergeVars || mergeHttpVars;
22-
const queryFn = config?.queryFn || createHttpQueryFn<T>(mergeVars);
10+
TParam = unknown,
11+
TSearch = unknown,
12+
TBody = unknown,
13+
THeader = unknown,
14+
TMeta = unknown,
15+
TData = unknown,
16+
TError = RequestError,
17+
TKey extends [HttpBuilderVars] = [HttpBuilderVars<TParam, TSearch, TBody, THeader, TMeta>],
18+
> extends MutationBuilder<HttpBuilderVars<TParam, TSearch, TBody, THeader, TMeta>, TData, TError, TKey> {
19+
protected declare _vars: HttpBuilderVars<TParam, TSearch, TBody, THeader, TMeta>;
20+
21+
constructor(
22+
config?: WithOptional<
23+
MutationBuilderConfig<HttpBuilderVars<TParam, TSearch, TBody, THeader, TMeta>, TData, TError, TKey>,
24+
'queryFn'
25+
>,
26+
) {
27+
const mergeVars = config?.mergeVars || createHttpMergeVarsFn();
28+
const queryFn = config?.queryFn || createHttpQueryFn(mergeVars);
29+
const queryKeyHashFn = config?.queryKeyHashFn || createHttpQueryHashFn();
30+
2331
super({
2432
mergeVars,
2533
queryFn,
34+
queryKeyHashFn,
2635
...config,
2736
vars: {
2837
method: 'post',
29-
...config?.vars,
38+
...config?.vars!,
3039
},
3140
});
3241
}
3342

34-
withBody<TBody>(body?: TBody): HttpMutationBuilder<AppendVarsType<T, { body: TBody }>> {
43+
withBody<TBody$>(body?: TBody$): HttpMutationBuilder<TParam, TSearch, TBody$, THeader, TMeta, TData, TError> {
3544
if (!body) return this as any;
3645
return this.withVars({ body }) as any;
3746
}
3847

39-
withHeaders<THeaders extends HttpBaseHeaders>(
40-
headers?: THeaders,
41-
): HttpMutationBuilder<AppendVarsType<T, { headers: THeaders }>> {
48+
withHeaders<THeaders$ extends HttpBaseHeaders>(
49+
headers?: THeaders$,
50+
): HttpMutationBuilder<TParam, TSearch, TBody, THeaders$, TMeta, TData, TError> {
4251
if (!headers) return this as any;
4352
return this.withVars({ headers }) as any;
4453
}
4554

46-
withParams<TParams extends HttpBaseParams>(
47-
params?: TParams,
48-
): HttpMutationBuilder<AppendVarsType<T, { params: TParams }>> {
55+
withParams<TParams$ extends HttpBaseParams>(
56+
params?: TParams$,
57+
): HttpMutationBuilder<TParams$, TSearch, TBody, THeader, TMeta, TData, TError> {
4958
if (!params) return this as any;
5059
return this.withVars({ params }) as any;
5160
}
5261

53-
withSearch<TSearch extends HttpBaseSearch>(
54-
search?: TSearch,
55-
): HttpMutationBuilder<AppendVarsType<T, { search: TSearch }>> {
62+
withSearch<TSearch$ extends HttpBaseSearch>(
63+
search?: TSearch$,
64+
): HttpMutationBuilder<TParam, TSearch$, TBody, THeader, TMeta, TData, TError> {
5665
if (!search) return this as any;
5766
return this.withVars({ search }) as any;
5867
}
5968

60-
withMeta<TMeta>(meta?: TMeta): HttpMutationBuilder<AppendVarsType<T, { meta: TMeta }>> {
69+
withMeta<TMeta$>(meta?: TMeta$): HttpMutationBuilder<TParam, TSearch, TBody, THeader, TMeta$, TData, TError> {
6170
if (!meta) return this as any;
6271
return this.withVars({ meta }) as any;
6372
}
6473

65-
withPath<const TPath extends string>(
66-
path: TPath,
67-
): ExtractPathParams<TPath> extends void
74+
withPath<const TPath$ extends string>(
75+
path: TPath$,
76+
): ExtractPathParams<TPath$> extends void
6877
? this
69-
: HttpMutationBuilder<AppendVarsType<T, { params: ExtractPathParams<TPath> }>> {
78+
: HttpMutationBuilder<ExtractPathParams<TPath$>, TSearch, TBody, THeader, TMeta, TData, TError> {
7079
return this.withVars({ path }) as any;
7180
}
7281

@@ -78,14 +87,6 @@ export class HttpMutationBuilder<
7887
return this.withVars({ method }) as any;
7988
}
8089

81-
declare withData: <TData>() => HttpMutationBuilder<SetDataType<T, TData>>;
82-
declare withError: <TError>() => HttpMutationBuilder<SetErrorType<T, TError>>;
83-
declare withVars: <TVars = T['vars'], const TReset extends boolean = false>(
84-
vars?: TVars,
85-
reset?: TReset,
86-
) => HttpMutationBuilder<AppendVarsType<T, Partial<TVars>, TReset>>;
87-
88-
declare withMiddleware: <TVars = T['vars'], TData = T['data'], TError = T['error']>(
89-
middleware: MiddlewareFn<TVars, TData, TError, T>,
90-
) => HttpMutationBuilder<SetAllTypes<T, TData, TError, TVars, true>>;
90+
declare withData: <TData$>() => HttpMutationBuilder<TParam, TSearch, TBody, THeader, TMeta, TData$, TError, TKey>;
91+
declare withError: <TError$>() => HttpMutationBuilder<TParam, TSearch, TBody, THeader, TMeta, TData, TError$, TKey>;
9192
}

src/builder/HttpQueryBuilder.ts

Lines changed: 40 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,70 @@
1+
import { RequestError } from '../http/errors';
12
import { ExtractPathParams, HttpMethod } from '../http/types';
23
import { WithOptional } from '../types/utils';
3-
import { HttpMutationBuilder } from './HttpMutationBuilder';
4-
import { MutationBuilder } from './MutationBuilder';
54
import { QueryBuilder, QueryBuilderConfig } from './QueryBuilder';
6-
import { MiddlewareFn } from './createMiddlewareFunction';
7-
import { PreprocessorFn } from './createPreprocessorFunction';
8-
import {
9-
HttpBaseHeaders,
10-
HttpBaseParams,
11-
HttpBaseSearch,
12-
HttpBuilderTypeTemplate,
13-
SetAllTypes,
14-
SetDataType,
15-
SetErrorType,
16-
} from './types';
17-
import { AppendVarsType } from './types';
18-
import { createHttpQueryFn, createHttpQueryHashFn, mergeHttpVars } from './utils';
5+
import { HttpBaseHeaders, HttpBaseParams, HttpBaseSearch, HttpBuilderVars } from './types';
6+
import { createHttpMergeVarsFn, createHttpQueryFn, createHttpQueryHashFn } from './utils';
197

20-
export class HttpQueryBuilder<T extends HttpBuilderTypeTemplate = HttpBuilderTypeTemplate> extends QueryBuilder<T> {
21-
constructor(config?: WithOptional<QueryBuilderConfig<T>, 'queryFn'>) {
22-
const mergeVars = config?.mergeVars || mergeHttpVars;
23-
const queryFn = config?.queryFn || createHttpQueryFn<T>(mergeVars);
8+
export class HttpQueryBuilder<
9+
TParam = unknown,
10+
TSearch = unknown,
11+
TBody = unknown,
12+
THeader = unknown,
13+
TMeta = unknown,
14+
TData = unknown,
15+
TError = RequestError,
16+
TKey extends [HttpBuilderVars] = [HttpBuilderVars<TParam, TSearch, TBody, THeader, TMeta>],
17+
> extends QueryBuilder<HttpBuilderVars<TParam, TSearch, TBody, THeader, TMeta>, TData, TError, TKey> {
18+
protected declare _vars: HttpBuilderVars<TParam, TSearch, TBody, THeader, TMeta>;
19+
20+
constructor(
21+
config?: WithOptional<
22+
QueryBuilderConfig<HttpBuilderVars<TParam, TSearch, TBody, THeader, TMeta>, TData, TError, TKey>,
23+
'queryFn'
24+
>,
25+
) {
26+
const mergeVars = config?.mergeVars || createHttpMergeVarsFn();
27+
const queryFn = config?.queryFn || createHttpQueryFn(mergeVars);
2428
const queryKeyHashFn = config?.queryKeyHashFn || createHttpQueryHashFn();
2529
super({ mergeVars, queryFn, queryKeyHashFn, ...config });
2630
}
2731

28-
withBody<TBody>(body?: TBody): HttpQueryBuilder<AppendVarsType<T, { body: TBody }>> {
32+
withBody<TBody$>(body?: TBody$): HttpQueryBuilder<TParam, TSearch, TBody$, THeader, TMeta, TData, TError, TKey> {
2933
if (!body) return this as any;
3034
return this.withVars({ body }) as any;
3135
}
3236

33-
withHeaders<THeaders extends HttpBaseHeaders>(
34-
headers?: THeaders,
35-
): HttpQueryBuilder<AppendVarsType<T, { headers: THeaders }>> {
37+
withHeaders<THeaders$ extends HttpBaseHeaders>(
38+
headers?: THeaders$,
39+
): HttpQueryBuilder<TParam, TSearch, TBody, THeaders$, TMeta, TData, TError, TKey> {
3640
if (!headers) return this as any;
3741
return this.withVars({ headers }) as any;
3842
}
3943

40-
withParams<TParams extends HttpBaseParams>(
41-
params?: TParams,
42-
): HttpQueryBuilder<AppendVarsType<T, { params: TParams }>> {
44+
withParams<TParams$ extends HttpBaseParams>(
45+
params?: TParams$,
46+
): HttpQueryBuilder<TParams$, TSearch, TBody, THeader, TMeta, TData, TError, TKey> {
4347
if (!params) return this as any;
4448
return this.withVars({ params }) as any;
4549
}
4650

47-
withSearch<TSearch extends HttpBaseSearch>(
48-
search?: TSearch,
49-
): HttpQueryBuilder<AppendVarsType<T, { search: TSearch }>> {
51+
withSearch<TSearch$ extends HttpBaseSearch>(
52+
search?: TSearch$,
53+
): HttpQueryBuilder<TParam, TSearch$, TBody, THeader, TMeta, TData, TError, TKey> {
5054
if (!search) return this as any;
5155
return this.withVars({ search }) as any;
5256
}
5357

54-
withMeta<TMeta>(meta?: TMeta): HttpQueryBuilder<AppendVarsType<T, { meta: TMeta }>> {
58+
withMeta<TMeta$>(meta?: TMeta$): HttpQueryBuilder<TParam, TSearch, TBody, THeader, TMeta$, TData, TError, TKey> {
5559
if (!meta) return this as any;
5660
return this.withVars({ meta }) as any;
5761
}
5862

59-
withPath<const TPath extends string>(
60-
path: TPath,
61-
): ExtractPathParams<TPath> extends void
63+
withPath<const TPath$ extends string>(
64+
path: TPath$,
65+
): ExtractPathParams<TPath$> extends void
6266
? this
63-
: HttpQueryBuilder<AppendVarsType<T, { params: ExtractPathParams<TPath> }>> {
67+
: HttpQueryBuilder<ExtractPathParams<TPath$>, TSearch, TBody, THeader, TMeta, TData, TError, TKey> {
6468
return this.withVars({ path }) as any;
6569
}
6670

@@ -72,21 +76,6 @@ export class HttpQueryBuilder<T extends HttpBuilderTypeTemplate = HttpBuilderTyp
7276
return this.withVars({ method }) as any;
7377
}
7478

75-
declare withData: <TData>() => HttpQueryBuilder<SetDataType<T, TData>>;
76-
declare withError: <TError>() => HttpQueryBuilder<SetErrorType<T, TError>>;
77-
declare withVars: <TVars = T['vars'], const TReset extends boolean = false>(
78-
vars?: TVars,
79-
reset?: TReset,
80-
) => HttpQueryBuilder<AppendVarsType<T, Partial<TVars>, TReset>>;
81-
82-
declare withPreprocessor: <TVars = T['vars']>(
83-
preprocessor: PreprocessorFn<TVars, T['vars']>,
84-
) => HttpQueryBuilder<AppendVarsType<T, TVars, true, true>>;
85-
86-
declare withMiddleware: <TVars = T['vars'], TData = T['data'], TError = T['error']>(
87-
middleware: MiddlewareFn<TVars, TData, TError, T>,
88-
) => HttpQueryBuilder<SetAllTypes<T, TData, TError, TVars, true>>;
89-
90-
declare asMutationBuilder: () => HttpMutationBuilder<T>;
91-
protected override MutationBuilderConstructor: typeof MutationBuilder = HttpMutationBuilder as typeof MutationBuilder;
79+
declare withData: <TData$>() => HttpQueryBuilder<TParam, TSearch, TBody, THeader, TMeta, TData$, TError, TKey>;
80+
declare withError: <TError$>() => HttpQueryBuilder<TParam, TSearch, TBody, THeader, TMeta, TData, TError$, TKey>;
9281
}

0 commit comments

Comments
 (0)