-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: 使用 mockm #4011
feat: 使用 mockm #4011
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,6 @@ node_modules | |
.nvmrc | ||
coverage | ||
CODEOWNERS | ||
.nitro | ||
.output | ||
|
||
|
||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/**/* |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* eslint-disable */ | ||
|
||
const { | ||
wrapApiData, | ||
useResponseSuccess, | ||
useResponseError, | ||
MOCK_USERS, | ||
MOCK_CODES, | ||
MOCK_MENUS, | ||
} = require(`../util.cjs`) | ||
|
||
/** @type {import('mockm/@types/config').Config} */ | ||
module.exports = util => { | ||
const { | ||
libObj: { mockjs }, | ||
} = util | ||
return { | ||
api: { | ||
'/api/auth/codes'(req, res) { | ||
const codes = MOCK_CODES.find((item) => item.username === req.username)?.codes ?? []; | ||
res.json(useResponseSuccess(codes)) | ||
}, | ||
'post /api/auth/login'(req, res) { | ||
const { password, username } = req.body; | ||
|
||
const findUser = MOCK_USERS.find( | ||
(item) => item.username === username && item.password === password, | ||
); | ||
|
||
if (!findUser) { | ||
return res.status(403).json(useResponseError('UnauthorizedException', '用户名或密码错误')) | ||
} | ||
|
||
const accessToken = Buffer.from(username).toString('base64'); | ||
res.json(useResponseSuccess({ | ||
accessToken, | ||
// TODO: refresh token | ||
refreshToken: accessToken, | ||
})); | ||
}, | ||
}, | ||
} | ||
} |
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* eslint-disable */ | ||
|
||
const { | ||
wrapApiData, | ||
useResponseSuccess, | ||
useResponseError, | ||
MOCK_USERS, | ||
MOCK_CODES, | ||
MOCK_MENUS, | ||
} = require(`../util.cjs`) | ||
|
||
/** @type {import('mockm/@types/config').Config} */ | ||
module.exports = util => { | ||
const { | ||
libObj: { mockjs }, | ||
} = util | ||
return { | ||
api: { | ||
'/api/menu/all'(req, res){ | ||
const menus = MOCK_MENUS.find((item) => item.username === req.username)?.menus ?? []; | ||
res.json(useResponseSuccess(menus)) | ||
|
||
}, | ||
}, | ||
} | ||
} |
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const { | ||
wrapApiData, | ||
useResponseSuccess, | ||
useResponseError, | ||
MOCK_USERS, | ||
MOCK_CODES, | ||
MOCK_MENUS, | ||
} = require(`../util.cjs`) | ||
|
||
/** @type {import('mockm/@types/config').Config} */ | ||
module.exports = util => { | ||
const { | ||
libObj: { mockjs }, | ||
} = util | ||
return { | ||
api: { | ||
'/api/user/info'(req, res) { | ||
const user = MOCK_USERS.find((item) => item.username === req.username); | ||
|
||
const { password: _pwd, ...userInfo } = user; | ||
res.json(useResponseSuccess(user)) | ||
}, | ||
}, | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"paths": {}, | ||
"disable": [] | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
openApiHistory/ | ||
request/ | ||
db.json | ||
httpHistory.json | ||
log.err.txt | ||
store.json |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"paths": {}, | ||
"disable": [] | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
const { | ||
wrapApiData, | ||
useResponseSuccess, | ||
useResponseError, | ||
} = require(`./util.cjs`) | ||
|
||
/** | ||
* 配置说明请参考文档: | ||
* https://hongqiye.com/doc/mockm/config/option.html | ||
* @type {import('mockm/@types/config').Config} | ||
*/ | ||
module.exports = util => { | ||
return { | ||
watch: [`./api/`], | ||
plugin: [], | ||
proxy: { | ||
'/': `http://www.httpbin.org/`, // 后端接口主域 | ||
}, | ||
api: { | ||
'use /'(req, res, next){ | ||
const noCheckList = [ | ||
`/`, | ||
`/public`, | ||
`/favicon.ico`, | ||
`/api/test`, | ||
`/api/status`, | ||
`/api/auth/login`, | ||
] | ||
if( req.path === `/` || noCheckList.some(item => { | ||
return item.startsWith(req.path) | ||
})) { | ||
next() | ||
} else { | ||
const token = req.get(`Authorization`) | ||
if (token) { | ||
const username = Buffer.from(token, 'base64').toString('utf8'); | ||
req.username = username; | ||
} else { | ||
return res.status(401).json(useResponseError('UnauthorizedException', 'Unauthorized Exception')) | ||
} | ||
next() | ||
} | ||
}, | ||
// 跳转到接口列表 | ||
'/api'(req, res){ | ||
const url = `http://127.0.0.1:${globalThis.config.testPort}/#/apiStudio` | ||
res.redirect(url) | ||
}, | ||
...require(`./api/auth.cjs`)(util).api, | ||
...require(`./api/menu.cjs`)(util).api, | ||
...require(`./api/user.cjs`)(util).api, | ||
'/api/status'(req, res) { | ||
const { status } = req.query | ||
res.status(status).json(useResponseError()) | ||
}, | ||
'get /api/test': `Test get handler`, | ||
'post /api/test': `Test post handler`, | ||
}, | ||
static: [], | ||
resHandleReplay: ({req, res}) => useResponseSuccess({}), | ||
resHandleJsonApi: ({req, res: {statusCode: code}, data}) => wrapApiData({code, data}), | ||
} | ||
} |
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reminder: Generate refresh token.
There is a TODO comment for generating a refresh token.
Do you want me to generate the code for generating a refresh token or open a GitHub issue to track this task?