@@ -9,14 +9,11 @@ import {
99 hashKey ,
1010 queryOptions ,
1111 useMutation ,
12- useQueries ,
1312 useQuery ,
14- type DefaultError ,
1513 type FetchQueryOptions ,
1614 type InvalidateQueryFilters ,
1715 type QueryClient ,
1816 type QueryKey ,
19- type UndefinedInitialDataOptions ,
2017 type UseMutationOptions ,
2118 type UseQueryOptions ,
2219 type UseQueryResult ,
@@ -29,17 +26,12 @@ import { invariant } from '~/util/invariant'
2926import type { ApiResult } from './__generated__/Api'
3027import { processServerError , type ApiError } from './errors'
3128import { navToLogin } from './nav-to-login'
32- import { type ResultsPage } from './util'
3329
3430/* eslint-disable @typescript-eslint/no-explicit-any */
35- export type Params < F > = F extends ( p : infer P ) => any ? P : never
36- export type Result < F > = F extends ( p : any ) => Promise < ApiResult < infer R > > ? R : never
37- export type ResultItem < F > =
38- Result < F > extends { items : ( infer R ) [ ] }
39- ? R extends Record < string , unknown >
40- ? R
41- : never
42- : never
31+ type Params < F > = F extends ( p : infer P ) => any ? P : never
32+ type Result < F > = F extends ( p : any ) => Promise < ApiResult < infer R > > ? R : never
33+
34+ export type ResultsPage < TItem > = { items : TItem [ ] ; nextPage ?: string }
4335
4436type ApiClient = Record < string , ( ...args : any ) => Promise < ApiResult < any > > >
4537/* eslint-enable @typescript-eslint/no-explicit-any */
@@ -92,17 +84,17 @@ Error message: ${error.message.replace(/\n/g, '\n' + ' '.repeat('Error message:
9284 * `queryKey` and `queryFn` are always constructed by our helper hooks, so we
9385 * only allow the rest of the options.
9486 */
95- type UseQueryOtherOptions < T , E = DefaultError > = Omit <
96- UndefinedInitialDataOptions < T , E > ,
97- 'queryKey' | 'queryFn'
87+ type UseQueryOtherOptions < T > = Omit <
88+ UseQueryOptions < T , ApiError > ,
89+ 'queryKey' | 'queryFn' | 'initialData'
9890>
9991
10092/**
10193 * `queryKey` and `queryFn` are always constructed by our helper hooks, so we
10294 * only allow the rest of the options.
10395 */
104- type FetchQueryOtherOptions < T , E = DefaultError > = Omit <
105- FetchQueryOptions < T , E > ,
96+ type FetchQueryOtherOptions < T > = Omit <
97+ FetchQueryOptions < T , ApiError > ,
10698 'queryKey' | 'queryFn'
10799>
108100
@@ -111,7 +103,7 @@ export const getApiQueryOptions =
111103 < M extends string & keyof A > (
112104 method : M ,
113105 params : Params < A [ M ] > ,
114- options : UseQueryOtherOptions < Result < A [ M ] > , ApiError > = { }
106+ options : UseQueryOtherOptions < Result < A [ M ] > > = { }
115107 ) =>
116108 queryOptions ( {
117109 queryKey : [ method , params ] ,
@@ -163,7 +155,7 @@ export const getListQueryOptionsFn =
163155 > (
164156 method : M ,
165157 params : Params < A [ M ] > ,
166- options : UseQueryOtherOptions < Result < A [ M ] > , ApiError > = { }
158+ options : UseQueryOtherOptions < Result < A [ M ] > > = { }
167159 ) : PaginatedQuery < Result < A [ M ] > > => {
168160 // We pull limit out of the query params rather than passing it in some
169161 // other way so that there is exactly one way of specifying it. If we had
@@ -190,7 +182,7 @@ export const getUseApiQuery =
190182 < M extends string & keyof A > (
191183 method : M ,
192184 params : Params < A [ M ] > ,
193- options : UseQueryOtherOptions < Result < A [ M ] > , ApiError > = { }
185+ options : UseQueryOtherOptions < Result < A [ M ] > > = { }
194186 ) =>
195187 useQuery ( getApiQueryOptions ( api ) ( method , params , options ) )
196188
@@ -199,7 +191,7 @@ export const getUsePrefetchedApiQuery =
199191 < M extends string & keyof A > (
200192 method : M ,
201193 params : Params < A [ M ] > ,
202- options : UseQueryOtherOptions < Result < A [ M ] > , ApiError > = { }
194+ options : UseQueryOtherOptions < Result < A [ M ] > > = { }
203195 ) => {
204196 const qOptions = getApiQueryOptions ( api ) ( method , params , options )
205197 return ensurePrefetched ( useQuery ( qOptions ) , qOptions . queryKey )
@@ -232,9 +224,6 @@ export function ensurePrefetched<TData, TError>(
232224 return result as SetNonNullable < typeof result , 'data' >
233225}
234226
235- export const usePrefetchedQuery = < TData > ( options : UseQueryOptions < TData , ApiError > ) =>
236- ensurePrefetched ( useQuery ( options ) , options . queryKey )
237-
238227const ERRORS_ALLOWED = 'errors-allowed'
239228
240229/** Result that includes both success and error so it can be cached by RQ */
@@ -289,35 +278,6 @@ export const getUseApiMutation =
289278 ...options ,
290279 } )
291280
292- /**
293- * Our version of `useQueries`, but with the key difference that all queries in
294- * a given call are using the same API method, and therefore all have the same
295- * request and response (`Params` and `Result`) types. Otherwise the types would
296- * be (perhaps literally) impossible.
297- */
298- export const getUseApiQueries =
299- < A extends ApiClient > ( api : A ) =>
300- < M extends string & keyof A > (
301- method : M ,
302- paramsArray : Params < A [ M ] > [ ] ,
303- options : UseQueryOtherOptions < Result < A [ M ] > , ApiError > = { }
304- ) => {
305- return useQueries ( {
306- queries : paramsArray . map (
307- ( params ) =>
308- ( {
309- queryKey : [ method , params ] ,
310- queryFn : ( { signal } ) =>
311- api [ method ] ( params , { signal } ) . then ( handleResult ( method ) ) ,
312- throwOnError : ( err : ApiError ) => err . statusCode === 404 ,
313- ...options ,
314- // Add params to the result for reassembly after the queries are returned
315- select : ( data ) => ( { ...data , params } ) ,
316- } ) satisfies UseQueryOptions < Result < A [ M ] > & { params : Params < A [ M ] > } , ApiError >
317- ) ,
318- } )
319- }
320-
321281export const wrapQueryClient = < A extends ApiClient > ( api : A , queryClient : QueryClient ) => ( {
322282 /**
323283 * Note that we only take a single argument, `method`, rather than allowing
@@ -340,7 +300,7 @@ export const wrapQueryClient = <A extends ApiClient>(api: A, queryClient: QueryC
340300 fetchQuery : < M extends string & keyof A > (
341301 method : M ,
342302 params : Params < A [ M ] > ,
343- options : FetchQueryOtherOptions < Result < A [ M ] > , ApiError > = { }
303+ options : FetchQueryOtherOptions < Result < A [ M ] > > = { }
344304 ) =>
345305 queryClient . fetchQuery ( {
346306 queryKey : [ method , params ] ,
@@ -350,7 +310,7 @@ export const wrapQueryClient = <A extends ApiClient>(api: A, queryClient: QueryC
350310 prefetchQuery : < M extends string & keyof A > (
351311 method : M ,
352312 params : Params < A [ M ] > ,
353- options : FetchQueryOtherOptions < Result < A [ M ] > , ApiError > = { }
313+ options : FetchQueryOtherOptions < Result < A [ M ] > > = { }
354314 ) =>
355315 queryClient . prefetchQuery ( {
356316 queryKey : [ method , params ] ,
0 commit comments