1.5.0 • Published 7 years ago
parrot-mocker v1.5.0
parrot-mocker

This package supports to intercept XHR/JSONP/Fetch requests and forward to the specified mock server.
How to use
Chrome plugin
Install from Chrome web store.
Or load the plugin by following Google's development guide. The plugin folder is crx.
If the development mode plugin is disabled by Chrome, you can uninstall and install it again.
Standalone
<head>
<!-- Make sure to be loaded first -->
<script type="text/javascript" src="dist/parrot.js"></script>
</head>Webpack
// 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
stringthe url of the API request - options
- reqType
stringthe type of request, likejsonp - pageUrl
stringthe url of the page - cookie
stringthe cookie of the page request - shouldSkip
functionskip rewritting if returns true. By default, no host or includinglocalwill be filtered. Its arguments are:- host
stringthe API host
- host
- reqType
For example, let's make a simple middleware for Koa,
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
Acknowledgement
- whatwg-fetch, fetch polyfill
- mozilla-doc-cookies, cookie utils
- cortex-cookie-manager, crx sample