{"id":13417406,"url":"https://github.com/enjoycoding/vite-plugin-mock-server","last_synced_at":"2025-03-15T02:31:17.489Z","repository":{"id":47313982,"uuid":"379939032","full_name":"enjoycoding/vite-plugin-mock-server","owner":"enjoycoding","description":"A mock server plugin for Vite.","archived":false,"fork":false,"pushed_at":"2024-08-22T11:04:20.000Z","size":140,"stargazers_count":60,"open_issues_count":5,"forks_count":13,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-05T05:30:48.714Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/enjoycoding.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-06-24T13:45:09.000Z","updated_at":"2024-12-24T05:33:47.000Z","dependencies_parsed_at":"2024-01-07T18:05:07.551Z","dependency_job_id":"b6e8ff9f-0227-4805-8eda-8567ab65271e","html_url":"https://github.com/enjoycoding/vite-plugin-mock-server","commit_stats":{"total_commits":9,"total_committers":2,"mean_commits":4.5,"dds":"0.11111111111111116","last_synced_commit":"de3cbcdf35f7543b0f5c41d435a65d8e368a656c"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enjoycoding%2Fvite-plugin-mock-server","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enjoycoding%2Fvite-plugin-mock-server/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enjoycoding%2Fvite-plugin-mock-server/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enjoycoding%2Fvite-plugin-mock-server/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/enjoycoding","download_url":"https://codeload.github.com/enjoycoding/vite-plugin-mock-server/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243674807,"owners_count":20329164,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-30T22:00:36.592Z","updated_at":"2025-03-15T02:31:17.119Z","avatar_url":"https://github.com/enjoycoding.png","language":"TypeScript","readme":"# vite-plugin-mock-server\r\n\r\n[![npm][npm-img]][npm-url]\r\n\r\nProvide local mocks for [Vite].\r\n\r\nA mock server plugin for [Vite], developed based on TypeScript. \r\nAnd support using TypeScript and JavaScript to write Mock API. When the Mock API file \r\nis modified, it will be hot updated automatically. Support and compatibility \r\nwith ***[express.js](https://github.com/expressjs/)*** middlewares.\r\n\r\n## Install\r\n\r\n**node version:** \u003e=12.0.0\r\n\r\n**vite version:** \u003e=2.0.0\r\n\r\n```bash\r\n# if using npm\r\nnpm i vite-plugin-mock-server -D\r\n# if using yarn\r\nyarn add vite-plugin-mock-server -D\r\n```\r\n\r\n### Run example\r\n\r\n```bash\r\ncd ./example\r\nnpm install\r\nnpm run dev\r\n```\r\n\r\n## Usage\r\n\r\n- Config plugin in vite.config.ts, compatible with express.js middlewares.\r\n\r\n```ts\r\nimport { defineConfig } from 'vite'\r\nimport vue from '@vitejs/plugin-vue'\r\nimport bodyParser from 'body-parser'\r\nimport cookieParser from 'cookie-parser'\r\nimport mockServer from 'vite-plugin-mock-server'\r\n\r\nexport default defineConfig({\r\n  plugins: [\r\n    vue(),\r\n    mockServer({\r\n      logLevel: 'info',\r\n      middlewares: [\r\n        cookieParser(),\r\n        bodyParser.json(),\r\n        bodyParser.urlencoded(),\r\n        bodyParser.text(),\r\n        bodyParser.raw()\r\n      ]\r\n    })\r\n  ]\r\n})\r\n```\r\n\r\n- Or just use it with the default parameters, place your mocks in the folder \"mock\" with name that prefix *.mock.ts or *mock.js, The default api to mock is '/api/'\r\n```ts\r\nimport { defineConfig } from 'vite'\r\nimport vue from '@vitejs/plugin-vue'\r\nimport mockServer from 'vite-plugin-mock-server'\r\n\r\nexport default defineConfig({\r\n  plugins: [\r\n    vue(),\r\n    mockServer())\r\n  ]\r\n})\r\n```\r\n\r\n## Module exports\r\n\r\n- MockOptions\r\n\r\n`mockModules` Ignore manual configuration, it will be filled in automatically.\r\n\r\n```ts\r\nexport type MockOptions = {\r\n  logLevel?: 'info' | 'error' | 'off'\r\n  urlPrefixes?: string[]\r\n  mockJsSuffix?: string\r\n  mockTsSuffix?: string\r\n  mockRootDir?: string\r\n  mockModules?: string[]\r\n  noHandlerResponse404?: boolean\r\n  middlewares?: MockLayer[]\r\n  printStartupLog?: boolean\r\n}\r\n\r\n// default options\r\nconst options: MockOptions = {\r\n  logLevel: 'info',\r\n  urlPrefixes: [ '/api/' ],\r\n  mockRootDir: './mock',\r\n  mockJsSuffix: '.mock.js',\r\n  mockTsSuffix: '.mock.ts',\r\n  noHandlerResponse404: true,\r\n  mockModules: [],\r\n  middlewares: [],\r\n  printStartupLog: true\r\n}\r\n```\r\n\r\n- Request\r\n  \r\n```ts\r\ntype Request = Connect.IncomingMessage \u0026 { \r\n  body?: any, \r\n  params?: { [key: string]: string }, \r\n  query?: { [key: string]: string },\r\n  cookies?: { [key: string]: string },\r\n  session?: any\r\n}\r\n```\r\n\r\n- MockFunction\r\n  \r\n```ts\r\nexport type MockFunction = {\r\n  (\r\n    req: Request, \r\n    res: http.ServerResponse, \r\n    /** @deprecated in 2.0, use req.params **/\r\n    urlVars?: { [key: string]: string }\r\n  ): void\r\n}\r\n```\r\n\r\n- MockHandler\r\n\r\n```ts\r\nexport type MockHandler = {\r\n  pattern: string,\r\n  method?: string,\r\n  handle: MockFunction\r\n}\r\n```\r\n\r\n- MockLayer\r\n\r\n```ts\r\nexport type MockLayer = (\r\n    req: Request,\r\n    res: http.ServerResponse,\r\n    next: Connect.NextFunction\r\n) =\u003e void;\r\n```\r\n\r\n## Mock file examples\r\n\r\nThe `pattern` is an ant-style path pattern string, \r\nuse ***[@howiefh/ant-path-matcher](https://www.npmjs.com/package/@howiefh/ant-path-matcher)*** \r\nto match the `pattern` and `request URL`.\r\n\r\n```ts\r\n// example/mock/es.mock.ts\r\n\r\nimport { MockHandler } from '../../src'\r\n\r\nconst mocks: MockHandler[] = [\r\n  {\r\n    pattern: '/api/test1/1',\r\n    handle: (req, res) =\u003e {\r\n      res.end('Hello world!' + req.url)\r\n    }\r\n  },\r\n  {\r\n    pattern: '/api/test1/*',\r\n    handle: (req, res) =\u003e {\r\n      res.end('Hello world!' + req.url)\r\n    }\r\n  },\r\n  {\r\n    pattern: '/api/test1/users/{userId}',\r\n    handle: (req, res) =\u003e {\r\n      const data = {\r\n        url: req.url,\r\n        params: req.params,\r\n        query: req.query,\r\n        body: req.body\r\n      }\r\n      res.setHeader('Content-Type', 'application/json')\r\n      res.end(JSON.stringify(data))\r\n    }\r\n  },\r\n  {\r\n    pattern: '/api/test1/body/json',\r\n    method: 'POST',\r\n    handle: (req, res) =\u003e {\r\n      res.setHeader('Content-Type', 'application/json')\r\n\r\n      //req is incomingMessage which extends stream.Readable \r\n      // --\u003e https://nodejs.org/api/stream.html#readablereadsize\r\n      // res.end need to be within the function\r\n      // there is a size limit for the bodyString to get parsed \r\n      req.on('data', (bodyString: string) =\u003e { \r\n        let body: object = JSON.parse(bodyString)\r\n        res.end(JSON.stringify(body))\r\n      })\r\n    }\r\n  },\r\n]\r\n\r\nexport default mocks\r\n\r\n// example/mock/apis/es2.mock.ts\r\n\r\nimport { MockHandler } from 'vite-plugin-mock-server'\r\n\r\nexport default (): MockHandler[] =\u003e [\r\n  {\r\n    pattern: '/api/test2/1',\r\n    handle: (req, res) =\u003e {\r\n      res.end('Hello world!' + req.url)\r\n    }\r\n  },\r\n  {\r\n    pattern: '/api/test2/2',\r\n    handle: (req, res) =\u003e {\r\n      res.statusCode = 203\r\n      res.end('Hello world!' + req.url)\r\n    }\r\n  }\r\n]\r\n```\r\n\r\n```js\r\n// example/mock/cjs.mock.js\r\n\r\nmodule.exports = [\r\n  {\r\n    pattern: '/api/merchant1',\r\n    method: 'GET',\r\n    handle: (req, res) =\u003e {\r\n      res.end('merchant1:' + req.url)\r\n    }\r\n  },\r\n  {\r\n    pattern: '/api/merchant2',\r\n    method: 'GET',\r\n    handle: (req, res) =\u003e {\r\n      res.end('merchant2:' + req.url)\r\n    }\r\n  },\r\n  {\r\n    pattern: '/api/merchant2',\r\n    method: 'GET',\r\n    handle: (req, res) =\u003e {\r\n      res.end('merchant3:' + req.url)\r\n    }\r\n  }\r\n]\r\n\r\n// example/mock/apis/cjs2.mock.js\r\n\r\nmodule.exports = [\r\n  {\r\n    pattern: '/api/hello1',\r\n    method: 'GET',\r\n    handle: (req, res) =\u003e {\r\n      res.end('hello1:' + req.url)\r\n    }\r\n  },\r\n  {\r\n    pattern: '/api/hello2',\r\n    method: 'GET',\r\n    handle: (req, res) =\u003e {\r\n      res.end('hello2:' + req.url)\r\n    }\r\n  },\r\n  {\r\n    pattern: '/api/hello3',\r\n    method: 'GET',\r\n    handle: (req, res) =\u003e {\r\n      res.end('hello2:' + req.url)\r\n    }\r\n  }\r\n]\r\n```\r\n\r\n## License\r\n\r\nMIT\r\n\r\n[npm-img]: https://img.shields.io/npm/v/vite-plugin-mock-server.svg\r\n[npm-url]: https://npmjs.com/package/vite-plugin-mock-server\r\n[Vite]: https://vitejs.dev","funding_links":[],"categories":["Plugins","TypeScript"],"sub_categories":["Framework-agnostic Plugins"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fenjoycoding%2Fvite-plugin-mock-server","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fenjoycoding%2Fvite-plugin-mock-server","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fenjoycoding%2Fvite-plugin-mock-server/lists"}