mock-get
v1.2.1
Published
Isomorphic light mock HTTP get request and return Promise
Downloads
2
Readme
mock-get
:arrow_upper_right: Isomorphic light mock HTTP get request and return Promise, support both node and browser environment
Install
$ npm install --save mock-get
Usage
// ES6
import get from 'mock-get';
// node
const get = require('mock-get');
get(delay, mockData, hasError);
delay
milliseconds you want to delay your request, default value is0
mockData
json object will be resolve in the Promise (optional)hasError
default is false, set to true if you want the Promise to reject (optional)
Use second parameter as boolean to set hasError
if you do not have your own mock data.
Mock Request
get(1000)
.then(res => console.log(res))
.catch(err => console.error(err));
will log the response data
Fire an Error
get(1000, true)
.then(res => console.log(res))
.catch(err => console.error(err));
will log error message
Use your own mock data
get(1000, { name: 'foo', value: 'bar' })
.then(res => console.log(res));
import myMockData from './myMockData';
get(1000, myMockData)
.then(res => console.log(res));
Default Response
This is how default response look like, you can use your own mock data if you want.
[{
"id": 1,
"name": "Bruce Banner",
"title": "Hulk",
"email": "[email protected]",
"gender": "Male",
"date": "12/18/1969",
"age": 46
}, {
"id": 2,
"name": "Steve Rogers",
"title": "Captain America",
"email": "[email protected]",
"gender": "Male",
"date": "7/4/1918",
"age": 98
}, {
"id": 3,
"name": "Natalia Romanoff",
"title": "Black Widow",
"email": "[email protected]",
"gender": "Female",
"date": "11/22/1984",
"age": 31
}, {
"id": 4,
"name": "Tony Stark",
"title": "Iron Man",
"email": "[email protected]",
"gender": "Male",
"date": "5/29/1970",
"age": 46
}, {
"id": 5,
"name": "Clint Barton",
"title": "Hawkeye",
"email": "[email protected]",
"gender": "Male",
"date": "1/7/1971",
"age": 45
}]
License
MIT