diff --git a/web/src/admin/Routes.ts b/web/src/admin/Routes.ts index f6df48cbbf..a08d375434 100644 --- a/web/src/admin/Routes.ts +++ b/web/src/admin/Routes.ts @@ -1,155 +1,266 @@ import "@goauthentik/admin/admin-overview/AdminOverviewPage"; -import { ID_REGEX, Route, SLUG_REGEX, UUID_REGEX } from "@goauthentik/elements/router/Route"; +import { ID_REGEX, SLUG_REGEX, UUID_REGEX } from "@goauthentik/elements/router/Route"; +import { RawRoute, makeRoute } from "@goauthentik/elements/router/routeUtils"; import { html } from "lit"; -export const ROUTES: Route[] = [ +export const _ROUTES: RawRoute[] = [ // Prevent infinite Shell loops - new Route(new RegExp("^/$")).redirect("/administration/overview"), - new Route(new RegExp("^#.*")).redirect("/administration/overview"), - new Route(new RegExp("^/library$")).redirect("/if/user/", true), + ["^/$", "/administration/overview"], + ["^#.*", "/administration/overview"], + ["^/library$", ["/if/user/", true]], // statically imported since this is the default route - new Route(new RegExp("^/administration/overview$"), async () => { - return html``; - }), - new Route(new RegExp("^/administration/dashboard/users$"), async () => { - await import("@goauthentik/admin/admin-overview/DashboardUserPage"); - return html``; - }), - new Route(new RegExp("^/administration/system-tasks$"), async () => { - await import("@goauthentik/admin/system-tasks/SystemTaskListPage"); - return html``; - }), - new Route(new RegExp("^/core/providers$"), async () => { - await import("@goauthentik/admin/providers/ProviderListPage"); - return html``; - }), - new Route(new RegExp(`^/core/providers/(?${ID_REGEX})$`), async (args) => { - await import("@goauthentik/admin/providers/ProviderViewPage"); - return html``; - }), - new Route(new RegExp("^/core/applications$"), async () => { - await import("@goauthentik/admin/applications/ApplicationListPage"); - return html``; - }), - new Route(new RegExp(`^/core/applications/(?${SLUG_REGEX})$`), async (args) => { - await import("@goauthentik/admin/applications/ApplicationViewPage"); - return html``; - }), - new Route(new RegExp("^/core/sources$"), async () => { - await import("@goauthentik/admin/sources/SourceListPage"); - return html``; - }), - new Route(new RegExp(`^/core/sources/(?${SLUG_REGEX})$`), async (args) => { - await import("@goauthentik/admin/sources/SourceViewPage"); - return html``; - }), - new Route(new RegExp("^/core/property-mappings$"), async () => { - await import("@goauthentik/admin/property-mappings/PropertyMappingListPage"); - return html``; - }), - new Route(new RegExp("^/core/tokens$"), async () => { - await import("@goauthentik/admin/tokens/TokenListPage"); - return html``; - }), - new Route(new RegExp("^/core/brands"), async () => { - await import("@goauthentik/admin/brands/BrandListPage"); - return html``; - }), - new Route(new RegExp("^/policy/policies$"), async () => { - await import("@goauthentik/admin/policies/PolicyListPage"); - return html``; - }), - new Route(new RegExp("^/policy/reputation$"), async () => { - await import("@goauthentik/admin/policies/reputation/ReputationListPage"); - return html``; - }), - new Route(new RegExp("^/identity/groups$"), async () => { - await import("@goauthentik/admin/groups/GroupListPage"); - return html``; - }), - new Route(new RegExp(`^/identity/groups/(?${UUID_REGEX})$`), async (args) => { - await import("@goauthentik/admin/groups/GroupViewPage"); - return html``; - }), - new Route(new RegExp("^/identity/users$"), async () => { - await import("@goauthentik/admin/users/UserListPage"); - return html``; - }), - new Route(new RegExp(`^/identity/users/(?${ID_REGEX})$`), async (args) => { - await import("@goauthentik/admin/users/UserViewPage"); - return html``; - }), - new Route(new RegExp("^/identity/roles$"), async () => { - await import("@goauthentik/admin/roles/RoleListPage"); - return html``; - }), - new Route(new RegExp(`^/identity/roles/(?${UUID_REGEX})$`), async (args) => { - await import("@goauthentik/admin/roles/RoleViewPage"); - return html``; - }), - new Route(new RegExp("^/flow/stages/invitations$"), async () => { - await import("@goauthentik/admin/stages/invitation/InvitationListPage"); - return html``; - }), - new Route(new RegExp("^/flow/stages/prompts$"), async () => { - await import("@goauthentik/admin/stages/prompt/PromptListPage"); - return html``; - }), - new Route(new RegExp("^/flow/stages$"), async () => { - await import("@goauthentik/admin/stages/StageListPage"); - return html``; - }), - new Route(new RegExp("^/flow/flows$"), async () => { - await import("@goauthentik/admin/flows/FlowListPage"); - return html``; - }), - new Route(new RegExp(`^/flow/flows/(?${SLUG_REGEX})$`), async (args) => { - await import("@goauthentik/admin/flows/FlowViewPage"); - return html``; - }), - new Route(new RegExp("^/events/log$"), async () => { - await import("@goauthentik/admin/events/EventListPage"); - return html``; - }), - new Route(new RegExp(`^/events/log/(?${UUID_REGEX})$`), async (args) => { - await import("@goauthentik/admin/events/EventViewPage"); - return html``; - }), - new Route(new RegExp("^/events/transports$"), async () => { - await import("@goauthentik/admin/events/TransportListPage"); - return html``; - }), - new Route(new RegExp("^/events/rules$"), async () => { - await import("@goauthentik/admin/events/RuleListPage"); - return html``; - }), - new Route(new RegExp("^/outpost/outposts$"), async () => { - await import("@goauthentik/admin/outposts/OutpostListPage"); - return html``; - }), - new Route(new RegExp("^/outpost/integrations$"), async () => { - await import("@goauthentik/admin/outposts/ServiceConnectionListPage"); - return html``; - }), - new Route(new RegExp("^/crypto/certificates$"), async () => { - await import("@goauthentik/admin/crypto/CertificateKeyPairListPage"); - return html``; - }), - new Route(new RegExp("^/admin/settings$"), async () => { - await import("@goauthentik/admin/admin-settings/AdminSettingsPage"); - return html``; - }), - new Route(new RegExp("^/blueprints/instances$"), async () => { - await import("@goauthentik/admin/blueprints/BlueprintListPage"); - return html``; - }), - new Route(new RegExp("^/debug$"), async () => { - await import("@goauthentik/admin/DebugPage"); - return html``; - }), - new Route(new RegExp("^/enterprise/licenses$"), async () => { - await import("@goauthentik/admin/enterprise/EnterpriseLicenseListPage"); - return html``; - }), + [ + "^/administration/overview$", + async () => { + return html``; + }, + ], + [ + "^/administration/dashboard/users$", + async () => { + await import("@goauthentik/admin/admin-overview/DashboardUserPage"); + return html``; + }, + ], + [ + "^/administration/system-tasks$", + async () => { + await import("@goauthentik/admin/system-tasks/SystemTaskListPage"); + return html``; + }, + ], + [ + "^/core/providers$", + async () => { + await import("@goauthentik/admin/providers/ProviderListPage"); + return html``; + }, + ], + [ + `^/core/providers/(?${ID_REGEX}])$`, + async (args) => { + await import("@goauthentik/admin/providers/ProviderViewPage"); + return html``; + }, + ], + [ + "^/core/applications$", + async () => { + await import("@goauthentik/admin/applications/ApplicationListPage"); + return html``; + }, + ], + [ + `^/core/applications/(?${SLUG_REGEX})$`, + async (args) => { + await import("@goauthentik/admin/applications/ApplicationViewPage"); + return html``; + }, + ], + [ + "^/core/sources$", + async () => { + await import("@goauthentik/admin/sources/SourceListPage"); + return html``; + }, + ], + [ + `^/core/sources/(?${SLUG_REGEX})$`, + async (args) => { + await import("@goauthentik/admin/sources/SourceViewPage"); + return html``; + }, + ], + [ + "^/core/property-mappings$", + async () => { + await import("@goauthentik/admin/property-mappings/PropertyMappingListPage"); + return html``; + }, + ], + [ + "^/core/tokens$", + async () => { + await import("@goauthentik/admin/tokens/TokenListPage"); + return html``; + }, + ], + [ + "^/core/brands", + async () => { + await import("@goauthentik/admin/brands/BrandListPage"); + return html``; + }, + ], + [ + "^/policy/policies$", + async () => { + await import("@goauthentik/admin/policies/PolicyListPage"); + return html``; + }, + ], + [ + "^/policy/reputation$", + async () => { + await import("@goauthentik/admin/policies/reputation/ReputationListPage"); + return html``; + }, + ], + [ + "^/identity/groups$", + async () => { + await import("@goauthentik/admin/groups/GroupListPage"); + return html``; + }, + ], + [ + `^/identity/groups/(?${UUID_REGEX})$`, + async (args) => { + await import("@goauthentik/admin/groups/GroupViewPage"); + return html``; + }, + ], + [ + "^/identity/users$", + async () => { + await import("@goauthentik/admin/users/UserListPage"); + return html``; + }, + ], + [ + `^/identity/users/(?${ID_REGEX})$`, + async (args) => { + await import("@goauthentik/admin/users/UserViewPage"); + return html``; + }, + ], + [ + "^/identity/roles$", + async () => { + await import("@goauthentik/admin/roles/RoleListPage"); + return html``; + }, + ], + [ + `^/identity/roles/(?${UUID_REGEX})$`, + async (args) => { + await import("@goauthentik/admin/roles/RoleViewPage"); + return html``; + }, + ], + [ + "^/flow/stages/invitations$", + async () => { + await import("@goauthentik/admin/stages/invitation/InvitationListPage"); + return html``; + }, + ], + [ + "^/flow/stages/prompts$", + async () => { + await import("@goauthentik/admin/stages/prompt/PromptListPage"); + return html``; + }, + ], + [ + "^/flow/stages$", + async () => { + await import("@goauthentik/admin/stages/StageListPage"); + return html``; + }, + ], + [ + "^/flow/flows$", + async () => { + await import("@goauthentik/admin/flows/FlowListPage"); + return html``; + }, + ], + [ + `^/flow/flows/(?${SLUG_REGEX})$`, + async (args) => { + await import("@goauthentik/admin/flows/FlowViewPage"); + return html``; + }, + ], + [ + "^/events/log$", + async () => { + await import("@goauthentik/admin/events/EventListPage"); + return html``; + }, + ], + [ + `^/events/log/(?${UUID_REGEX})$`, + async (args) => { + await import("@goauthentik/admin/events/EventViewPage"); + return html``; + }, + ], + [ + "^/events/transports$", + async () => { + await import("@goauthentik/admin/events/TransportListPage"); + return html``; + }, + ], + [ + "^/events/rules$", + async () => { + await import("@goauthentik/admin/events/RuleListPage"); + return html``; + }, + ], + [ + "^/outpost/outposts$", + async () => { + await import("@goauthentik/admin/outposts/OutpostListPage"); + return html``; + }, + ], + [ + "^/outpost/integrations$", + async () => { + await import("@goauthentik/admin/outposts/ServiceConnectionListPage"); + return html``; + }, + ], + [ + "^/crypto/certificates$", + async () => { + await import("@goauthentik/admin/crypto/CertificateKeyPairListPage"); + return html``; + }, + ], + [ + "^/admin/settings$", + async () => { + await import("@goauthentik/admin/admin-settings/AdminSettingsPage"); + return html``; + }, + ], + [ + "^/blueprints/instances$", + async () => { + await import("@goauthentik/admin/blueprints/BlueprintListPage"); + return html``; + }, + ], + [ + "^/debug$", + async () => { + await import("@goauthentik/admin/DebugPage"); + return html``; + }, + ], + [ + "^/enterprise/licenses$", + async () => { + await import("@goauthentik/admin/enterprise/EnterpriseLicenseListPage"); + return html``; + }, + ], ]; + +export const ROUTES = _ROUTES.map(makeRoute); diff --git a/web/src/elements/router/routeUtils.ts b/web/src/elements/router/routeUtils.ts new file mode 100644 index 0000000000..702a3713f1 --- /dev/null +++ b/web/src/elements/router/routeUtils.ts @@ -0,0 +1,37 @@ +import { Route, RouteArgs } from "@goauthentik/elements/router/Route"; +import { P, match } from "ts-pattern"; + +import { TemplateResult } from "lit"; + +type RouteInvoke = + | ((_1: RouteArgs) => Promise>) + | (() => Promise>); + +type _RawRedirect = [string, string | [string, boolean]]; +type _RawRoute = [string, RouteInvoke]; +export type RawRoute = _RawRoute | _RawRedirect; + +// eslint-disable-next-line @typescript-eslint/no-explicit-any +const isLoader = (v: any): v is RouteInvoke => typeof v === "function"; + +// When discriminating between redirects and loaders, the loader type is irrelevant to routing +// correctly. The loader type *is* still well-typed to prevent an incompatible loader being added to +// a route, but the two different loader types-- ones that take arguments, and ones that do not-- do +// not matter to the route builder. + +// On the other hand, the two different kinds of redirects *do* matter, but only because JavaScript +// makes a distinction between methods of one argument that can be `call`'ed and methods of more +// than one argument that must be `apply`'d. (Spread arguments are converted to call or apply as +// needed). + +// prettier-ignore +export function makeRoute(route: RawRoute): Route { + return match(route) + .with([P.string, P.when(isLoader)], + ([path, loader]) => new Route(new RegExp(path), loader)) + .with([P.string, P.string], + ([path, redirect]) => new Route(new RegExp(path)).redirect(redirect)) + .with([P.string, [P.string, P.boolean]], + ([path, redirect]) => new Route(new RegExp(path)).redirect(...redirect)) + .exhaustive(); +} diff --git a/web/src/user/Routes.ts b/web/src/user/Routes.ts index 72738e1821..e74561f1a5 100644 --- a/web/src/user/Routes.ts +++ b/web/src/user/Routes.ts @@ -1,15 +1,20 @@ -import { Route } from "@goauthentik/elements/router/Route"; +import { RawRoute, makeRoute } from "@goauthentik/elements/router/routeUtils"; import "@goauthentik/user/LibraryPage/LibraryPage"; import { html } from "lit"; -export const ROUTES: Route[] = [ +export const _ROUTES: RawRoute[] = [ // Prevent infinite Shell loops - new Route(new RegExp("^/$")).redirect("/library"), - new Route(new RegExp("^#.*")).redirect("/library"), - new Route(new RegExp("^/library$"), async () => html``), - new Route(new RegExp("^/settings$"), async () => { - await import("@goauthentik/user/user-settings/UserSettingsPage"); - return html``; - }), + ["^/$", "/library"], + ["^#.*", "/library"], + ["^/library$", async () => html``], + [ + "^/settings$", + async () => { + await import("@goauthentik/user/user-settings/UserSettingsPage"); + return html``; + }, + ], ]; + +export const ROUTES = _ROUTES.map(makeRoute);