Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!: make page param patch for request #10

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions src/core/types/DataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface DataSource<
fetchContext: TFetchContext,
request: TRequest,
) => Promise<TResponse> | TResponse;
tags?: (params: ActualParams<this, TParams, TRequest>) => DataSourceTag[];
tags?: (params: ActualParams<TParams, TRequest>) => DataSourceTag[];

transformParams?: (params: TParams) => TRequest;
transformResponse?: (response: TResponse) => TData;
Expand Down Expand Up @@ -65,7 +65,7 @@ export type DataSourceParams<TDataSource> =
infer _TState,
infer _TFetchContext
>
? ActualParams<TDataSource, TParams, TRequest>
? ActualParams<TParams, TRequest>
: never;

export type DataSourceRequest<TDataSource> =
Expand Down Expand Up @@ -173,12 +173,8 @@ export type DataSourceFetchContext<TDataSource> =
? TFetchContext
: never;

export type ActualParams<TDataSource extends AnyDataSource, TParams, TRequest> =
| (unknown extends TParams
? TRequest
: undefined extends TDataSource['transformParams']
? TRequest
: TParams)
export type ActualParams<TParams, TRequest> =
| (unknown extends TParams ? TRequest : TParams)
| typeof idle;

export type ActualData<TData, TResponse> = unknown extends TData ? TResponse : TData;
6 changes: 3 additions & 3 deletions src/react-query/impl/infinite/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ export type InfiniteQueryDataSource<TParams, TRequest, TResponse, TData, TError>
TError,
InfiniteQueryObserverOptions<TResponse, TError, ActualData<TData, TResponse>, TResponse>,
ResultWrapper<InfiniteQueryObserverResult<ActualData<TData, TResponse>, TError>>,
QueryFunctionContext<DataSourceKey, TParams>
QueryFunctionContext<DataSourceKey, Partial<TRequest> | undefined>
> & {
type: 'infinite';
next: (lastPage: TResponse, allPages: TResponse[]) => Partial<TParams> | undefined;
prev?: (firstPage: TResponse, allPages: TResponse[]) => Partial<TParams> | undefined;
next: (lastPage: TResponse, allPages: TResponse[]) => Partial<TRequest> | undefined;
prev?: (firstPage: TResponse, allPages: TResponse[]) => Partial<TRequest> | undefined;
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
53 changes: 23 additions & 30 deletions src/react-query/impl/infinite/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import type {
QueryFunctionContext,
} from '@tanstack/react-query';

import {
type DataSourceContext,
type DataSourceData,
type DataSourceError,
type DataSourceKey,
type DataSourceOptions,
type DataSourceParams,
type DataSourceResponse,
type DataSourceState,
composeFullKey,
idle,
import {composeFullKey, idle} from '../../../core';
import type {
DataSourceContext,
DataSourceData,
DataSourceError,
DataSourceKey,
DataSourceOptions,
DataSourceParams,
DataSourceRequest,
DataSourceResponse,
DataSourceState,
} from '../../../core';
import {normalizeStatus} from '../../utils/normalizeStatus';

Expand All @@ -40,31 +40,24 @@ export const composeOptions = <TDataSource extends AnyInfiniteQueryDataSource>(
enabled: params !== idle,
queryKey: composeFullKey(dataSource, params),
queryFn: (
fetchContext: QueryFunctionContext<DataSourceKey, DataSourceParams<TDataSource>>,
fetchContext: QueryFunctionContext<
DataSourceKey,
Partial<DataSourceRequest<TDataSource>> | undefined
>,
) => {
const actualParams = fetchContext.pageParam ?? params;
const actualParams = transformParams ? transformParams(params) : params;
const request =
typeof actualParams === 'object'
? {...actualParams, ...fetchContext.pageParam}
: actualParams;

return dataSource.fetch(
context,
fetchContext,
transformParams ? transformParams(actualParams) : actualParams,
);
return dataSource.fetch(context, fetchContext, request);
},
select: transformResponse
? (data) => ({...data, pages: data.pages.map(transformResponse)})
: undefined,
getNextPageParam: (lastPage, allPages) => {
const nextParamsPatch = next(lastPage, allPages);

return nextParamsPatch ? {...params, ...nextParamsPatch} : undefined;
},
getPreviousPageParam: prev
? (firstPage, allPages) => {
const prevParamsPatch = prev(firstPage, allPages);

return prevParamsPatch ? {...params, ...prevParamsPatch} : undefined;
}
: undefined,
getNextPageParam: next,
getPreviousPageParam: prev,
...options,
};
};
Expand Down