Add common app routes to the protocol renderer router (#2272)

This commit is contained in:
Sebastian Malton authored and GitHub committed 2021-03-17 13:57:49 -04:00
1 parent ca39379b3a
commit 0561f6bfd0
17 files changed
+129 -33

No files matched your search

+3 -1
View File
@@ -184,10 +184,12 @@ export abstract class LensProtocolRouter extends Singleton {
* @param pathSchema the URI path schema to match against for this handler
* @param handler a function that will be called if a protocol path matches
*/
public addInternalHandler(urlSchema: string, handler: RouteHandler): void {
public addInternalHandler(urlSchema: string, handler: RouteHandler): this {
pathToRegexp(urlSchema); // verify now that the schema is valid
logger.info(`${LensProtocolRouter.LoggingPrefix}: internal registering ${urlSchema}`);
this.internalRoutes.set(urlSchema, handler);
return this;
}
/**
+8 -2
View File
@@ -3,14 +3,20 @@ import { compile } from "path-to-regexp";
export interface IURLParams<P extends object = {}, Q extends object = {}> {
params?: P;
query?: Q;
fragment?: string;
}
export function buildURL<P extends object = {}, Q extends object = {}>(path: string | any) {
const pathBuilder = compile(String(path));
return function ({ params, query }: IURLParams<P, Q> = {}) {
return function ({ params, query, fragment }: IURLParams<P, Q> = {}): string {
const queryParams = query ? new URLSearchParams(Object.entries(query)).toString() : "";
const parts = [
pathBuilder(params),
queryParams && `?${queryParams}`,
fragment && `#${fragment}`,
];
return pathBuilder(params) + (queryParams ? `?${queryParams}` : "");
return parts.filter(Boolean).join("");
};
}