@kevisual/router 0.0.4-alpha-1 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,48 @@
1
- import { Schema, Rule } from './validator/index.ts';
2
- export type RouterContextT = {
1
+ import { Schema } from 'zod';
2
+ export { Schema } from 'zod';
3
+
4
+ type BaseRule = {
5
+ value?: any;
6
+ required?: boolean;
7
+ message?: string;
8
+ };
9
+ type RuleString = {
10
+ type: 'string';
11
+ minLength?: number;
12
+ maxLength?: number;
13
+ regex?: string;
14
+ } & BaseRule;
15
+ type RuleNumber = {
16
+ type: 'number';
17
+ min?: number;
18
+ max?: number;
19
+ } & BaseRule;
20
+ type RuleBoolean = {
21
+ type: 'boolean';
22
+ } & BaseRule;
23
+ type RuleArray = {
24
+ type: 'array';
25
+ items: Rule;
26
+ minItems?: number;
27
+ maxItems?: number;
28
+ } & BaseRule;
29
+ type RuleObject = {
30
+ type: 'object';
31
+ properties: {
32
+ [key: string]: Rule;
33
+ };
34
+ } & BaseRule;
35
+ type RuleAny = {
36
+ type: 'any';
37
+ } & BaseRule;
38
+ type Rule = RuleString | RuleNumber | RuleBoolean | RuleArray | RuleObject | RuleAny;
39
+ declare const createSchema: (rule: Rule) => Schema;
40
+
41
+ type RouterContextT = {
3
42
  code?: number;
4
43
  [key: string]: any;
5
44
  };
6
- export type RouteContext<T = {
45
+ type RouteContext<T = {
7
46
  code?: number;
8
47
  }, S = any> = {
9
48
  query?: {
@@ -34,10 +73,11 @@ export type RouteContext<T = {
34
73
  [key: string]: any;
35
74
  }) => Promise<any>;
36
75
  index?: number;
76
+ throw?: (code?: number | string, message?: string, tips?: string) => void;
37
77
  } & T;
38
- export type Run<T = any> = (ctx?: RouteContext<T>) => Promise<typeof ctx | null | void>;
39
- export type NextRoute = Pick<Route, 'id' | 'path' | 'key'>;
40
- export type RouteOpts = {
78
+ type Run<T = any> = (ctx: RouteContext<T>) => Promise<typeof ctx | null | void>;
79
+ type NextRoute = Pick<Route, 'id' | 'path' | 'key'>;
80
+ type RouteOpts = {
41
81
  path?: string;
42
82
  key?: string;
43
83
  id?: string;
@@ -66,10 +106,10 @@ export type RouteOpts = {
66
106
  idUsePath?: boolean;
67
107
  isDebug?: boolean;
68
108
  };
69
- export type DefineRouteOpts = Omit<RouteOpts, 'idUsePath' | 'verify' | 'verifyKey' | 'nextRoute'>;
109
+ type DefineRouteOpts = Omit<RouteOpts, 'idUsePath' | 'verify' | 'verifyKey' | 'nextRoute'>;
70
110
  declare const pickValue: readonly ["path", "key", "id", "description", "type", "validator", "middleware"];
71
- export type RouteInfo = Pick<Route, (typeof pickValue)[number]>;
72
- export declare class Route {
111
+ type RouteInfo = Pick<Route, (typeof pickValue)[number]>;
112
+ declare class Route {
73
113
  path?: string;
74
114
  key?: string;
75
115
  id?: string;
@@ -143,10 +183,12 @@ export declare class Route {
143
183
  [key: string]: any;
144
184
  }): void;
145
185
  setData(data: any): this;
186
+ throw(code?: number | string, message?: string, tips?: string): void;
146
187
  }
147
- export declare class QueryRouter {
188
+ declare class QueryRouter {
148
189
  routes: Route[];
149
190
  maxNextRoute: number;
191
+ context?: RouteContext;
150
192
  constructor();
151
193
  add(route: Route): void;
152
194
  /**
@@ -190,6 +232,7 @@ export declare class QueryRouter {
190
232
  }, ctx?: RouteContext & {
191
233
  [key: string]: any;
192
234
  }): Promise<any>;
235
+ setContext(ctx: RouteContext): Promise<void>;
193
236
  getList(): RouteInfo[];
194
237
  getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn<T>, ctx?: RouteContext): (msg: {
195
238
  path: string;
@@ -207,6 +250,8 @@ export declare class QueryRouter {
207
250
  }>;
208
251
  exportRoutes(): Route[];
209
252
  importRoutes(routes: Route[]): void;
253
+ importRouter(router: QueryRouter): void;
254
+ throw(code?: number | string, message?: string, tips?: string): void;
210
255
  }
211
256
  type QueryRouterServerOpts = {
212
257
  handleFn?: HandleFn;
@@ -228,9 +273,52 @@ interface HandleFn<T = any> {
228
273
  * QueryRouterServer
229
274
  * @description 移除server相关的功能,只保留router相关的功能,和http.createServer不相关,独立
230
275
  */
231
- export declare class QueryRouterServer extends QueryRouter {
276
+ declare class QueryRouterServer extends QueryRouter {
232
277
  handle: any;
233
278
  constructor(opts?: QueryRouterServerOpts);
234
279
  setHandle(wrapperFn?: HandleFn, ctx?: RouteContext): void;
280
+ use(path: string, fn: (ctx: any) => any, opts?: RouteOpts): void;
281
+ addRoute(route: Route): void;
282
+ Route: typeof Route;
283
+ route(opts: RouteOpts): Route;
284
+ route(path: string, key?: string): Route;
285
+ route(path: string, opts?: RouteOpts): Route;
286
+ route(path: string, key?: string, opts?: RouteOpts): Route;
287
+ call(message: {
288
+ path: string;
289
+ key: string;
290
+ payload?: any;
291
+ }, ctx?: RouteContext & {
292
+ [key: string]: any;
293
+ }): Promise<any>;
294
+ run({ path, key, payload }: {
295
+ path: string;
296
+ key: string;
297
+ payload?: any;
298
+ }): Promise<any>;
299
+ }
300
+
301
+ /** 自定义错误 */
302
+ declare class CustomError extends Error {
303
+ code?: number;
304
+ data?: any;
305
+ message: string;
306
+ tips?: string;
307
+ constructor(code?: number | string, message?: string, tips?: string);
308
+ static fromCode(code?: number): CustomError;
309
+ static fromErrorData(code?: number, data?: any): CustomError;
310
+ static parseError(e: CustomError): {
311
+ code: number;
312
+ data: any;
313
+ message: string;
314
+ tips: string;
315
+ };
316
+ parse(e?: CustomError): {
317
+ code: number;
318
+ data: any;
319
+ message: string;
320
+ tips: string;
321
+ };
235
322
  }
236
- export {};
323
+
324
+ export { CustomError, QueryRouter, QueryRouterServer, Route, type RouteContext, type RouteOpts, type Rule, type Run, createSchema };