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

jsoning-api

v1.0.0

Published

Mock rest api

Downloads

5

Readme

JSONing API

JSONing API is a versatile tool that allows you to mock REST APIs. It allows to easily simulate and test API behaviors, providing a dynamic environment to craft custom responses and logic.

Install

$ npm install jsoning-api

# Or clone the repository
$ git clone https://github.com/cyrilbois/jsoning-api.git
$ cd jsoning-api
$ npm install

Configuration

Create a db.json file

{
  {
    "products": [
      {
        "id": 1, "name": "T-shirt", "price": 19.99
      },
      {
        "id": 2, "name": "Jeans", "price": 49.99
      }
    ],
    "users": [
      {
        "id": 1, "username": "johndoe", "email": "[email protected]"
      },
      {
        "id": 2, "username": "janedoe", "email": "[email protected]"
      }
    ]
}
}

In this example, you have created 2 resources: "products" and "users" (2 objects for each resource).

Create a rules.json file (you can provide an empty array)

[
  {
    "input": { "method": "GET", "path": "/status/401" }, 
    "output": { "status": 401, "response": {"error": "demo error"} },
    "stop" : true
  }
]

With this rule, calling the resource /status/401 will return a 401 error.

Usage

Start the REST API service

$ npm start

Get a REST API

$ curl http://localhost:3000/products/1
{
    "id": 1,
    "name": "T-shirt",
    "price": 19.99
}

Routes

The REST API handles different HTTP methods for creating, retrieving, updating, and deleting resources

GET     /products     Returns all products
GET     /products/2   Returns the product with ID 2
POST    /products     Create a new product
GET     /products/2   Returns the product with ID 2
PUT     /products/2   Update the product with ID 2
PATCH   /products/2   Update partially the product with ID 2
DELETE  /products/2   Delete the product with ID 2

Pagination

  • page
  • limit
GET     /products?limit=5         Returns the first 5 products (Page defaults to 1)
GET     /products?page=2          Returns 10 products from the second page (default limit is 10)
GET     /products?page=2&limit=5  Returns 5 products from the second page (Page starts at 1)

Custom rules

Custom rules are defined using JSON. With these JSON-based rules, you can set conditions based on the request (such as HTTP method, headers, and payload) and specify the response (including HTTP status code and payload). This allows you to tailor the API's behavior to meet specific testing and development needs.

Rules are applied before the API execution (which reads, writes, or deletes data).

If multiple rules match the request, only one is executed (the first one found).

The HTTP status and payload defined by the rules take precedence over those set by the API.

Here is an example of a rule:

  {
    "input": { "method": "GET", "path": "/status/401" }, 
    "output": { "status": 401, "response": {"error": "demo error"} },
    "stop" : true
  }

The stop attribute indicates whether to halt after applying the rule or to continue with the API execution.

Input

The input object is used to represent the different criteria that can be tested in the request.

| Attribute | Description                                                 |
|-----------|-------------------------------------------------------------|
| headers   | The headers (Object: each header is an attribute)           |
| method    | The HTTP method ('GET', 'POST', 'PUT', 'PATCH', 'DELETE')   |
| path      | The pathname of the URL (e.g., /users/23).                  |
| payload   | The payload (object or string)                              |

Output

The output object is used to update the response if the rule match.

| Attribute | Description                                 |
|-----------|---------------------------------------------|
| status    | Sets the HTTP status (e.g., 200, 404, etc.) |
| response  | Sets the response payload                   |
| headers   | Sets the headers                            |

Tests

Launch tests

$ npm test

License

MIT License