From 99f6f58f525931711db45d69857853869b74325d Mon Sep 17 00:00:00 2001 From: xion Date: Wed, 18 Sep 2024 05:19:20 +0800 Subject: [PATCH] feat: add types and list change --- .npmrc | 3 +- package.json | 17 +- pnpm-lock.yaml | 21 ++ src/routes/container/list.ts | 58 ++++-- src/routes/container/models/index.ts | 17 +- src/routes/types.ts | 1 + src/type.ts | 2 + types/index.d.ts | 279 ++++----------------------- 8 files changed, 127 insertions(+), 271 deletions(-) create mode 100644 src/routes/types.ts diff --git a/.npmrc b/.npmrc index eaf6f94..5f969a8 100644 --- a/.npmrc +++ b/.npmrc @@ -1 +1,2 @@ -@abearxiong:registry=https://npm.pkg.github.com \ No newline at end of file +@abearxiong:registry=https://npm.pkg.github.com +@build:registry=https://npm.xiongxiao.me \ No newline at end of file diff --git a/package.json b/package.json index 13b0ec5..d2a7c6e 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,10 @@ { - "name": "@abearxiong/code-flow", + "name": "@build/code-flow", "version": "0.0.2", "description": "code的flow流程成图", "type": "module", "main": "index.js", + "author": "abearxiong", "scripts": { "watch": "cross-env ENV=production webpack --mode=production --watch", "dev": "cross-env NODE_ENV=development nodemon --delay 2.5 -e js,cjs,mjs --exec node dist/app.cjs", @@ -19,14 +20,14 @@ "docker:push": "docker push docker.xiongxiao.me/code-flow:v0.0.2", "docker:run": "docker run -it --name code-flow -p 4000:4000 docker.xiongxiao.me/code-flow:v0.0.2", "docker:build:gitea": "docker build -t git.xiongxiao.me/abearxiong/code-flow:v0.0.2 .", - "docker:push:gitea": "docker push git.xiongxiao.me/abearxiong/code-flow:v0.0.2" + "docker:push:gitea": "docker push git.xiongxiao.me/abearxiong/code-flow:v0.0.2", + "dts": "./node_modules/.bin/dts-bundle-generator -o types/index.d.ts src/type.ts" }, "keywords": [], "types": "types/index.d.ts", "files": [ - "dist" + "types" ], - "author": "", "license": "ISC", "dependencies": { "@abearxiong/router": "0.0.1-alpha.27", @@ -35,7 +36,7 @@ "@babel/preset-env": "^7.25.4", "@babel/preset-typescript": "^7.24.7", "dayjs": "^1.11.13", - "dts-bundle": "^0.7.3", + "dts-bundle-generator": "^9.5.1", "json5": "^2.2.3", "jsonwebtoken": "^9.0.2", "lodash-es": "^4.17.21", @@ -43,11 +44,11 @@ "pg": "^8.12.0", "sequelize": "^6.37.3", "socket.io": "^4.7.5", - "strip-ansi": "^7.1.0" + "strip-ansi": "^7.1.0", + "zod": "^3.23.8" }, "devDependencies": { "@types/crypto-js": "^4.2.2", - "@types/dts-bundle": "^0.0.35", "@types/jest": "^29.5.13", "@types/jsonwebtoken": "^9.0.6", "@types/lodash-es": "^4.17.12", @@ -70,4 +71,4 @@ "webpack-cli": "^5.1.4", "webpack-node-externals": "^3.0.0" } -} +} \ No newline at end of file diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e3ae9d3..803859d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -29,6 +29,9 @@ importers: dts-bundle: specifier: ^0.7.3 version: 0.7.3 + dts-bundle-generator: + specifier: ^9.5.1 + version: 9.5.1 json5: specifier: ^2.2.3 version: 2.2.3 @@ -53,6 +56,9 @@ importers: strip-ansi: specifier: ^7.1.0 version: 7.1.0 + zod: + specifier: ^3.23.8 + version: 3.23.8 devDependencies: '@types/crypto-js': specifier: ^4.2.2 @@ -1437,6 +1443,11 @@ packages: dottie@2.0.6: resolution: {integrity: sha512-iGCHkfUc5kFekGiqhe8B/mdaurD+lakO9txNnTvKtA6PISrw86LgqHvRzWYPyoE2Ph5aMIrCw9/uko6XHTKCwA==} + dts-bundle-generator@9.5.1: + resolution: {integrity: sha512-DxpJOb2FNnEyOzMkG11sxO2dmxPjthoVWxfKqWYJ/bI/rT1rvTMktF5EKjAYrRZu6Z6t3NhOUZ0sZ5ZXevOfbA==} + engines: {node: '>=14.0.0'} + hasBin: true + dts-bundle@0.7.3: resolution: {integrity: sha512-EEAEuPRk8QyKhoN90NHTh+spSQujkkvOnKWUfuzpmC/fgryiWopL1SegSktx0UsoPfNidIGVDN7/AXpBDBv0WQ==} engines: {node: '>= 0.10.0'} @@ -2860,6 +2871,9 @@ packages: resolution: {integrity: sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==} engines: {node: '>=10'} + zod@3.23.8: + resolution: {integrity: sha512-XBx9AXhXktjUqnepgTiE5flcKIYWi/rme0Eaj+5Y0lftuGBq+jyRu/md4WnuxqgP1ubdpNCsYEYPxrzVHD8d6g==} + snapshots: '@abearxiong/router@0.0.1-alpha.27': {} @@ -4524,6 +4538,11 @@ snapshots: dottie@2.0.6: {} + dts-bundle-generator@9.5.1: + dependencies: + typescript: 5.6.2 + yargs: 17.7.2 + dts-bundle@0.7.3: dependencies: '@types/detect-indent': 0.1.30 @@ -6058,3 +6077,5 @@ snapshots: yn@3.1.1: {} yocto-queue@0.1.0: {} + + zod@3.23.8: {} diff --git a/src/routes/container/list.ts b/src/routes/container/list.ts index f3d3f88..485a40f 100644 --- a/src/routes/container/list.ts +++ b/src/routes/container/list.ts @@ -1,5 +1,5 @@ import { app } from '../../app.ts'; -import { ContainerModel, ContainerData } from './models/index.ts'; +import { ContainerModel, ContainerData, Container } from './models/index.ts'; const list = app.route({ path: 'container', @@ -15,27 +15,49 @@ list.addTo(app); const add = app.route({ path: 'container', - key: 'add', + key: 'update', }); add.run = async (ctx) => { - // const data = ctx.query; - const data: ContainerData = { - className: 'name', - style: { - color: 'red', + const data = ctx.query.data; + + const _data: Container = { + title: '', + description: '', + code: '', + source: '', + type: '', + sourceType: '', + data: { + className: '', + style: {}, + showChild: true, + shadowRoot: false, }, - showChild: true, - shadowRoot: false, }; - const container = await ContainerModel.create({ - title: 'title', - data: data as ContainerData, - description: 'description', - code: `console.log('hello world')`, - source: 'source', - type: 'typescript', - }); - ctx.body = container; + const container = { + ..._data, + ...data, + }; + let containerModel: any = null; + if (container.id) { + containerModel = await ContainerModel.findByPk(container.id); + if (containerModel) { + containerModel.update(container); + containerModel.save(); + } + } else { + try{ + + containerModel = await ContainerModel.create({ + ...container, + }); + } catch (e) { + console.log('error', e); + } + console.log('containerModel', container); + } + + ctx.body = containerModel; return ctx; }; add.addTo(app); diff --git a/src/routes/container/models/index.ts b/src/routes/container/models/index.ts index bcab9da..14f8755 100644 --- a/src/routes/container/models/index.ts +++ b/src/routes/container/models/index.ts @@ -7,6 +7,16 @@ export interface ContainerData { showChild?: boolean; shadowRoot?: boolean; } +export type Container = { + id?: string; + title?: string; + description?: string; + type?: string; + code?: string; + source?: string; + sourceType?: string; + data?: ContainerData; +}; export class ContainerModel extends Model { declare id: string; declare title: string; @@ -14,6 +24,7 @@ export class ContainerModel extends Model { declare type: string; declare code: string; declare source: string; + declare sourceType: string; declare data: ContainerData; } ContainerModel.init( @@ -37,13 +48,17 @@ ContainerModel.init( defaultValue: '', }, code: { - type: DataTypes.STRING, + type: DataTypes.TEXT, defaultValue: '', }, source: { type: DataTypes.STRING, defaultValue: '', }, + sourceType: { + type: DataTypes.STRING, + defaultValue: '', + }, data: { type: DataTypes.JSON, defaultValue: {}, diff --git a/src/routes/types.ts b/src/routes/types.ts new file mode 100644 index 0000000..9d6d279 --- /dev/null +++ b/src/routes/types.ts @@ -0,0 +1 @@ +export * from './container/type.ts' \ No newline at end of file diff --git a/src/type.ts b/src/type.ts index c183e39..94fc53f 100644 --- a/src/type.ts +++ b/src/type.ts @@ -1,3 +1,5 @@ import { CodeManager } from './admin/dashboard/load.ts'; +import { ContainerData } from './routes/types.ts'; export { CodeManager }; +export { ContainerData }; diff --git a/types/index.d.ts b/types/index.d.ts index e123743..157478f 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1,248 +1,41 @@ -// 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; - -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', -}); - +// Generated by dts-bundle-generator v9.5.1 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; + 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', +declare enum RouterCodeType { + route = "route", + middleware = "middleware" +} +declare enum CodeStatus { + running = "running", + stop = "stop", + fail = "fail" +} +export type CodeManager = { + fn?: any; + status?: CodeStatus; + errorMsg?: string; + lock?: boolean; +} & Partial; +export interface ContainerData { + style?: { + [key: string]: string; + }; + className?: string; + showChild?: boolean; + shadowRoot?: boolean; } -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(); - -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, -}); - +export {};