{"id":19227541,"url":"https://github.com/sxei/mockjs-fetch","last_synced_at":"2025-04-21T01:31:36.810Z","repository":{"id":57300201,"uuid":"173110040","full_name":"sxei/mockjs-fetch","owner":"sxei","description":"加2行代码让你的mock.js支持fetch","archived":false,"fork":false,"pushed_at":"2023-02-01T11:19:51.000Z","size":10,"stargazers_count":13,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-01T07:54:01.226Z","etag":null,"topics":["fetch","mock","mockjs"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sxei.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-02-28T12:40:56.000Z","updated_at":"2024-07-22T02:40:49.000Z","dependencies_parsed_at":"2023-02-17T05:00:24.466Z","dependency_job_id":null,"html_url":"https://github.com/sxei/mockjs-fetch","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sxei%2Fmockjs-fetch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sxei%2Fmockjs-fetch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sxei%2Fmockjs-fetch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sxei%2Fmockjs-fetch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sxei","download_url":"https://codeload.github.com/sxei/mockjs-fetch/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249982512,"owners_count":21355704,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fetch","mock","mockjs"],"created_at":"2024-11-09T15:23:47.548Z","updated_at":"2025-04-21T01:31:36.485Z","avatar_url":"https://github.com/sxei.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mockjs-fetch\n\n鉴于`Mock.js`不支持拦截`fetch`发起的`ajax`，本模块即为`Mock.js`的补充。安装：\n\n```bash\nnpm i mockjs-fetch --save\n```\n\n有2种使用方式，可以搭配mock.js一起使用，也可以独立使用，两种方式各有区别：\n\n* 搭配`mock.js`使用时完整继承`mock.js`的各种语法，功能更强大，但是鉴于`mock.js`在upload场景有一些至今未解决的bug，使用时有一些不太方便；\n* 独立使用，只拦截fetch、不对`XMLHttpRequest`做拦截，不支持`'id|+1'`等mockjs专属语法，适合只需要对fetch做一些简单mock的场景；\n\n\u003e !!!请注意，线上环境务必注释mock相关代码的引入，避免对线上产生一些不必要的兼容性问题!!!\n\n## 搭配mock.js一起使用\n\n只需加2行代码就可以让你的`mock.js`支持`fetch`：\n\n```js\nimport Mock from 'mockjs';\n// 1.x 导入方式\n// import mockFetch from 'mockjs-fetch';\n// 2.x 导入方式\nimport { mockFetch } from 'mockjs-fetch';\nmockFetch(Mock);\n```\n\n兼容`Mock.js`以下语法：\n\n```js\nMock.setup({timeout: 400});\nMock.setup({timeout: '200-400'});\n```\n\n完整示例：\n\n```js\nimport Mock from 'mockjs';\n// 1.x 导入方式\n// import mockFetch from 'mockjs-fetch';\n// 2.x 导入方式\nimport { mockFetch } from 'mockjs-fetch';\nmockFetch(Mock);\n\nMock.setup({\n    timeout: '200-400', // mockFetch支持 mockjs 已有的 timeout 设置项\n    debug: true, // mockFetch新增的设置项，如果开启，请求时会打印一些日志\n});\n\nMock.mock(/testMockFetch\\.json/, {\n    code: 0,\n    data: {\n        total: 47,\n        'data|10': [\n            {\n                name: '小茗同学',\n                age: 18,\n                address: '中国北京朝阳区'\n            },\n        ],\n    },\n});\n\nconst resp = await fetch('/aaa/testMockFetch.json').then(resp =\u003e resp.json());\nconsole.log('输出结果：', resp);\n```\n\n## 独立使用\n\n只对fetch进行拦截：\n\n```js\nimport { Mock } from 'mockjs-fetch';\n\nMock.setup({\n    timeout: '200-400',\n    debug: true, // 如果开启，请求时会打印一些日志\n});\n\n// 直接原样输出（注意不支持 'id|+1' 等特殊语法）\nMock.mock(/testMockFetch\\.json/, {\n    code: 0,\n    data: {\n        total: 47,\n        dataList: [\n            {\n                name: '小茗同学',\n                age: 18,\n                address: '中国北京朝阳区'\n            },\n        ],\n    },\n});\n\n// 第二个参数支持写function，根据入参返回不同的结果，params会自动根据GET或POST取合适的值\nMock.mock(/testMockFetch2\\.json/, ({url, params }) =\u003e {\n    if (parasm.a === 1) {\n        return {code: 500};\n    }\n    return {code: 0};\n});\n\nconst resp = await fetch('/aaa/testMockFetch.json').then(resp =\u003e resp.json());\nconsole.log('输出结果：', resp);\nconst resp2 = await fetch('/aaa/testMockFetch2.json').then(resp =\u003e resp.json());\nconsole.log('输出结果：', resp2);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsxei%2Fmockjs-fetch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsxei%2Fmockjs-fetch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsxei%2Fmockjs-fetch/lists"}