feat: 设置默认的context
This commit is contained in:
parent
303c579e92
commit
780d744a16
@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"$schema": "https://json.schemastore.org/package",
|
"$schema": "https://json.schemastore.org/package",
|
||||||
"name": "@kevisual/router",
|
"name": "@kevisual/router",
|
||||||
"version": "0.0.4-alpha-7",
|
"version": "0.0.4-alpha-8",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"module": "dist/index.js",
|
"module": "dist/index.js",
|
||||||
|
@ -26,7 +26,7 @@ export class App<T = {}> {
|
|||||||
const router = opts?.router || new QueryRouter();
|
const router = opts?.router || new QueryRouter();
|
||||||
const server = opts?.server || new Server(opts?.serverOptions || {});
|
const server = opts?.server || new Server(opts?.serverOptions || {});
|
||||||
server.setHandle(router.getHandle(router, opts?.routerHandle, opts?.routerContext));
|
server.setHandle(router.getHandle(router, opts?.routerHandle, opts?.routerContext));
|
||||||
|
router.setContext(opts?.routerContext);
|
||||||
this.router = router;
|
this.router = router;
|
||||||
this.server = server;
|
this.server = server;
|
||||||
if (opts?.io) {
|
if (opts?.io) {
|
||||||
@ -81,7 +81,7 @@ export class App<T = {}> {
|
|||||||
}
|
}
|
||||||
async call(message: { path: string; key: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) {
|
async call(message: { path: string; key: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) {
|
||||||
const router = this.router;
|
const router = this.router;
|
||||||
return await router.parse(message, ctx);
|
return await router.call(message, ctx);
|
||||||
}
|
}
|
||||||
exportRoutes() {
|
exportRoutes() {
|
||||||
return this.router.exportRoutes();
|
return this.router.exportRoutes();
|
||||||
@ -97,4 +97,3 @@ export class App<T = {}> {
|
|||||||
throw new CustomError(...args);
|
throw new CustomError(...args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -262,6 +262,7 @@ export class Route {
|
|||||||
export class QueryRouter {
|
export class QueryRouter {
|
||||||
routes: Route[];
|
routes: Route[];
|
||||||
maxNextRoute = 40;
|
maxNextRoute = 40;
|
||||||
|
context?: RouteContext = {}; // default context for call
|
||||||
constructor() {
|
constructor() {
|
||||||
this.routes = [];
|
this.routes = [];
|
||||||
}
|
}
|
||||||
@ -479,7 +480,10 @@ export class QueryRouter {
|
|||||||
return await this.runRoute(path, key, ctx);
|
return await this.runRoute(path, key, ctx);
|
||||||
}
|
}
|
||||||
async call(message: { path: string; key: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) {
|
async call(message: { path: string; key: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) {
|
||||||
return await this.parse(message, ctx);
|
return await this.parse(message, { ...this.context, ...ctx });
|
||||||
|
}
|
||||||
|
async setContext(ctx: RouteContext) {
|
||||||
|
this.context = ctx;
|
||||||
}
|
}
|
||||||
getList(): RouteInfo[] {
|
getList(): RouteInfo[] {
|
||||||
return this.routes.map((r) => {
|
return this.routes.map((r) => {
|
||||||
@ -534,6 +538,7 @@ export class QueryRouterServer extends QueryRouter {
|
|||||||
constructor(opts?: QueryRouterServerOpts) {
|
constructor(opts?: QueryRouterServerOpts) {
|
||||||
super();
|
super();
|
||||||
this.handle = this.getHandle(this, opts?.handleFn, opts?.context);
|
this.handle = this.getHandle(this, opts?.handleFn, opts?.context);
|
||||||
|
this.setContext(opts?.context);
|
||||||
}
|
}
|
||||||
setHandle(wrapperFn?: HandleFn, ctx?: RouteContext) {
|
setHandle(wrapperFn?: HandleFn, ctx?: RouteContext) {
|
||||||
this.handle = this.getHandle(this, wrapperFn, ctx);
|
this.handle = this.getHandle(this, wrapperFn, ctx);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user