Skip to content

ar4mirez/hapi-pres

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

hapi-pres

npm version CI License: ISC

Autoload pre-requirements for HapiJS routes from a directory.

Requirements

  • Node.js >= 18
  • @hapi/hapi ^21

Installation

npm install @ar4mirez/hapi-pres

Usage

const Hapi = require('@hapi/hapi');

const init = async () => {
  const server = new Hapi.Server({ host: 'localhost', port: 3000 });

  await server.register({
    plugin: require('@ar4mirez/hapi-pres'),
    options: {
      dirname: '/pre'
    }
  });

  // Pre-requirements are now available via server.pre.*
  // Example: server.pre.myPreCheck(request, h)

  await server.start();
  console.log(`Server running at: ${server.info.uri}`);
};

init();

The plugin scans the configured directory for pre-requirement files and decorates the server with them under a configurable prefix. Each exported function in the scanned files becomes accessible via server.<prefix>.<filename>.

Example pre-requirement file (lib/pre/auth.js):

module.exports = async (request, h) => {
  // perform pre-check, e.g. token validation
  return { userId: 42 };
};

Using it in a route:

server.route({
  method: 'GET',
  path: '/protected',
  options: {
    pre: [{ method: server.pre.auth, assign: 'auth' }],
    handler: async (request, h) => {
      return { user: request.pre.auth.userId };
    }
  }
});

API / Options

Option Type Default Description
cwd string process.cwd() Base directory for scanning
dirname string '/pre' Subdirectory to scan for pre files
filter RegExp /^(.+)\.js$/ File filter regex
excludeDirs RegExp /^\.(git|svn)$/ Directories to exclude from scanning
recursive boolean true Scan subdirectories recursively
prefix string 'pre' Server decoration key

License

ISC © Angel Ramirez

About

Hapi plugin for autoload pre-requirements.

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors