1.3.0 • Published 5 months ago
http-mock-json v1.3.0
HTTP MOCK JSON
Allows to create a mock server and test the frontend without depending on the backend.
Installation and use 🔧
Install library.
npm install http-mock-json --save-dev
Run the command.
mock-server init
Follow the instructions as in the example image.
A mocks folder will be created that will contain a first endpoint.
Mock structure
Key Example Description endpoint data/animals
,data/animal/:parameter
API route httpVerb GET
,POST
GET
,POST
,PUT
,PATCH
,DELETE
nameResponse success
,error
,error-401
Response name that the mock will use responses A mock can have multiple responses (array), each identified with a name
.name Response name (unique) statusCode HTTP Status Codes headers Headers in json format body Response in json format Add the responses you want to simulate in the body. (You can change the mock response by changing the
nameResponse
).Execute command
mock-server start
Commands ⚙️
init
Create the folder that will contain the mocks.
mock-server init
Flag Default Description -p --path root
Indicates the location of the mocks in a specific folder. -m --mock true
Create a first mock. -s --script true
Add script to start the mock in the package.json file. Example:
mock-server init --path apps/folder1 --mock false --script false
start
Start mock server.
mock-server start
Flag Default Description -p --port 3500
Indicates the port where the mock will be executed -f --path root
Indicates the location of the mocks in a specific folder. Example:
mock-server start --port 3001 --path apps/folder1
add
Create a mock.
mock-server add
Flag Default Description -p --path root
Indicates the location of the mocks in a specific folder. Example:
mock-server add --path apps/folder1
Recommendations 📋
- Review the advanced examples.
- A single json file can contain many mocks.
- There can be many json files each with their respective mocks.
Advanced examples
{
"data/animals": {
"GET": {
"nameResponse": "AnimalsError",
"responses": [
{
"name": "AnimalsList",
"statusCode": "200",
"body": {
"example": "data"
}
},
{
"name": "AnimalsError",
"statusCode": "404",
"body": {
"example-error": "error"
}
}
]
},
"POST": {
"nameResponse": "AnimalsError",
"responses": [
{
"name": "AnimalsSave",
"statusCode": "201",
"body": {
"example": "data"
}
},
{
"name": "AnimalsError",
"statusCode": "404",
"body": {
"example-error": "error"
}
}
]
}
}
}
{
"data/animals/:id": {
"GET": {
"nameResponse": "AnimalsList",
"responses": [
{
"name": "AnimalsList",
"statusCode": "200",
"body": {
"example": "data"
}
},
{
"name": "AnimalsError",
"statusCode": "404",
"body": {
"example-error": "error"
}
}
]
},
"POST": {
"nameResponse": "AnimalsSave",
"responses": [
{
"name": "AnimalsSave",
"statusCode": "201",
"body": {
"example": "data"
}
},
{
"name": "AnimalsError",
"statusCode": "404",
"body": {
"example-error": "error"
}
}
]
}
},
"data/brands": {
"GET": {
"nameResponse": "BrandsList3",
"responses": [
{
"name": "BrandsList",
"statusCode": "200",
"body": {
"example": "data1"
}
},
{
"name": "BrandsList2",
"statusCode": "200",
"body": {
"example": "data2"
}
},
{
"name": "BrandsList3",
"statusCode": "200",
"body": {
"example": "data3"
}
}
]
}
}
}
License 📖
http-mock-json is MIT licensed.
Author ✒️
1.3.0
5 months ago
1.2.9
5 months ago
1.2.8
8 months ago
1.2.7
8 months ago
1.2.6
8 months ago
1.2.5
8 months ago
1.2.4
8 months ago
1.2.3
8 months ago
1.2.2
8 months ago
1.2.1
8 months ago
1.1.1
8 months ago
1.1.0
8 months ago
1.0.0
8 months ago
0.1.5
3 years ago
0.1.4
3 years ago
0.1.3
3 years ago
0.1.2
3 years ago
0.1.1
3 years ago
0.1.0
3 years ago