feat: 添加middleware
This commit is contained in:
		
							
								
								
									
										770
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										770
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										20
									
								
								package.json
									
									
									
									
									
								
							
							
						
						
									
										20
									
								
								package.json
									
									
									
									
									
								
							| @@ -29,12 +29,12 @@ | ||||
|   "author": "", | ||||
|   "license": "ISC", | ||||
|   "dependencies": { | ||||
|     "@abearxiong/router": "^0.0.1-alpha.12", | ||||
|     "@abearxiong/router": "^0.0.1-alpha.13", | ||||
|     "@abearxiong/use-config": "^0.0.1", | ||||
|     "@babel/core": "^7.24.7", | ||||
|     "@babel/preset-env": "^7.24.7", | ||||
|     "@babel/core": "^7.25.2", | ||||
|     "@babel/preset-env": "^7.25.4", | ||||
|     "@babel/preset-typescript": "^7.24.7", | ||||
|     "dayjs": "^1.11.11", | ||||
|     "dayjs": "^1.11.13", | ||||
|     "dts-bundle": "^0.7.3", | ||||
|     "json5": "^2.2.3", | ||||
|     "jsonwebtoken": "^9.0.2", | ||||
| @@ -51,8 +51,8 @@ | ||||
|     "@types/jest": "^29.5.12", | ||||
|     "@types/jsonwebtoken": "^9.0.6", | ||||
|     "@types/lodash-es": "^4.17.12", | ||||
|     "@types/node": "^20.14.9", | ||||
|     "@types/superagent": "^8.1.7", | ||||
|     "@types/node": "^22.5.1", | ||||
|     "@types/superagent": "^8.1.9", | ||||
|     "@types/supertest": "^6.0.2", | ||||
|     "@types/webpack-env": "^1.18.5", | ||||
|     "concurrently": "^8.2.2", | ||||
| @@ -62,13 +62,13 @@ | ||||
|     "jest": "^29.7.0", | ||||
|     "nodemon": "^3.1.4", | ||||
|     "supertest": "^7.0.0", | ||||
|     "ts-jest": "^29.1.5", | ||||
|     "ts-jest": "^29.2.5", | ||||
|     "ts-loader": "^9.5.1", | ||||
|     "ts-node": "^10.9.2", | ||||
|     "typescript": "^5.5.2", | ||||
|     "webpack": "^5.92.1", | ||||
|     "typescript": "^5.5.4", | ||||
|     "webpack": "^5.94.0", | ||||
|     "webpack-cli": "^5.1.4", | ||||
|     "webpack-node-externals": "^3.0.0" | ||||
|   }, | ||||
|   "packageManager": "yarn@1.22.19+sha1.4ba7fc5c6e704fce2066ecbfb0b0d8976fe62447" | ||||
|   "packageManager": "yarn@1.22.22" | ||||
| } | ||||
|   | ||||
| @@ -31,13 +31,14 @@ const templateFn = (codeStr: string) => { | ||||
| `; | ||||
| }; | ||||
| 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; | ||||
|   try { | ||||
|     const fn: any = new Function('ctx', templateFn(codeStr)); | ||||
|     // run code | ||||
|     const codeRunRoute = new Route(path, key, { id }); | ||||
|     codeRunRoute.run = fn; | ||||
|     codeRunRoute.middleware = middleware; | ||||
|     router.removeById(id); // TODO: | ||||
|     router.add(codeRunRoute); | ||||
|     return { | ||||
| @@ -64,7 +65,7 @@ export const loadOne = async (item: RouterCodeModel) => { | ||||
| export const load = async function () { | ||||
|   const codes = await RouterCodeModel.findAll(); | ||||
|   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) { | ||||
|       return { | ||||
|         ...item.toJSON(), | ||||
| @@ -73,6 +74,7 @@ export const load = async function () { | ||||
|         id, | ||||
|         code, | ||||
|         project, | ||||
|         middleware, | ||||
|         status: CodeStatus.stop, | ||||
|       }; | ||||
|     } | ||||
| @@ -82,6 +84,7 @@ export const load = async function () { | ||||
|       // run code | ||||
|       const codeRunRoute = new Route(path, key, { id }); | ||||
|       codeRunRoute.run = fn; | ||||
|       codeRunRoute.middleware = middleware; | ||||
|       router.add(codeRunRoute); | ||||
|       return { | ||||
|         ...item.toJSON(), | ||||
|   | ||||
| @@ -109,7 +109,7 @@ router.add(startRouterById); | ||||
| // add or update router | ||||
| export const updateRouter = new Route('admin', 'updateRouter'); | ||||
| 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) { | ||||
|     ctx.body = 'path and key is required'; | ||||
|     ctx.code = 500; | ||||
| @@ -126,6 +126,7 @@ updateRouter.run = async (ctx) => { | ||||
|     codeRouter.path = path; | ||||
|     codeRouter.key = key; | ||||
|     codeRouter.code = code; | ||||
|     codeRouter.middleware = middleware; | ||||
|     try { | ||||
|       codeRouter.exec = await transform(code); | ||||
|     } catch (e) { | ||||
| @@ -138,7 +139,7 @@ updateRouter.run = async (ctx) => { | ||||
|   } else { | ||||
|     try { | ||||
|       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(); | ||||
|       codeRouter = newCodeRouter; | ||||
|     } catch (e) { | ||||
|   | ||||
| @@ -39,15 +39,4 @@ | ||||
|     "src/**/*.test.ts", | ||||
|     "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)" //匹配不包含abearxiong的node_modules当中的字符串 | ||||
|     ] | ||||
|   } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user