添加mock

This commit is contained in:
LittleBoy 2023-02-24 16:53:44 +08:00
parent 399e337ebf
commit 1c4dc4fa6e
6 changed files with 860 additions and 6 deletions

52
mock/user.mock.ts Normal file
View File

@ -0,0 +1,52 @@
import { MockHandler } from 'vite-plugin-mock-server'
type UserModel = {
username: string;
password: string;
avatar: string;
}
type LoginUserModel = {
token: string;
user: UserModel;
}
const loginUserList: LoginUserModel[] = []
const userMock: MockHandler[] = [
{
pattern: '/api/user/login',
method:'POST',
handle: (req, res) => {
const data = { code: 1 }
res.end(JSON.stringify(data))
}
},
{
pattern: '/api/test1/*',
handle: (req, res) => {
res.end('Hello world!' + req.url)
}
},
{
pattern: '/api/test1/users/{userId}',
handle: (req, res, pathVars) => {
const data = {
url: req.url,
pathVars: pathVars
}
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify(data))
}
},
{
pattern: '/api/test1/users/{userId}/{blogId}',
handle: (req, res, pathVars) => {
const data = {
url: req.url,
pathVars: pathVars
}
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify(data))
}
}
]
export default userMock

796
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -10,6 +10,8 @@
"type-check": "vue-tsc --noEmit"
},
"dependencies": {
"axios": "^1.3.4",
"less": "^4.1.3",
"pinia": "^2.0.32",
"vue": "^3.2.47",
"vue-router": "^4.1.6"
@ -22,6 +24,7 @@
"npm-run-all": "^4.1.5",
"typescript": "~4.7.4",
"vite": "^4.1.3",
"vite-plugin-mock-server": "^1.0.4",
"vue-tsc": "^1.1.5"
}
}

View File

@ -9,7 +9,7 @@ import HelloWorld from './components/HelloWorld.vue'
<div class="wrapper">
<HelloWorld msg="You did it!" />
<nav>
<RouterLink to="/">Home</RouterLink>
<RouterLink to="/about">About</RouterLink>

5
src/components/Intro.vue Normal file
View File

@ -0,0 +1,5 @@
<template>
</template>
<script setup lang="ts"></script>
<style></style>

View File

@ -3,10 +3,16 @@ import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import mockServer from 'vite-plugin-mock-server'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue(), vueJsx()],
plugins: [
vue(),
vueJsx(),
mockServer({
mockRootDir: './mock'
})],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url))