@mocks-rs/mocks
v1.0.9
Published
Mock REST APIs from JSON with zero coding within seconds.
Maintainers
Readme
mocks
Mock REST APIs from JSON with zero coding within seconds.
Complete Documentation - For detailed usage, advanced features, and examples.
Install
npm install -g @mocks-rs/mocksOr use without installing:
npx @mocks-rs/mocks --helpQuick Start
1. Initialize a storage file
Create a JSON file using the init command:
npx @mocks-rs/mocks init storage.jsonOr if installed globally:
mocks init storage.jsonThis creates a storage.json file with sample data. Use the --empty option to create an empty structure:
npx @mocks-rs/mocks init --empty storage.jsonOr if installed globally:
mocks init --empty storage.json2. Run a REST API server
Start the mock server using your JSON file:
npx @mocks-rs/mocks run storage.jsonOr if installed globally:
mocks run storage.jsonExample JSON structure:
{
"posts": [
{ "id": "01J7BAKH37HPG116ZRRFKHBDGB", "title": "first post", "views": 100 },
{ "id": "01J7BAKH37GE8B688PT4RC7TP4", "title": "second post", "views": 10 }
],
"profile": { "id": "01J7BAQE1GMD78FN3J0FJCNS8T", "name": "mocks" }
}3. Use your mock API
This automatically creates REST endpoints:
# List all posts
curl http://localhost:3000/posts
# Get a specific post
curl http://localhost:3000/posts/01J7BAKH37HPG116ZRRFKHBDGB
# Get profile (singleton resource)
curl http://localhost:3000/profile
# Create a new post
curl -X POST http://localhost:3000/posts \
-H "Content-Type: application/json" \
-d '{"title": "new post", "views": 0}'
# Health check
curl http://localhost:3000/_hcAvailable Endpoints
For each resource in your JSON file, mocks automatically creates:
GET /{resource}- List all itemsGET /{resource}/{id}- Get specific itemPOST /{resource}- Create new itemPUT /{resource}/{id}- Replace entire itemPATCH /{resource}/{id}- Partial updateDELETE /{resource}/{id}- Delete itemGET /_hc- Health check (returns 204)
Documentation
For detailed information about advanced configuration, query parameters, filtering, and more features, visit the complete documentation.
Contributing
This project is open source. Visit the GitHub repository to contribute or report issues.
License
This project is licensed under the MIT license.
