Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

hapi-demo

Package Overview
Dependencies
Maintainers
8
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hapi-demo

hapi plugin to configure demo handlers

  • 0.0.5
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
8
Created
Source

hapi-demo Build Status

This Repo is a Hapi plugin, that adds the possibility to define demo handlers.

The demo handler will be called instead of the original handler, if certain conditions are met.

A possible use-case is to setup routes with demo data, but without adding additional code into the existing code-base.

install

npm i --save hapi-demo

usage

register

Set up a hapi server and register the plugin.

import Hapi from 'hapi';
import HapiDemo from 'hapi-demo'

const server = new Hapi.Server();
server.register(HapiDemo)
  .then(() => {
    console.log('plugin has been registered successfully');
  });

route setup

When setting up routes, configure the demo handler if necessary:

import Joi from 'joi';

server.route({
  method: 'GET',
  path: '/test',
  handler(request, reply) {
    return reply('test handler called');
  },
  config: {
    plugins: {
      'hapi-demo': {
        shouldApplyHandler(request) {
          return request.query.demo;
        },
        handler(request, reply) {
          return reply('demo handler called');
        }
      }
    },
    validate: {
      query: Joi.object().keys({
        demo: Joi.boolean()
      })
    }
  }
});

Now when calling the /test route without any query parameters the normal hander will be called, but when calling it with query parameters like /test?demo=true, then the demo handler will be called.

API

plugin options
route options
  • test: {function(request: Hapi.Request) : Boolean} - test function that returns if the demo-handler should be called
  • handler: {function(reqest: Hapi.Request, reply: Hapi.Reply)} - demo handler

Keywords

FAQs

Package last updated on 16 Jul 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc