npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hapi-mock

v2.0.0-beta.3

Published

A simple HAPI plug-in for mocking endpoints

Downloads

4

Readme

hapi-mock

Hapi server plugin for adding mock behavior to endpoints.

Build Status Coverage Status node code style Types License Status

Tested with Hapi 19/20 on Node 12/14/16.

Install

npm install hapi-mock

Purpose

This plugin provides a simple way to add mock behavior to Hapi endpoints. It is experimental at this point of time.

v2 is going away from jexl expressions towards es6 conditions. And it provides TypeScript support.

Usage

ES6

The plugin is registered with the Hapi server something like this:

const Hapi = require('@hapi/hapi');
const hapiMock = require('hapi-mock');

// ...

const mock = {
  plugin: hapiMock,
  options: { // registration options
    triggerByHeader: true, // this is the default
    headerName: 'x-hapi-mock', // this is the default
  },
};

await server.register(mock);

Your route configuration might look like this (referring to specific mock cases as a separate module):

const myMocks = require('./my-mocks'); // separate module

server.route({
  method: 'GET',
  path: '/example/{id}',
  options: {
    // ...
    plugins: {
      'hapi-mock': { // add mock behavior to this endpoint
        mocks: myMocks,
      },
    },
  },
  // ...
});

The file my-mocks.js provides some mock cases, e.g.,

module.exports = [{
  condition: ({ params }) => params.id === '4711',
  code: 418,
}, {
  condition: ({ query }) => query.id === 'foo',
  type: 'application/json',
  body: {
    bar: 'qux',
  },
}, {
  condition: ({ headers }) => headers['x-mock-case'] === '13',
  code: 200, // this is the default
  type: 'text/plain', // this is the default
  body: 'case 13',
  headers: {
    'x-mock-foo': 'bar',
  },
}];

condition gets the Hapi request object as an input parameter and returns true if the mock case is applicable. There are response parameters code (default 200 or 204), type (default text/plain), body (default empty), and headers (default {}) to specify the mock behavior. See below for details.

TypeScript

Types for TypeScript are provided. A typical mock file looks like this:

import * as hapiMock from 'hapi-mock';

export default [{
  title: 'case 4711',
  condition: ({ params }) => params.id === '4711',
  code: 418,
  body: '4711',
}, ...] as hapiMock.MockCase[];

Options

Register Options

In TypeScript it's hapiMock.RegisterOptions. Properties are:

  • triggerByHeader (optional, boolean, default is true) -- When to apply mocks (provided that an endpoint has mocks configured). If true, mocks are only applied when the request header x-hapi-mock is set (any value). If false mocks are always applied.

  • headerName (optional, string, default is x-hapi-mock) -- As request header, it must be set to activate mocks (unless triggerByHeader is false). As response header, it tells which mock case was actually applied (if any).

  • continueIfNoneMatch (optional, boolean, default is true) -- What should be done if mocks are configured with an endpoint but none is matching. If true, the request is passed on. If false, the response is status code 422 "Unprocessable Entity".

Route Options

In TypeScript it's hapiMock.RouteOptions. Properties are:

  • mocks (required, Array) -- List of mock cases for the respective endpoint.

  • continueIfNoneMatch (optional, boolean, default is the register option continueIfNoneMatch above) -- What should be done if mocks are configured with an endpoint but none is matching. If true, the request is passed on. If false, the response is status code 422 "Unprocessable Entity".

Mock Cases

In TypeScript it's hapiMock.MockCase. Properties are:

  • title (required, string) -- A descriptive title of the mock case.

  • condition (required, a function (request: Hapi.Request) => boolean) -- The condition when the mock case applies.

  • code (optional, number, default is 200 or 204) -- The response's status code.

  • type (optional, string, default is text/plain) -- The response's content-type.

  • body (optional, string or any object) -- The response's body.

  • headers (optional, an object) -- The response's specific headers.