router添加 throw
This commit is contained in:
parent
df737e5f27
commit
303c579e92
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,3 +1,6 @@
|
|||||||
node_modules
|
node_modules
|
||||||
src/app.config.json5
|
src/app.config.json5
|
||||||
dist
|
dist
|
||||||
|
.npmrc
|
||||||
|
|
||||||
|
.turbo
|
@ -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-6",
|
"version": "0.0.4-alpha-7",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"module": "dist/index.js",
|
"module": "dist/index.js",
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
import { QueryRouter, Route, RouteContext, RouteOpts } from './route.ts';
|
import { QueryRouter, Route, RouteContext, RouteOpts } from './route.ts';
|
||||||
import { Server, Cors } from './server/server.ts';
|
import { Server, Cors } from './server/server.ts';
|
||||||
import { WsServer } from './server/ws-server.ts';
|
import { WsServer } from './server/ws-server.ts';
|
||||||
|
import { CustomError } from './result/error.ts';
|
||||||
|
|
||||||
type RouterHandle = (msg: { path: string; [key: string]: any }) => { code: string; data?: any; message?: string; [key: string]: any };
|
type RouterHandle = (msg: { path: string; [key: string]: any }) => { code: string; data?: any; message?: string; [key: string]: any };
|
||||||
type AppOptions<T = {}> = {
|
type AppOptions<T = {}> = {
|
||||||
router?: QueryRouter;
|
router?: QueryRouter;
|
||||||
@ -90,4 +92,9 @@ export class App<T = {}> {
|
|||||||
importApp(app: App) {
|
importApp(app: App) {
|
||||||
this.importRoutes(app.exportRoutes());
|
this.importRoutes(app.exportRoutes());
|
||||||
}
|
}
|
||||||
|
throw(code?: number | string, message?: string, tips?: string): void;
|
||||||
|
throw(...args: any[]) {
|
||||||
|
throw new CustomError(...args);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
10
src/route.ts
10
src/route.ts
@ -32,6 +32,7 @@ export type RouteContext<T = { code?: number }, S = any> = {
|
|||||||
error?: any;
|
error?: any;
|
||||||
call?: (message: { path: string; key: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) => Promise<any>;
|
call?: (message: { path: string; key: string; payload?: any }, ctx?: RouteContext & { [key: string]: any }) => Promise<any>;
|
||||||
index?: number;
|
index?: number;
|
||||||
|
throw?: (code?: number | string, message?: string, tips?: string) => void;
|
||||||
} & T;
|
} & T;
|
||||||
|
|
||||||
export type Run<T = any> = (ctx: RouteContext<T>) => Promise<typeof ctx | null | void>;
|
export type Run<T = any> = (ctx: RouteContext<T>) => Promise<typeof ctx | null | void>;
|
||||||
@ -252,6 +253,10 @@ export class Route {
|
|||||||
this.data = data;
|
this.data = data;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
throw(code?: number | string, message?: string, tips?: string): void;
|
||||||
|
throw(...args: any[]) {
|
||||||
|
throw new CustomError(...args);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class QueryRouter {
|
export class QueryRouter {
|
||||||
@ -465,6 +470,7 @@ export class QueryRouter {
|
|||||||
ctx = ctx || {};
|
ctx = ctx || {};
|
||||||
ctx.query = { ...ctx.query, ...query, ...payload };
|
ctx.query = { ...ctx.query, ...query, ...payload };
|
||||||
ctx.state = {};
|
ctx.state = {};
|
||||||
|
ctx.throw = this.throw;
|
||||||
// put queryRouter to ctx
|
// put queryRouter to ctx
|
||||||
// TODO: 是否需要queryRouter,函数内部处理router路由执行,这应该是避免去内部去包含的功能过
|
// TODO: 是否需要queryRouter,函数内部处理router路由执行,这应该是避免去内部去包含的功能过
|
||||||
ctx.queryRouter = this;
|
ctx.queryRouter = this;
|
||||||
@ -505,6 +511,10 @@ export class QueryRouter {
|
|||||||
importRouter(router: QueryRouter) {
|
importRouter(router: QueryRouter) {
|
||||||
this.importRoutes(router.routes);
|
this.importRoutes(router.routes);
|
||||||
}
|
}
|
||||||
|
throw(code?: number | string, message?: string, tips?: string): void;
|
||||||
|
throw(...args: any[]) {
|
||||||
|
throw new CustomError(...args);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type QueryRouterServerOpts = {
|
type QueryRouterServerOpts = {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user