feat: add Container and Page Module

This commit is contained in:
xion 2024-09-17 01:59:48 +08:00
parent 321a4b41e7
commit 0a9e5c1d4f
21 changed files with 6335 additions and 13271 deletions

8334
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -6,8 +6,8 @@
"main": "index.js",
"scripts": {
"watch": "cross-env ENV=production webpack --mode=production --watch",
"dev": "cross-env NODE_ENV=development nodemon -e js,cjs,mjs --exec node dist/app.cjs",
"dev:watch":"concurrently -n \"Watch,Dev\" -c \"green,blue\" \"npm run watch\" \"npm run dev\" ",
"dev": "cross-env NODE_ENV=development nodemon --delay 2.5 -e js,cjs,mjs --exec node dist/app.cjs",
"dev:watch": "concurrently -n \"Watch,Dev\" -c \"green,blue\" \"npm run watch\" \"sleep 1 && npm run dev\" ",
"test": "NODE_ENV=development node --experimental-vm-modules node_modules/jest/bin/jest.js --detectOpenHandles ",
"build": "cross-env ENV=production webpack --mode=production",
"build:sh": "cross-env webpack --mode=production -c ./webpack.shell.config.cjs",
@ -29,7 +29,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"@abearxiong/router": "^0.0.1-alpha.13",
"@abearxiong/router": "0.0.1-alpha.27",
"@abearxiong/use-config": "^0.0.1",
"@babel/core": "^7.25.2",
"@babel/preset-env": "^7.25.4",
@ -48,14 +48,14 @@
"devDependencies": {
"@types/crypto-js": "^4.2.2",
"@types/dts-bundle": "^0.0.35",
"@types/jest": "^29.5.12",
"@types/jest": "^29.5.13",
"@types/jsonwebtoken": "^9.0.6",
"@types/lodash-es": "^4.17.12",
"@types/node": "^22.5.1",
"@types/node": "^22.5.5",
"@types/superagent": "^8.1.9",
"@types/supertest": "^6.0.2",
"@types/webpack-env": "^1.18.5",
"concurrently": "^8.2.2",
"concurrently": "^9.0.1",
"copy-webpack-plugin": "^12.0.2",
"cross-env": "^7.0.3",
"fork-ts-checker-webpack-plugin": "^9.0.2",
@ -65,10 +65,9 @@
"ts-jest": "^29.2.5",
"ts-loader": "^9.5.1",
"ts-node": "^10.9.2",
"typescript": "^5.5.4",
"typescript": "^5.6.2",
"webpack": "^5.94.0",
"webpack-cli": "^5.1.4",
"webpack-node-externals": "^3.0.0"
},
"packageManager": "yarn@1.22.22"
}
}

6060
pnpm-lock.yaml generated Normal file

File diff suppressed because it is too large Load Diff

15
src/admin/core.ts Normal file
View File

@ -0,0 +1,15 @@
import { app } from '../app.ts';
new app.Route('admin', 'getRouteList')
.define(async (ctx) => {
const list = app.router.getList();
// ctx.body = list.filter((r) => !r.path.startsWith('admin'));
ctx.body = list.map((r) => {
return {
path: r.path,
key: r.key,
};
});
return ctx;
})
.addTo(app);

View File

@ -12,6 +12,7 @@ export type CodeManager = {
fn?: any;
status?: CodeStatus;
errorMsg?: string;
lock?: boolean; // 是否锁定
} & Partial<RouterCode>;
const codeDemoRun = `async function run(ctx) {

View File

@ -10,6 +10,7 @@ export enum LoadStatus {
export const manager = {
loaded: LoadStatus.LOADING, // 是否已经加载
list: [] as CodeManager[],
shareLocalList: [] as CodeManager[],
};
// 更新

View File

@ -1,3 +1,4 @@
import './router.ts';
import './manager.ts';
import './npm.ts';
import './core.ts';

View File

@ -1,23 +1,16 @@
import { App } from '@abearxiong/router';
import { useConfig } from '@abearxiong/use-config';
import { handleMessage } from './route.ts';
import { server as routerServer } from './modules/router.ts';
import http from 'http';
import { dynamicImport } from './lib/dynamic-import.ts';
const config = useConfig();
routerServer.setHandle(handleMessage);
useConfig();
const server = http.createServer((req, res) => {
// 设置跨域
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
res.setHeader('Access-Control-Max-Age', '86400');
res.setHeader('Content-Type', 'application/json');
// routerServer.handle(req, res);
return routerServer.callback()(req, res);
});
server.listen(config.port, () => {
console.log(`Server running at http://localhost:${config.port}/`);
export const app = new App({
serverOptions: {
cors: {
origin: '*',
},
},
routerContext: {
import: dynamicImport,
},
});

9
src/index.ts Normal file
View File

@ -0,0 +1,9 @@
import { useConfig } from '@abearxiong/use-config';
import { app } from './app.ts';
import './route.ts'
const config = useConfig();
export { app };
app.listen(config.port, () => {
console.log(`server is running at http://localhost:${config.port}`);
});

View File

@ -1,5 +1,4 @@
import { QueryRouter, Server } from '@abearxiong/router';
export const router = new QueryRouter();
export const server = new Server({
path: '/api/router',
});
import { app } from '../app.ts';
export const router = app.router;
export const server = app.server;

View File

@ -1,28 +1,3 @@
import { router } from './modules/router.ts';
import './demo/index.ts';
import './admin/index.ts';
import { dynamicImport } from './lib/dynamic-import.ts';
type Message = {
path: string;
key?: string;
};
export const handleMessage = async function (m: Message) {
if (!m) {
return {
code: 400,
message: 'message is empty',
};
}
const res = await router.parse(m, {
// @ts-ignore
import: dynamicImport,
});
const { code, body, message } = res;
// console.log('response', res);
return { code, data: body, message };
};
import './routes/index.ts';

View File

@ -0,0 +1 @@
import './list.ts';

View File

@ -0,0 +1,56 @@
import { app } from '../../app.ts';
import { ContainerModel, ContainerData } from './models/index.ts';
const list = app.route({
path: 'container',
key: 'list',
});
list.run = async (ctx) => {
const list = await ContainerModel.findAll();
ctx.body = list;
return ctx;
};
list.addTo(app);
const add = app.route({
path: 'container',
key: 'add',
});
add.run = async (ctx) => {
// const data = ctx.query;
const data: ContainerData = {
className: 'name',
style: {
color: 'red',
},
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;
return ctx;
};
add.addTo(app);
const deleteRoute = app.route({
path: 'container',
key: 'delete',
});
deleteRoute.run = async (ctx) => {
const id = ctx.query.id;
const container = await ContainerModel.findByPk(id);
if (container) {
await container.destroy();
}
ctx.body = container;
return ctx;
};
deleteRoute.addTo(app);

View File

@ -0,0 +1,64 @@
import { sequelize } from '../../../modules/sequelize.ts';
import { DataTypes, Model } from 'sequelize';
export interface ContainerData {
style?: { [key: string]: string };
className?: string;
showChild?: boolean;
shadowRoot?: boolean;
}
export class ContainerModel extends Model {
declare id: string;
declare title: string;
declare description: string;
declare type: string;
declare code: string;
declare source: string;
declare data: ContainerData;
}
ContainerModel.init(
{
id: {
type: DataTypes.UUID,
primaryKey: true,
defaultValue: DataTypes.UUIDV4,
comment: 'id',
},
title: {
type: DataTypes.STRING,
defaultValue: '',
},
description: {
type: DataTypes.STRING,
defaultValue: '',
},
type: {
type: DataTypes.STRING,
defaultValue: '',
},
code: {
type: DataTypes.STRING,
defaultValue: '',
},
source: {
type: DataTypes.STRING,
defaultValue: '',
},
data: {
type: DataTypes.JSON,
defaultValue: {},
},
uid: {
type: DataTypes.UUID,
allowNull: true,
},
},
{
sequelize,
tableName: 'kv_container',
},
);
ContainerModel.sync({ alter: true, logging: false }).catch((e) => {
console.error('ContainerModel sync', e);
});

View File

@ -0,0 +1,3 @@
import { ContainerData } from './models/index.ts';
export { ContainerData };

1
src/routes/index.ts Normal file
View File

@ -0,0 +1 @@
import './container/index.ts';

1
src/routes/page/index.ts Normal file
View File

@ -0,0 +1 @@
import './list.ts'

42
src/routes/page/list.ts Normal file
View File

@ -0,0 +1,42 @@
import { app } from '../../app.ts';
import { PageModel } from './models/index.ts';
app
.route({
path: 'page',
key: 'list',
})
.define(async (ctx) => {
ctx.body = await PageModel.findAll();
return ctx;
})
.addTo(app);
app
.route({
path: 'page',
key: 'add',
})
.define(async (ctx) => {
const data = ctx.query;
const page = await PageModel.create(data);
ctx.body = page;
return ctx;
})
.addTo(app);
app
.route({
path: 'page',
key: 'delete',
})
.define(async (ctx) => {
const id = ctx.query.id;
const page = await PageModel.findByPk(id);
if (page) {
await page.destroy();
}
ctx.body = page;
return ctx;
})
.addTo(app);

View File

@ -0,0 +1,54 @@
import { sequelize } from '../../../modules/sequelize.ts';
import { DataTypes, Model } from 'sequelize';
export interface PageData {
edges: any[];
nodes: any[];
viewport: any;
[key: string]: any;
}
export class PageModel extends Model {
declare id: string;
declare title: string;
declare description: string;
declare type: string;
declare data: PageData;
}
PageModel.init(
{
id: {
type: DataTypes.UUID,
primaryKey: true,
defaultValue: DataTypes.UUIDV4,
comment: 'id',
},
title: {
type: DataTypes.STRING,
defaultValue: '',
},
description: {
type: DataTypes.STRING,
defaultValue: '',
},
type: {
type: DataTypes.STRING,
defaultValue: '',
},
data: {
type: DataTypes.JSON,
defaultValue: {},
},
uid: {
type: DataTypes.UUID,
allowNull: true,
},
},
{
sequelize,
tableName: 'kv_page',
},
);
PageModel.sync({ alter: true, logging: false }).catch((e) => {
console.error('PageModel sync', e);
});

View File

@ -22,7 +22,7 @@ if (ENV === 'init') {
*/
module.exports = {
mode: 'production',
entry: path.join(__dirname, './src/app.ts'),
entry: path.join(__dirname, './src/index.ts'),
target: 'node',
// devtool: 'source-map',
output: {

4877
yarn.lock

File diff suppressed because it is too large Load Diff