feat: add type dts and apiKeyObject
This commit is contained in:
		
							
								
								
									
										248
									
								
								types/index.d.ts
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										248
									
								
								types/index.d.ts
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @@ -0,0 +1,248 @@ | ||||
| // Generated by dts-bundle v0.7.3 | ||||
| // Dependencies for this module: | ||||
| //   ../@abearxiong/router | ||||
| //   ../sequelize | ||||
| //   ../@abearxiong/use-config | ||||
|  | ||||
| import { Route } from '@abearxiong/router'; | ||||
| import { QueryRouter, Server } from '@abearxiong/router'; | ||||
| import { DataTypes, Model } from 'sequelize'; | ||||
| import { useConfig } from '@abearxiong/use-config'; | ||||
| import { Sequelize } from 'sequelize'; | ||||
|  | ||||
|  | ||||
| export { CodeManager }; | ||||
|  | ||||
|  | ||||
| export enum CodeStatus { | ||||
|     running = 'running', | ||||
|     stop = 'stop', | ||||
|     fail = 'fail', | ||||
| } | ||||
|  | ||||
| export type CodeManager = { | ||||
|     fn?: any; | ||||
|     status?: CodeStatus; | ||||
|     errorMsg?: string; | ||||
| } & Partial<RouterCode>; | ||||
|  | ||||
| const codeDemoRun = `async function run(ctx) { | ||||
|     ctx.body = 'test js'; | ||||
|     return ctx; | ||||
| }`; | ||||
| const templateFn = (codeStr: string) => { | ||||
|     return ` | ||||
|     ${codeStr} | ||||
|     if(run) { | ||||
|         return run(ctx); | ||||
|     } | ||||
|     if(main) { | ||||
|         return main(ctx); | ||||
|     } | ||||
|     return 'no run or main function'; | ||||
| `; | ||||
| }; | ||||
| export const loadOne = async (item: RouterCodeModel) => { | ||||
|     const { path, key, id, code, exec, project } = 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; | ||||
|         router.removeById(id); // TODO: | ||||
|         router.add(codeRunRoute); | ||||
|         return { | ||||
|             ...item.toJSON(), | ||||
|             path, | ||||
|             key, | ||||
|             id, | ||||
|             project, | ||||
|             fn, | ||||
|             status: CodeStatus.running, | ||||
|         }; | ||||
|     } catch (e) { | ||||
|         console.error('error id:', id, '\n', e); | ||||
|         return { | ||||
|             path, | ||||
|             key, | ||||
|             id, | ||||
|             project, | ||||
|             status: CodeStatus.fail, | ||||
|             errorMsg: e.message.toString(), | ||||
|         }; | ||||
|     } | ||||
| }; | ||||
| 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(); | ||||
|         if (!active) { | ||||
|             return { | ||||
|                 ...item.toJSON(), | ||||
|                 path, | ||||
|                 key, | ||||
|                 id, | ||||
|                 code, | ||||
|                 project, | ||||
|                 status: CodeStatus.stop, | ||||
|             }; | ||||
|         } | ||||
|         try { | ||||
|             const codeStr = exec || code; | ||||
|             const fn: any = new Function('ctx', templateFn(codeStr)); | ||||
|             // run code | ||||
|             const codeRunRoute = new Route(path, key, { id }); | ||||
|             codeRunRoute.run = fn; | ||||
|             router.add(codeRunRoute); | ||||
|             return { | ||||
|                 ...item.toJSON(), | ||||
|                 path, | ||||
|                 key, | ||||
|                 id, | ||||
|                 code, | ||||
|                 project, | ||||
|                 type: item.type, | ||||
|                 fn, | ||||
|                 status: CodeStatus.running, | ||||
|             }; | ||||
|         } catch (e) { | ||||
|             console.error('error id:', id, '\n', e); | ||||
|             return { | ||||
|                 path, | ||||
|                 key, | ||||
|                 id, | ||||
|                 code, | ||||
|                 project, | ||||
|                 type: item.type, | ||||
|                 status: CodeStatus.fail, | ||||
|                 errorMsg: e.message.toString(), | ||||
|             }; | ||||
|         } | ||||
|     }); | ||||
|     return codeManager; | ||||
| }; | ||||
|  | ||||
| export const router = new QueryRouter(); | ||||
| export const server = new Server({ | ||||
|     path: '/api/router', | ||||
| }); | ||||
|  | ||||
|  | ||||
| export type RouterCode = { | ||||
|     id: string; | ||||
|     path: string; | ||||
|     key: string; | ||||
|     active: boolean; | ||||
|     project: string; | ||||
|     code: string; | ||||
|     exec: string; | ||||
|     type: RouterCodeType; | ||||
|     middleware: string[]; | ||||
|     next: string; | ||||
|     data: any; | ||||
|     validator: any; | ||||
| }; | ||||
|  | ||||
| export enum RouterCodeType { | ||||
|     route = 'route', | ||||
|     middleware = 'middleware', | ||||
| } | ||||
|  | ||||
| export class RouterCodeModel extends Model { | ||||
|     declare id: string; | ||||
|     declare path: string; | ||||
|     declare key: string; | ||||
|     declare active: boolean; | ||||
|     declare project: string; | ||||
|     declare code: string; | ||||
|     declare exec: string; | ||||
|     declare type: RouterCodeType; | ||||
|     declare middleware: string[]; | ||||
|     declare next: string; // 如果是中间件,不存在 | ||||
|     declare data: any; // 内容 | ||||
|     declare validator: any; | ||||
| } | ||||
| RouterCodeModel.init( | ||||
|     { | ||||
|         id: { | ||||
|             type: DataTypes.UUID, | ||||
|             primaryKey: true, | ||||
|             defaultValue: DataTypes.UUIDV4, | ||||
|             comment: '用户id', | ||||
|         }, | ||||
|         path: { | ||||
|             type: DataTypes.STRING, | ||||
|             allowNull: false, | ||||
|         }, | ||||
|         key: { | ||||
|             type: DataTypes.STRING, | ||||
|             allowNull: false, | ||||
|         }, | ||||
|         active: { | ||||
|             type: DataTypes.BOOLEAN, | ||||
|             defaultValue: false, | ||||
|         }, | ||||
|         project: { | ||||
|             type: DataTypes.STRING, | ||||
|             defaultValue: 'default', | ||||
|         }, | ||||
|         code: { | ||||
|             type: DataTypes.STRING, | ||||
|             defaultValue: '', | ||||
|         }, | ||||
|         exec: { | ||||
|             type: DataTypes.STRING, // 对代码进行编译后的代码 | ||||
|             defaultValue: '', | ||||
|         }, | ||||
|         type: { | ||||
|             type: DataTypes.ENUM(RouterCodeType.route, RouterCodeType.middleware), | ||||
|             defaultValue: RouterCodeType.route, | ||||
|         }, | ||||
|         middleware: { | ||||
|             type: DataTypes.ARRAY(DataTypes.STRING), | ||||
|             defaultValue: [], | ||||
|         }, | ||||
|         next: { | ||||
|             type: DataTypes.STRING, | ||||
|             defaultValue: '', | ||||
|         }, | ||||
|         data: { | ||||
|             type: DataTypes.JSON, | ||||
|             defaultValue: {}, | ||||
|         }, | ||||
|         validator: { | ||||
|             type: DataTypes.JSON, | ||||
|             defaultValue: {}, | ||||
|         }, | ||||
|     }, | ||||
|     { | ||||
|         sequelize, | ||||
|         tableName: 'cf_router_code', | ||||
|     }, | ||||
| ); | ||||
|  | ||||
|  | ||||
| type PostgresConfig = { | ||||
|     postgres: { | ||||
|         username: string; | ||||
|         password: string; | ||||
|         host: string; | ||||
|         port: number; | ||||
|         database: string; | ||||
|     }; | ||||
| }; | ||||
| const config = useConfig<PostgresConfig>(); | ||||
|  | ||||
| const postgresConfig = config.postgres; | ||||
|  | ||||
| if (!postgresConfig) { | ||||
|     console.error('postgres config is required'); | ||||
|     process.exit(1); | ||||
| } | ||||
| export const sequelize = new Sequelize({ | ||||
|     dialect: 'postgres', | ||||
|     ...postgresConfig, | ||||
|     // logging: false, | ||||
| }); | ||||
|  | ||||
		Reference in New Issue
	
	Block a user