Giter VIP home page Giter VIP logo

ajax-fake's Introduction

ajax-fake

English|简体中文

We can use ajax-fake to intercept native ajax to do something hacking, such as mocking ajax response, status, delay and so on.

Example

import { fake, unfake } from 'ajax-fake'

const mockData = [
  {
    path: '/api/v1/article/list',
    method: 'GET',
    response: `{"success": true, "data": "hello", "code": 0}`,
  },
]

fake({
  // XMLHttpRequest can's be written if true
  force: true,
  // custom request match
  onRequestMatch: ({ requestMethod, requestUrl }) => {
    // find matched item
    const matchedItem = mockData.find((item) => {
      const { path, method } = item
      return requestMethod.toUpperCase() === method.toUpperCase() && requestUrl === path
    })
    if (matchedItem) {
      return {
        // ajax-fake will simulate ajax request if matched is true
        matched: true,
        response: matchedItem.response,
        // ajax-fake has intercepted request when mathced, we can also send a real request by sendRealXhr
        // note that there are two requests with sendRealXhr true, one for simulate, another for real request
        // the simulate one will not appear in Chrome Network panel, the real request is additional, we still handle request result with simulate one
        // the option intents to make request more tricky
        sendRealXhr: true,
        status: 200,
        delay: 2000,
      }
    } else {
      return {
        // send real ajax request if not matched
        matched: false,
      }
    }
  },
})

// cancel ajax request intercept
unfake()

ajax-fake's People

Contributors

jcanno avatar hans000 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.