1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Add protocol handler for lens://main/install-extension.

Signed-off-by: Panu Horsmalahti <phorsmalahti@mirantis.com>
This commit is contained in:
Panu Horsmalahti 2020-12-08 14:48:23 +02:00 committed by Sebastian Malton
parent e40c8a6299
commit 0c6a7ba9e4

View File

@ -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<ExtensionId, Map<string, RouteHandler>>();
private extentionRoutes = new Map<ExtensionId, Map<string, RouteHandler>>();
private internalRoutes = new Map<string, RouteHandler>();
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<string, RouteHandler>, url: Url, matchExtension = false): void {
const matches = Array.from(routes.entries())
.map(([schema, handler]): [match<Record<string, string>>, 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);
}
}