@unanim/mock-server
v3.0.0
Published
A package to server JSON files and test Post endpoints
Downloads
8
Readme
@unanim/mock-server
A very simple package to serve static mock JSONs and test post payloads and simulate server responses
Installation
# With yarn
yarn add -D @unanim/mock-server
# With pnpm
pnpm add -D @unanim/mock-server
Usage
Add a script to your project's package.json scripts section:
{
"scripts": {
...
"mock": "mock-server",
...
}
}
Then you can just run:
# With yarn
yarn mock
# With pnpm
pnpm mock
Behavior
Mock-server will spin up a server at the port 8000 (to change the default port see #options).
It will serve any JSON file present in the ~/public/json
folder in your project (to change the default path).
GET
Mock-server let you get any JSON available in you static JSON dir.
Example
Assuming you have this files:
/public/json/en/common.json
/public/json/en/page/home.json
This GET endpoints will return matching JSON files
http://localhost/en/common
http://localhost/en/common.json
http://localhost/en/page/home
http://localhost/en/page/home.json
POST
You can post data in the /post/{slug} endpoint and it will:
- Print in the console the payload (after handling formData and parsing JSON)
- Return the content in the JSON found at /public/json/post/{slug}.json
- Optionnaly, delay the response with the
delay
query param (in seconds, default: 0) - Optionnaly, return the status code with the
code
query param (default: 200 if found)
Example
Assuming you have this file in your project:
/public/json/post/newsletter.json
This POST endpoints will be available:
# POST
http://localhost/post/newsletter
http://localhost/post/newsletter?delay=2
http://localhost/post/newsletter?code=300
Options
Dir (default: public/json)
If your static JSON files are located in a different directory pass the dir argument:
mock-server --mockDir=static/json
Port (default: 8000)
If you wish to serve in a different port pass the port argument
mock-server --port=3000
© Made with love and lots of coffee by Unanim.studio