feat: add QueryRouterServer

This commit is contained in:
xion 2024-10-31 00:45:12 +08:00
parent 158b12d811
commit 52f5f58baf
5 changed files with 106 additions and 21 deletions

View 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);
});

View File

@ -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"
}
}
}

View File

@ -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,
}),
],
},
];

View File

@ -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';

View File

@ -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;
}
}