You've already forked authentication-service
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:
@@ -110,6 +110,11 @@ const paginationFamily = atomFamily((userId: string) => {
|
|||||||
return paginationAtom;
|
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 AppSessionsList: React.FC<{ userId: string }> = ({ userId }) => {
|
||||||
const [pending, startTransition] = useTransition();
|
const [pending, startTransition] = useTransition();
|
||||||
const result = useAtomValue(appSessionListFamily(userId));
|
const result = useAtomValue(appSessionListFamily(userId));
|
||||||
@@ -131,7 +136,8 @@ const AppSessionsList: React.FC<{ userId: string }> = ({ userId }) => {
|
|||||||
<H5>Apps</H5>
|
<H5>Apps</H5>
|
||||||
</header>
|
</header>
|
||||||
{appSessions.edges.map((session) => {
|
{appSessions.edges.map((session) => {
|
||||||
switch (session.node.__typename) {
|
const type = session.node.__typename;
|
||||||
|
switch (type) {
|
||||||
case "Oauth2Session":
|
case "Oauth2Session":
|
||||||
return (
|
return (
|
||||||
<OAuth2Session key={session.cursor} session={session.node} />
|
<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} />
|
<CompatSession key={session.cursor} session={session.node} />
|
||||||
);
|
);
|
||||||
default:
|
default:
|
||||||
throw new Error("Unexpected session type.");
|
unknownSessionType(type);
|
||||||
}
|
}
|
||||||
})}
|
})}
|
||||||
<PaginationControls
|
<PaginationControls
|
||||||
|
|||||||
@@ -49,6 +49,11 @@ const useRouteWithRedirect = (): [Route, boolean] => {
|
|||||||
return [route, redirecting];
|
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 Router: React.FC = () => {
|
||||||
const [route, redirecting] = useRouteWithRedirect();
|
const [route, redirecting] = useRouteWithRedirect();
|
||||||
|
|
||||||
@@ -73,6 +78,8 @@ const Router: React.FC = () => {
|
|||||||
return <VerifyEmail id={route.id} />;
|
return <VerifyEmail id={route.id} />;
|
||||||
case "unknown":
|
case "unknown":
|
||||||
return <>Unknown route {JSON.stringify(route.segments)}</>;
|
return <>Unknown route {JSON.stringify(route.segments)}</>;
|
||||||
|
default:
|
||||||
|
unknownRoute(route);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user