Skip to content

Commit 01e2e9a

Browse files
feat: remove the transformError (#36)
1 parent 85ac92c commit 01e2e9a

File tree

8 files changed

+92
-194
lines changed

8 files changed

+92
-194
lines changed

src/core/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@ export type {
99
DataSourceResponse,
1010
DataSourceData,
1111
DataSourceError,
12-
DataSourceErrorResponse,
1312
DataSourceOptions,
1413
DataSourceState,
1514
DataSourceFetchContext,
1615
ActualParams,
1716
ActualData,
18-
ActualResponse,
1917
} from './types/DataSource';
2018
export type {DataManager} from './types/DataManager';
2119
export type {DataLoaderStatus} from './types/DataLoaderStatus';

src/core/types/DataSource.ts

Lines changed: 7 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type {idle} from '../constants';
33
export type DataSourceKey = ReadonlyArray<unknown>;
44
export type DataSourceTag = string;
55

6+
declare const errorHintSymbol: unique symbol;
67
declare const stateHintSymbol: unique symbol;
78

89
export interface DataSource<
@@ -12,7 +13,6 @@ export interface DataSource<
1213
TResponse,
1314
TData,
1415
TError,
15-
TErrorResponse,
1616
TOptions,
1717
TState,
1818
TFetchContext,
@@ -27,21 +27,15 @@ export interface DataSource<
2727
tags?: (params: ActualParams<NoInfer<TParams>, NoInfer<TRequest>>) => DataSourceTag[];
2828

2929
transformParams?: (params: TParams) => NoInfer<TRequest>;
30-
transformResponse?: (
31-
response: ActualResponse<NoInfer<TResponse>, NoInfer<TErrorResponse>>,
32-
) => TData;
33-
34-
/**
35-
* When set, the `fetch` errors will be transformed into data without changing the state to error.
36-
*/
37-
transformError?: (error: TError) => TErrorResponse;
30+
transformResponse?: (response: TResponse) => TData;
3831

3932
options?: Partial<TOptions>;
4033
[stateHintSymbol]?: TState;
34+
[errorHintSymbol]?: TError;
4135
}
4236

4337
// eslint-disable-next-line @typescript-eslint/no-explicit-any
44-
export type AnyDataSource = DataSource<any, any, any, any, any, any, any, any, any, any>;
38+
export type AnyDataSource = DataSource<any, any, any, any, any, any, any, any, any>;
4539

4640
export type DataSourceContext<TDataSource> =
4741
TDataSource extends DataSource<
@@ -51,7 +45,6 @@ export type DataSourceContext<TDataSource> =
5145
infer _TResponse,
5246
infer _TData,
5347
infer _TError,
54-
infer _TErrorResponse,
5548
infer _TOptions,
5649
infer _TState,
5750
infer _TFetchContext
@@ -67,7 +60,6 @@ export type DataSourceParams<TDataSource> =
6760
infer _TResponse,
6861
infer _TData,
6962
infer _TError,
70-
infer _TErrorResponse,
7163
infer _TOptions,
7264
infer _TState,
7365
infer _TFetchContext
@@ -83,7 +75,6 @@ export type DataSourceRequest<TDataSource> =
8375
infer _TResponse,
8476
infer _TData,
8577
infer _TError,
86-
infer _TErrorResponse,
8778
infer _TOptions,
8879
infer _TState,
8980
infer _TFetchContext
@@ -99,12 +90,11 @@ export type DataSourceResponse<TDataSource> =
9990
infer TResponse,
10091
infer _TData,
10192
infer _TError,
102-
infer TErrorResponse,
10393
infer _TOptions,
10494
infer _TState,
10595
infer _TFetchContext
10696
>
107-
? ActualResponse<TResponse, TErrorResponse>
97+
? TResponse
10898
: never;
10999

110100
export type DataSourceData<TDataSource> =
@@ -115,12 +105,11 @@ export type DataSourceData<TDataSource> =
115105
infer TResponse,
116106
infer TData,
117107
infer _TError,
118-
infer TErrorResponse,
119108
infer _TOptions,
120109
infer _TState,
121110
infer _TFetchContext
122111
>
123-
? ActualData<TResponse, TData, TErrorResponse>
112+
? ActualData<TData, TResponse>
124113
: never;
125114

126115
export type DataSourceError<TDataSource> =
@@ -131,30 +120,13 @@ export type DataSourceError<TDataSource> =
131120
infer _TResponse,
132121
infer _TData,
133122
infer TError,
134-
infer _TErrorResponse,
135123
infer _TOptions,
136124
infer _TState,
137125
infer _TFetchContext
138126
>
139127
? TError
140128
: never;
141129

142-
export type DataSourceErrorResponse<TDataSource> =
143-
TDataSource extends DataSource<
144-
infer _TContenxt,
145-
infer _TParams,
146-
infer _TRequest,
147-
infer _TResponse,
148-
infer _TData,
149-
infer _TError,
150-
infer TErrorResponse,
151-
infer _TOptions,
152-
infer _TState,
153-
infer _TFetchContext
154-
>
155-
? TErrorResponse
156-
: never;
157-
158130
export type DataSourceOptions<TDataSource> =
159131
TDataSource extends DataSource<
160132
infer _TContenxt,
@@ -163,7 +135,6 @@ export type DataSourceOptions<TDataSource> =
163135
infer _TResponse,
164136
infer _TData,
165137
infer _TError,
166-
infer _TErrorResponse,
167138
infer TOptions,
168139
infer _TState,
169140
infer _TFetchContext
@@ -179,7 +150,6 @@ export type DataSourceState<TDataSource> =
179150
infer _TResponse,
180151
infer _TData,
181152
infer _TError,
182-
infer _TErrorResponse,
183153
infer _TOptions,
184154
infer TState,
185155
infer _TFetchContext
@@ -195,7 +165,6 @@ export type DataSourceFetchContext<TDataSource> =
195165
infer _TResponse,
196166
infer _TData,
197167
infer _TError,
198-
infer _TErrorResponse,
199168
infer _TOptions,
200169
infer _TState,
201170
infer TFetchContext
@@ -207,10 +176,4 @@ export type ActualParams<TParams, TRequest> =
207176
| (unknown extends TParams ? TRequest : TParams)
208177
| typeof idle;
209178

210-
export type ActualResponse<TResponse, TErrorResponse> = unknown extends TErrorResponse
211-
? TResponse
212-
: TResponse | TErrorResponse;
213-
214-
export type ActualData<TResponse, TData, TErrorResponse> = unknown extends TData
215-
? ActualResponse<TResponse, TErrorResponse>
216-
: TData;
179+
export type ActualData<TData, TResponse> = unknown extends TData ? TResponse : TData;
Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
11
import type {InfiniteQueryDataSource} from './types';
22

3-
export const makeInfiniteQueryDataSource = <
4-
TParams,
5-
TRequest,
6-
TResponse,
7-
TData,
8-
TError,
9-
TErrorResponse,
10-
>(
11-
config: Omit<
12-
InfiniteQueryDataSource<TParams, TRequest, TResponse, TData, TError, TErrorResponse>,
13-
'type'
14-
>,
15-
): InfiniteQueryDataSource<TParams, TRequest, TResponse, TData, TError, TErrorResponse> => ({
3+
export const makeInfiniteQueryDataSource = <TParams, TRequest, TResponse, TData, TError>(
4+
config: Omit<InfiniteQueryDataSource<TParams, TRequest, TResponse, TData, TError>, 'type'>,
5+
): InfiniteQueryDataSource<TParams, TRequest, TResponse, TData, TError> => ({
166
...config,
177
type: 'infinite',
188
});

src/react-query/impl/infinite/types.ts

Lines changed: 41 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,7 @@ import type {
88
} from '@tanstack/react-query';
99
import type {Overwrite} from 'utility-types';
1010

11-
import type {
12-
ActualData,
13-
ActualResponse,
14-
DataLoaderStatus,
15-
DataSource,
16-
DataSourceKey,
17-
} from '../../../core';
11+
import type {ActualData, DataLoaderStatus, DataSource, DataSourceKey} from '../../../core';
1812
import type {QueryDataSourceContext} from '../../types/base';
1913
import type {QueryDataAdditionalOptions} from '../../types/options';
2014

@@ -35,70 +29,61 @@ export type InfiniteQueryObserverExtendedOptions<
3529
>
3630
>;
3731

38-
export type InfiniteQueryDataSource<TParams, TRequest, TResponse, TData, TError, TErrorResponse> =
39-
DataSource<
40-
QueryDataSourceContext,
41-
TParams,
42-
TRequest,
43-
TResponse,
44-
TData,
45-
TError,
46-
TErrorResponse,
47-
InfiniteQueryObserverExtendedOptions<
48-
ActualResponse<NoInfer<TResponse>, NoInfer<TErrorResponse>>,
49-
NoInfer<TError>,
50-
InfiniteData<
51-
ActualData<NoInfer<TResponse>, NoInfer<TData>, NoInfer<TErrorResponse>>,
52-
Partial<NoInfer<TRequest>>
53-
>,
54-
ActualResponse<NoInfer<TResponse>, NoInfer<TErrorResponse>>,
55-
DataSourceKey,
56-
Partial<NoInfer<TRequest>>
57-
>,
58-
ResultWrapper<
59-
InfiniteQueryObserverResult<
60-
InfiniteData<
61-
ActualData<NoInfer<TResponse>, NoInfer<TData>, NoInfer<TErrorResponse>>,
62-
Partial<NoInfer<TRequest>>
63-
>,
64-
NoInfer<TError>
65-
>,
66-
NoInfer<TRequest>,
67-
NoInfer<TResponse>,
68-
NoInfer<TData>,
69-
NoInfer<TError>,
70-
NoInfer<TErrorResponse>
32+
export type InfiniteQueryDataSource<TParams, TRequest, TResponse, TData, TError> = DataSource<
33+
QueryDataSourceContext,
34+
TParams,
35+
TRequest,
36+
TResponse,
37+
TData,
38+
TError,
39+
InfiniteQueryObserverExtendedOptions<
40+
NoInfer<TResponse>,
41+
NoInfer<TError>,
42+
InfiniteData<ActualData<NoInfer<TData>, NoInfer<TResponse>>, Partial<TRequest>>,
43+
NoInfer<TResponse>,
44+
DataSourceKey,
45+
Partial<NoInfer<TRequest>>
46+
>,
47+
ResultWrapper<
48+
InfiniteQueryObserverResult<
49+
InfiniteData<ActualData<NoInfer<TData>, NoInfer<TResponse>>, Partial<TRequest>>,
50+
NoInfer<TError>
7151
>,
72-
QueryFunctionContext<DataSourceKey, Partial<NoInfer<TRequest>>>
73-
> & {
74-
type: 'infinite';
75-
next: (
76-
lastPage: ActualResponse<NoInfer<TResponse>, NoInfer<TErrorResponse>>,
77-
allPages: Array<ActualResponse<NoInfer<TResponse>, NoInfer<TErrorResponse>>>,
78-
) => Partial<NoInfer<TRequest>> | undefined | null;
79-
prev?: (
80-
firstPage: ActualResponse<NoInfer<TResponse>, NoInfer<TErrorResponse>>,
81-
allPages: Array<ActualResponse<NoInfer<TResponse>, NoInfer<TErrorResponse>>>,
82-
) => Partial<NoInfer<TRequest>> | undefined | null;
83-
};
52+
NoInfer<TRequest>,
53+
NoInfer<TResponse>,
54+
NoInfer<TData>,
55+
NoInfer<TError>
56+
>,
57+
QueryFunctionContext<DataSourceKey, Partial<NoInfer<TRequest>>>
58+
> & {
59+
type: 'infinite';
60+
next: (
61+
lastPage: NoInfer<TResponse>,
62+
allPages: NoInfer<TResponse>[],
63+
) => Partial<NoInfer<TRequest>> | null | undefined;
64+
prev?: (
65+
firstPage: NoInfer<TResponse>,
66+
allPages: NoInfer<TResponse>[],
67+
) => Partial<NoInfer<TRequest>> | null | undefined;
68+
};
8469

8570
// eslint-disable-next-line @typescript-eslint/no-explicit-any
86-
export type AnyInfiniteQueryDataSource = InfiniteQueryDataSource<any, any, any, any, any, any>;
71+
export type AnyInfiniteQueryDataSource = InfiniteQueryDataSource<any, any, any, any, any>;
8772

8873
// It is used instead of `Partial<DataSourceRequest<TDataSource>>` because TS can't calculate type
8974
// eslint-disable-next-line @typescript-eslint/no-explicit-any
9075
export type AnyPageParam = Partial<any>;
9176

92-
type ResultWrapper<TResult, TRequest, TResponse, TData, TError, TErrorResponse> =
77+
type ResultWrapper<TResult, TRequest, TResponse, TData, TError> =
9378
TResult extends InfiniteQueryObserverResult<
94-
InfiniteData<ActualData<TResponse, TData, TErrorResponse>, Partial<TRequest>>,
79+
InfiniteData<ActualData<TData, TResponse>, Partial<TRequest>>,
9580
TError
9681
>
9782
? Overwrite<
9883
TResult,
9984
{
10085
status: DataLoaderStatus;
101-
data: Array<FlatArray<Array<ActualData<TResponse, TData, TErrorResponse>>, 1>>;
86+
data: Array<FlatArray<Array<ActualData<TData, TResponse>>, 1>>;
10287
}
10388
> & {
10489
originalStatus: TResult['status'];

src/react-query/impl/infinite/utils.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,17 @@ export const composeOptions = <TDataSource extends AnyInfiniteQueryDataSource>(
3636
DataSourceKey,
3737
AnyPageParam
3838
> => {
39-
const {transformParams, transformError, transformResponse, next, prev} = dataSource;
39+
const {transformParams, transformResponse, next, prev} = dataSource;
4040

4141
const queryFn = async (
4242
fetchContext: QueryFunctionContext<DataSourceKey, AnyPageParam>,
4343
): Promise<DataSourceResponse<TDataSource> | typeof undefinedSymbol | typeof nullSymbol> => {
4444
const request = transformParams ? transformParams(params) : params;
4545
const paginatedRequest = {...request, ...fetchContext.pageParam};
4646

47-
try {
48-
const response = await dataSource.fetch(context, fetchContext, paginatedRequest);
47+
const response = await dataSource.fetch(context, fetchContext, paginatedRequest);
4948

50-
return formatNullableValue(response);
51-
} catch (error) {
52-
if (!transformError) {
53-
throw error;
54-
}
55-
56-
return formatNullableValue(transformError(error));
57-
}
49+
return formatNullableValue(response);
5850
};
5951

6052
const selectPage = (
Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
11
import type {PlainQueryDataSource} from './types';
22

3-
export const makePlainQueryDataSource = <
4-
TParams,
5-
TRequest,
6-
TResponse,
7-
TData,
8-
TError,
9-
TErrorResponse,
10-
>(
11-
config: Omit<
12-
PlainQueryDataSource<TParams, TRequest, TResponse, TData, TError, TErrorResponse>,
13-
'type'
14-
>,
15-
): PlainQueryDataSource<TParams, TRequest, TResponse, TData, TError, TErrorResponse> => ({
3+
export const makePlainQueryDataSource = <TParams, TRequest, TResponse, TData, TError>(
4+
config: Omit<PlainQueryDataSource<TParams, TRequest, TResponse, TData, TError>, 'type'>,
5+
): PlainQueryDataSource<TParams, TRequest, TResponse, TData, TError> => ({
166
...config,
177
type: 'plain',
188
});

0 commit comments

Comments
 (0)