All files / src routes.map.ts

100% Statements 36/36
100% Branches 10/10
100% Functions 8/8
100% Lines 35/35

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69    6x   32x 32x 32x 32x 32x       18x       31x       6x       6x       2x       7x 7x 7x 13x   7x       7x 7x 7x 12x 12x 12x 33x 33x 33x   33x 33x 33x 71x 70x   71x   33x   12x   7x     6x    
'use strict';
 
export default class RoutesMap {
	public static add(method: string, route: string): void {
		method = method.toUpperCase();
		let routes: string[] = RoutesMap._routes.get(method) || [];
		routes.push(route);
		routes = Array.from(new Set(routes)).filter(Boolean).sort();
		RoutesMap._routes.set(method, routes);
	}
 
	public static getMethods(): string[] {
		return Array.from(RoutesMap._routes.keys()).filter(Boolean).sort();
	}
 
	public static getRoutes(method: string): string[] | null {
		return RoutesMap._routes.get(method?.toUpperCase() || '');
	}
 
	public static keys(): string[] {
		return Array.from(RoutesMap._routes.keys());
	}
 
	public static values(): string[][] {
		return Array.from(RoutesMap._routes.values());
	}
 
	public static clear(): void {
		RoutesMap._routes.clear();
	}
 
	public static list(): any {
		const obj: any = {};
		const methods: string[] = RoutesMap.getMethods();
		for (const method of methods) {
			obj[method] = RoutesMap.getRoutes(method);
		}
		return obj;
	}
 
	public static json(): any {
		const obj: any = {};
		const methods: string[] = RoutesMap.getMethods();
		for (const method of methods) {
			const apis: any = {};
			const routes: string[] = RoutesMap.getRoutes(method);
			for (const route of routes) {
				let parts: string[] = route.split('/');
				parts = parts.filter(Boolean);
				const last: string = parts.join('.');
				// Replace lodash _.set with native object assignment
				const keys = last.split('.');
				let current = apis;
				for (let i = 0; i < keys.length - 1; i++) {
					if (!(keys[i] in current)) {
						current[keys[i]] = {};
					}
					current = current[keys[i]];
				}
				current[keys[keys.length - 1]] = true;
			}
			obj[method] = apis;
		}
		return obj;
	}
 
	private static readonly _routes: Map<string, string[]> = new Map<string, string[]>();
}