https://github.com/chinesedfan/parrot-mocker
Intercept requests to the mock server
https://github.com/chinesedfan/parrot-mocker
chrome-extension fetch-mock jsonp-mock mock-responses parrot-mocker xhr-mock
Last synced: 29 days ago
JSON representation
Intercept requests to the mock server
- Host: GitHub
- URL: https://github.com/chinesedfan/parrot-mocker
- Owner: chinesedfan
- License: mit
- Created: 2017-03-29T08:28:16.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2022-11-24T11:32:01.000Z (over 2 years ago)
- Last Synced: 2025-04-10T14:46:14.479Z (about 1 month ago)
- Topics: chrome-extension, fetch-mock, jsonp-mock, mock-responses, parrot-mocker, xhr-mock
- Language: JavaScript
- Homepage: https://chrome.google.com/webstore/detail/parrotmocker/hdhamekapmnmceohfdbfelofidflfelm
- Size: 121 KB
- Stars: 8
- Watchers: 2
- Forks: 2
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# parrot-mocker [](https://badge.fury.io/js/parrot-mocker) [](https://travis-ci.org/chinesedfan/parrot-mocker) [](https://coveralls.io/github/chinesedfan/parrot-mocker?branch=master) [][license]
This package supports to intercept XHR/JSONP/Fetch requests and forward to the specified [mock server](https://github.com/chinesedfan/parrot-mocker-web).
## How to use
### Chrome plugin
Install from [Chrome web store](https://chrome.google.com/webstore/detail/parrotmocker/hdhamekapmnmceohfdbfelofidflfelm).
Or load the plugin by following Google's [development guide](https://developer.chrome.com/extensions/getstarted#unpacked). The plugin folder is `crx`.
If the development mode plugin is disabled by Chrome, you can uninstall and install it again.
### Standalone
```js
```
### Webpack
```js
// Modifiy webpack.config.js to prepend this package to dependencies of each entry
module.exports = {
entry: ['parrot-mocker', './yours/src/entry.js']
};
```### Node.js
We export a function to rewrite the request url.
#### wrapUrl(urlStr, options)
- urlStr `string` the url of the API request
- options
- reqType `string` the type of request, like `jsonp`
- pageUrl `string` the url of the page
- cookie `string` the cookie of the page request
- shouldSkip `function` skip rewritting if returns true. By default, no host or including `local` will be filtered. Its arguments are:
- host `string` the API hostFor example, let's make a simple middleware for Koa,
```js
var fetch = require('node-fetch');
var wrapUrl = require('parrot-mocker').wrapUrl;module.exports = function*(next) {
this.fetch = (url, options) => {
return fetch(wrapUrl(url, {
pageUrl: this.url,
cookie: this.header.cookie
}));
};yield* next;
};
```## License
[MIT][license]
## Acknowledgement
* [whatwg-fetch](https://www.npmjs.com/package/whatwg-fetch), fetch polyfill
* [mozilla-doc-cookies](https://www.npmjs.com/package/mozilla-doc-cookies), cookie utils
* [cortex-cookie-manager](https://github.com/cortexjs/cortex-cookie-manager), crx sample[license]: https://github.com/chinesedfan/parrot-mocker/blob/master/LICENSE