All files / src/functions add.route.ts

30.76% Statements 8/26
0% Branches 0/7
0% Functions 0/1
36.36% Lines 8/22

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    2x   2x   2x 2x 2x 2x   2x                                           2x  
'use strict';
 
import * as _ from 'lodash';
 
import RoutesMap from '../routes.map';
 
import fixUrl from './fix.url';
import fixTags from './fix.tags';
import fixSchema from './fix.schema';
import fixRouteMethod from './fix.route.method';
 
const addRoute: Function = (fastify: any, route: any, relativeFilePath: string, verbose: boolean = false): void => {
	Iif (!_.isPlainObject(route)) {
		Iif (verbose) console.log('[@owservable/fastify-auto-routes] -> addRoute: ROUTE PROBLEM', relativeFilePath, route);
		return;
	}
 
	Iif (!_.has(route, 'url')) {
		Iif (verbose) console.log('[@owservable/fastify-auto-routes] -> addRoute: MISSING URL WARNING', relativeFilePath);
		_.set(route, 'url', '/');
	}
 
	const {url} = route;
	Iif (!_.startsWith(_.toLower(url), relativeFilePath)) _.set(route, 'url', fixUrl(url, relativeFilePath));
 
	route.method = fixRouteMethod(route, verbose);
	route.schema = fixSchema(route);
	route.schema.tags = fixTags(route, relativeFilePath);
 
	fastify.route(route);
	RoutesMap.add(route.method, route.url);
	Iif (verbose) console.log('[@owservable/fastify-auto-routes] -> addRoute: Added route', route.method, route.url, '\n');
};
export default addRoute;