Skip to content

Commit

Permalink
fix!: make page param patch for request (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
DakEnviy authored Aug 7, 2024
1 parent db63dc7 commit e76f88e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 41 deletions.
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

0 comments on commit e76f88e

Please sign in to comment.