@kevisual/router 0.0.4-alpha-1 → 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.
- package/dist/{route.d.ts → router-browser.d.ts} +98 -12
- package/dist/router-browser.js +5974 -0
- package/dist/router.d.ts +507 -0
- package/dist/{index.js → router.js} +67 -41
- package/package.json +18 -6
- package/dist/app.d.ts +0 -63
- package/dist/connect.d.ts +0 -30
- package/dist/index.d.ts +0 -15
- package/dist/io.d.ts +0 -3
- package/dist/result/error.d.ts +0 -22
- package/dist/result/index.d.ts +0 -27
- package/dist/server/handle-server.d.ts +0 -10
- package/dist/server/index.d.ts +0 -2
- package/dist/server/parse-body.d.ts +0 -2
- package/dist/server/server.d.ts +0 -54
- package/dist/server/ws-server.d.ts +0 -37
- package/dist/static.d.ts +0 -1
- package/dist/utils/parse.d.ts +0 -3
- package/dist/utils/pick.d.ts +0 -1
- package/dist/validator/index.d.ts +0 -1
- package/dist/validator/rule.d.ts +0 -40
|
@@ -1,9 +1,48 @@
|
|
|
1
|
-
import { Schema
|
|
2
|
-
export
|
|
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
|
-
|
|
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
|
-
|
|
39
|
-
|
|
40
|
-
|
|
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
|
-
|
|
109
|
+
type DefineRouteOpts = Omit<RouteOpts, 'idUsePath' | 'verify' | 'verifyKey' | 'nextRoute'>;
|
|
70
110
|
declare const pickValue: readonly ["path", "key", "id", "description", "type", "validator", "middleware"];
|
|
71
|
-
|
|
72
|
-
|
|
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
|
-
|
|
188
|
+
declare class QueryRouter {
|
|
148
189
|
routes: Route[];
|
|
149
190
|
maxNextRoute: number;
|
|
150
191
|
constructor();
|
|
@@ -207,6 +248,8 @@ export declare class QueryRouter {
|
|
|
207
248
|
}>;
|
|
208
249
|
exportRoutes(): Route[];
|
|
209
250
|
importRoutes(routes: Route[]): void;
|
|
251
|
+
importRouter(router: QueryRouter): void;
|
|
252
|
+
throw(code?: number | string, message?: string, tips?: string): void;
|
|
210
253
|
}
|
|
211
254
|
type QueryRouterServerOpts = {
|
|
212
255
|
handleFn?: HandleFn;
|
|
@@ -228,9 +271,52 @@ interface HandleFn<T = any> {
|
|
|
228
271
|
* QueryRouterServer
|
|
229
272
|
* @description 移除server相关的功能,只保留router相关的功能,和http.createServer不相关,独立
|
|
230
273
|
*/
|
|
231
|
-
|
|
274
|
+
declare class QueryRouterServer extends QueryRouter {
|
|
232
275
|
handle: any;
|
|
233
276
|
constructor(opts?: QueryRouterServerOpts);
|
|
234
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
|
+
};
|
|
235
320
|
}
|
|
236
|
-
|
|
321
|
+
|
|
322
|
+
export { CustomError, QueryRouter, QueryRouterServer, Route, type RouteContext, type RouteOpts, type Rule, type Run, createSchema };
|