Skip to content

Commit 653b572

Browse files
authored
cleanup: delete unused API helper things (#2577)
cut a few lines by cleaning up types and shuffling things
1 parent 175d75b commit 653b572

File tree

4 files changed

+22
-68
lines changed

4 files changed

+22
-68
lines changed

app/api/client.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,21 @@
55
*
66
* Copyright Oxide Computer Company
77
*/
8-
import { QueryClient } from '@tanstack/react-query'
8+
import { QueryClient, useQuery, type UseQueryOptions } from '@tanstack/react-query'
99

1010
import { Api } from './__generated__/Api'
11+
import { type ApiError } from './errors'
1112
import {
13+
ensurePrefetched,
1214
getApiQueryOptions,
1315
getListQueryOptionsFn,
1416
getUseApiMutation,
15-
getUseApiQueries,
1617
getUseApiQuery,
1718
getUseApiQueryErrorsAllowed,
1819
getUsePrefetchedApiQuery,
1920
wrapQueryClient,
2021
} from './hooks'
2122

22-
export {
23-
ensurePrefetched,
24-
usePrefetchedQuery,
25-
PAGE_SIZE,
26-
type PaginatedQuery,
27-
} from './hooks'
28-
2923
export const api = new Api({
3024
// unit tests run in Node, whose fetch implementation requires a full URL
3125
host: process.env.NODE_ENV === 'test' ? 'http://testhost' : '',
@@ -42,7 +36,6 @@ export const apiq = getApiQueryOptions(api.methods)
4236
*/
4337
export const getListQFn = getListQueryOptionsFn(api.methods)
4438
export const useApiQuery = getUseApiQuery(api.methods)
45-
export const useApiQueries = getUseApiQueries(api.methods)
4639
/**
4740
* Same as `useApiQuery`, except we use `invariant(data)` to ensure the data is
4841
* already there in the cache at request time, which means it has been
@@ -53,6 +46,9 @@ export const usePrefetchedApiQuery = getUsePrefetchedApiQuery(api.methods)
5346
export const useApiQueryErrorsAllowed = getUseApiQueryErrorsAllowed(api.methods)
5447
export const useApiMutation = getUseApiMutation(api.methods)
5548

49+
export const usePrefetchedQuery = <TData>(options: UseQueryOptions<TData, ApiError>) =>
50+
ensurePrefetched(useQuery(options), options.queryKey)
51+
5652
// Needs to be defined here instead of in app so we can use it to define
5753
// `apiQueryClient`, which provides API-typed versions of QueryClient methods
5854
export const queryClient = new QueryClient({

app/api/hooks.ts

Lines changed: 15 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -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'
2926
import type { ApiResult } from './__generated__/Api'
3027
import { processServerError, type ApiError } from './errors'
3128
import { 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

4436
type 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-
238227
const 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-
321281
export 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],

app/api/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ export type { ApiTypes }
2121

2222
export * as PathParams from './path-params'
2323

24-
export type { Params, Result, ResultItem } from './hooks'
24+
export { ensurePrefetched, PAGE_SIZE, type PaginatedQuery, type ResultsPage } from './hooks'
2525
export type { ApiError } from './errors'
2626
export { navToLogin } from './nav-to-login'

app/api/util.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ import type {
2323
VpcFirewallRuleUpdate,
2424
} from './__generated__/Api'
2525

26-
export type ResultsPage<TItem> = { items: TItem[]; nextPage?: string }
27-
2826
// API limits encoded in https://github.com/oxidecomputer/omicron/blob/main/nexus/src/app/mod.rs
2927

3028
export const MAX_NICS_PER_INSTANCE = 8

0 commit comments

Comments
 (0)