update dependencies and router functionality
- Update package version to 0.0.34 - Upgrade @kevisual/local-proxy to 0.0.8 and @kevisual/query to 0.0.31 - Update rollup, cookie, selfsigned, and zod dependencies - Remove unused share property from Route class - Refactor queryRouter context to use app property - Improve call method logic to prioritize path + key - Update queryRoute to use call method - Enhance HttpChain class with better constructor and parse method 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
14
src/route.ts
14
src/route.ts
@@ -115,7 +115,6 @@ export class Route<U = { [key: string]: any }> {
|
||||
*/
|
||||
key?: string;
|
||||
id?: string;
|
||||
share? = false;
|
||||
run?: Run;
|
||||
nextRoute?: NextRoute; // route to run after this route
|
||||
description?: string;
|
||||
@@ -587,9 +586,7 @@ export class QueryRouter {
|
||||
ctx.query = { ...ctx.query, ...query, ...payload };
|
||||
ctx.state = { ...ctx?.state };
|
||||
ctx.throw = this.throw;
|
||||
// put queryRouter to ctx
|
||||
// TODO: 是否需要queryRouter,函数内部处理router路由执行,这应该是避免去内部去包含的功能过
|
||||
ctx.queryRouter = this;
|
||||
ctx.app = this;
|
||||
ctx.call = this.call.bind(this);
|
||||
ctx.queryRoute = this.queryRoute.bind(this);
|
||||
ctx.index = 0;
|
||||
@@ -610,7 +607,10 @@ export class QueryRouter {
|
||||
async call(message: { id?: string; path?: string; key?: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) {
|
||||
let path = message.path;
|
||||
let key = message.key;
|
||||
if (message.id) {
|
||||
// 优先 path + key
|
||||
if (path) {
|
||||
return await this.parse({ ...message, path, key }, { ...this.context, ...ctx });
|
||||
} else if (message.id) {
|
||||
const route = this.routes.find((r) => r.id === message.id);
|
||||
if (route) {
|
||||
path = route.path;
|
||||
@@ -619,8 +619,6 @@ export class QueryRouter {
|
||||
return { code: 404, body: null, message: 'Not found route' };
|
||||
}
|
||||
return await this.parse({ ...message, path, key }, { ...this.context, ...ctx });
|
||||
} else if (path) {
|
||||
return await this.parse({ ...message, path, key }, { ...this.context, ...ctx });
|
||||
} else {
|
||||
return { code: 404, body: null, message: 'Not found path' };
|
||||
}
|
||||
@@ -633,7 +631,7 @@ export class QueryRouter {
|
||||
* @returns
|
||||
*/
|
||||
async queryRoute(message: { id?: string; path: string; key?: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) {
|
||||
const res = await this.parse(message, { ...this.context, ...ctx });
|
||||
const res = await this.call(message, { ...this.context, ...ctx });
|
||||
return {
|
||||
code: res.code,
|
||||
data: res.body,
|
||||
|
||||
@@ -122,17 +122,33 @@ type HttpChainOpts = {
|
||||
req?: Req;
|
||||
res?: ServerResponse;
|
||||
simpleRouter?: SimpleRouter;
|
||||
server?: Server;
|
||||
};
|
||||
|
||||
/**
|
||||
* HttpChain 类, 用于链式调用,router.get内部使用
|
||||
*/
|
||||
|
||||
export class HttpChain {
|
||||
/**
|
||||
* 请求对象, 每一次请求都是不一样的
|
||||
*/
|
||||
req: Req;
|
||||
/**
|
||||
* 响应对象, 每一次请求响应都是不一样的
|
||||
*/
|
||||
res: ServerResponse;
|
||||
simpleRouter: SimpleRouter;
|
||||
server: Server;
|
||||
hasSetHeader: boolean = false;
|
||||
isSseSet: boolean = false;
|
||||
constructor(opts?: HttpChainOpts) {
|
||||
this.req = opts?.req;
|
||||
this.res = opts?.res;
|
||||
if (opts?.res) {
|
||||
this.res = opts.res;
|
||||
}
|
||||
if (opts?.req) {
|
||||
this.req = opts.req;
|
||||
}
|
||||
this.simpleRouter = opts?.simpleRouter;
|
||||
}
|
||||
setReq(req: Req) {
|
||||
@@ -200,7 +216,13 @@ export class HttpChain {
|
||||
this.server.listen(opts, callback);
|
||||
return this;
|
||||
}
|
||||
parse() {
|
||||
/**
|
||||
* 外部 parse 方法
|
||||
* @returns
|
||||
*/
|
||||
parse(opts?: { listenOptions?: ListenOptions, listenCallBack?: () => void }) {
|
||||
const { listenOptions, listenCallBack } = opts || {};
|
||||
|
||||
if (!this.server || !this.simpleRouter) {
|
||||
throw new Error('Server and SimpleRouter must be set before calling parse');
|
||||
}
|
||||
@@ -216,6 +238,9 @@ export class HttpChain {
|
||||
}
|
||||
}
|
||||
};
|
||||
if (listenOptions) {
|
||||
this.server.listen(listenOptions, listenCallBack);
|
||||
}
|
||||
this.server.on('request', listener);
|
||||
return () => {
|
||||
that.server.removeListener('request', listener);
|
||||
|
||||
Reference in New Issue
Block a user