All files / src/functions add.fastify.routes.ts

100% Statements 46/46
100% Branches 25/25
100% Functions 3/3
100% Lines 34/34

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    2x 2x 2x         2x 2x       2x   2x         9x 9x   9x 13x 9x   9x 12x 12x 12x   8x 8x   8x 8x 8x   8x 8x 8x 2x 2x 4x     6x 6x       13x 9x 9x 1x     2x  
'use strict';
 
import * as fs from 'fs';
import * as path from 'path';
import {hrtime} from 'node:process';
 
import {IncomingMessage, Server, ServerResponse} from 'http';
import {FastifyInstance} from 'fastify';
 
import addRoute from './add.route';
import cleanRelativePath from './clean.relative.path';
 
let routesRootFolder: string;
 
const NS_PER_SEC: number = 1e9;
 
const addFastifyRoutes = (
	fastify: FastifyInstance<Server<typeof IncomingMessage, typeof ServerResponse>, IncomingMessage, ServerResponse<IncomingMessage>>,
	folder: string,
	verbose: boolean = false
): void => {
	if (verbose) console.log('\n[@owservable/fastify-auto-routes] -> addFastifyRoutes:', folder);
	if (!routesRootFolder) routesRootFolder = folder;
 
	const fileNames: string[] = fs.readdirSync(folder);
	const files: string[] = fileNames.filter((name) => !fs.lstatSync(path.join(folder, name)).isDirectory());
	if (verbose) console.log('[@owservable/fastify-auto-routes] -> addFastifyRoutes:', folder, `${files.length} files`);
 
	for (const file of files) {
		if (verbose) console.log('[@owservable/fastify-auto-routes] -> addFastifyRoutes: processing...', `${folder}/${file}`);
		const ext: string = path.extname(file);
		if (ext !== '.ts' && ext !== '.js') continue;
 
		const absoluteFilePath: string = path.join(folder, file);
		const relativeFilePath: string = cleanRelativePath(routesRootFolder, absoluteFilePath, ext);
 
		const start: number = Number(hrtime.bigint());
		if (verbose) console.log('[@owservable/fastify-auto-routes] -> addFastifyRoutes: loading file...', absoluteFilePath);
		const routes = require(absoluteFilePath);
 
		const time: number = Number(Number(hrtime.bigint()) - start) / NS_PER_SEC;
		if (verbose) console.log('[@owservable/fastify-auto-routes] -> addFastifyRoutes: loaded file', `[${time.toFixed(3)}s] ${folder}/${file}`);
		if (Array.isArray(routes)) {
			if (verbose) console.log('[@owservable/fastify-auto-routes] -> addFastifyRoutes:', absoluteFilePath, `${routes.length} routes`);
			for (const route of routes) {
				addRoute(fastify, route, relativeFilePath, verbose);
			}
		} else {
			if (verbose) console.log('[@owservable/fastify-auto-routes] -> addFastifyRoutes:', absoluteFilePath, '1 route');
			addRoute(fastify, routes, relativeFilePath, verbose);
		}
	}
 
	const folders: string[] = fileNames.filter((name: string) => fs.lstatSync(path.join(folder, name)).isDirectory());
	if (verbose) console.log('[@owservable/fastify-auto-routes] -> addFastifyRoutes:', folder, 'subfolders:', `${folders.length} subfolders`);
	for (const sub of folders) {
		addFastifyRoutes(fastify, path.join(folder, sub), verbose);
	}
};
export default addFastifyRoutes;