-
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 4 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, | ||
})); | ||
Comment on lines
+23
to
+39
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
}, | ||
}, | ||
} | ||
} |
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,19 @@ | ||||||||||||||||||||||||||||||
module.exports = { | ||||||||||||||||||||||||||||||
key: `get-user`, | ||||||||||||||||||||||||||||||
main() { | ||||||||||||||||||||||||||||||
return { | ||||||||||||||||||||||||||||||
useCreated(app) { | ||||||||||||||||||||||||||||||
app.use((req, res, next) => { | ||||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||||
const token = req.get(`Authorization`); | ||||||||||||||||||||||||||||||
const username = Buffer.from(token, 'base64').toString('utf8'); | ||||||||||||||||||||||||||||||
req.username = username; | ||||||||||||||||||||||||||||||
} catch { | ||||||||||||||||||||||||||||||
// ... | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add logging for errors and improve error handling. The current error handling is minimal and lacks logging. This can make debugging difficult. - } catch {
- // ...
+ } catch (error) {
+ console.error('Error decoding Authorization header:', error);
+ res.status(400).send('Invalid Authorization header'); Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||
next(); | ||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||
}; |
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,61 @@ | ||
const { | ||
wrapApiData, | ||
useResponseSuccess, | ||
useResponseError, | ||
} = require(`./util.cjs`) | ||
const getUser = require(`./get-user.cjs`) | ||
|
||
/** | ||
* 配置说明请参考文档: | ||
* https://hongqiye.com/doc/mockm/config/option.html | ||
* @type {import('mockm/@types/config').Config} | ||
*/ | ||
module.exports = util => { | ||
return { | ||
watch: [`./api/`], | ||
plugin: [getUser], | ||
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) { | ||
return res.status(401).json(useResponseError('UnauthorizedException', 'Unauthorized Exception')) | ||
} | ||
next() | ||
} | ||
wll8 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
// 跳转到接口列表 | ||
'/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.
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.
Fix Markdownlint issues by removing dollar signs.
The dollar signs before the commands should be removed to comply with Markdownlint rules.
Committable suggestion
Tools
Markdownlint