https://github.com/tbxark/rmock
Modify HTTP JSON responses without needing HTTP capture and MitM.
https://github.com/tbxark/rmock
cli-app http-mock
Last synced: about 2 months ago
JSON representation
Modify HTTP JSON responses without needing HTTP capture and MitM.
- Host: GitHub
- URL: https://github.com/tbxark/rmock
- Owner: TBXark
- License: mit
- Created: 2022-03-31T02:37:44.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2024-09-13T01:45:53.000Z (about 1 year ago)
- Last Synced: 2024-09-13T14:42:17.431Z (about 1 year ago)
- Topics: cli-app, http-mock
- Language: JavaScript
- Homepage:
- Size: 247 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# rmock
Modify the http json response, no need for http capture and MitM, and solutions of cross domain problems.
### Install
```shell
npm install -g https://github.com/TBXark/rmock.git
```### Usage
```
Usage: rmock [options]Options:
-p, --port port (default: 3000)
-t, --target target url (default: "https://api.github.com")
-tp, --targetPort target port
-c, --capture capture all request or none (default: "all")
-r, --router external router file (default: null)
-rh, --requestHeader show request header (default: false)
-rb, --requestBody show request body (default: true)
-res, --responseBody show response body (default: true)
-pj, --prettyJson print response json pretty (default: false)
-h --help read more information
```### Example
```js
// example.js// Import core modules using require
//
// const os = require('os');
// console.log(os.arch());async function register(router, utils, importModule) {
// Commonly used functions are injected by default
//
//
const {mapJSON, customMapper, redirect} = utils; // eslint-disable-line// Import custom modules using importModule
//
// get default export manually
const {default: fetch} = await importModule('node-fetch');
// import internal modules
// const log = await importModule('./log.mjs');// Example
//
//// 1. disable some api log
router.get('/', mapJSON(null, {canLog: false}));// 2. change response body
router.get('/status', mapJSON((res, ctx) => {
return {
...res,
inject: 'Hello World!!!',
};
}));// 3. async mapper
router.get('/users/:id', mapJSON(async (res, ctx) => {
const {id} = ctx.params;
const repos = await fetch(`https://api.github.com/users/${id}/repos`).then((res) => res.json());
return {
...res,
repos,
};
}));
}// use commonjs export
exports.register = register;```
```shell
rmock -p 3000 -t https://api.github.com -c all -r example.js
```