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

api-mocks

v1.0.2

Published

Middleware for mocking any api

Downloads

15

Readme

Api Mocks

Api-mocks is a simple and full customizable mock server from your API specifications. It's based on json-schema-faker to provide consistent and meaningful fake data to generate all sorts of payloads.

Usage

Api-mocks is based on two parts, routes definitions and server rendering. You can find server and definitions examples in /examples folder.

Api Specifications

Create

Create a new set of mocks with ApiMock instance

let routes = {
  '/members/X': {
    GET: membersXGetRoute
  },
  '/visits': {
    GET: visitsGetRoute
  },
  '/visits/X': {
    GET: visitGetRoute
  }
};

new ApiMocks({
  name: 'myapi', // optionnal name of your set
  routes, // list of routes
  patterns // optionnal rewrite patterns
});

Routes

A route is a name/verb object pattern defined by a Route instance

let routes = {
  '/members/X': { // root path is removed
    GET: membersXGetRoute,
    PUT: membersXPutRoute
  }
};

In this example membersXGetRoute is the combination of a content memberDefinition and a http satus code

const Route = require('api-mocks').Route;

const memberDefinition = require('./member-definition');

class MembersXGetRoute extends Route {
  constructor() {
    super(memberDefinition.getMock(), 200);
  }
}

module.exports = MembersXGetRoute;

Definitions

Definition is a json-schema-faker mock schema, you can find fake data generators here. Examples show you a JSONapi payload schema.

Patterns

You can rewrite some url with regexp pattern, it's usefull for url with resource id in there that are hard to mock otherwise. By default numbers in url are replace by a X.

This example create a set of patterns...

const Pattern = require('api-mocks').Pattern;

module.exports = [
  new Pattern({
    regexp: /\/visits\/last/,
    methods: ['GET'],
    path: url => url.replace(/last$/, 'X')
  })
];

...to use visits/X route definition for visits/last

let routes = {
  '/visits/X': {
    GET: visitGetRoute
  }
};

Servers

Api-mocks provides two examples of gulp server middleware (manipulate request and response data) implementations: browser-sync and gulp-connect

browser-sync

const serve = require('browser-sync');
serve({
  port: 3000,
  ...
  middleware: [
    {
      route: '/myapi', // api routes start for all your uris
      handle: routesMapping.middleware // API specifications (created by ApiMocks instance)
    }
  ]
});

gulp-connect

const server = require('gulp-connect');

server.server({
  port: 3000,
  ...
  middleware() {
    let mockMiddleware = (request, response, next) => {
      let apiTest = /^\/myapi/; // api routes start for all your uris
      let isApiUrl = apiTest.test(request.url); // check request url
      request.url = request.url.replace(apiTest, ''); // replace root url pattern
      // if api match use mocked API
      return isApiUrl && routesMapping.middleware(request, response, next) || next();
    };

    return [mockMiddleware];
  }
});

Demo

In order to display demo run

npm install

them you can run

gulp connect-browser-sync

or

gulp connect-gulp

Once the server is running you can type any of routes mapping url in the browser (http://localhost:3000/myapi/members/1736876) or use javascript in console debugger with fetch api.