@moviecast/hapi-endpoint

2.1.2 • Public • Published

HAPI Endpoint Plugin

Build Status Current Version Coverage Status

A plugin for hapi to add versioned endpoints based on the file structure.

What it does

hapi-endpoint will scan directories to find versioned endpoints. It will automatically add prefixes for the version and the directory of the route file. So if you have a file in endpoints/v1/auth/login.js it will automatically prefix the route with v1/auth.

By default hapi-endpoint will scan any file in endpoints/v*, this can be changed in the options of the plugin.

As an extra hapi-endpoint will also check the version of the application and add shortcuts to routes without the version in the prefix of the route. So if your api is version 1 and you have the file endpoints/v1/auth/login.js with path /login it will be prefixed with v1/auth/login and /auth/login. It is also possible to add an api-version header which specifies which version of the api you want to use.

Installation

yarn add @moviecast/hapi-endpoint or npm install @moviecast/hapi-endpoint --save

Usage

Start by adding the plugin to your existing server, an example is shown below.

// src/server.js

const path = require('path');
const Hapi = require('hapi');
const server = new Hapi.Server();

(async() => {
    await server.register({
        plugin: require('@moviecast/hapi-endpoint'),
        options: {
            // The path to the endpoints directory (optional)
            path: path.join(__dirname, 'endpoints'), // Defaults to path.join(process.cwd(), 'endpoints')

            // Specify the valid versions here (required)
            validVersions: [1],

            // The current application version (required), used as fallback on non versioned routes
            version: 1,

            // An option to add a prefix before all your routes (optional)
            prefix: '/api', // Defaults to ''
        }
    });

    await server.start();

    console.info(`Server started at ${server.info.url}`);
})();

Your routes should follow the default route markup specified by hapi, an example route:

// src/endpoints/v1/auth/login.js

module.exports = {
    method: 'POST',
    path: '/login',
    handler: async () => {
        // Check username and passport

        // Do more stuff

        return {
            success: true
        }
    }
}

Package Sidebar

Install

npm i @moviecast/hapi-endpoint

Weekly Downloads

0

Version

2.1.2

License

MIT

Unpacked Size

8.64 kB

Total Files

7

Last publish

Collaborators

  • greydev
  • itsdizzy