Skip to content

Commit f8d2f36

Browse files
authored
Prevent import from '.' (#1960)
prevent import from '.'
1 parent e0b676d commit f8d2f36

File tree

10 files changed

+75
-63
lines changed

10 files changed

+75
-63
lines changed

.eslintrc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"eqeqeq": ["error", "always", { "null": "ignore" }],
4141
"jsx-a11y/label-has-associated-control": [2, { "controlComponents": ["button"] }],
4242
"no-param-reassign": "error",
43+
"no-restricted-imports": ["error", { "paths": ["."] }],
4344
"no-return-assign": "error",
4445
"no-unused-vars": "off",
4546
"prefer-arrow-callback": "off",

libs/api/client.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
5+
*
6+
* Copyright Oxide Computer Company
7+
*/
8+
import { QueryClient } from '@tanstack/react-query'
9+
10+
import { Api } from './__generated__/Api'
11+
import {
12+
getUseApiMutation,
13+
getUseApiQueries,
14+
getUseApiQuery,
15+
getUseApiQueryClient,
16+
getUseApiQueryErrorsAllowed,
17+
getUsePrefetchedApiQuery,
18+
wrapQueryClient,
19+
} from './hooks'
20+
21+
export const api = new Api({
22+
// unit tests run in Node, whose fetch implementation requires a full URL
23+
host: process.env.NODE_ENV === 'test' ? 'http://testhost' : '',
24+
})
25+
26+
// add the API client to window for use from the browser JS console. requests
27+
// will use the session cookie, same as normal API calls
28+
if (typeof window !== 'undefined') {
29+
// @ts-expect-error
30+
window.oxide = api.methods
31+
}
32+
33+
export type ApiMethods = typeof api.methods
34+
35+
export const useApiQuery = getUseApiQuery(api.methods)
36+
export const useApiQueries = getUseApiQueries(api.methods)
37+
/**
38+
* Same as `useApiQuery`, except we use `invariant(data)` to ensure the data is
39+
* already there in the cache at request time, which means it has been
40+
* prefetched in a loader. Whenever this hook is used, there should be an e2e
41+
* test loading the page to exercise the invariant in CI.
42+
*/
43+
export const usePrefetchedApiQuery = getUsePrefetchedApiQuery(api.methods)
44+
export const useApiQueryErrorsAllowed = getUseApiQueryErrorsAllowed(api.methods)
45+
export const useApiMutation = getUseApiMutation(api.methods)
46+
47+
// Needs to be defined here instead of in app so we can use it to define
48+
// `apiQueryClient`, which provides API-typed versions of QueryClient methods
49+
export const queryClient = new QueryClient({
50+
defaultOptions: {
51+
queries: {
52+
retry: false,
53+
staleTime: 10000,
54+
refetchOnWindowFocus: false,
55+
},
56+
},
57+
})
58+
59+
// to be used in loaders, which are outside the component tree and therefore
60+
// don't have access to context
61+
export const apiQueryClient = wrapQueryClient(api.methods, queryClient)
62+
63+
// to be used to retrieve the typed query client in components
64+
export const useApiQueryClient = getUseApiQueryClient(api.methods)

libs/api/index.ts

Lines changed: 1 addition & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -5,66 +5,11 @@
55
*
66
* Copyright Oxide Computer Company
77
*/
8-
import { QueryClient } from '@tanstack/react-query'
98

109
// for convenience so we can do `import type { ApiTypes } from '@oxide/api'`
1110
import type * as ApiTypes from './__generated__/Api'
12-
import { Api } from './__generated__/Api'
13-
import {
14-
getUseApiMutation,
15-
getUseApiQueries,
16-
getUseApiQuery,
17-
getUseApiQueryClient,
18-
getUseApiQueryErrorsAllowed,
19-
getUsePrefetchedApiQuery,
20-
wrapQueryClient,
21-
} from './hooks'
22-
23-
export const api = new Api({
24-
// unit tests run in Node, whose fetch implementation requires a full URL
25-
host: process.env.NODE_ENV === 'test' ? 'http://testhost' : '',
26-
})
27-
28-
// add the API client to window for use from the browser JS console. requests
29-
// will use the session cookie, same as normal API calls
30-
if (typeof window !== 'undefined') {
31-
// @ts-expect-error
32-
window.oxide = api.methods
33-
}
34-
35-
export type ApiMethods = typeof api.methods
36-
37-
export const useApiQuery = getUseApiQuery(api.methods)
38-
export const useApiQueries = getUseApiQueries(api.methods)
39-
/**
40-
* Same as `useApiQuery`, except we use `invariant(data)` to ensure the data is
41-
* already there in the cache at request time, which means it has been
42-
* prefetched in a loader. Whenever this hook is used, there should be an e2e
43-
* test loading the page to exercise the invariant in CI.
44-
*/
45-
export const usePrefetchedApiQuery = getUsePrefetchedApiQuery(api.methods)
46-
export const useApiQueryErrorsAllowed = getUseApiQueryErrorsAllowed(api.methods)
47-
export const useApiMutation = getUseApiMutation(api.methods)
48-
49-
// Needs to be defined here instead of in app so we can use it to define
50-
// `apiQueryClient`, which provides API-typed versions of QueryClient methods
51-
export const queryClient = new QueryClient({
52-
defaultOptions: {
53-
queries: {
54-
retry: false,
55-
staleTime: 10000,
56-
refetchOnWindowFocus: false,
57-
},
58-
},
59-
})
60-
61-
// to be used in loaders, which are outside the component tree and therefore
62-
// don't have access to context
63-
export const apiQueryClient = wrapQueryClient(api.methods, queryClient)
64-
65-
// to be used to retrieve the typed query client in components
66-
export const useApiQueryClient = getUseApiQueryClient(api.methods)
6711

12+
export * from './client'
6813
export * from './roles'
6914
export * from './util'
7015
export * from './__generated__/Api'

libs/api/roles.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import { useMemo } from 'react'
1515

1616
import { lowestBy, sortBy } from '@oxide/util'
1717

18-
import { usePrefetchedApiQuery } from '.'
1918
import type { FleetRole, IdentityType, ProjectRole, SiloRole } from './__generated__/Api'
19+
import { usePrefetchedApiQuery } from './client'
2020

2121
/**
2222
* Union of all the specific roles, which are all the same, which makes making

libs/table/cells/BooleanCell.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// "true" because it insists it's a boolean
1212
import { Disabled12Icon, Success12Icon } from '@oxide/ui'
1313

14-
import type { Cell } from '.'
14+
import type { Cell } from './Cell'
1515

1616
export const BooleanCell = ({ value }: Cell<boolean>) =>
1717
value ? (

libs/table/cells/EnabledCell.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import type { VpcFirewallRuleStatus } from '@oxide/api'
99
import { Badge, Success12Icon } from '@oxide/ui'
1010

11-
import type { Cell } from '.'
11+
import type { Cell } from './Cell'
1212

1313
export const EnabledCell = ({ value }: Cell<VpcFirewallRuleStatus>) =>
1414
value === 'enabled' ? (

libs/table/cells/FirewallFilterCell.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
import type { VpcFirewallRuleFilter } from '@oxide/api'
99
import { Badge } from '@oxide/ui'
1010

11-
import { TypeValueCell, type Cell } from '.'
11+
import { type Cell } from './Cell'
12+
import { TypeValueCell } from './TypeValueCell'
1213

1314
export const FirewallFilterCell = ({
1415
value: { hosts, ports, protocols },

libs/table/cells/TypeValueListCell.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
*
66
* Copyright Oxide Computer Company
77
*/
8-
import { TypeValueCell, type Cell, type TypeValue } from '.'
8+
import { type Cell } from './Cell'
9+
import { TypeValueCell, type TypeValue } from './TypeValueCell'
910

1011
export const TypeValueListCell = ({ value }: Cell<TypeValue[]>) => (
1112
<div>

libs/util/math.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
*/
88
import { afterAll, beforeAll, describe, expect, it } from 'vitest'
99

10-
import { GiB } from '.'
1110
import { round, splitDecimal } from './math'
11+
import { GiB } from './units'
1212

1313
it('rounds properly', () => {
1414
expect(round(0.456, 2)).toEqual(0.46)

libs/util/math.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Copyright Oxide Computer Company
77
*/
88

9-
import { splitOnceBy } from '.'
9+
import { splitOnceBy } from './array'
1010

1111
/**
1212
* Get the two parts of a number (before decimal and after-and-including

0 commit comments

Comments
 (0)