Skip to content

Commit 439c7dd

Browse files
authored
refactor: redo API helpers (#2971)
* new apiq and apiqErrorsAllowed * getListQFn * useApiMutation takes method directly * fix hooks test * some example conversions * fix all the call sites * turns out we don't actually want the signal for regular queries * add comment about not passing the signal * absorb hooks file into app/api/client.ts * apiq -> q, api.methods -> api * fix outdated comment about useErrorBoundary, which is now throwOnError * fix safety test * better handleResult * apiqErrorsAllowed -> qErrorsAllowed, comments
1 parent 601cdfa commit 439c7dd

File tree

120 files changed

+913
-828
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

120 files changed

+913
-828
lines changed
Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { describe, expect, it, vi } from 'vitest'
1111

1212
import { project } from '@oxide/api-mocks'
1313

14-
import { apiq, useApiMutation } from '..'
14+
import { api, q, useApiMutation } from '..'
1515
import type { DiskCreate } from '../__generated__/Api'
1616
import { overrideOnce } from '../../../test/unit/server'
1717

@@ -35,16 +35,17 @@ export function Wrapper({ children }: { children: React.ReactNode }) {
3535

3636
const config = { wrapper: Wrapper }
3737

38-
const renderProjectList = () => renderHook(() => useQuery(apiq('projectList', {})), config)
38+
const renderProjectList = () => renderHook(() => useQuery(q(api.projectList, {})), config)
3939

4040
// 503 is a special key in the MSW server that returns a 503
4141
const renderGetProject503 = () =>
4242
renderHook(
43-
() => useQuery(apiq('projectView', { path: { project: 'project-error-503' } })),
43+
() => useQuery(q(api.projectView, { path: { project: 'project-error-503' } })),
4444
config
4545
)
4646

47-
const renderCreateProject = () => renderHook(() => useApiMutation('projectCreate'), config)
47+
const renderCreateProject = () =>
48+
renderHook(() => useApiMutation(api.projectCreate), config)
4849

4950
const createParams = {
5051
body: { name: 'abc', description: '', hello: 'a' },
@@ -120,7 +121,7 @@ describe('useApiQuery', () => {
120121
function BadApiCall() {
121122
try {
122123
// oxlint-disable-next-line react-hooks/rules-of-hooks
123-
useQuery(apiq('projectView', { path: { project: 'nonexistent' } }))
124+
useQuery(q(api.projectView, { path: { project: 'nonexistent' } }))
124125
} catch (e) {
125126
onError(e)
126127
}
@@ -143,8 +144,8 @@ describe('useApiQuery', () => {
143144
const { result } = renderHook(
144145
() =>
145146
useQuery(
146-
apiq(
147-
'projectView',
147+
q(
148+
api.projectView,
148149
{ path: { project: 'nonexistent' } },
149150
{ throwOnError: false }
150151
)
@@ -205,7 +206,7 @@ describe('useApiMutation', () => {
205206
}
206207

207208
it('passes through raw response', async () => {
208-
const { result } = renderHook(() => useApiMutation('diskCreate'), config)
209+
const { result } = renderHook(() => useApiMutation(api.diskCreate), config)
209210

210211
act(() => result.current.mutate(diskCreate404Params))
211212

@@ -219,7 +220,7 @@ describe('useApiMutation', () => {
219220
})
220221

221222
it('parses error json if possible', async () => {
222-
const { result } = renderHook(() => useApiMutation('diskCreate'), config)
223+
const { result } = renderHook(() => useApiMutation(api.diskCreate), config)
223224

224225
act(() => result.current.mutate(diskCreate404Params))
225226

@@ -286,3 +287,12 @@ describe('useApiMutation', () => {
286287
})
287288
})
288289
})
290+
291+
// we're relying on the name property of the API method for the queryKey, so we
292+
// need to make sure nothing changes in the generated client to cause the API
293+
// methods to not have a name
294+
it('apiq queryKey', () => {
295+
const params = { path: { silo: 'abc' } }
296+
const queryOptions = q(api.siloView, { path: { silo: 'abc' } })
297+
expect(queryOptions.queryKey).toEqual(['siloView', params])
298+
})

app/api/__tests__/safety.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const grepFiles = (s: string) =>
3535
it('mock-api is only referenced in test files', () => {
3636
expect(grepFiles('api-mocks')).toMatchInlineSnapshot(`
3737
[
38-
"app/api/__tests__/hooks.spec.tsx",
38+
"app/api/__tests__/client.spec.tsx",
3939
"mock-api/msw/db.ts",
4040
"test/e2e/instance-create.e2e.ts",
4141
"test/e2e/inventory.e2e.ts",

0 commit comments

Comments
 (0)