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

25% Statements 5/20
0% Branches 0/8
0% Functions 0/3
27.77% Lines 5/18

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    1x           1x   1x   1x                                                       1x  
'use strict';
 
import * as _ from 'lodash';
 
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> => {
	Iif (verbose) console.log('\n[@owservable/fastify-auto-routes] -> addActionRoutes:', folderName);
	const actionPaths: string[] = listSubfoldersFilesByFolderName(root, folderName);
 
	for (const actionPath of actionPaths) {
		Iif (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();
 
		Iif (typeof action.routes === 'function' && typeof action.asController === 'function') {
			const config = await action.routes();
			if (_.isArray(config)) {
				_.each(config, (conf) => {
					addActionRoute(fastify, action, conf, verbose);
				});
			} else {
				addActionRoute(fastify, action, config, verbose);
			}
		}
	}
};
export default addActionRoutes;