npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mockjs-fetch

v2.0.0

Published

Let your mock.js support fetch.

Downloads

78

Readme

mockjs-fetch

鉴于Mock.js不支持拦截fetch发起的ajax,本模块即为Mock.js的补充。安装:

npm i mockjs-fetch --save

有2种使用方式,可以搭配mock.js一起使用,也可以独立使用,两种方式各有区别:

  • 搭配mock.js使用时完整继承mock.js的各种语法,功能更强大,但是鉴于mock.js在upload场景有一些至今未解决的bug,使用时有一些不太方便;
  • 独立使用,只拦截fetch、不对XMLHttpRequest做拦截,不支持'id|+1'等mockjs专属语法,适合只需要对fetch做一些简单mock的场景;

!!!请注意,线上环境务必注释mock相关代码的引入,避免对线上产生一些不必要的兼容性问题!!!

搭配mock.js一起使用

只需加2行代码就可以让你的mock.js支持fetch

import Mock from 'mockjs';
// 1.x 导入方式
// import mockFetch from 'mockjs-fetch';
// 2.x 导入方式
import { mockFetch } from 'mockjs-fetch';
mockFetch(Mock);

兼容Mock.js以下语法:

Mock.setup({timeout: 400});
Mock.setup({timeout: '200-400'});

完整示例:

import Mock from 'mockjs';
// 1.x 导入方式
// import mockFetch from 'mockjs-fetch';
// 2.x 导入方式
import { mockFetch } from 'mockjs-fetch';
mockFetch(Mock);

Mock.setup({
    timeout: '200-400', // mockFetch支持 mockjs 已有的 timeout 设置项
    debug: true, // mockFetch新增的设置项,如果开启,请求时会打印一些日志
});

Mock.mock(/testMockFetch\.json/, {
    code: 0,
    data: {
        total: 47,
        'data|10': [
            {
                name: '小茗同学',
                age: 18,
                address: '中国北京朝阳区'
            },
        ],
    },
});

const resp = await fetch('/aaa/testMockFetch.json').then(resp => resp.json());
console.log('输出结果:', resp);

独立使用

只对fetch进行拦截:

import { Mock } from 'mockjs-fetch';

Mock.setup({
    timeout: '200-400',
    debug: true, // 如果开启,请求时会打印一些日志
});

// 直接原样输出(注意不支持 'id|+1' 等特殊语法)
Mock.mock(/testMockFetch\.json/, {
    code: 0,
    data: {
        total: 47,
        dataList: [
            {
                name: '小茗同学',
                age: 18,
                address: '中国北京朝阳区'
            },
        ],
    },
});

// 第二个参数支持写function,根据入参返回不同的结果,params会自动根据GET或POST取合适的值
Mock.mock(/testMockFetch2\.json/, ({url, params }) => {
    if (parasm.a === 1) {
        return {code: 500};
    }
    return {code: 0};
});

const resp = await fetch('/aaa/testMockFetch.json').then(resp => resp.json());
console.log('输出结果:', resp);
const resp2 = await fetch('/aaa/testMockFetch2.json').then(resp => resp.json());
console.log('输出结果:', resp2);