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-mock-up

v1.0.8

Published

Emulates REST API services by mapping API Request endpoints to predefined API Response values.

Downloads

5

Readme

api-mock-up

Emulates REST API services by mapping API Request endpoints to predefined API Response values.

Version

1.0.8

Installation

Using NPM:

npm install -D api-mock-up

or

npm install -g api-mock-up

to install it globally.

Usage

CLI Usage

api-mock-up[options]

Options:

| Options | Alias | Description | Type | Required | Default | | ------------ | ----- | --------------------------------------------------------- | --------- | ---------- | ------- | | --configFile | -f | The path to the API Configuration file | [string] | [required] | | | --port | -p | The port on which the server will listen for API requests | [number] | [optional] | 3000 | | --version | -v | Show version number | [boolean] | | | | --help | -h | Show help | [boolean] | | |

The API Configuration File

The API Configuration file is a JSON (or YAML) document that maps endpoint requests to their corresponding response values, and it is to be passed to the application as the --configFile (alias -f) launch parameter or as the CONFIG_FILE environment variable.

This JSON document must match the following JSON Schema:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "name": {
      "type": "string"
    },
    "description": {
      "type": "string",
    },
    "endPoints": {
      "type": "array",
      "uniqueItems": true,
      "items": {
        "$ref": "#/$defs/ServiceEndPoint"
      }
    }
  },

  "required": ["name", "endPoints"],

  "$defs": {
    "ServiceEndPointRequest": {
      "type": "object",
      "properties": {
        "path": {
          "type": "string"
        },
        "method": {
          "type": "string",
          "enum": ["GET", "PATCH", "POST" "PUT", "DELETE"]
        },
        "queryParams": {
          "type": "object"
        },
        "headers": {
          "type": "object"
        },
        "body": {
          "type": "object"
        }
      },
      "required": ["path"]
    },

    "ServiceEndPointResponse": {
      "type": "object",
      "properties": {
        "payload": {
          "anyOf": [
            { "type": "array" },
            { "type": "string" },
            { "type": "number" },
            { "type": "integer" },
            { "type": "boolean" },
            { "type": "object" },
            { "type": "null" },
          ],
        },
      },
      "required": ["payload"],
    }

    "ServiceEndPoint": {
      "type": "object",
      "properties": {
        "request": {
          "$ref": "#/$defs/ServiceEndPointRequest"
        },
        "response": {
          "$ref": "#/$defs/ServiceEndPointResponse",
        }
      },
      "required": ["request", "response"]
    },

    "ServiceError": {
      "type": "object",
      "properties": {
        "message": {
          "type": "string"
        },
        "code": {
          "type": "string"
        },
        "stack": {
          "type": "string"
        }
      },
      "required": ["message"]
    }
  }
}

As the JSON Schema above indicates, the API Configuration document consists of

  • name field - a string which is the name the service,
  • description field - an optional string which describes the service
  • endPoints field - an array which contains the collection of API endpoints to emulate.

Each API endpoint must contain a request field and a response field.

The request field must contain the path (or the url) of the API endpoint and the HTTP method, as one of "GET" | "PATCH" | "POST" | "PUT" | "DELETE" options. If the method is not provided, the "GET" option will be used by default.

Additionally, the request may contain optional fields like:

  • queryParams - a key-value pair map of URL Query parameters,
  • headers - a key-value pair map of HTTP Request Headers,
  • body - a key-value pair map of HTTP Request body

The response field, on the other hand, is an object containing the type of CRUD action performed (as string), the status code (as number), and the payload. The payload can be either an error, in case of failure, or, in case of success, the predefined value of any valid JSON type, such as string, number, boolean, object, null, etc.

Examples

Defining an API Configuration File example

Here is an example of API Configuration File in JSON format:

{
  "name": "demo",
  "description": "Api Mockup Demo",
  "endPoints": [
    {
      "request": {
        "path": "/api"
      },
      "response": {
        "payload": [
          {
            "value": "Hello!"
          },
          {
            "value": "Welcome!"
          }
        ]
      }
    },
    {
      "request": {
        "path": "/api/joke"
      },
      "response": {
        "payload": {
          "$ref": "https://v2.jokeapi.dev/joke/Programming"
        }
      }
    },
    {
      "request": {
        "path": "/api/jokes"
      },
      "response": {
        "payload": [
          {
            "$ref": "https://v2.jokeapi.dev/joke/Programming"
          },
          {
            "$ref": "https://v2.jokeapi.dev/joke/Programming"
          }
        ]
      }
    },
    {
      "request": {
        "path": "/api/admin",
        "method": "GET"
      },
      "response": {
        "payload": {
          "id": 1,
          "name": "Admin User"
        }
      }
    },
    {
      "request": {
        "path": "/api/users",
        "queryParams": {}
      },
      "response": {
        "payload": [
          {
            "id": 1,
            "name": "John"
          },
          {
            "id": 2,
            "name": "Jane Doe"
          }
        ]
      }
    },
    {
      "request": {
        "path": "/api/user",
        "method": "POST"
      },
      "response": {
        "payload": {
          "$ref": "test/responses/new-user.json"
        }
      }
    },
    {
      "request": {
        "path": "/api/user/3",
        "method": "PUT"
      },
      "response": {
        "payload": {
          "id": 3,
          "name": "Updated New User"
        }
      }
    },
    {
      "request": {
        "path": "/api/user/:id",
        "method": "PUT"
      },
      "response": {
        "payload": {
          "$ref": "test/responses/updated-user.json"
        }
      }
    },
    {
      "request": {
        "path": "/api/user/1",
        "method": "GET"
      },
      "response": {
        "payload": {
          "id": 1,
          "name": "John"
        }
      }
    },
    {
      "request": {
        "path": "/api/user/2",
        "method": "GET"
      },
      "response": {
        "payload": {
          "id": 2,
          "name": "Jane Doe"
        }
      }
    },
    {
      "request": {
        "path": "/api/user/:key",
        "method": "GET"
      },
      "response": {
        "payload": {
          "id": "xyz",
          "name": "Any One"
        }
      }
    }
  ]
}

And the same Configuration File in YAML format:

name: demo
description: Api Mockup Demo
endPoints:
  - request:
      path: "/api"
    response:
      payload:
        - value: Hello!
        - value: Welcome!
  - request:
      path: "/api/joke"
    response:
      payload:
        "$ref": https://v2.jokeapi.dev/joke/Programming
  - request:
      path: "/api/jokes"
    response:
      payload:
        - "$ref": https://v2.jokeapi.dev/joke/Programming
        - "$ref": https://v2.jokeapi.dev/joke/Programming
  - request:
      path: "/api/admin"
      method: GET
    response:
      payload:
        id: 1
        name: Admin User
  - request:
      path: "/api/users"
      queryParams: {}
    response:
      payload:
        - id: 1
          name: John
        - id: 2
          name: Jane Doe
  - request:
      path: "/api/user"
      method: POST
    response:
      payload:
        "$ref": test/responses/new-user.json
  - request:
      path: "/api/user/3"
      method: PUT
    response:
      payload:
        id: 3
        name: Updated New User
  - request:
      path: "/api/user/:id"
      method: PUT
    response:
      payload:
        "$ref": test/responses/updated-user.json
  - request:
      path: "/api/user/1"
      method: GET
    response:
      payload:
        id: 1
        name: John
  - request:
      path: "/api/user/2"
      method: GET
    response:
      payload:
        id: 2
        name: Jane Doe
  - request:
      path: "/api/user/:key"
      method: GET
    response:
      payload:
        id: xyz
        name: Any One

Note: If the payload contains the field $ref, the $ref is expected to be the path or the url to an external resource located in the local file system or on the web. All relative paths will be resolved based on the process.cwd() (the current working directory).

Launching the app with an API Configuration file

api-mock-up -f ~/config.json

Launching the app with an API Configuration file and a port number

api-mock-up -f ~/config.json -p 1234

Launching the app with another service in parallel

A module, such as concurrently, can help launch the api-mock-up service in parallel with another app development service, such as a React development server (in the case of a React app).

concurrently --kill-others "npm start" "api-mock-up -f configFile.json -p 9009"

The command above can easily be defined as a NPM script, such as the following

"start:dev": "concurrently --kill-others \"npm start\" \"api-mock-up -f ~/configFile.json -p 1234\"",

and included in the package.json file, as in the following example:

// package.json
{
  ...
  "scripts": {
    ...
    "start": "react-app-rewired start",
    "start:dev": "concurrently --kill-others \"npm start\" \"api-mock-up -f ~/configFile.json -p 1234\"",
    ...
  },
  ...
}

Request examples

Response examples for requests to an instance using the API Configuration file in the example above:

  • GET request
http://localhost:3000/

returns this documentation page.

  • GET request
http://localhost:3000/ping
//or
http://localhost:3000/api/ping

returns

{
  "action": "PING",
  "payload": {
    "message": "PING: PONG"
  },
  "status": 200
}
  • GET request
http://localhost:3000/api/user/1

returns

{
  "action": "DATA",
  "payload": {
    "id": 1,
    "name": "John"
  },
  "status": 404
}
  • GET request
http://localhost:3000/api/user/

returns

{
  "action": "DATA",
  "payload": {
    "error": "Resource not found"
  },
  "status": 404
}
  • POST request
http://localhost:3000/api/user/

returns

{
  "action": "DATA",
  "payload": {
    "id": 3,
    "name": "New User"
  },
  "status": 200
}