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

instant-mock-rest-api

v12.0.0

Published

Quick and Easy web API mock server.

Downloads

44

Readme

Build and Push to npm registry

Change the application version in package.json and build + push to npm repo

npm login

npm run build

npm push

Build Status codecov npm version

instant-mock

instant-mock is a quick and easy web API mock server.

Installing globally

Installation via npm:

npm install -g instant-mock

Usage

mkdir mymock
cd mymock
instant-mock init
instant-mock

You can open http://localhost:3000 to view the instant-mock web console.

All mock API is mounted on http://localhost:3000/mock. Please try GET to http://localhost:3000/mock/users by curl or web browser. It is sample mock API created by instant-mock init.

Configuration

Servce configuration is wrote on server.yml

http:
  host: localhost
  port: 3000

socket:
  host: localhost
  port: 3010

Creating your mocks

API definition

You can create mock definition file to mock directory. Mock API URL is auto generated by directory path. mock/api-name/@METHOD is mapped to METHOD: http://localhost:3000/mock/api-name METHOD can use get/post/put/patch/delete.

If you need route parameters, can use PARAM directory. mock/api-name/$id/@get is mapped to GET: http://localhost:3000/mock/api-name/:id

Request parser

Request for mock is parsed by user definition parser file. User definition parser file name start with "parser-" and format is yaml or js.

Try create a parser file below, and access to http://localhost:3000/mock/books after restart the instant-mock.

/mock/books/@get/parser-default.yml:

status: 200                   # Response status code.
headers:                      # Response headers.
  Content-Type: application/text
rawBody: 'test body'          # Response body.

Define the response body to other file

You can define response body to a any file.

./mock/books/@get/body.json:

{
  "key": "value"
}

./mock/books/@get/parser-default.yml:

status: 200
headers:
  Content-Type: application/json
body: 'body.json'    # Response body file.

Change a reponse by request

Parser file can define multiple resonse for switing by request. Define request parsing rule to if, and response to then.

Try create a parser file below, and access to http://localhost:3000/mock/books/:id after restart the instant-mock. If :id is "1" then response body is "user 1", and if it is "3" then response is 404.

./mock/books/$id/@get/parser-default.yml:

- if:
    params:
      id: 1
  then:
    rawBody: 'book 1'

- if:
    params:
      id: 2
  then:
    rawBody: 'book 2'

- then:
    status: 404

if is can use params/query/body, and it is "and" condition.

  • params: Route parameter.
  • query: Query string parameter.
  • body: Parsed json body parameter.

Advanced parser

YAML format parser is support simple rule only. Use a js parser if you need more advanced rules.

./mock/shelves/:id/@get/parser-default.js:

exports.default = function (req) {
  return {
    status: 200,
    headers: {
      'content-type': 'application/text',
    },
    rawBody: 'your book is ' + req.params.id,
  };
};

Web console

You can open http://localhost:3000 to view the instant-mock web console. Web console can change parser, and show mock api access logs.

Web console

Publishing a new version to npm repo

Modify the app version in package.json

Run the command `npm run build`

npm login

npm publish