diff --git a/src/main/protocol-handler/router.ts b/src/main/protocol-handler/router.ts index 40e5ac1967..f401728fb2 100644 --- a/src/main/protocol-handler/router.ts +++ b/src/main/protocol-handler/router.ts @@ -5,6 +5,7 @@ import { match, matchPath } from "react-router"; export enum RoutingErrorType { INVALID_PROTOCOL = "invalid-protocol", INVALID_HOST = "invalid-host", + INVALID_PATHNAME = "invalid-pathname", NO_HANDLER = "no-handler", NO_EXTENSION_ID = "no-ext-id", MISSING_EXTENSION = "missing-ext", @@ -21,6 +22,8 @@ export class RoutingError extends Error { return "invalid host"; case RoutingErrorType.INVALID_PROTOCOL: return "invalid protocol"; + case RoutingErrorType.INVALID_PATHNAME: + return "invalid pathname"; case RoutingErrorType.NO_HANDLER: return "no handler"; case RoutingErrorType.NO_EXTENSION_ID: @@ -47,7 +50,7 @@ interface ExtensionIdMatch { } export class LensProtocolRouter extends Singleton { - private exentionRoutes = new Map>(); + private extentionRoutes = new Map>(); private internalRoutes = new Map(); private static ExtensionIDSchema = `/:${EXT_ID_MATCH}/`; @@ -65,6 +68,8 @@ export class LensProtocolRouter extends Singleton { return this._route(this.internalRoutes, url); case "extension": return this._routeToExtension(url); + case "main": + return this._routeToMain(url); default: throw new RoutingError(RoutingErrorType.INVALID_HOST, url); @@ -79,7 +84,7 @@ export class LensProtocolRouter extends Singleton { } const { [EXT_ID_MATCH]: id } = match.params; - const routes = this.exentionRoutes.get(id); + const routes = this.extentionRoutes.get(id); if (!routes) { throw new RoutingError(RoutingErrorType.MISSING_EXTENSION, url); @@ -88,6 +93,19 @@ export class LensProtocolRouter extends Singleton { this._route(routes, url, true); } + private _routeToMain(url: Url): void { + if (url.pathname === "/install-extension") { + // .query is e.g. {"@mirantis/lens-extension-cc": ""}, convert it to "@mirantis/lens-extension-cc" + const packageName = Object.keys(url.query)[0]; + + console.log(`Installing ${packageName} from lens:// url`); + + // TODO: Open extensions page + } else { + throw new RoutingError(RoutingErrorType.INVALID_PATHNAME, url); + } + } + private _route(routes: Map, url: Url, matchExtension = false): void { const matches = Array.from(routes.entries()) .map(([schema, handler]): [match>, RouteHandler] => { @@ -117,14 +135,14 @@ export class LensProtocolRouter extends Singleton { } public extensionOn(id: ExtensionId, urlSchema: string, handler: RouteHandler): void { - if (!this.exentionRoutes.has(id)) { - this.exentionRoutes.set(id, new Map()); + if (!this.extentionRoutes.has(id)) { + this.extentionRoutes.set(id, new Map()); } if (urlSchema.includes(`:${EXT_ID_MATCH}`)) { throw new TypeError("Invalid url path schema"); } - this.exentionRoutes.get(id).set(urlSchema, handler); + this.extentionRoutes.get(id).set(urlSchema, handler); } }