All files / src/functions add.route.ts

31.57% Statements 6/19
0% Branches 0/9
0% Functions 0/1
40% Lines 6/15

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    2x 2x 2x   2x   2x                               2x  
'use strict';
 
import * as _ from 'lodash';
import fixUrl from './fix.url';
import RoutesMap from '../routes.map';
 
const METHODS: string[] = ['GET', 'HEAD', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'PATCH'];
 
const addRoute = (fastify: any, route: any, relativeFilePath: string, verbose: boolean = false): void => {
	Iif (!_.has(route, 'url')) _.set(route, 'url', '/');
 
	const {url} = route;
	Iif (!_.startsWith(_.toLower(url), relativeFilePath)) route.url = fixUrl(url, relativeFilePath);
 
	if (_.has(route, 'method')) route.method = _.toUpper(route.method);
	else _.set(route, 'method', 'GET');
 
	Iif (_.isPlainObject(route) && METHODS.includes(route.method)) {
		Iif (verbose) console.log('[@owservable/fastify-auto-routes] -> Initializing route', route.url);
 
		fastify.route(route);
		RoutesMap.add(route.method, route.url);
	}
};
export default addRoute;