Home

Awesome

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, passThrough) {
    // 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

    // to pass through this mock, call the 4th parameter as a function, the return value will be ignored
    passThrough()

    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

}

TODO

License

MIT