Skip to content

Commit 693d39c

Browse files
committed
aaaaah got it to wrap the api object too
1 parent a972f1d commit 693d39c

File tree

8 files changed

+27
-21
lines changed

8 files changed

+27
-21
lines changed

apps/web-console/src/app-layout/AppLayout.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react'
22
import styled from 'styled-components'
33

4-
import { useApiData, api } from '@oxide/api'
4+
import { useApi } from '@oxide/api'
55
import { GlobalNav, OperationList, ProjectList } from '@oxide/ui'
66
import Wordmark from '../assets/wordmark.svg'
77

@@ -56,7 +56,7 @@ const GlobalNavContainer = styled.header`
5656
`
5757

5858
export default ({ children }: AppLayoutProps) => {
59-
const { data: projects } = useApiData(api, 'apiProjectsGet', {})
59+
const { data: projects } = useApi('apiProjectsGet', {})
6060

6161
return (
6262
<Wrapper>

apps/web-console/src/pages/instance/InstancePage.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react'
22
import { useParams } from 'react-router-dom'
33
import styled from 'styled-components'
44

5-
import { useApiData, api } from '@oxide/api'
5+
import { useApi } from '@oxide/api'
66

77
import {
88
Breadcrumbs,
@@ -74,7 +74,7 @@ const InstancePage = () => {
7474
const breadcrumbs = useBreadcrumbs()
7575
const { projectName, instanceName } = useParams<Params>()
7676

77-
const { data, error } = useApiData(api, 'apiProjectInstancesGetInstance', {
77+
const { data, error } = useApi('apiProjectInstancesGetInstance', {
7878
instanceName,
7979
projectName,
8080
})

apps/web-console/src/pages/instance/InstancesPage.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import styled from 'styled-components'
33

44
import { useParams, Link } from 'react-router-dom'
55

6-
import { useApiData, api } from '@oxide/api'
6+
import { useApi } from '@oxide/api'
77
import { Breadcrumbs, PageHeader, TextWithIcon } from '@oxide/ui'
88
import { useBreadcrumbs } from '../../hooks'
99

@@ -20,7 +20,7 @@ const InstancesPage = () => {
2020
const breadcrumbs = useBreadcrumbs()
2121

2222
const { projectName } = useParams<Params>()
23-
const { data } = useApiData(api, 'apiProjectInstancesGet', { projectName })
23+
const { data } = useApi('apiProjectInstancesGet', { projectName })
2424

2525
if (!data) return <div>loading</div>
2626

apps/web-console/src/pages/projects/ProjectPage.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import styled from 'styled-components'
33

44
import { useParams, Link } from 'react-router-dom'
55

6-
import { useApiData, api } from '@oxide/api'
6+
import { useApi } from '@oxide/api'
77
import { Breadcrumbs, PageHeader, TextWithIcon } from '@oxide/ui'
88
import { useBreadcrumbs } from '../../hooks'
99

@@ -20,10 +20,10 @@ const ProjectPage = () => {
2020
const breadcrumbs = useBreadcrumbs()
2121

2222
const { projectName } = useParams<Params>()
23-
const { data: project } = useApiData(api, 'apiProjectsGetProject', {
23+
const { data: project } = useApi('apiProjectsGetProject', {
2424
projectName,
2525
})
26-
const { data: instances } = useApiData(api, 'apiProjectInstancesGet', {
26+
const { data: instances } = useApi('apiProjectInstancesGet', {
2727
projectName,
2828
})
2929

apps/web-console/src/pages/projects/ProjectsPage.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import styled from 'styled-components'
33

44
import { Link } from 'react-router-dom'
55

6-
import { useApiData, api } from '@oxide/api'
6+
import { useApi } from '@oxide/api'
77
import { useBreadcrumbs } from '../../hooks'
88
import { Breadcrumbs, PageHeader, TextWithIcon } from '@oxide/ui'
99

@@ -14,7 +14,7 @@ const Title = styled(TextWithIcon).attrs({
1414

1515
const ProjectsPage = () => {
1616
const breadcrumbs = useBreadcrumbs()
17-
const { data } = useApiData(api, 'apiProjectsGet', {})
17+
const { data } = useApi('apiProjectsGet', {})
1818

1919
if (!data) return <div>loading</div>
2020

libs/api/hooks/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { useApiData } from './use-api-data'
1+
export { getUseApi } from './use-api-data'

libs/api/hooks/use-api-data.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,16 @@ type Response<A, K extends keyof A> = A[K] extends (p: any) => Promise<infer R>
3838
// We also sort the keys in the params object so that { a: 1, b: 2 }
3939
// and { b: 2, a: 1 } are considered equivalent. SWR accepts an array
4040
// of strings as well as a single string.
41-
export function useApiData<
42-
A extends PickByValue<A, (p: any) => Promise<any>>,
43-
K extends keyof A
44-
>(api: A, method: K, params: ReqParams<A, K>) {
45-
const paramsStr = JSON.stringify(sortObj(params))
46-
return useSWR<Response<A, K>>([method, paramsStr], () => api[method](params))
41+
export function getUseApi<A extends PickByValue<A, (p: any) => Promise<any>>>(
42+
api: A
43+
) {
44+
function useApi<K extends keyof A>(method: K, params: ReqParams<A, K>) {
45+
const paramsStr = JSON.stringify(sortObj(params))
46+
return useSWR<Response<A, K>>([method, paramsStr], () =>
47+
api[method](params)
48+
)
49+
}
50+
return useApi
4751
}
4852

4953
/* eslint-enable @typescript-eslint/no-explicit-any */

libs/api/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
export * from './__generated__'
2-
export * from './hooks'
1+
import { getUseApi } from './hooks'
32

43
import { DefaultApi, Configuration } from './__generated__'
54

@@ -8,4 +7,7 @@ const config =
87
? new Configuration({ basePath: process.env.API_URL })
98
: new Configuration({ basePath: '/api' })
109

11-
export const api = new DefaultApi(config)
10+
const api = new DefaultApi(config)
11+
12+
export const useApi = getUseApi(api)
13+
export * from './__generated__'

0 commit comments

Comments
 (0)