feat: 添加middleware

This commit is contained in:
xion 2024-08-28 22:51:07 +08:00
parent 70a363db86
commit 321a4b41e7
6 changed files with 823 additions and 755 deletions

770
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -29,12 +29,12 @@
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"@abearxiong/router": "^0.0.1-alpha.12", "@abearxiong/router": "^0.0.1-alpha.13",
"@abearxiong/use-config": "^0.0.1", "@abearxiong/use-config": "^0.0.1",
"@babel/core": "^7.24.7", "@babel/core": "^7.25.2",
"@babel/preset-env": "^7.24.7", "@babel/preset-env": "^7.25.4",
"@babel/preset-typescript": "^7.24.7", "@babel/preset-typescript": "^7.24.7",
"dayjs": "^1.11.11", "dayjs": "^1.11.13",
"dts-bundle": "^0.7.3", "dts-bundle": "^0.7.3",
"json5": "^2.2.3", "json5": "^2.2.3",
"jsonwebtoken": "^9.0.2", "jsonwebtoken": "^9.0.2",
@ -51,8 +51,8 @@
"@types/jest": "^29.5.12", "@types/jest": "^29.5.12",
"@types/jsonwebtoken": "^9.0.6", "@types/jsonwebtoken": "^9.0.6",
"@types/lodash-es": "^4.17.12", "@types/lodash-es": "^4.17.12",
"@types/node": "^20.14.9", "@types/node": "^22.5.1",
"@types/superagent": "^8.1.7", "@types/superagent": "^8.1.9",
"@types/supertest": "^6.0.2", "@types/supertest": "^6.0.2",
"@types/webpack-env": "^1.18.5", "@types/webpack-env": "^1.18.5",
"concurrently": "^8.2.2", "concurrently": "^8.2.2",
@ -62,13 +62,13 @@
"jest": "^29.7.0", "jest": "^29.7.0",
"nodemon": "^3.1.4", "nodemon": "^3.1.4",
"supertest": "^7.0.0", "supertest": "^7.0.0",
"ts-jest": "^29.1.5", "ts-jest": "^29.2.5",
"ts-loader": "^9.5.1", "ts-loader": "^9.5.1",
"ts-node": "^10.9.2", "ts-node": "^10.9.2",
"typescript": "^5.5.2", "typescript": "^5.5.4",
"webpack": "^5.92.1", "webpack": "^5.94.0",
"webpack-cli": "^5.1.4", "webpack-cli": "^5.1.4",
"webpack-node-externals": "^3.0.0" "webpack-node-externals": "^3.0.0"
}, },
"packageManager": "yarn@1.22.19+sha1.4ba7fc5c6e704fce2066ecbfb0b0d8976fe62447" "packageManager": "yarn@1.22.22"
} }

View File

@ -31,13 +31,14 @@ const templateFn = (codeStr: string) => {
`; `;
}; };
export const loadOne = async (item: RouterCodeModel) => { export const loadOne = async (item: RouterCodeModel) => {
const { path, key, id, code, exec, project } = item.toJSON(); const { path, key, id, code, exec, project, middleware } = item.toJSON();
const codeStr = exec || code; const codeStr = exec || code;
try { try {
const fn: any = new Function('ctx', templateFn(codeStr)); const fn: any = new Function('ctx', templateFn(codeStr));
// run code // run code
const codeRunRoute = new Route(path, key, { id }); const codeRunRoute = new Route(path, key, { id });
codeRunRoute.run = fn; codeRunRoute.run = fn;
codeRunRoute.middleware = middleware;
router.removeById(id); // TODO: router.removeById(id); // TODO:
router.add(codeRunRoute); router.add(codeRunRoute);
return { return {
@ -64,7 +65,7 @@ export const loadOne = async (item: RouterCodeModel) => {
export const load = async function () { export const load = async function () {
const codes = await RouterCodeModel.findAll(); const codes = await RouterCodeModel.findAll();
const codeManager: CodeManager[] = codes.map((item) => { const codeManager: CodeManager[] = codes.map((item) => {
const { path, key, id, code, exec, project, active } = item.toJSON(); const { path, key, id, code, exec, project, active, middleware } = item.toJSON();
if (!active) { if (!active) {
return { return {
...item.toJSON(), ...item.toJSON(),
@ -73,6 +74,7 @@ export const load = async function () {
id, id,
code, code,
project, project,
middleware,
status: CodeStatus.stop, status: CodeStatus.stop,
}; };
} }
@ -82,6 +84,7 @@ export const load = async function () {
// run code // run code
const codeRunRoute = new Route(path, key, { id }); const codeRunRoute = new Route(path, key, { id });
codeRunRoute.run = fn; codeRunRoute.run = fn;
codeRunRoute.middleware = middleware;
router.add(codeRunRoute); router.add(codeRunRoute);
return { return {
...item.toJSON(), ...item.toJSON(),

View File

@ -109,7 +109,7 @@ router.add(startRouterById);
// add or update router // add or update router
export const updateRouter = new Route('admin', 'updateRouter'); export const updateRouter = new Route('admin', 'updateRouter');
updateRouter.run = async (ctx) => { updateRouter.run = async (ctx) => {
let { path, key, id, code, type = 'route' } = ctx.query; let { path, key, id, code, middleware, type = 'route' } = ctx.query;
if (!path && !key) { if (!path && !key) {
ctx.body = 'path and key is required'; ctx.body = 'path and key is required';
ctx.code = 500; ctx.code = 500;
@ -126,6 +126,7 @@ updateRouter.run = async (ctx) => {
codeRouter.path = path; codeRouter.path = path;
codeRouter.key = key; codeRouter.key = key;
codeRouter.code = code; codeRouter.code = code;
codeRouter.middleware = middleware;
try { try {
codeRouter.exec = await transform(code); codeRouter.exec = await transform(code);
} catch (e) { } catch (e) {
@ -138,7 +139,7 @@ updateRouter.run = async (ctx) => {
} else { } else {
try { try {
const exec = await transform(code); const exec = await transform(code);
const newCodeRouter = new RouterCodeModel({ path, key, code, exec, type }); const newCodeRouter = new RouterCodeModel({ path, key, code, exec, type, middleware });
await newCodeRouter.save(); await newCodeRouter.save();
codeRouter = newCodeRouter; codeRouter = newCodeRouter;
} catch (e) { } catch (e) {

View File

@ -39,15 +39,4 @@
"src/**/*.test.ts", "src/**/*.test.ts",
"webpack.config.cjs", "webpack.config.cjs",
], ],
"ts-node": {
// Do not forget to `npm i -D tsconfig-paths`
"require": [
// "tsconfig-paths/register"
],
// "skipIgnore": true,
"ignore": [
// "(?:^|/)node_modules/",
// "(?:^|/)node_modules/(?!.*abearxiong)" //abearxiongnode_modules
]
}
} }

765
yarn.lock

File diff suppressed because it is too large Load Diff