Last active
December 15, 2024 08:43
-
-
Save ognis1205/bf521400a1edca6b966c7b371c1347f5 to your computer and use it in GitHub Desktop.
openapi-typescript utility usage example.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; | |
import { CLIENT } from '../context/catalog'; | |
import { route as _route, isError, assertNever } from '../utils/openapi'; | |
import type { | |
paths as CatalogApi, | |
components as CatalogComponent, | |
} from '../types/api/catalog.gen'; | |
import type { | |
paths as ControlApi, | |
components as ControlComponent, | |
} from '../types/api/control.gen'; | |
import type { | |
Router, | |
Model, | |
PathParam, | |
RequestBody, | |
SuccessResponseBody, | |
} from '../utils/openapi'; | |
const routeCatalog: Router<CatalogApi> = _route; | |
export const useGetCatalog = ({ name }: UseGetCatalogArgs) => { | |
return useQuery<SuccessResponseBody<CatalogApi, '/catalogs/{name}', 'get'>>({ | |
queryKey: ['getCatalog', name], | |
queryFn: async () => { | |
const api = routeCatalog({ | |
client: CLIENT, | |
request: { | |
path: '/catalogs/{name}', | |
method: 'get', | |
params: { | |
paths: { | |
name, | |
}, | |
}, | |
}, | |
errorMessage: 'Failed to fetch catalog', | |
}); | |
const response = await api.call(); | |
if (isError(response)) { | |
// NOTE: | |
// When an expected error occurs, as defined in the OpenAPI specification, the following line will | |
// be executed. This block serves as a placeholder for expected errors. | |
return assertNever(response.data.status); | |
} else { | |
return response.data; | |
} | |
}, | |
}); | |
}; | |
const routeControl: Router<ControlApi> = _route; | |
export const useGetCurrentUser = () => { | |
const expectedErrorCodes = [ | |
401, // UNAUTHORIZED | |
] as const; | |
type ErrorCode = (typeof expectedErrorCodes)[number]; | |
const isExpectedError = (response: { | |
status: number; | |
data: any; | |
}): response is ErrorResponseBody< | |
ControlApi, | |
'/scim2/Me', | |
'get', | |
ErrorCode | |
> => expectedErrorCodes.map(Number).includes(response.status); | |
return useQuery<SuccessResponseBody<ControlApi, '/scim2/Me', 'get'> | null>({ | |
queryKey: ['getUser'], | |
queryFn: async () => { | |
const api = routeControl({ | |
client: CLIENT, | |
request: { | |
path: '/scim2/Me', | |
method: 'get', | |
}, | |
errorMessage: 'Failed to fetch user', | |
errorTypeGuard: isExpectedError, | |
}); | |
const response = await api.call(); | |
if (isError(response)) { | |
switch (response.data.status) { | |
case 401: | |
return null; | |
default: | |
return assertNever(response.data.status); | |
} | |
} else { | |
return response.data; | |
} | |
}, | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment