Coder Social home page Coder Social logo

mock-get's Introduction

mock-get

npm Travis license

โ†—๏ธ 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 is 0
  • 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

mock-get's People

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.