feat: add export and import
This commit is contained in:
parent
6f0faba703
commit
158b12d811
@ -11,7 +11,7 @@
|
||||
"license": "ISC",
|
||||
"description": "",
|
||||
"dependencies": {
|
||||
"@abearxiong/router": "../.."
|
||||
"@kevisual/router": "link:../.."
|
||||
},
|
||||
"devDependencies": {
|
||||
"ts-node": "^10.9.2",
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { App } from '@abearxiong/router';
|
||||
import { App } from '@kevisual/router';
|
||||
|
||||
const app = new App();
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { Route, App } from '@abearxiong/router';
|
||||
import { Route, App } from '@kevisual/router';
|
||||
|
||||
const app = new App({ io: true });
|
||||
app.listen(4002);
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { Route, App } from '@abearxiong/router';
|
||||
import { Route, App } from '@kevisual/router';
|
||||
|
||||
const app = new App();
|
||||
app.listen(4003);
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { QueryRouter, Route, Server } from '@abearxiong/router';
|
||||
import { QueryRouter, Route, Server } from '@kevisual/router';
|
||||
|
||||
const router = new QueryRouter();
|
||||
|
||||
|
76
demo/simple/src/test-two-app/app.ts
Normal file
76
demo/simple/src/test-two-app/app.ts
Normal file
@ -0,0 +1,76 @@
|
||||
import { App } from '@kevisual/router';
|
||||
|
||||
const app1 = new App();
|
||||
|
||||
app1
|
||||
.route({
|
||||
path: 'app1',
|
||||
key: '01',
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
ctx.body = '01';
|
||||
return ctx;
|
||||
})
|
||||
.addTo(app1);
|
||||
|
||||
app1
|
||||
.route({
|
||||
path: 'app1',
|
||||
key: '02',
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
ctx.body = '02';
|
||||
return ctx;
|
||||
})
|
||||
.addTo(app1);
|
||||
|
||||
const app2 = new App();
|
||||
|
||||
app2
|
||||
.route({
|
||||
path: 'app2',
|
||||
key: '01',
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
ctx.body = 'app2' + '01';
|
||||
return ctx;
|
||||
})
|
||||
.addTo(app2);
|
||||
|
||||
app2
|
||||
.route({
|
||||
path: 'app2',
|
||||
key: '02',
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
ctx.body = 'app2' + '02';
|
||||
return ctx;
|
||||
})
|
||||
.addTo(app2);
|
||||
const app3 = new App();
|
||||
app3
|
||||
.route({
|
||||
path: 'app3',
|
||||
key: '01',
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
ctx.body = 'app3' + '01';
|
||||
return ctx;
|
||||
})
|
||||
.addTo(app3);
|
||||
|
||||
const app = new App();
|
||||
|
||||
app.importRoutes(app1.exportRoutes());
|
||||
|
||||
app.importRoutes(app2.exportRoutes());
|
||||
|
||||
app.importApp(app3);
|
||||
|
||||
app.listen(4003, () => {
|
||||
console.log(`http://localhost:4003/api/router?path=app1&key=02`);
|
||||
console.log(`http://localhost:4003/api/router?path=app1&key=01`);
|
||||
console.log(`http://localhost:4003/api/router?path=app2&key=02`);
|
||||
console.log(`http://localhost:4003/api/router?path=app2&key=01`);
|
||||
console.log(`http://localhost:4003/api/router?path=app3&key=01`);
|
||||
});
|
@ -1,4 +1,4 @@
|
||||
import { Route, QueryRouter, RouteContext } from '@abearxiong/router';
|
||||
import { Route, QueryRouter, RouteContext } from '@kevisual/router';
|
||||
const qr = new QueryRouter();
|
||||
|
||||
qr.add(
|
||||
|
20
package.json
20
package.json
@ -1,7 +1,7 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/package",
|
||||
"name": "@kevisual/router",
|
||||
"version": "0.0.3",
|
||||
"version": "0.0.4-alpha-2",
|
||||
"description": "",
|
||||
"main": "dist/index.js",
|
||||
"module": "dist/index.js",
|
||||
@ -16,22 +16,22 @@
|
||||
"dist"
|
||||
],
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"author": "abearxiong",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@rollup/plugin-commonjs": "^26.0.1",
|
||||
"@rollup/plugin-node-resolve": "^15.2.4",
|
||||
"@rollup/plugin-typescript": "^12.1.0",
|
||||
"@rollup/plugin-commonjs": "^28.0.1",
|
||||
"@rollup/plugin-node-resolve": "^15.3.0",
|
||||
"@rollup/plugin-typescript": "^12.1.1",
|
||||
"@types/lodash-es": "^4.17.12",
|
||||
"@types/node": "^22.5.5",
|
||||
"@types/node": "^22.7.9",
|
||||
"@types/ws": "^8.5.12",
|
||||
"lodash-es": "^4.17.21",
|
||||
"nanoid": "^5.0.7",
|
||||
"rollup": "^4.22.4",
|
||||
"rollup": "^4.24.0",
|
||||
"ts-loader": "^9.5.1",
|
||||
"ts-node": "^10.9.2",
|
||||
"tslib": "^2.7.0",
|
||||
"typescript": "^5.6.2",
|
||||
"tslib": "^2.8.0",
|
||||
"typescript": "^5.6.3",
|
||||
"zod": "^3.23.8"
|
||||
},
|
||||
"repository": {
|
||||
|
@ -81,4 +81,13 @@ export class App<T = {}> {
|
||||
const router = this.router;
|
||||
return await router.parse(message, ctx);
|
||||
}
|
||||
exportRoutes() {
|
||||
return this.router.exportRoutes();
|
||||
}
|
||||
importRoutes(routes: any[]) {
|
||||
this.router.importRoutes(routes);
|
||||
}
|
||||
importApp(app: App) {
|
||||
this.importRoutes(app.exportRoutes());
|
||||
}
|
||||
}
|
||||
|
15
src/route.ts
15
src/route.ts
@ -439,7 +439,7 @@ export class QueryRouter {
|
||||
return await this.runRoute(path, key, ctx);
|
||||
}
|
||||
// clear body
|
||||
ctx.body = JSON.parse(JSON.stringify(ctx.body||''));
|
||||
ctx.body = JSON.parse(JSON.stringify(ctx.body || ''));
|
||||
if (!ctx.code) ctx.code = 200;
|
||||
return ctx;
|
||||
} else {
|
||||
@ -492,6 +492,19 @@ export class QueryRouter {
|
||||
return { code, data: body, message };
|
||||
};
|
||||
}
|
||||
exportRoutes() {
|
||||
return this.routes.map((r) => {
|
||||
return r;
|
||||
});
|
||||
}
|
||||
importRoutes(routes: Route[]) {
|
||||
for (let route of routes) {
|
||||
this.add(route);
|
||||
}
|
||||
}
|
||||
importRouter(router: QueryRouter) {
|
||||
this.importRoutes(router.routes);
|
||||
}
|
||||
}
|
||||
|
||||
type QueryRouterServerOpts = {
|
||||
|
Loading…
x
Reference in New Issue
Block a user