@kevisual/router 0.0.3 → 0.0.4-alpha-7

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,8 +183,9 @@ 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;
150
191
  constructor();
@@ -205,6 +246,10 @@ export declare class QueryRouter {
205
246
  data: any;
206
247
  message: any;
207
248
  }>;
249
+ exportRoutes(): Route[];
250
+ importRoutes(routes: Route[]): void;
251
+ importRouter(router: QueryRouter): void;
252
+ throw(code?: number | string, message?: string, tips?: string): void;
208
253
  }
209
254
  type QueryRouterServerOpts = {
210
255
  handleFn?: HandleFn;
@@ -226,9 +271,52 @@ interface HandleFn<T = any> {
226
271
  * QueryRouterServer
227
272
  * @description 移除server相关的功能,只保留router相关的功能,和http.createServer不相关,独立
228
273
  */
229
- export declare class QueryRouterServer extends QueryRouter {
274
+ declare class QueryRouterServer extends QueryRouter {
230
275
  handle: any;
231
276
  constructor(opts?: QueryRouterServerOpts);
232
277
  setHandle(wrapperFn?: HandleFn, ctx?: RouteContext): void;
278
+ use(path: string, fn: (ctx: any) => any, opts?: RouteOpts): void;
279
+ addRoute(route: Route): void;
280
+ Route: typeof Route;
281
+ route(opts: RouteOpts): Route;
282
+ route(path: string, key?: string): Route;
283
+ route(path: string, opts?: RouteOpts): Route;
284
+ route(path: string, key?: string, opts?: RouteOpts): Route;
285
+ call(message: {
286
+ path: string;
287
+ key: string;
288
+ payload?: any;
289
+ }, ctx?: RouteContext & {
290
+ [key: string]: any;
291
+ }): Promise<any>;
292
+ run({ path, key, payload }: {
293
+ path: string;
294
+ key: string;
295
+ payload?: any;
296
+ }): Promise<any>;
297
+ }
298
+
299
+ /** 自定义错误 */
300
+ declare class CustomError extends Error {
301
+ code?: number;
302
+ data?: any;
303
+ message: string;
304
+ tips?: string;
305
+ constructor(code?: number | string, message?: string, tips?: string);
306
+ static fromCode(code?: number): CustomError;
307
+ static fromErrorData(code?: number, data?: any): CustomError;
308
+ static parseError(e: CustomError): {
309
+ code: number;
310
+ data: any;
311
+ message: string;
312
+ tips: string;
313
+ };
314
+ parse(e?: CustomError): {
315
+ code: number;
316
+ data: any;
317
+ message: string;
318
+ tips: string;
319
+ };
233
320
  }
234
- export {};
321
+
322
+ export { CustomError, QueryRouter, QueryRouterServer, Route, type RouteContext, type RouteOpts, type Rule, type Run, createSchema };