Installations
npm install webpack-api-mocker
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
11.14.0
NPM Version
6.9.0
Score
65.2
Supply Chain
85
Quality
67.7
Maintenance
50
Vulnerability
99.6
License
Releases
Contributors
Unable to fetch Contributors
Languages
JavaScript (49.16%)
TypeScript (43.6%)
HTML (5.18%)
CSS (2.06%)
Developer
Download Statistics
Total Downloads
261,411
Last Day
285
Last Week
1,815
Last Month
6,069
Last Year
51,180
GitHub Statistics
487 Stars
545 Commits
68 Forks
8 Watching
13 Branches
18 Contributors
Bundle Size
711.01 kB
Minified
263.68 kB
Minified + Gzipped
Package Meta Information
Latest Version
1.7.6
Package Id
webpack-api-mocker@1.7.6
Unpacked Size
19.35 kB
Size
6.99 kB
File Count
6
NPM Version
6.9.0
Node Version
11.14.0
Total Downloads
Cumulative downloads
Total Downloads
261,411
Last day
-36%
285
Compared to previous day
Last week
-4%
1,815
Compared to previous week
Last month
90.6%
6,069
Compared to previous month
Last year
20.1%
51,180
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
Rename 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
⚠️ No wildcard asterisk - use parameters instead *
(.*)
, suport v1.7.3+
Delayed Responsev
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 mockerFile = ['./mock/index.js']; 2// or 3// const mockerFile = './mock/index.js'; 4apiMocker(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/index.js
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/index.js')) 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/index.js'), { 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
12 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 10
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
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 existing vulnerabilities detected
Reason
packaging workflow detected
Details
- Info: Project packages its releases by way of GitHub Actions.: .github/workflows/ci.yml:8
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/ci.yml:1
- Info: no jobLevel write permissions found
Reason
Found 0/20 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:14: update your workflow using https://app.stepsecurity.io/secureworkflow/jaywcjlove/mocker-api/ci.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:15: update your workflow using https://app.stepsecurity.io/secureworkflow/jaywcjlove/mocker-api/ci.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/ci.yml:21: update your workflow using https://app.stepsecurity.io/secureworkflow/jaywcjlove/mocker-api/ci.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/ci.yml:41: update your workflow using https://app.stepsecurity.io/secureworkflow/jaywcjlove/mocker-api/ci.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/ci.yml:49: update your workflow using https://app.stepsecurity.io/secureworkflow/jaywcjlove/mocker-api/ci.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/ci.yml:55: update your workflow using https://app.stepsecurity.io/secureworkflow/jaywcjlove/mocker-api/ci.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/ci.yml:58: update your workflow using https://app.stepsecurity.io/secureworkflow/jaywcjlove/mocker-api/ci.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/ci.yml:66: update your workflow using https://app.stepsecurity.io/secureworkflow/jaywcjlove/mocker-api/ci.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/ci.yml:74: update your workflow using https://app.stepsecurity.io/secureworkflow/jaywcjlove/mocker-api/ci.yml/master?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/ci.yml:33
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 7 third-party GitHubAction dependencies pinned
- Info: 0 out of 1 npmCommand dependencies pinned
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
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 10 are checked with a SAST tool
Score
6
/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 MoreOther packages similar to webpack-api-mocker
mocker-api
This is dev support mock RESTful API.
webpack-mock-server
Mocks api requests for webpack-dev-server with hot-replacement
mocker-api-path
This is dev support mock RESTful API.
webpack-mocker
webpack-mocker is a webpack-dev-server middleware that creates mocks for REST APIs. It will be helpful when you try to test your application without the actual REST API server.