{"size": 1984, "ext": "js", "max_stars_repo_path": "src/routes/index.js", "max_stars_repo_name": "manju4ever/hapi-moleculer-starter", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/routes/index.js", "max_issues_repo_name": "manju4ever/hapi-moleculer-starter", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/routes/index.js", "max_forks_repo_name": "manju4ever/hapi-moleculer-starter", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 26.4533333333, "max_line_length": 76, "alphanum_fraction": 0.5463709677}
import Joi from '@hapi/joi';

const enableAuthHeader = routes => routes.map(route => {
    if (route.config.auth && route.config.auth === 'jwt') {
      const modded_route = {
        ...route,
        config: {
          ...route.config,
          validate: {
            ...route.config.validate,
            headers: Joi.object({
              Authorization: Joi.string().description(
                'Authorization header containing the JSON Web Token',
              ),
            }).options({
              allowUnknown: true,
            }),
          },
        },
      };
      return modded_route;
    }
    return route;
  });

// Auto Load All Routes with file *.route.js from the routes/ folder
const AllRoutesAsObject = ((fileExtn = '.route.js') =>
  require('fs')
    .readdirSync(__dirname)
    .filter(fileName =>
      new RegExp(`${fileExtn}$`, 'ig').test(fileName),
    )
    .map(fileName => fileName.split('.')[0])
    .reduce((defaultExportObj, nextFileName) => {
      try {
        return {
          ...defaultExportObj,
          [nextFileName]: require(`${__dirname}/${nextFileName}${fileExtn}`)
            .default,
        };
      } catch (err) {
        throw err;
      }
    }, {}))('.route.js');

const AllRoutes = Object.values(AllRoutesAsObject).reduce(
  (finalRoutesArr = [], nextRoutesObj = []) => {
    if (nextRoutesObj.__proto__ === [].__proto__) {
      return [...finalRoutesArr, ...nextRoutesObj];
    }
    if (nextRoutesObj.__proto__ === {}.__proto__) {
      return [...finalRoutesArr, nextRoutesObj];
    }
    return finalRoutesArr;
  },
  [],
);

const IndexRoute = {
  path: '/',
  method: 'GET',
  config: {
    description: 'Root of all beauty !',
    tags: ['api'],
  },
  handler: async (request, h) =>
    h.response({
      message: 'Hey there buddy ! Wanna work with us ?',
      writeTo: 'manju.desappa@gmail.com',
      callMeOn: '+91759769413',
    }),
};

export default [IndexRoute, ...enableAuthHeader(AllRoutes)];
