1
0
mirror of https://github.com/matrix-org/matrix-authentication-service.git synced 2025-11-20 12:02:22 +03:00

Type-level check that we handle all session types & route types

This commit is contained in:
Quentin Gliech
2023-09-21 09:39:47 +02:00
parent 51633a1853
commit 0896757a63
2 changed files with 15 additions and 2 deletions

View File

@@ -110,6 +110,11 @@ const paginationFamily = atomFamily((userId: string) => {
return paginationAtom;
});
// A type-safe way to ensure we've handled all session types
const unknownSessionType = (type: never): never => {
throw new Error(`Unknown session type: ${type}`);
};
const AppSessionsList: React.FC<{ userId: string }> = ({ userId }) => {
const [pending, startTransition] = useTransition();
const result = useAtomValue(appSessionListFamily(userId));
@@ -131,7 +136,8 @@ const AppSessionsList: React.FC<{ userId: string }> = ({ userId }) => {
<H5>Apps</H5>
</header>
{appSessions.edges.map((session) => {
switch (session.node.__typename) {
const type = session.node.__typename;
switch (type) {
case "Oauth2Session":
return (
<OAuth2Session key={session.cursor} session={session.node} />
@@ -141,7 +147,7 @@ const AppSessionsList: React.FC<{ userId: string }> = ({ userId }) => {
<CompatSession key={session.cursor} session={session.node} />
);
default:
throw new Error("Unexpected session type.");
unknownSessionType(type);
}
})}
<PaginationControls

View File

@@ -49,6 +49,11 @@ const useRouteWithRedirect = (): [Route, boolean] => {
return [route, redirecting];
};
// A type-safe way to ensure we've handled all routes
const unknownRoute = (route: never): never => {
throw new Error(`Invalid route: ${JSON.stringify(route)}`);
};
const Router: React.FC = () => {
const [route, redirecting] = useRouteWithRedirect();
@@ -73,6 +78,8 @@ const Router: React.FC = () => {
return <VerifyEmail id={route.id} />;
case "unknown":
return <>Unknown route {JSON.stringify(route.segments)}</>;
default:
unknownRoute(route);
}
};