Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jcanno/ajax-fake
Ajax Faker
https://github.com/jcanno/ajax-fake
ajax fake interceptor requests
Last synced: about 1 month ago
JSON representation
Ajax Faker
- Host: GitHub
- URL: https://github.com/jcanno/ajax-fake
- Owner: Jcanno
- License: mit
- Created: 2021-09-30T09:23:21.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2022-01-13T12:36:18.000Z (almost 3 years ago)
- Last Synced: 2024-11-10T20:45:57.193Z (about 2 months ago)
- Topics: ajax, fake, interceptor, requests
- Language: TypeScript
- Homepage:
- Size: 52.7 KB
- Stars: 9
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ajax-fake
English|[简体中文](https://github.com/Jcanno/ajax-fake/blob/master/README.zh-CN.md)
We can use `ajax-fake` to intercept native ajax to do something hacking, such as mocking ajax response, status, delay and so on.
## Example
```js
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()
```