Skip to content

mike-robertson/mock-my-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mock-my-api

A simple api mocking util for when you need something simple, quickly. It spins up a server using express.

Example

import mockApi from 'mock-my-api';

const myMock = Object.create(mockApi);
myMock.init({
  endpoints: [
    {
      path: '/path/with/:params',
      data: { test: 'myResponse' },
      method: 'GET',
    }, {
      path: '/path/again/:id',
      // You can also pass a function which behaves as a normal express callback for an endpoint.
      data: (req, res) => {
        res.json({ test: req.params.id });
      },
      delay: {  // number or { min: number, max: number } - default: 0 - unit: ms
        min: 500,
        max: 2000
      }
    }, {
      path: '/something',
      data: { test: 'something' },
      errorRate: 0.5, // 0 - 1
      errorResponse: { // { status: number, body: anything } - default: { status: 500, body: { error: 'error' } }
        status: 500, // number - default: 500
        body: { // body - default: { error: 'error' }
          error: 'Error!'
        }
      },
    }
  ],
  defaultHeaders: [
    {
      name: 'Access-Control-Allow-Origin',
      value: '*',
    },
  ],
});

myMock.start(3000); // Port number goes here. Defaults to 3001.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published