{"_id":"@booxood/mocker-api","_rev":"1-a93832fa03d23fcf1fb994178ed9f758","name":"@booxood/mocker-api","dist-tags":{"latest":"2.9.0"},"versions":{"2.9.0":{"name":"@booxood/mocker-api","version":"2.9.0","description":"This is dev support mock RESTful API.","bin":{"mocker":"lib/bin/mocker.js"},"main":"lib/index.js","typings":"lib/index.d.ts","repository":{"type":"git","url":"git+https://github.com/booxood/mocker-api.git"},"jest":{},"license":"MIT","author":{"name":"Kenny Wong","email":"wowohoo@qq.com"},"keywords":["express","webpack-dev-server","webpack-api-mocker","webpack","mocker-api","mock","mocker","api","fetch","react","create-react-app","TypeScript"],"dependencies":{"@babel/runtime":"7.14.5","@types/body-parser":"1.19.0","@types/detect-port":"1.3.0","@types/express":"4.17.12","@types/http-proxy":"1.17.6","@types/minimist":"1.2.1","@types/node":"14.17.2","body-parser":"1.19.0","chokidar":"3.5.1","clear-module":"4.1.1","colors-cli":"1.0.27","detect-port":"1.3.0","express":"4.17.1","http-proxy":"1.18.1","local-ip-url":"1.0.3","minimist":"1.2.5","path-to-regexp":"6.2.0"},"bugs":{"url":"https://github.com/booxood/mocker-api/issues"},"homepage":"https://github.com/booxood/mocker-api#readme","_id":"@booxood/mocker-api@2.9.0","_nodeVersion":"14.15.0","_npmVersion":"6.14.8","_npmUser":{"name":"booxood","email":"liucw.mail@gmail.com"},"dist":{"integrity":"sha512-r69dJX3SJFez5wPvG2hfwHP752y7jye5bza2fdUPKPPPSKVd++aswWhl6kaWDpGeF6YcLTB97m3hVcWz7+DMkQ==","shasum":"3a97a55a53535fb3fcd0b64782e2cf61ab21416a","tarball":"https://registry.npmjs.org/@booxood/mocker-api/-/mocker-api-2.9.0.tgz","fileCount":20,"unpackedSize":123111,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJgy3PFCRA9TVsSAnZWagAArEsP/1aBjSqdy+VflSHia/+p\nZUqbA0LONJbHUsaGusMwXM2qh37DPI5Tm9HXme72uL5hAr1lsOiBJz1NejC8\naq+5Em4u4ZLE8x2MiKlHiRK5ppw7b7eRZKHYbSYMCHYlNr7fF/Wk20Eyyven\nZI5ZKOpEpsS8zraDAaPecz5WOx5qbafsJ2plj4V4fqZw38lT8TIRokpWW7KF\ni4pEPJokJDQKI98CYOMD5PLEq/os94Qis+irKppBYb+30sKRTmzP9YChLGgD\nZviOUIV8T8l4CyPyqvoQCjaz3/kJU2WvMNM/9q6KPgb5z7b/9vcvLCZ6iKjE\nHhEaLGKGx9FAC7LwtIJmMnw3JFZG/Ut7EGj08TOtXBrIHzTNHMp2u/xudGZT\nLuTukfxvQXxNiLV/Tgy9YoWUL266mM5O2kr2PnD8v5YJdaePi1J5KldiFUMO\nRTd5HEwa94kwSMTycokB4sQJsTgZBumcKMejqpVUWbAu3hOVIM5VPjiprFOY\nDc14xWXcuYhPPGEMJgRHojeUrMJqgfeom/MKK1bxxygrQ/by0wdHK8gV7ide\nol3c46MI4/PuvRY27aU6BGykTWUhTQ93RkEv0LK5pci0yCUNsLt3XJd7zA49\nTNTVmW6DeCD1vVI2XIUzamdBmDbYYyeJ8xhBN9R5UanqbX+9RUNA329+7zwx\nCEKd\r\n=5qdE\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCuec7dgSlxKHVbyRUMNpse7h8GiCqfLAFYQ9h1CaVuWQIgYQnm/EQ2Knjm/nfK3ky1EY1KzYoKwBq7rjrgL0bh/Rk="}]},"directories":{},"maintainers":[{"name":"booxood","email":"liucw.mail@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/mocker-api_2.9.0_1623946180680_0.33352623940260173"},"_hasShrinkwrap":false}},"time":{"created":"2021-06-17T16:09:40.625Z","2.9.0":"2021-06-17T16:09:40.922Z","modified":"2022-04-04T20:11:59.568Z"},"maintainers":[{"name":"booxood","email":"liucw.mail@gmail.com"}],"description":"This is dev support mock RESTful API.","homepage":"https://github.com/booxood/mocker-api#readme","keywords":["express","webpack-dev-server","webpack-api-mocker","webpack","mocker-api","mock","mocker","api","fetch","react","create-react-app","TypeScript"],"repository":{"type":"git","url":"git+https://github.com/booxood/mocker-api.git"},"author":{"name":"Kenny Wong","email":"wowohoo@qq.com"},"bugs":{"url":"https://github.com/booxood/mocker-api/issues"},"license":"MIT","readme":"<p align=\"center\">\n  <a href=\"https://uiwjs.github.io/npm-unpkg/#/pkg/mocker-api/file/README.md\">\n    <img alt=\"Mocker API LOGO\" src=\"https://user-images.githubusercontent.com/1680273/105883915-5845a780-6042-11eb-8eee-614ba512a74a.png\">\n  </a>\n</p>\n\n\n<p align=\"center\">\n  <a href=\"https://github.com/jaywcjlove/mocker-api/actions\">\n    <img alt=\"Build & Deploy\" src=\"https://github.com/jaywcjlove/mocker-api/workflows/Build%20and%20Deploy/badge.svg\">\n  </a>\n  <a href=\"https://github.com/jaywcjlove/mocker-api/issues\">\n    <img alt=\"Issues\" src=\"https://img.shields.io/github/issues/jaywcjlove/mocker-api.svg\">\n  </a>\n  <a href=\"https://github.com/jaywcjlove/mocker-api/network\">\n    <img alt=\"Forks\" src=\"https://img.shields.io/github/forks/jaywcjlove/mocker-api.svg\">\n  </a>\n  <a href=\"https://github.com/jaywcjlove/mocker-api/stargazers\">\n    <img alt=\"Stars\" src=\"https://img.shields.io/github/stars/jaywcjlove/mocker-api.svg\">\n  </a>\n  <a href=\"https://uiwjs.github.io/npm-unpkg/#/pkg/mocker-api/file/README.md\">\n    <img src=\"https://img.shields.io/badge/Open%20in-unpkg-blue\" alt=\"Open in unpkg\">\n  </a>\n  <a href=\"https://gitee.com/jaywcjlove/mocker-api\">\n    <img alt=\"Release\" src=\"https://jaywcjlove.github.io/sb/ico/gitee.svg\">\n  </a>\n  <a href=\"https://www.npmjs.com/package/mocker-api\">\n    <img alt=\"npm version\" src=\"https://img.shields.io/npm/v/mocker-api.svg\">\n  </a>\n</p>\n\n<p align=\"center\">\n  <a href=\"https://github.com/jaywcjlove/mocker-api/blob/master/README-zh.md\">中文</a> · \n  <a href=\"#quick-start\">Quick Start</a> · \n  <a href=\"#usage\">Usage</a> · \n  <a href=\"#options\">Options</a> · \n  <a href=\"#delayed-response\">Delayed</a> · \n  <a href=\"#example\">Example</a> · \n  <a href=\"#license\">License</a>\n</p>\n\n`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.\n\n**Features:**  \n\n🔥 Built in support for hot Mocker file replacement.  \n🚀 Quickly and easily configure the API via JSON.  \n🌱 Mock API proxying made simple.  \n💥 Can be used independently without relying on [webpack](https://github.com/webpack/webpack) and [webpack-dev-server](https://github.com/webpack/webpack-dev-server).\n\n## Quick Start\n\n```bash\nmkdir mocker-app && cd mocker-app\n\n# Create a mocker configuration file based on rules\ntouch api.js\n\n# Global install dependent.\nnpm install mocker-api -g\n\n# Default port: 3721\nmocker ./api.js\n\n# Designated port\n# Run server at localhost:8000\nmocker ./api.js --host localhost --port 8000\n```\n\n## Installation\n\nyou can put it the `package.json` config as a current project dependency.\n\n```bash\nnpm install mocker-api --save-dev\n```\n\n## Usage\n\n`mocker-api` dev support mock, configured in `mocker/index.js`.\n\nyou can modify the [http-proxy](https://www.npmjs.com/package/http-proxy) options and add the event listeners by adding the httpProxy configuration\n\n```js\nconst proxy = {\n  // Priority processing.\n  // apiMocker(app, path, option)\n  // This is the option parameter setting for apiMocker\n  _proxy: {\n    proxy: {\n      // Turn a path string such as `/user/:name` into a regular expression.\n      // https://www.npmjs.com/package/path-to-regexp\n      '/repos/(.*)': 'https://api.github.com/',\n      '/:owner/:repo/raw/:ref/(.*)': 'http://127.0.0.1:2018',\n      '/api/repos/(.*)': 'http://127.0.0.1:3721/'\n    },\n    // rewrite target's url path. Object-keys will be used as RegExp to match paths.\n    // https://github.com/jaywcjlove/mocker-api/issues/62\n    pathRewrite: {\n      '^/api/repos/': '/repos/',\n    },\n    changeHost: true,\n    // modify the http-proxy options\n    httpProxy: {\n      options: {\n        ignorePath: true,\n      },\n      listeners: {\n        proxyReq: function (proxyReq, req, res, options) {\n          console.log('proxyReq');\n        },\n      },\n    },    \n  },\n  // =====================\n  // The default GET request.\n  // https://github.com/jaywcjlove/mocker-api/pull/63\n  '/api/user': {\n    id: 1,\n    username: 'kenny',\n    sex: 6\n  },\n  'GET /api/user': {\n    id: 1,\n    username: 'kenny',\n    sex: 6\n  },\n  'GET /api/user/list': [\n    {\n      id: 1,\n      username: 'kenny',\n      sex: 6\n    }, {\n      id: 2,\n      username: 'kenny',\n      sex: 6\n    }\n  ],\n  'GET /api/:owner/:repo/raw/:ref/(.*)': (req, res) => {\n    const { owner, repo, ref } = req.params;\n    // http://localhost:8081/api/admin/webpack-mock-api/raw/master/add/ddd.md\n    // owner => admin\n    // repo => webpack-mock-api\n    // ref => master\n    // req.params[0] => add/ddd.md\n    return res.json({\n      id: 1,\n      owner, repo, ref,\n      path: req.params[0]\n    });\n  },\n  'POST /api/login/account': (req, res) => {\n    const { password, username } = req.body;\n    if (password === '888888' && username === 'admin') {\n      return res.json({\n        status: 'ok',\n        code: 0,\n        token: \"sdfsdfsdfdsf\",\n        data: {\n          id: 1,\n          username: 'kenny',\n          sex: 6\n        }\n      });\n    } else {\n      return res.status(403).json({\n        status: 'error',\n        code: 403\n      });\n    }\n  },\n  'DELETE /api/user/:id': (req, res) => {\n    console.log('---->', req.body)\n    console.log('---->', req.params.id)\n    res.send({ status: 'ok', message: '删除成功！' });\n  }\n}\nmodule.exports = proxy;\n```\n\n## Options \n\n- [`proxy`](https://www.npmjs.com/package/path-to-regexp) => `{}` Proxy settings, Turn a path string such as `/user/:name` into a regular expression.\n- [`pathRewrite`](https://github.com/jaywcjlove/mocker-api/issues/62) => `{}` rewrite target's url path. Object-keys will be used as RegExp to match paths. [#62](https://github.com/jaywcjlove/mocker-api/issues/62)\n- `withFullUrlPath=false` => `Boolean` the proxy regular expression support full url path. if the proxy regular expression like `/test?a=1&b=1` can be matched. [#25](https://github.com/jaywcjlove/mocker-api/issues/25)\n- `priority` => `proxy` priority `proxy` or `mocker` [#151](https://github.com/jaywcjlove/mocker-api/issues/151)\n- `changeHost` => `Boolean` Setting req headers host.\n- `httpProxy` => `{}` Set the [listen event](https://github.com/nodejitsu/node-http-proxy#listening-for-proxy-events) and [configuration](https://github.com/nodejitsu/node-http-proxy#options) of [http-proxy](https://github.com/nodejitsu/node-http-proxy)    \n- [`bodyParserJSON`](https://github.com/expressjs/body-parser/tree/56a2b73c26b2238bc3050ad90af9ab9c62f4eb97#bodyparserjsonoptions) JSON body parser\n- [`bodyParserText`](https://github.com/expressjs/body-parser/tree/56a2b73c26b2238bc3050ad90af9ab9c62f4eb97#bodyparsertextoptions) Text body parser\n- [`bodyParserRaw`](https://github.com/expressjs/body-parser/tree/56a2b73c26b2238bc3050ad90af9ab9c62f4eb97#bodyparserrawoptions) Raw body parser\n- [`bodyParserUrlencoded`](https://github.com/expressjs/body-parser/tree/56a2b73c26b2238bc3050ad90af9ab9c62f4eb97#bodyparserurlencodedoptions) URL-encoded form body parser\n- `bodyParserConf` => `{}` bodyParser settings. eg： `bodyParserConf : {'text/plain': 'text','text/html': 'text'}` will parsed `Content-Type='text/plain' and Content-Type='text/html'` with `bodyParser.text`  \n- [`watchOptions`](https://github.com/paulmillr/chokidar#api) => `object` Options object as defined [chokidar api options](https://github.com/paulmillr/chokidar#api)\n- `header` => `{}` Access Control Allow options.\n  ```js\n  {\n    header: {\n      'Access-Control-Allow-Methods': 'POST, GET, OPTIONS, PUT, DELETE',\n    }\n  }\n  ```\n\n⚠️ No wildcard asterisk ~~`*`~~ - use parameters instead `(.*)`, support `v1.7.3+`\n\n## Delayed Response\n\nYou can use functional tool to enhance mock. [#17](https://github.com/jaywcjlove/webpack-api-mocker/issues/17)\n\n```js\nconst delay = require('mocker-api/lib/delay');\nconst noProxy = process.env.NO_PROXY === 'true';\n\nconst proxy = {\n  'GET /api/user': {\n    id: 1,\n    username: 'kenny',\n    sex: 6\n  },\n  // ...\n}\nmodule.exports = (noProxy ? {} : delay(proxy, 1000));\n```\n\n## apiMocker\n\n```js\napiMocker(app, mockerFilePath[, options])\napiMocker(app, Mocker[, options])\n```\n\nMulti entry `mocker` file watching\n\n```js\nconst apiMocker = require('mocker-api');\nconst mockerFile = ['./mock/index.js'];\n// or\n// const mockerFile = './mock/index.js';\napiMocker(app, mockerFile, options)\n```\n\n## Example\n\n### Using With Command\n\n[Base example](example/base)\n\n>⚠️  Not dependent on [webpack](https://github.com/webpack/webpack) and [webpack-dev-server](https://github.com/webpack/webpack-dev-server). \n\n```bash\n# Global install dependent.\nnpm install mocker-api -g\n# Run server\nmocker ./mocker/index.js\n```\n\nOr you can put it the `package.json` config as a current project dependency.\n\n```diff\n{\n  \"name\": \"base-example\",\n  \"scripts\": {\n+    \"api\": \"mocker ./mocker\"\n  },\n  \"devDependencies\": {\n+    \"mocker-api\": \"2.9.0\"\n  },\n+  \"mocker\": {\n+    \"port\": 7788\n+  },\n  \"license\": \"MIT\"\n}\n```\n\n### Using With Express\n\n[Express example](example/express)\n\nTo use api mocker on your [express](https://github.com/expressjs/express) projects.\n\n>⚠️  Not dependent on [webpack](https://github.com/webpack/webpack) and [webpack-dev-server](https://github.com/webpack/webpack-dev-server).\n\n```diff\nconst express = require('express');\n+ const path = require('path');\n+ const apiMocker = require('mocker-api');\n\nconst app = express();\n\n+ apiMocker(app, path.resolve('./mocker/index.js'))\napp.listen(8080);\n```\n\nor\n\n```diff\nconst express = require('express');\n+ const apiMocker = require('mocker-api');\n\nconst app = express();\n\n+ apiMocker(app, {\n+   'GET /api/user': {\n+     id: 1,\n+     sex: 0\n+   }\n+ });\n\napp.listen(8080);\n```\n\n### Using With Webpack\n\n[webpack example](example/webpack)\n\nTo use api mocker on your [Webpack](https://github.com/webpack/webpack) projects, simply add a setup options to your [webpack-dev-server](https://github.com/webpack/webpack-dev-server) options:\n\nChange your config file to tell the dev server where to look for files: `webpack.config.js`.\n\n```diff\nconst HtmlWebpackPlugin = require('html-webpack-plugin');\n+ const path = require('path');\n+ const apiMocker = require('mocker-api');\n\nmodule.exports = {\n  entry: {\n    app: './src/index.js',\n  },\n  output: {\n    filename: '[name].bundle.js',\n    path: path.resolve(__dirname, 'dist')\n  },\n+ devServer: {\n+   ...\n+   before(app){\n+     apiMocker(app, path.resolve('./mocker/index.js'), {\n+       proxy: {\n+         '/repos/(.*)': 'https://api.github.com/',\n+         '/:owner/:repo/raw/:ref/(.*)': 'http://127.0.0.1:2018'\n+       },\n+       changeHost: true,\n+     })\n+   }\n+ },\n  plugins: [\n    new HtmlWebpackPlugin({\n      template: path.resolve('./public/index.html'),\n      title: 'Webpack App Mocker API'\n    })\n  ],\n};\n```\n\nMust have a file suffix! For example: `./mocker/index.js`.\n\nLet's add a script to easily run the dev server as well: `package.json`\n\n```diff\n{\n  \"name\": \"development\",\n  \"version\": \"1.0.0\",\n  \"description\": \"\",\n  \"main\": \"webpack.config.js\",\n  \"scripts\": {\n    \"test\": \"echo \\\"Error: no test specified\\\" && exit 1\",\n+    \"start\": \"webpack serve --progress --mode development\",\n    \"build\": \"webpack --mode production\"\n  },\n  \"keywords\": [],\n  \"author\": \"\",\n  \"license\": \"MIT\",\n  \"devDependencies\": {\n    \"html-webpack-plugin\": \"4.5.0\",\n    \"mocker-api\": \"2.9.0\",\n    \"webpack\": \"5.22.0\",\n    \"webpack-cli\": \"4.5.0\",\n    \"webpack-dev-server\": \"3.11.2\"\n  }\n}\n```\n\nMock API proxying made simple.\n\n```diff\n{\n  before(app){\n+   apiMocker(app, path.resolve('./mocker/index.js'), {\n+     proxy: {\n+       '/repos/(.*)': 'https://api.github.com/',\n+     },\n+     changeHost: true,\n+   })\n  }\n}\n```\n\n### Using With create-react-app\n\n[create-react-app example](example/create-react-app)\n\nTo use api mocker on your [create-react-app](https://github.com/facebook/create-react-app/blob/3f699fd08044de9ab0ce1991a66b376d3e1956a8/docusaurus/docs/proxying-api-requests-in-development.md) projects. create [`src/setupProxy.js`](https://github.com/jaywcjlove/mocker-api/blob/64a093685b05c70ab0ddcf3fd5dbede7871efa8a/example/create-react-app/src/setupProxy.js#L1-L11) and place the following contents in it:\n\n```diff\n+ const apiMocker = require('mocker-api');\n+ const path = require('path');\n\nmodule.exports = function(app) {\n+  apiMocker(app, path.resolve('./mocker/index.js'), {\n+    proxy: {\n+      '/repos/(.*)': 'https://api.github.com/',\n+    },\n+    changeHost: true,\n+  });\n};\n```\n\n```diff\n{\n  .....\n  \"devDependencies\": {\n+    \"mocker-api\": \"2.9.0\"\n  },\n  ....\n}\n```\n\n### Development\n\n```shell\n$ yarn install\n$ yarn run build\n$ yarn run watch\n$ yarn run test\n```\n\n## License\n\n[MIT © Kenny Wong](https://github.com/jaywcjlove/mocker-api/blob/master/LICENSE)\n","readmeFilename":"README.md"}