Installations
npm install mocker-api-path
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
10.16.0
NPM Version
6.9.0
Score
60
Supply Chain
84.2
Quality
65.7
Maintenance
50
Vulnerability
97.3
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (98.22%)
HTML (1.78%)
Developer
Download Statistics
Total Downloads
1,704
Last Day
1
Last Week
10
Last Month
27
Last Year
232
GitHub Statistics
1 Stars
164 Commits
1 Watching
3 Branches
Bundle Size
1.62 MB
Minified
560.83 kB
Minified + Gzipped
Package Meta Information
Latest Version
2.0.1
Package Id
mocker-api-path@2.0.1
Unpacked Size
59.19 kB
Size
13.80 kB
File Count
14
NPM Version
6.9.0
Node Version
10.16.0
Total Downloads
Cumulative downloads
Total Downloads
1,704
Last day
0%
1
Compared to previous day
Last week
400%
10
Compared to previous week
Last month
17.4%
27
Compared to previous month
Last year
21.5%
232
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
mocker-api
mocker-api
that creates mocks for REST APIs. It will be helpful when you try to test your application without the actual REST API server.
⚠️ webpack-api-mocker => mocker-api
has become powerful and can be used independently of webpack. This is new namewebpack-api-mocker
mocker-api
.can still be used, New content will be posted onwebpack-api-mocker
mocker-api
.
Features:
🔥 Built in support for hot Mocker file replacement.
🚀 Quickly and easily configure the API via JSON.
🌱 Mock API proxying made simple.
💥 Can be used independently without relying on webpack and webpack-dev-server.
Quick Start
1mkdir mocker-app && cd mocker-app 2 3# Create a mocker configuration file based on rules 4touch api.js 5 6# Global install dependent. 7npm install mocker-api -g 8# Run server 9mocker ./api.js
Installation
you can put it the package.json
config as a current project dependency.
1npm install mocker-api --save-dev
Usage
mocker-api
dev support mock, configured in mocker/index.js
.
you can modify the http-proxy options and add the event listeners by adding the httpProxy configuration
1const proxy = { 2 // Priority processing. 3 // apiMocker(app, path, option) 4 // This is the option parameter setting for apiMocker 5 _proxy: { 6 proxy: { 7 '/repos/(.*)': 'https://api.github.com/', 8 '/:owner/:repo/raw/:ref/(.*)': 'http://127.0.0.1:2018' 9 }, 10 changeHost: true, 11 // modify the http-proxy options 12 httpProxy: { 13 options: { 14 ignorePath: true, 15 }, 16 listeners: { 17 proxyReq: function (proxyReq, req, res, options) { 18 console.log('proxyReq'); 19 }, 20 }, 21 }, 22 }, 23 // ===================== 24 'GET /api/user': { 25 id: 1, 26 username: 'kenny', 27 sex: 6 28 }, 29 'GET /api/user/list': [ 30 { 31 id: 1, 32 username: 'kenny', 33 sex: 6 34 }, { 35 id: 2, 36 username: 'kenny', 37 sex: 6 38 } 39 ], 40 'GET /api/:owner/:repo/raw/:ref/(.*)': (req, res) => { 41 const { owner, repo, ref } = req.params; 42 // http://localhost:8081/api/admin/webpack-mock-api/raw/master/add/ddd.md 43 // owner => admin 44 // repo => webpack-mock-api 45 // ref => master 46 // req.params[0] => add/ddd.md 47 return res.json({ 48 id: 1, 49 owner, repo, ref, 50 path: req.params[0] 51 }); 52 }, 53 'POST /api/login/account': (req, res) => { 54 const { password, username } = req.body; 55 if (password === '888888' && username === 'admin') { 56 return res.json({ 57 status: 'ok', 58 code: 0, 59 token: "sdfsdfsdfdsf", 60 data: { 61 id: 1, 62 username: 'kenny', 63 sex: 6 64 } 65 }); 66 } else { 67 return res.status(403).json({ 68 status: 'error', 69 code: 403 70 }); 71 } 72 }, 73 'DELETE /api/user/:id': (req, res) => { 74 console.log('---->', req.body) 75 console.log('---->', req.params.id) 76 res.send({ status: 'ok', message: '删除成功!' }); 77 } 78} 79module.exports = proxy;
Options
proxy
=>{}
Proxy settings.changeHost
=>{}
Setting req headers host.httpProxy
=>{}
Set the listen event and configuration of http-proxybodyParserJSON
JSON body parserbodyParserText
Text body parserbodyParserRaw
Raw body parserbodyParserUrlencoded
URL-encoded form body parserbodyParserConf
=>{}
bodyParser settings. eg:bodyParserConf : {'text/plain': 'text','text/html': 'text'}
will parsedContent-Type='text/plain' and Content-Type='text/html'
withbodyParser.text
rap
=>{}
rap configurationurl
=>string
rap api url, eg:http://rap2api.taobao.org
id
=>number
rap repository idappId
=>string
QTrade Private Rap need this paramappSecret
=>string
QTrade Private Rap need this param
⚠️ No wildcard asterisk - use parameters instead *
(.*)
, suport v1.7.3+
Delayed Response
You can use functional tool to enhance mock. #17
1const delay = require('mocker-api/utils/delay'); 2const noProxy = process.env.NO_PROXY === 'true'; 3 4const proxy = { 5 'GET /api/user': { 6 id: 1, 7 username: 'kenny', 8 sex: 6 9 }, 10 // ... 11} 12module.exports = (noProxy ? {} : delay(proxy, 1000));
apiMocker
1apiMocker(app, mocker[,proxy])
Multi entry mocker
file watching
1const apiMocker = require('mocker-api'); 2const mockerFile = ['./mock']; 3// or 4// const mockerFile = './mock'; 5apiMocker(app, mockerFile, proxy)
Using With Command
⚠️ Not dependent on webpack and webpack-dev-server.
1# Global install dependent. 2npm install mocker-api -g 3# Run server 4mocker ./mocker
Or you can put it the package.json
config as a current project dependency.
1{ 2 "name": "base-example", 3 "scripts": { 4+ "api": "mocker ./mocker" 5 }, 6 "devDependencies": { 7+ "mocker-api": "^1.6.4" 8 }, 9 "license": "MIT" 10}
Using With Express
⚠️ Not dependent on webpack and webpack-dev-server.
1const express = require('express'); 2+ const path = require('path'); 3+ const apiMocker = require('mocker-api'); 4 5const app = express(); 6 7+ apiMocker(app, path.resolve('./mocker')) 8app.listen(8080);
Using With Webpack
To use api mocker on your Webpack projects, simply add a setup options to your webpack-dev-server options:
Change your config file to tell the dev server where to look for files: webpack.config.js
.
1const HtmlWebpackPlugin = require('html-webpack-plugin'); 2+ const path = require('path'); 3+ const apiMocker = require('mocker-api'); 4 5module.exports = { 6 entry: { 7 app: './src/index.js', 8 print: './src/print.js' 9 }, 10 devtool: 'inline-source-map', 11+ devServer: { 12+ ... 13+ before(app){ 14+ apiMocker(app, path.resolve('./mocker'), { 15+ proxy: { 16+ '/repos/*': 'https://api.github.com/', 17+ '/:owner/:repo/raw/:ref/*': 'http://127.0.0.1:2018' 18+ }, 19+ changeHost: true, 20+ }) 21+ } 22+ }, 23 plugins: [ 24 new HtmlWebpackPlugin({ 25 title: 'Development' 26 }) 27 ], 28 output: { 29 filename: '[name].bundle.js', 30 path: require.resolve(__dirname, 'dist') 31 } 32};
Must have a file suffix! For example: ./mocker/index.js
.
Let's add a script to easily run the dev server as well: package.json
1 { 2 "name": "development", 3 "version": "1.0.0", 4 "description": "", 5 "main": "webpack.config.js", 6 "scripts": { 7 "test": "echo \"Error: no test specified\" && exit 1", 8+ "start": "webpack-dev-server --open", 9 "build": "webpack" 10 }, 11 "keywords": [], 12 "author": "", 13 "license": "MIT", 14 "devDependencies": { 15 .... 16 } 17 }
Mock API proxying made simple.
1{ 2 before(app){ 3+ apiMocker(app, path.resolve('./mocker/index.js'), { 4+ proxy: { 5+ '/repos/*': 'https://api.github.com/', 6+ }, 7+ changeHost: true, 8+ }) 9 } 10}
License
MIT © Kenny Wong
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
no SAST tool detected
Details
- Warn: no pull requests merged into dev branch
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
94 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-6chw-6frg-f759
- Warn: Project is vulnerable to: GHSA-v88g-cgmw-v5xw
- Warn: Project is vulnerable to: GHSA-whgm-jr23-g3j9
- Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw
- Warn: Project is vulnerable to: GHSA-qwcr-r2fm-qrc7
- Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg
- Warn: Project is vulnerable to: GHSA-x9w5-v3q2-3rhw
- Warn: Project is vulnerable to: GHSA-c6rq-rjc2-86v2
- Warn: Project is vulnerable to: GHSA-pxg6-pf52-xh8x
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
- Warn: Project is vulnerable to: GHSA-p28h-cc7q-c4fg
- Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c
- Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq
- Warn: Project is vulnerable to: GHSA-3wcq-x3mq-6r9p
- Warn: Project is vulnerable to: GHSA-vh7m-p724-62c2
- Warn: Project is vulnerable to: GHSA-r9p9-mrjm-926w
- Warn: Project is vulnerable to: GHSA-434g-2637-qmqr
- Warn: Project is vulnerable to: GHSA-49q7-c7j4-3p7m
- Warn: Project is vulnerable to: GHSA-977x-g7h5-7qgw
- Warn: Project is vulnerable to: GHSA-f7q4-pwc6-w24p
- Warn: Project is vulnerable to: GHSA-fc9h-whq2-v747
- Warn: Project is vulnerable to: GHSA-6h5x-7c5m-7cr7
- Warn: Project is vulnerable to: GHSA-rv95-896h-c2vc
- Warn: Project is vulnerable to: GHSA-qw6h-vgh9-j6wx
- Warn: Project is vulnerable to: GHSA-74fj-2j2h-c42q
- Warn: Project is vulnerable to: GHSA-pw2r-vq6v-hr8c
- Warn: Project is vulnerable to: GHSA-jchw-25xp-jwwc
- Warn: Project is vulnerable to: GHSA-cxjh-pqwp-8mfp
- Warn: Project is vulnerable to: GHSA-8r6j-v8pm-fqw3
- Warn: Project is vulnerable to: MAL-2023-462
- Warn: Project is vulnerable to: GHSA-vvj3-85vf-fgmw
- Warn: Project is vulnerable to: GHSA-pfq8-rq6v-vf5m
- Warn: Project is vulnerable to: GHSA-6x33-pw7p-hmpq
- Warn: Project is vulnerable to: GHSA-c7qv-q95q-8v27
- Warn: Project is vulnerable to: GHSA-qqgx-2p2h-9c37
- Warn: Project is vulnerable to: GHSA-78xj-cgh5-2h22
- Warn: Project is vulnerable to: GHSA-2p57-rm9w-gvfp
- Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h
- Warn: Project is vulnerable to: GHSA-6c8f-qphg-qjgp
- Warn: Project is vulnerable to: GHSA-76p3-8jx3-jpfq
- Warn: Project is vulnerable to: GHSA-3rfm-jhwj-7488
- Warn: Project is vulnerable to: GHSA-hhq3-ff78-jv3g
- Warn: Project is vulnerable to: GHSA-p6mc-m468-83gw
- Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9
- Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-vh95-rmgr-6w4m
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
- Warn: Project is vulnerable to: GHSA-fhjf-83wg-r2j9
- Warn: Project is vulnerable to: GHSA-92xj-mqp7-vmcj
- Warn: Project is vulnerable to: GHSA-wxgw-qj99-44c2
- Warn: Project is vulnerable to: GHSA-5rrq-pxf6-6jx5
- Warn: Project is vulnerable to: GHSA-8fr3-hfg3-gpgp
- Warn: Project is vulnerable to: GHSA-gf8q-jrpm-jvxq
- Warn: Project is vulnerable to: GHSA-2r2c-g63r-vccr
- Warn: Project is vulnerable to: GHSA-cfm4-qjh2-4765
- Warn: Project is vulnerable to: GHSA-x4jg-mjrx-434g
- Warn: Project is vulnerable to: GHSA-rp65-9cf3-cjxr
- Warn: Project is vulnerable to: GHSA-9wv6-86v2-598j
- Warn: Project is vulnerable to: GHSA-rhx6-c78j-4q9w
- Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp
- Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw
- Warn: Project is vulnerable to: GHSA-m6fv-jmcg-4jfg
- Warn: Project is vulnerable to: GHSA-h9rv-jmmf-4pgx
- Warn: Project is vulnerable to: GHSA-hxcc-f52p-wc94
- Warn: Project is vulnerable to: GHSA-cm22-4g7w-348p
- Warn: Project is vulnerable to: GHSA-4g88-fppr-53pp
- Warn: Project is vulnerable to: GHSA-4jqc-8m5r-9rpr
- Warn: Project is vulnerable to: GHSA-c9g6-9335-x697
- Warn: Project is vulnerable to: GHSA-vx3p-948g-6vhq
- Warn: Project is vulnerable to: GHSA-j44m-qm6p-hp7m
- Warn: Project is vulnerable to: GHSA-3jfq-g458-7qm9
- Warn: Project is vulnerable to: GHSA-r628-mhmh-qjhw
- Warn: Project is vulnerable to: GHSA-9r2w-394v-53qc
- Warn: Project is vulnerable to: GHSA-5955-9wpr-37jh
- Warn: Project is vulnerable to: GHSA-qq89-hq3f-393p
- Warn: Project is vulnerable to: GHSA-f5x3-32g6-xq36
- Warn: Project is vulnerable to: GHSA-4wf5-vphf-c2xc
- Warn: Project is vulnerable to: GHSA-46c4-8wrp-j99v
- Warn: Project is vulnerable to: GHSA-9m6j-fcg5-2442
- Warn: Project is vulnerable to: GHSA-hh27-ffr2-f2jc
- Warn: Project is vulnerable to: GHSA-rqff-837h-mm52
- Warn: Project is vulnerable to: GHSA-8v38-pw62-9cw2
- Warn: Project is vulnerable to: GHSA-hgjh-723h-mx2j
- Warn: Project is vulnerable to: GHSA-jf5r-8hm2-f872
- Warn: Project is vulnerable to: GHSA-wr3j-pwj9-hqq6
- Warn: Project is vulnerable to: GHSA-g78m-2chm-r7qv
- Warn: Project is vulnerable to: GHSA-c4w7-xm78-47vh
- Warn: Project is vulnerable to: GHSA-p9pc-299p-vxgp
- Warn: Project is vulnerable to: GHSA-896r-f27r-55mw
- Warn: Project is vulnerable to: GHSA-mh8j-9jvh-gjf6
- Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6
- Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3
Score
1.7
/10
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More