Comparing version 0.1.0 to 0.1.1
{ | ||
"name": "mpwrapper", | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"description": "Monkey Patch Wrapper that support multiple wrapping and unwrapping", | ||
@@ -16,3 +16,4 @@ "main": "build/src/index.js", | ||
"precompile": "tsc --version", | ||
"start": "webpack-dev-server -d --progress --colors --port 8091 --config webpack.config.js --hot --inline --host 0.0.0.0 --content-base example", | ||
"start": "webpack-dev-server -d --progress --colors --port 8091 --config webpack.config.js --no-inline --host 0.0.0.0 --content-base example", | ||
"start:node": "node example/node.js", | ||
"tdd": "yarn test -- --watch-extensions ts --watch", | ||
@@ -19,0 +20,0 @@ "test": "nyc ts-mocha -p tsconfig.json test/**/*.ts", |
118
README.md
@@ -15,11 +15,117 @@ # mpwrapper - Monkey Patch Wrapper | ||
## Usage | ||
## Usage in node.js | ||
```js | ||
const https = require('https'); | ||
const mpWrapper = require('mpwrapper'); | ||
const wrapper1 = mpWrapper.wrap(https, 'get', (original) => { | ||
return function () { | ||
console.log('before request 1'); | ||
const result = original.apply(this, arguments); | ||
console.log('after request 1'); | ||
return result; | ||
}; | ||
}); | ||
const wrapper2 = mpWrapper.wrap(https, 'get', (original) => { | ||
return function () { | ||
console.log('before request 2'); | ||
const result = original.apply(this, arguments); | ||
console.log('after request 2'); | ||
return result; | ||
}; | ||
}); | ||
function getData(callback) { | ||
const url = 'https://raw.githubusercontent.com/obecny/mpwrapper/master/package.json'; | ||
https.get(url, (response) => { | ||
let data = ''; | ||
response.on('data', (chunk) => { | ||
data += chunk; | ||
}); | ||
response.on('end', () => { | ||
const info = JSON.parse(data); | ||
console.log(`${info.name} - ${info.version}`); | ||
callback(); | ||
}); | ||
}); | ||
} | ||
getData(()=> { | ||
wrapper1.unwrap(); | ||
getData(()=> { | ||
wrapper2.unwrap(); | ||
getData(()=> { | ||
console.log('done'); | ||
}); | ||
}); | ||
}); | ||
// before request 1 | ||
// before request 2 | ||
// after request 2 | ||
// after request 1 | ||
// mpwrapper - 0.1.0 | ||
// before request 2 | ||
// after request 2 | ||
// mpwrapper - 0.1.0 | ||
// mpwrapper - 0.1.0 | ||
// done | ||
``` | ||
// first run to build | ||
npm run watch | ||
// run this and open browser at http://localhost:8091 | ||
npm run start | ||
## Usage in browser | ||
```js | ||
import * as mpWrapper from 'mpwrapper'; | ||
const wrapper1 = mpWrapper.wrap(XMLHttpRequest.prototype, 'open', (original) => { | ||
return function () { | ||
console.log('before request 1'); | ||
const result = original.apply(this, arguments); | ||
console.log('after request 1'); | ||
return result; | ||
}; | ||
}); | ||
const wrapper2 = mpWrapper.wrap(XMLHttpRequest.prototype, 'open', (original) => { | ||
return function () { | ||
console.log('before request 2'); | ||
const result = original.apply(this, arguments); | ||
console.log('after request 2'); | ||
return result; | ||
}; | ||
}); | ||
function getData(callback) { | ||
const url = 'https://raw.githubusercontent.com/obecny/mpwrapper/master/package.json'; | ||
const req = new XMLHttpRequest(); | ||
req.open('GET', url, true); | ||
req.send(); | ||
req.onload = function () { | ||
const info = JSON.parse(req.responseText); | ||
console.log(`${info.name} - ${info.version}`); | ||
callback(); | ||
}; | ||
} | ||
getData(()=> { | ||
wrapper1.unwrap(); | ||
getData(()=> { | ||
wrapper2.unwrap(); | ||
getData(()=> { | ||
console.log('done'); | ||
}); | ||
}); | ||
}); | ||
// before request 1 | ||
// before request 2 | ||
// after request 2 | ||
// after request 1 | ||
// mpwrapper - 0.1.0 | ||
// before request 2 | ||
// after request 2 | ||
// mpwrapper - 0.1.0 | ||
// mpwrapper - 0.1.0 | ||
// done | ||
``` | ||
Check the example at example/index.js | ||
@@ -26,0 +132,0 @@ ## License |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22571
138