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

simplefakeapi

v1.3.5

Published

Creates a simple fake REST API from a single json file.

Downloads

14

Readme

Fake REST API

Creates a simple fake REST API from a single json file.

  • Exporting to Postman as a collection with variables
  • Advance built-in queries
  • Functions for GET and POST endpoints
  • Deploying to VMs
  • Auth, Signout, and Refresh
  • Session management

Setup

  • Make sure you create a file named fakeAPI.json with content on your project's root folder. You can see the content of the file below.
  • Add script "fakeapi": "npx fakeapi" to package.json
  • npm run fakeapi

Check your APIs on http://localhost:4000/systemtest by default.

Your service will be run on http://localhost:4000

Display APIs

Base on your fakeAPI.json SETTINGS

http://localhost:{port}/{name}/{version} if you pass SETTINGS.version as empty or remove it path will be

http://localhost:{port}/{name}

Documentation

Create a file named fakeAPI.json with content same as below

Modify fakeAPI.json base on your API choice

{
    "GET": {
        "testfunc/{siteId}": {
            "function": "test"
        },
        "store/{id}/status": {
            "status": "Open",
            "storeId": "{id}",
            "address": "stores/{id}[address,city,state]"
        },
        "all": {
            "users": "users/{*}",
            "departments": "departments/{*}",
            "stores": "stores/{*}"
        },
        "usernames": {
            "users": "users/{*}[name]"
        },
        "itusers/{dip}": {
            "users": "users/{dip}[name]: Object"
        },
        "departments": {
            "departments": "departments/{*}[department]: Array"
        }
    },
    "POST": {
        "department/testpost/{employeeId}": {
            "function": "testpost",
            "body": {
                "startDate": "2021-02-18",
                "endDate": "2021-02-28"
            }
        },
        "department/employee/{employeeId}": {
            "queryonly": true,
            "result": {
                "some": "data",
                "somemore": "data here",
                "user": "users/{employeeId}"
            },
            "body": {
                "startDate": "2021-02-18",
                "endDate": "2021-02-28"
            }
        },
        "departments": {
            "result": {
                "message": "created"
            },
            "where": "departments/{*}",
            "body": {
                "id": 1,
                "department": "IT"
            }
        },
        "createUser": {
            "result": {
                "message": "created"
            },
            "where": "users/{*}",
            "body": {
                "id": 1,
                "name": "name",
                "lastName": "lastName",
                "username": "[email protected]",
                "password": "password"
            }
        }
     
    },
    "PUT": {
        "departments/{id}/name/{department}": {
            "result": {
                "message": "updated"
            },
            "where": "departments/{id,department}",
            "body": {
                "id": 1,
                "department": "HR"
            }
        },
        "updateUser/{lastName}/department/{dip}": {
            "result": {
                "message": "updated"
            },
            "where": "users/{dip,lastName}",
            "body": {
                "id": 1,
                "name": "Salih",
                "lastName": "Onder",
                "dip": 1
            }
        }
    },
    "DELETE": {
        "deleteUser/{id}": {
            "result": {
                "message": "deleted"
            },
            "where": "users/{id}"
        },
        "departments/{id}": {
            "result": {
                "message": "deleted"
            },
            "where": "departments/{id}"
        }
    },
    "ERROR": {
        "404": {
            "status": {
                "code": "404",
                "messages": [
                    {
                        "type": "Error",
                        "message": "Not found.."
                    }
                ]
            }
        },
        "500": {
            "status": {
                "code": "500",
                "messages": [
                    {
                        "type": "Error",
                        "message": "No response!"
                    }
                ]
            }
        }
    },
    "DATA": {
        "departments": {
            "1": {
                "id": 1,
                "department": "IT"
            },
            "2": {
                "id": 2,
                "department": "HR"
            },
            "3": {
                "id": 3,
                "department": "MARKETING"
            }
        },
        "users": [
            {
                "id": 1,
                "name": "Salih",
                "lastName": "Onder",
                "dip": 1,
                "username": "[email protected]",
                "password": "password",
                "companyid": 1,
                "employeeid": 1
            },
            {
                "id": 2,
                "name": "Sinan",
                "lastName": "Onder",
                "dip": 1,
                "username": "[email protected]",
                "password": "password",
                "companyid": 1,
                "employeeid": 2
            },
            {
                "id": 3,
                "name": "Semih",
                "lastName": "Onder",
                "dip": 1,
                "username": "[email protected]",
                "password": "password",
                "companyid": 1,
                "employeeid": 3
            }
            
            
        ],
        "stores": [
            {
                "id": 1,
                "address": "6301 Northwest Loop 410",
                "city": "San Antonio",
                "state": "TX",
                "zip": "78238",
                "mall": "Ingram Park Mall"
            },
            {
                "id": 2,
                "address": "2310 SW Military Dr",
                "city": "San Antonio",
                "state": "TX",
                "zip": "78224",
                "mall": "South Park Mall"
            },
            {
                "id": 3,
                "address": "2310 SW Military Dr",
                "city": "Lubbock",
                "state": "TX",
                "zip": "79414",
                "mall": "South Plains Mall"
            }
            
            
        ]
    },
    "SETTING": {
        "name": "systemtest",
        "version": "",
        "versionNumber": "1.0.0",
        "headers": [],
        "port": 4000
    }
}

GET

http://localhost:4000/systemtest/all

"GET": {
 "all": {
            "users": "users/{*}",
            "departments": "departments/{*}",
    }
}
  • GET users/{*}: List all users from fakeAPI.json --> DATA
  • GET users/{id}: List user with given id from fakeAPI.json --> DATA
  • GET users/{*}[name] : List all users and displays name only
  • GET users/{dip}[name]: Object : List users with given dip - department id as Object
  • GET departments/{*}[department]: Array: List all departments with names as Array

POST

http://localhost:4000/systemtest/createUser

"POST": {
    "createUser": {
        "result": { "message": "created" },
        "where": "users/{*}",
        "body": {
            "id": 1,
            "name": "name",
            "lastName": "lastName"
        }
    }
}
  • Post body should include all properties, and extra properties are optional
  • Where defines which section of data to add new data

PUT

http://localhost:4000/systemtest/users/1/lastname/Onder

"PUT": {
        "users/{dip}/lastname/{lastName}": {
            "result": { "message": "updated" },
            "where": "users/{dip,lastName}",
            "body": {
                "name": "updated name"
            }
        }
    },
  • Updates names of users who work for IT department and last name is Onder

DELETE

http://localhost:4000/systemtest/users/1/lastname/Onder

"DELETE": {
        "users/{dip}/lastname/{lastName}": {
            "result": { "message": "updated" },
            "where": "users/{dip,lastName}",
        }
    },
  • Deletes users who work for IT department and last name is Onder

OTHER RESPONSES

in order to make 404 etc responses, create a section under ERROR in fakeAPI.json as,

"404": {
            "code": "404",
            "messages": [
                {
                    "type": "Error",
                    "message": "Invalid Id"
                }
            ]
      }

then request it for example like /path:404

REQUEST EXAMPLES

// FETCH

fetch("http://localhost:4000/systemtest/all")
  .then((res) => res.json())
  .then((res) => console.log(res));

const credentials = "sinan:password";
fetch("http://localhost:4000/systemtest/authenticate", {
  headers: new Headers({
    Authorization: "Basic " + btoa(credentials),
  }),
})
  .then((res) => res.json())
  .then((res) => console.log(res));

fetch("http://localhost:4000/systemtest/createUser", {
  method: "POST",
  body: JSON.stringify({
    id: 6,
    name: "new user",
    lastName: "new lastName",
  }),
})
  .then((res) => res.json())
  .then((res) => console.log(res));

fetch("http://localhost:4000/systemtest/updateUser/2", {
  method: "PUT",
  body: JSON.stringify({
    name: "name1",
    lastName: "lastName1",
    password: "password1",
  }),
})
  .then((res) => res.json())
  .then((res) => console.log(res));

fetch("http://localhost:4000/systemtest/deleteUser/1", {
  method: "DELETE",
})
  .then((res) => res.json())
  .then((res) => console.log(res));

//AXIOS

axios("http://localhost:4000/systemtest/all").then((res) =>
  console.log(res.data)
);

const credentials = "sinan:password";
axios("http://localhost:4000/systemtest/authenticate", {
  headers: {
    Authorization: "Basic " + btoa(credentials),
  },
}).then((res) => console.log(res.data));

axios
  .post("http://localhost:4000/systemtest/createUser", {
    id: 7,
    name: "new user",
    lastName: "new lastName",
  })
  .then((res) => console.log(res.data));

axios
  .put("http://localhost:4000/systemtest/updateUser/2", {
    name: "name1",
    lastName: "lastName1",
    password: "password1",
  })
  .then((res) => console.log(res.data));

axios({
  method: "delete",
  url: "http://localhost:4000/systemtest/deleteUser/1",
});

AUTHENTICATION

/authenticate

Basic Authentication is supported. In order to add/remove/update username and password of a user, see DATA.users section

http://localhost:4000/systemtest/authenticate

It will return user info on success, including tokens field. tokens[0].auth is a JWT token that you should send on each request as Authorization header with value of 'Bearer ' + tokens[0].auth

"tokens": [	
		{
	    	   "auth": ""
		}	
	]

/signout In order to sign out, just add Authorization header and make the request. A successfull response will be

{"Signout successfully"}

/refresh In order to refresh your token, just add Authorization header and make the request. A successfull response will be

{"tokens":[{"auth":"eyJ0eXAi...","expiration":720}],"session":"","referenceId":"","status":""}

HEADERS

You can force each api to use certain headers on each request thru SETTING.headers

FUNCTIONS

If you need more complicated process to manupulate the data, you may use functions feature. It works only on GET and POST

Example

  • Create a function on functions.js like test

Any function takes 2 paramaters, data and params. All query and body parameters will be included.

const Functions = {
    test: (data, params) => {
        return `siteId ${params.siteId}`
    },
    testpost: (data, params) => {
        return `START DATE ${params.startDate} for EMPLOYEE employeeId ${params.employeeId}`
    }
}

module.exports = Functions;
  • on GET request modify endpoints like this on file fakeAPI.json
...
"GET": {
        "testfunc/{siteId}": {
            "function": "test"
        },
...
  • on POST request modify endpoints like this on file fakeAPI.json
...
"POST": {
"jobs/testpost/{employeeId}": {
            "function": "testpost",
            "body": {
                "startDate": "2021-02-18",
            }
        },
...

POSTMAN

You can automatically convert all APIs to postman collection with environment variables. You will need one of the environment variables at least.

  • Postman Collection
  • Local Environment Variables
  • Development Environment Variables

CLOUD

Set up on a AWS VM

  • Create an EC2 linux VM
  • Install Node
  • Create a folder named fakeapiexample
  • npm init to create the package.json
  • Install simpleake api package from a zip file or from repo
  • Create fakeAPI.json file and copy content from example content on this README file vim fakeAPI.json
  • go to service folder cd /lib/systemd/system
  • create service file vim fakeapi.service
  • Paste content below
[Unit]
Description=Simple fake API service
Documentation=https://www.npmjs.com/package/simplefakeapi
After=network.target

[Service]
Environment=NODE_PORT=4000
Type=simple
User=ubuntu
ExecStart=/usr/bin/script.sh
Restart=on-failure

[Install]
WantedBy=multi-user.target
  • Go to /usr/bin/ cd /usr/bin/
  • Create a shell file for service to use vim script.sh
  • Copy content below;
#!/bin/bash
cd /home/ubuntu/fakeapiexample
set +e
/home/ubuntu/.nvm/versions/node/v18.3.0/bin/npx fakeapi
set -e
  • systemctl daemon-reload
  • systemctl start fakapi
  • systemctl status fakapi
  • Open the port 4000 of VM

Author

License

MIT