{"id":22260898,"url":"https://github.com/jcanno/ajax-fake","last_synced_at":"2025-07-28T08:32:30.478Z","repository":{"id":45027882,"uuid":"412001059","full_name":"Jcanno/ajax-fake","owner":"Jcanno","description":"Ajax Faker","archived":false,"fork":false,"pushed_at":"2022-01-13T12:36:18.000Z","size":54,"stargazers_count":9,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-11-10T20:45:57.193Z","etag":null,"topics":["ajax","fake","interceptor","requests"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/Jcanno.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2021-09-30T09:23:21.000Z","updated_at":"2024-04-13T08:50:44.000Z","dependencies_parsed_at":"2022-09-05T09:40:47.176Z","dependency_job_id":null,"html_url":"https://github.com/Jcanno/ajax-fake","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/Jcanno%2Fajax-fake","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jcanno%2Fajax-fake/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jcanno%2Fajax-fake/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Jcanno%2Fajax-fake/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Jcanno","download_url":"https://codeload.github.com/Jcanno/ajax-fake/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227886375,"owners_count":17834992,"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":["ajax","fake","interceptor","requests"],"created_at":"2024-12-03T09:10:41.500Z","updated_at":"2024-12-03T09:10:42.218Z","avatar_url":"https://github.com/Jcanno.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ajax-fake\n\nEnglish|[简体中文](https://github.com/Jcanno/ajax-fake/blob/master/README.zh-CN.md)\n\nWe can use `ajax-fake` to intercept native ajax to do something hacking, such as mocking ajax response, status, delay and so on.\n\n## Example\n\n```js\nimport { fake, unfake } from 'ajax-fake'\n\nconst mockData = [\n  {\n    path: '/api/v1/article/list',\n    method: 'GET',\n    response: `{\"success\": true, \"data\": \"hello\", \"code\": 0}`,\n  },\n]\n\nfake({\n  // XMLHttpRequest can's be written if true\n  force: true,\n  // custom request match\n  onRequestMatch: ({ requestMethod, requestUrl }) =\u003e {\n    // find matched item\n    const matchedItem = mockData.find((item) =\u003e {\n      const { path, method } = item\n      return requestMethod.toUpperCase() === method.toUpperCase() \u0026\u0026 requestUrl === path\n    })\n    if (matchedItem) {\n      return {\n        // ajax-fake will simulate ajax request if matched is true\n        matched: true,\n        response: matchedItem.response,\n        // ajax-fake has intercepted request when mathced, we can also send a real request by sendRealXhr\n        // note that there are two requests with sendRealXhr true, one for simulate, another for real request\n        // the simulate one will not appear in Chrome Network panel, the real request is additional, we still handle request result with simulate one\n        // the option intents to make request more tricky\n        sendRealXhr: true,\n        status: 200,\n        delay: 2000,\n      }\n    } else {\n      return {\n        // send real ajax request if not matched\n        matched: false,\n      }\n    }\n  },\n})\n\n// cancel ajax request intercept\nunfake()\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjcanno%2Fajax-fake","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjcanno%2Fajax-fake","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjcanno%2Fajax-fake/lists"}