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

100% Statements 19/19
100% Branches 11/11
100% Functions 3/3
100% Lines 17/17

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            2x   2x   2x         7x 7x 7x   7x 6x     6x 6x   6x 3x 3x 1x 2x     2x         2x  
'use strict';
 
import {FastifyInstance} from 'fastify';
import {IncomingMessage, Server, ServerResponse} from 'http';
 
import {ActionAsControllerInterface} from '@owservable/actions';
import {listSubfoldersFilesByFolderName} from '@owservable/folders';
 
import addActionRoute from './add.action.route';
 
const addActionRoutes: Function = async (
	fastify: FastifyInstance<Server<typeof IncomingMessage, typeof ServerResponse>, IncomingMessage, ServerResponse<IncomingMessage>>,
	root: string,
	folderName: string,
	verbose: boolean = false
): Promise<void> => {
	if (verbose) console.log('\n[@owservable/fastify-auto-routes] -> addActionRoutes:', folderName);
	const actionPaths: string[] = listSubfoldersFilesByFolderName(root, folderName);
 
	for (const actionPath of actionPaths) {
		if (verbose) console.log('[@owservable/fastify-auto-routes] -> Initializing route', actionPath);
 
		// tslint:disable-next-line:callable-types
		const ActionClass: new () => ActionAsControllerInterface = require(actionPath).default;
		const action: ActionAsControllerInterface = new ActionClass();
 
		if (typeof action.routes === 'function' && typeof action.asController === 'function') {
			const config = await action.routes();
			if (Array.isArray(config)) {
				config.forEach((conf) => {
					addActionRoute(fastify, action, conf, verbose);
				});
			} else {
				addActionRoute(fastify, action, config, verbose);
			}
		}
	}
};
export default addActionRoutes;