File tree Expand file tree Collapse file tree
definitions/helpers/prepare-query-context Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import { prepareQueryContext } from "." ;
2+
3+ describe ( "prepareQueryContext" , ( ) => {
4+ it ( "should keep signal lazy when merging it into meta" , ( ) => {
5+ const signal = new AbortController ( ) . signal ;
6+ const signalGetter = vi . fn ( ( ) => signal ) ;
7+ const context = {
8+ queryKey : [ "posts" , "list" ] ,
9+ get signal ( ) {
10+ return signalGetter ( ) ;
11+ } ,
12+ } as any ;
13+
14+ const meta = prepareQueryContext ( context , { foo : "bar" } ) ;
15+
16+ expect ( meta . foo ) . toBe ( "bar" ) ;
17+ expect ( meta . queryKey ) . toEqual ( [ "posts" , "list" ] ) ;
18+ expect ( signalGetter ) . not . toHaveBeenCalled ( ) ;
19+
20+ expect ( meta . signal ) . toBe ( signal ) ;
21+ expect ( signalGetter ) . toHaveBeenCalledTimes ( 1 ) ;
22+ } ) ;
23+ } ) ;
Original file line number Diff line number Diff line change @@ -4,13 +4,19 @@ type Context =
44 | QueryFunctionContext < QueryKey , any >
55 | QueryFunctionContext < QueryKey , never > ;
66
7+ type QueryContextMeta < TMeta extends Record < string , unknown > | undefined > = (
8+ TMeta extends Record < string , unknown > ? TMeta : Record < string , never >
9+ ) &
10+ Pick < Context , "queryKey" | "signal" > ;
11+
712export const prepareQueryContext = (
813 context : Context ,
9- ) : Pick < Context , "queryKey" | "signal" > => {
10- const queryContext : Pick < Context , "queryKey" | "signal" > = {
14+ meta ?: Record < string , unknown > ,
15+ ) : QueryContextMeta < typeof meta > => {
16+ const queryContext = {
17+ ...( meta ?? { } ) ,
1118 queryKey : context . queryKey ,
12- signal : undefined as any ,
13- } ;
19+ } as QueryContextMeta < typeof meta > ;
1420
1521 Object . defineProperty ( queryContext , "signal" , {
1622 enumerable : true ,
Original file line number Diff line number Diff line change @@ -173,10 +173,7 @@ export const useCustom = <
173173 url,
174174 method,
175175 ...config ,
176- meta : {
177- ...combinedMeta ,
178- ...prepareQueryContext ( context as any ) ,
179- } ,
176+ meta : prepareQueryContext ( context as any , combinedMeta ) ,
180177 } ) ,
181178 ...queryOptions ,
182179 meta : {
Original file line number Diff line number Diff line change @@ -254,10 +254,7 @@ export const useInfiniteList = <
254254 ( context . pageParam as number ) ?? prefferedPagination . currentPage ,
255255 } ;
256256
257- const meta = {
258- ...combinedMeta ,
259- ...prepareQueryContext ( context ) ,
260- } ;
257+ const meta = prepareQueryContext ( context , combinedMeta ) ;
261258
262259 return getList < TQueryFnData > ( {
263260 resource : resource ?. name || "" ,
Original file line number Diff line number Diff line change @@ -254,10 +254,7 @@ export const useList = <
254254 } )
255255 . get ( ) ,
256256 queryFn : ( context ) => {
257- const meta = {
258- ...combinedMeta ,
259- ...prepareQueryContext ( context ) ,
260- } ;
257+ const meta = prepareQueryContext ( context , combinedMeta ) ;
261258 return getList < TQueryFnData > ( {
262259 resource : resource ?. name ?? "" ,
263260 pagination : prefferedPagination ,
Original file line number Diff line number Diff line change @@ -188,10 +188,7 @@ export const useMany = <
188188 } )
189189 . get ( ) ,
190190 queryFn : ( context ) => {
191- const meta = {
192- ...combinedMeta ,
193- ...prepareQueryContext ( context as any ) ,
194- } ;
191+ const meta = prepareQueryContext ( context as any , combinedMeta ) ;
195192
196193 if ( getMany ) {
197194 return getMany ( {
Original file line number Diff line number Diff line change @@ -185,10 +185,7 @@ export const useOne = <
185185 getOne < TQueryFnData > ( {
186186 resource : resource ?. name ?? "" ,
187187 id : id ! ,
188- meta : {
189- ...combinedMeta ,
190- ...prepareQueryContext ( context as any ) ,
191- } ,
188+ meta : prepareQueryContext ( context as any , combinedMeta ) ,
192189 } ) ,
193190 ...queryOptions ,
194191 enabled : isEnabled ,
You can’t perform that action at this time.
0 commit comments