Code Monkey home page Code Monkey logo

vue-resource-mock's Introduction

Vue-Resource-Mock

Vue 1.x Vue 2.x Build Status npm version

This package requires no specific version of Vue but vue-resource. It works for both Vue/Vue2 as the time of this writing.

Copy and modified from https://github.com/airtonix/vue-resource-mock

Open to PR/Issue :)

Usage

Install

After npm install vue-resource-mock --save-dev,

import Vue from 'vue'
import VueResource from 'vue-resource'
import VueResourceMock from 'vue-resource-mock'
import MockData from 'path/to/file'   // MockData syntax down below

Vue.use(VueResource)

if (devMode) { // don't use it on your production build
  Vue.use(VueResourceMock, MockData, /* { silent: true/false } */) // after use vue-resource
}

Mock data

export default {

  // basic mock
  ['GET */path/to/resource'] (pathMatch, query, request, pass) {
    // before respond, you can check the path and query parameters with `pathMatch` & `query`
    // powered by 'url-pattern' & 'qs'
    // https://www.npmjs.com/package/url-pattern
    // https://www.npmjs.com/package/qs
    let body = { /* whatever */ }
    return {
      body: body,
      status: 200,
      statusText: 'OK',
      headers: { /*headers*/ },
      delay: 500, // millisecond
    }
  },

  // shorthand mock
  ['PUT */path/to/resource']: 200, // respond with only status code

  ['POST */path/to/resource']: { /*whatever*/ }, // respond with only body, status code = 200

  // example pass-through: if the query string variable 'mock=true' set, return mock, otherwise pass-through
  ['GET */path/to/resource'] (pathMatch, query, request, pass) {
    let runMock = (request.params && request.params.mock === 'true');
    let body = { /* whatever */ }
    return (runMock) ? {
        body: body,
        status: 200,
        statusText: 'OK',
        headers: { /*headers*/ }
      } : pass;
  }

}

License

MIT

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.