From 89bdc949aa26e9d5d4f1d5558d59b5000dcbf021 Mon Sep 17 00:00:00 2001 From: Mikhail Golbakh Date: Wed, 7 Aug 2024 23:32:18 +0300 Subject: [PATCH] fix!: make page param patch for request BREAKING CHANGE: `next` and `prev` are `Partial | undefined` instead of `Partial | undefined`, DataSource no longer supports in `ActualParams` type Release-As: 0.2.1 --- src/core/types/DataSource.ts | 12 ++---- src/react-query/impl/infinite/types.ts | 6 +-- src/react-query/impl/infinite/utils.ts | 53 +++++++++++--------------- 3 files changed, 30 insertions(+), 41 deletions(-) diff --git a/src/core/types/DataSource.ts b/src/core/types/DataSource.ts index 51383e2..f08ae43 100644 --- a/src/core/types/DataSource.ts +++ b/src/core/types/DataSource.ts @@ -24,7 +24,7 @@ export interface DataSource< fetchContext: TFetchContext, request: TRequest, ) => Promise | TResponse; - tags?: (params: ActualParams) => DataSourceTag[]; + tags?: (params: ActualParams) => DataSourceTag[]; transformParams?: (params: TParams) => TRequest; transformResponse?: (response: TResponse) => TData; @@ -65,7 +65,7 @@ export type DataSourceParams = infer _TState, infer _TFetchContext > - ? ActualParams + ? ActualParams : never; export type DataSourceRequest = @@ -173,12 +173,8 @@ export type DataSourceFetchContext = ? TFetchContext : never; -export type ActualParams = - | (unknown extends TParams - ? TRequest - : undefined extends TDataSource['transformParams'] - ? TRequest - : TParams) +export type ActualParams = + | (unknown extends TParams ? TRequest : TParams) | typeof idle; export type ActualData = unknown extends TData ? TResponse : TData; diff --git a/src/react-query/impl/infinite/types.ts b/src/react-query/impl/infinite/types.ts index 94ca2a0..bd8f7ec 100644 --- a/src/react-query/impl/infinite/types.ts +++ b/src/react-query/impl/infinite/types.ts @@ -17,11 +17,11 @@ export type InfiniteQueryDataSource TError, InfiniteQueryObserverOptions, TResponse>, ResultWrapper, TError>>, - QueryFunctionContext + QueryFunctionContext | undefined> > & { type: 'infinite'; - next: (lastPage: TResponse, allPages: TResponse[]) => Partial | undefined; - prev?: (firstPage: TResponse, allPages: TResponse[]) => Partial | undefined; + next: (lastPage: TResponse, allPages: TResponse[]) => Partial | undefined; + prev?: (firstPage: TResponse, allPages: TResponse[]) => Partial | undefined; }; // eslint-disable-next-line @typescript-eslint/no-explicit-any diff --git a/src/react-query/impl/infinite/utils.ts b/src/react-query/impl/infinite/utils.ts index 6089f36..c7b8e91 100644 --- a/src/react-query/impl/infinite/utils.ts +++ b/src/react-query/impl/infinite/utils.ts @@ -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'; @@ -40,31 +40,24 @@ export const composeOptions = ( enabled: params !== idle, queryKey: composeFullKey(dataSource, params), queryFn: ( - fetchContext: QueryFunctionContext>, + fetchContext: QueryFunctionContext< + DataSourceKey, + Partial> | 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, }; };