feat: add QueryRouterServer
This commit is contained in:
parent
158b12d811
commit
52f5f58baf
22
demo/simple/src/browser/app.ts
Normal file
22
demo/simple/src/browser/app.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import { QueryRouterServer } from '@kevisual/router';
|
||||
|
||||
const router = new QueryRouterServer();
|
||||
|
||||
router
|
||||
.route({
|
||||
path: 'hello',
|
||||
key: 'world',
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
ctx.body = 'Hello, world!';
|
||||
})
|
||||
.addTo(router);
|
||||
|
||||
router
|
||||
.run({
|
||||
path: 'hello',
|
||||
key: 'world',
|
||||
})
|
||||
.then((res) => {
|
||||
console.log(res);
|
||||
});
|
12
package.json
12
package.json
@ -1,7 +1,7 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/package",
|
||||
"name": "@kevisual/router",
|
||||
"version": "0.0.4-alpha-2",
|
||||
"version": "0.0.4-alpha-3",
|
||||
"description": "",
|
||||
"main": "dist/index.js",
|
||||
"module": "dist/index.js",
|
||||
@ -43,5 +43,15 @@
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
},
|
||||
"exports": {
|
||||
".": {
|
||||
"import": "./dist/index.js",
|
||||
"require": "./dist/index.js"
|
||||
},
|
||||
"./browser": {
|
||||
"import": "./dist/route.js",
|
||||
"require": "./dist/route.js"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,19 +6,39 @@ import commonjs from '@rollup/plugin-commonjs';
|
||||
/**
|
||||
* @type {import('rollup').RollupOptions}
|
||||
*/
|
||||
export default {
|
||||
input: 'src/index.ts', // TypeScript 入口文件
|
||||
output: {
|
||||
file: 'dist/index.js', // 输出文件
|
||||
format: 'es', // 输出格式设置为 ES 模块
|
||||
export default [
|
||||
{
|
||||
input: 'src/index.ts', // TypeScript 入口文件
|
||||
output: {
|
||||
file: 'dist/index.js', // 输出文件
|
||||
format: 'es', // 输出格式设置为 ES 模块
|
||||
},
|
||||
plugins: [
|
||||
resolve(), // 使用 @rollup/plugin-node-resolve 解析 node_modules 中的模块
|
||||
// commonjs(),
|
||||
typescript({
|
||||
allowImportingTsExtensions: true,
|
||||
noEmit: true,
|
||||
}), // 使用 @rollup/plugin-typescript 处理 TypeScript 文件
|
||||
],
|
||||
external: ['ws'],
|
||||
},
|
||||
plugins: [
|
||||
resolve(), // 使用 @rollup/plugin-node-resolve 解析 node_modules 中的模块
|
||||
// commonjs(),
|
||||
typescript({
|
||||
allowImportingTsExtensions: true,
|
||||
noEmit: true,
|
||||
}), // 使用 @rollup/plugin-typescript 处理 TypeScript 文件
|
||||
],
|
||||
external: ['ws']
|
||||
};
|
||||
{
|
||||
input: 'src/route.ts',
|
||||
output: {
|
||||
file: 'dist/route.js',
|
||||
format: 'es',
|
||||
},
|
||||
plugins: [
|
||||
resolve({
|
||||
browser: true,
|
||||
}),
|
||||
commonjs(),
|
||||
typescript({
|
||||
allowImportingTsExtensions: true,
|
||||
noEmit: true,
|
||||
declaration: false,
|
||||
}),
|
||||
],
|
||||
},
|
||||
];
|
||||
|
@ -11,11 +11,6 @@ export { Server, handleServer } from './server/index.ts';
|
||||
*/
|
||||
export { CustomError } from './result/error.ts';
|
||||
|
||||
/**
|
||||
* 返回结果
|
||||
*/
|
||||
export { Result } from './result/index.ts';
|
||||
|
||||
export { Rule, Schema, createSchema } from './validator/index.ts';
|
||||
|
||||
export { App } from './app.ts';
|
||||
|
38
src/route.ts
38
src/route.ts
@ -528,4 +528,42 @@ export class QueryRouterServer extends QueryRouter {
|
||||
setHandle(wrapperFn?: HandleFn, ctx?: RouteContext) {
|
||||
this.handle = this.getHandle(this, wrapperFn, ctx);
|
||||
}
|
||||
use(path: string, fn: (ctx: any) => any, opts?: RouteOpts) {
|
||||
const route = new Route(path, '', opts);
|
||||
route.run = fn;
|
||||
this.add(route);
|
||||
}
|
||||
addRoute(route: Route) {
|
||||
this.add(route);
|
||||
}
|
||||
|
||||
Route = Route;
|
||||
route(opts: RouteOpts): Route;
|
||||
route(path: string, key?: string): Route;
|
||||
route(path: string, opts?: RouteOpts): Route;
|
||||
route(path: string, key?: string, opts?: RouteOpts): Route;
|
||||
route(...args: any[]) {
|
||||
const [path, key, opts] = args;
|
||||
if (typeof path === 'object') {
|
||||
return new Route(path.path, path.key, path);
|
||||
}
|
||||
if (typeof path === 'string') {
|
||||
if (opts) {
|
||||
return new Route(path, key, opts);
|
||||
}
|
||||
if (key && typeof key === 'object') {
|
||||
return new Route(path, key?.key || '', key);
|
||||
}
|
||||
return new Route(path, key);
|
||||
}
|
||||
return new Route(path, key, opts);
|
||||
}
|
||||
async call(message: { path: string; key: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) {
|
||||
return await this.parse(message, ctx);
|
||||
}
|
||||
async run({ path, key, payload }: { path: string; key: string; payload?: any }) {
|
||||
const handle = this.handle;
|
||||
const end = handle({ path, key, ...payload });
|
||||
return end;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user