@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.
- package/dist/{route.d.ts → router-browser.d.ts} +100 -12
- package/dist/router-browser.js +5974 -0
- package/dist/router.d.ts +507 -0
- package/dist/{index.js → router.js} +86 -41
- package/package.json +24 -12
- package/dist/app.d.ts +0 -60
- 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
package/dist/router.d.ts
ADDED
|
@@ -0,0 +1,507 @@
|
|
|
1
|
+
import { Schema } from 'zod';
|
|
2
|
+
export { Schema } from 'zod';
|
|
3
|
+
import http, { IncomingMessage, ServerResponse } from 'http';
|
|
4
|
+
import { WebSocketServer, WebSocket } from 'ws';
|
|
5
|
+
|
|
6
|
+
type BaseRule = {
|
|
7
|
+
value?: any;
|
|
8
|
+
required?: boolean;
|
|
9
|
+
message?: string;
|
|
10
|
+
};
|
|
11
|
+
type RuleString = {
|
|
12
|
+
type: 'string';
|
|
13
|
+
minLength?: number;
|
|
14
|
+
maxLength?: number;
|
|
15
|
+
regex?: string;
|
|
16
|
+
} & BaseRule;
|
|
17
|
+
type RuleNumber = {
|
|
18
|
+
type: 'number';
|
|
19
|
+
min?: number;
|
|
20
|
+
max?: number;
|
|
21
|
+
} & BaseRule;
|
|
22
|
+
type RuleBoolean = {
|
|
23
|
+
type: 'boolean';
|
|
24
|
+
} & BaseRule;
|
|
25
|
+
type RuleArray = {
|
|
26
|
+
type: 'array';
|
|
27
|
+
items: Rule;
|
|
28
|
+
minItems?: number;
|
|
29
|
+
maxItems?: number;
|
|
30
|
+
} & BaseRule;
|
|
31
|
+
type RuleObject = {
|
|
32
|
+
type: 'object';
|
|
33
|
+
properties: {
|
|
34
|
+
[key: string]: Rule;
|
|
35
|
+
};
|
|
36
|
+
} & BaseRule;
|
|
37
|
+
type RuleAny = {
|
|
38
|
+
type: 'any';
|
|
39
|
+
} & BaseRule;
|
|
40
|
+
type Rule = RuleString | RuleNumber | RuleBoolean | RuleArray | RuleObject | RuleAny;
|
|
41
|
+
declare const createSchema: (rule: Rule) => Schema;
|
|
42
|
+
|
|
43
|
+
type RouterContextT = {
|
|
44
|
+
code?: number;
|
|
45
|
+
[key: string]: any;
|
|
46
|
+
};
|
|
47
|
+
type RouteContext<T = {
|
|
48
|
+
code?: number;
|
|
49
|
+
}, S = any> = {
|
|
50
|
+
query?: {
|
|
51
|
+
[key: string]: any;
|
|
52
|
+
};
|
|
53
|
+
/** return body */
|
|
54
|
+
body?: number | string | Object;
|
|
55
|
+
/** return code */
|
|
56
|
+
code?: number;
|
|
57
|
+
/** return msg */
|
|
58
|
+
message?: string;
|
|
59
|
+
state?: S;
|
|
60
|
+
currentPath?: string;
|
|
61
|
+
currentKey?: string;
|
|
62
|
+
currentRoute?: Route;
|
|
63
|
+
progress?: [[string, string]][];
|
|
64
|
+
nextQuery?: {
|
|
65
|
+
[key: string]: any;
|
|
66
|
+
};
|
|
67
|
+
end?: boolean;
|
|
68
|
+
queryRouter?: QueryRouter;
|
|
69
|
+
error?: any;
|
|
70
|
+
call?: (message: {
|
|
71
|
+
path: string;
|
|
72
|
+
key: string;
|
|
73
|
+
payload?: any;
|
|
74
|
+
}, ctx?: RouteContext & {
|
|
75
|
+
[key: string]: any;
|
|
76
|
+
}) => Promise<any>;
|
|
77
|
+
index?: number;
|
|
78
|
+
throw?: (code?: number | string, message?: string, tips?: string) => void;
|
|
79
|
+
} & T;
|
|
80
|
+
type Run<T = any> = (ctx: RouteContext<T>) => Promise<typeof ctx | null | void>;
|
|
81
|
+
type NextRoute = Pick<Route, 'id' | 'path' | 'key'>;
|
|
82
|
+
type RouteOpts = {
|
|
83
|
+
path?: string;
|
|
84
|
+
key?: string;
|
|
85
|
+
id?: string;
|
|
86
|
+
run?: Run;
|
|
87
|
+
nextRoute?: NextRoute;
|
|
88
|
+
description?: string;
|
|
89
|
+
middleware?: Route[] | string[];
|
|
90
|
+
type?: 'route' | 'middleware';
|
|
91
|
+
/**
|
|
92
|
+
* validator: {
|
|
93
|
+
* packageName: {
|
|
94
|
+
* type: 'string',
|
|
95
|
+
* required: true,
|
|
96
|
+
* },
|
|
97
|
+
* }
|
|
98
|
+
*/
|
|
99
|
+
validator?: {
|
|
100
|
+
[key: string]: Rule;
|
|
101
|
+
};
|
|
102
|
+
schema?: {
|
|
103
|
+
[key: string]: Schema<any>;
|
|
104
|
+
};
|
|
105
|
+
isVerify?: boolean;
|
|
106
|
+
verify?: (ctx?: RouteContext, dev?: boolean) => boolean;
|
|
107
|
+
verifyKey?: (key: string, ctx?: RouteContext, dev?: boolean) => boolean;
|
|
108
|
+
idUsePath?: boolean;
|
|
109
|
+
isDebug?: boolean;
|
|
110
|
+
};
|
|
111
|
+
type DefineRouteOpts = Omit<RouteOpts, 'idUsePath' | 'verify' | 'verifyKey' | 'nextRoute'>;
|
|
112
|
+
declare const pickValue: readonly ["path", "key", "id", "description", "type", "validator", "middleware"];
|
|
113
|
+
type RouteInfo = Pick<Route, (typeof pickValue)[number]>;
|
|
114
|
+
declare class Route {
|
|
115
|
+
path?: string;
|
|
116
|
+
key?: string;
|
|
117
|
+
id?: string;
|
|
118
|
+
share?: boolean;
|
|
119
|
+
run?: Run;
|
|
120
|
+
nextRoute?: NextRoute;
|
|
121
|
+
description?: string;
|
|
122
|
+
middleware?: (Route | string)[];
|
|
123
|
+
type?: string;
|
|
124
|
+
private _validator?;
|
|
125
|
+
schema?: {
|
|
126
|
+
[key: string]: Schema<any>;
|
|
127
|
+
};
|
|
128
|
+
data?: any;
|
|
129
|
+
isVerify?: boolean;
|
|
130
|
+
isDebug?: boolean;
|
|
131
|
+
constructor(path: string, key?: string, opts?: RouteOpts);
|
|
132
|
+
private createSchema;
|
|
133
|
+
/**
|
|
134
|
+
* set validator and create schema
|
|
135
|
+
* @param validator
|
|
136
|
+
*/
|
|
137
|
+
set validator(validator: {
|
|
138
|
+
[key: string]: Rule;
|
|
139
|
+
});
|
|
140
|
+
get validator(): {
|
|
141
|
+
[key: string]: Rule;
|
|
142
|
+
};
|
|
143
|
+
/**
|
|
144
|
+
* has code, body, message in ctx, return ctx if has error
|
|
145
|
+
* @param ctx
|
|
146
|
+
* @param dev
|
|
147
|
+
* @returns
|
|
148
|
+
*/
|
|
149
|
+
verify(ctx: RouteContext, dev?: boolean): void;
|
|
150
|
+
/**
|
|
151
|
+
* Need to manully call return ctx fn and configure body, code, message
|
|
152
|
+
* @param key
|
|
153
|
+
* @param ctx
|
|
154
|
+
* @param dev
|
|
155
|
+
* @returns
|
|
156
|
+
*/
|
|
157
|
+
verifyKey(key: string, ctx: RouteContext, dev?: boolean): {
|
|
158
|
+
message: string;
|
|
159
|
+
path: string;
|
|
160
|
+
key: string;
|
|
161
|
+
error: any;
|
|
162
|
+
} | {
|
|
163
|
+
message: string;
|
|
164
|
+
path: string;
|
|
165
|
+
key: string;
|
|
166
|
+
error?: undefined;
|
|
167
|
+
};
|
|
168
|
+
setValidator(validator: {
|
|
169
|
+
[key: string]: Rule;
|
|
170
|
+
}): this;
|
|
171
|
+
define<T extends {
|
|
172
|
+
[key: string]: any;
|
|
173
|
+
} = RouterContextT>(opts: DefineRouteOpts): this;
|
|
174
|
+
define<T extends {
|
|
175
|
+
[key: string]: any;
|
|
176
|
+
} = RouterContextT>(fn: Run<T>): this;
|
|
177
|
+
define<T extends {
|
|
178
|
+
[key: string]: any;
|
|
179
|
+
} = RouterContextT>(key: string, fn: Run<T>): this;
|
|
180
|
+
define<T extends {
|
|
181
|
+
[key: string]: any;
|
|
182
|
+
} = RouterContextT>(path: string, key: string, fn: Run<T>): this;
|
|
183
|
+
addTo(router: QueryRouter | {
|
|
184
|
+
add: (route: Route) => void;
|
|
185
|
+
[key: string]: any;
|
|
186
|
+
}): void;
|
|
187
|
+
setData(data: any): this;
|
|
188
|
+
throw(code?: number | string, message?: string, tips?: string): void;
|
|
189
|
+
}
|
|
190
|
+
declare class QueryRouter {
|
|
191
|
+
routes: Route[];
|
|
192
|
+
maxNextRoute: number;
|
|
193
|
+
constructor();
|
|
194
|
+
add(route: Route): void;
|
|
195
|
+
/**
|
|
196
|
+
* remove route by path and key
|
|
197
|
+
* @param route
|
|
198
|
+
*/
|
|
199
|
+
remove(route: Route | {
|
|
200
|
+
path: string;
|
|
201
|
+
key: string;
|
|
202
|
+
}): void;
|
|
203
|
+
/**
|
|
204
|
+
* remove route by id
|
|
205
|
+
* @param uniqueId
|
|
206
|
+
*/
|
|
207
|
+
removeById(unique: string): void;
|
|
208
|
+
/**
|
|
209
|
+
* 执行route
|
|
210
|
+
* @param path
|
|
211
|
+
* @param key
|
|
212
|
+
* @param ctx
|
|
213
|
+
* @returns
|
|
214
|
+
*/
|
|
215
|
+
runRoute(path: string, key: string, ctx?: RouteContext): any;
|
|
216
|
+
/**
|
|
217
|
+
* 第一次执行
|
|
218
|
+
* @param message
|
|
219
|
+
* @param ctx
|
|
220
|
+
* @returns
|
|
221
|
+
*/
|
|
222
|
+
parse(message: {
|
|
223
|
+
path: string;
|
|
224
|
+
key?: string;
|
|
225
|
+
payload?: any;
|
|
226
|
+
}, ctx?: RouteContext & {
|
|
227
|
+
[key: string]: any;
|
|
228
|
+
}): Promise<any>;
|
|
229
|
+
call(message: {
|
|
230
|
+
path: string;
|
|
231
|
+
key: string;
|
|
232
|
+
payload?: any;
|
|
233
|
+
}, ctx?: RouteContext & {
|
|
234
|
+
[key: string]: any;
|
|
235
|
+
}): Promise<any>;
|
|
236
|
+
getList(): RouteInfo[];
|
|
237
|
+
getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn<T>, ctx?: RouteContext): (msg: {
|
|
238
|
+
path: string;
|
|
239
|
+
key?: string;
|
|
240
|
+
[key: string]: any;
|
|
241
|
+
}) => Promise<{
|
|
242
|
+
[key: string]: any;
|
|
243
|
+
code: string;
|
|
244
|
+
data?: any;
|
|
245
|
+
message?: string;
|
|
246
|
+
} | {
|
|
247
|
+
code: any;
|
|
248
|
+
data: any;
|
|
249
|
+
message: any;
|
|
250
|
+
}>;
|
|
251
|
+
exportRoutes(): Route[];
|
|
252
|
+
importRoutes(routes: Route[]): void;
|
|
253
|
+
importRouter(router: QueryRouter): void;
|
|
254
|
+
throw(code?: number | string, message?: string, tips?: string): void;
|
|
255
|
+
}
|
|
256
|
+
type QueryRouterServerOpts = {
|
|
257
|
+
handleFn?: HandleFn;
|
|
258
|
+
context?: RouteContext;
|
|
259
|
+
};
|
|
260
|
+
interface HandleFn<T = any> {
|
|
261
|
+
(msg: {
|
|
262
|
+
path: string;
|
|
263
|
+
[key: string]: any;
|
|
264
|
+
}, ctx?: any): {
|
|
265
|
+
code: string;
|
|
266
|
+
data?: any;
|
|
267
|
+
message?: string;
|
|
268
|
+
[key: string]: any;
|
|
269
|
+
};
|
|
270
|
+
(res: RouteContext<T>): any;
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* QueryRouterServer
|
|
274
|
+
* @description 移除server相关的功能,只保留router相关的功能,和http.createServer不相关,独立
|
|
275
|
+
*/
|
|
276
|
+
declare class QueryRouterServer extends QueryRouter {
|
|
277
|
+
handle: any;
|
|
278
|
+
constructor(opts?: QueryRouterServerOpts);
|
|
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
|
+
declare class Connect {
|
|
302
|
+
path: string;
|
|
303
|
+
key?: string;
|
|
304
|
+
_fn?: (ctx?: RouteContext) => Promise<RouteContext>;
|
|
305
|
+
description?: string;
|
|
306
|
+
connects: {
|
|
307
|
+
path: string;
|
|
308
|
+
key?: string;
|
|
309
|
+
}[];
|
|
310
|
+
share: boolean;
|
|
311
|
+
constructor(path: string);
|
|
312
|
+
use(path: string): void;
|
|
313
|
+
useList(paths: string[]): void;
|
|
314
|
+
useConnect(connect: Connect): void;
|
|
315
|
+
useConnectList(connects: Connect[]): void;
|
|
316
|
+
getPathList(): string[];
|
|
317
|
+
set fn(fn: (ctx?: RouteContext) => Promise<RouteContext>);
|
|
318
|
+
get fn(): (ctx?: RouteContext) => Promise<RouteContext>;
|
|
319
|
+
}
|
|
320
|
+
declare class QueryConnect {
|
|
321
|
+
connects: Connect[];
|
|
322
|
+
constructor();
|
|
323
|
+
add(connect: Connect): void;
|
|
324
|
+
remove(connect: Connect): void;
|
|
325
|
+
getList(): {
|
|
326
|
+
path: string;
|
|
327
|
+
key: string;
|
|
328
|
+
}[];
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
type Listener = (...args: any[]) => void;
|
|
332
|
+
type Cors = {
|
|
333
|
+
/**
|
|
334
|
+
* @default '*''
|
|
335
|
+
*/
|
|
336
|
+
origin?: string | undefined;
|
|
337
|
+
};
|
|
338
|
+
type ServerOpts = {
|
|
339
|
+
/**path default `/api/router` */
|
|
340
|
+
path?: string;
|
|
341
|
+
/**handle Fn */
|
|
342
|
+
handle?: (msg?: {
|
|
343
|
+
path: string;
|
|
344
|
+
key?: string;
|
|
345
|
+
[key: string]: any;
|
|
346
|
+
}) => any;
|
|
347
|
+
cors?: Cors;
|
|
348
|
+
};
|
|
349
|
+
declare class Server {
|
|
350
|
+
path: string;
|
|
351
|
+
private _server;
|
|
352
|
+
handle: ServerOpts['handle'];
|
|
353
|
+
private _callback;
|
|
354
|
+
private cors;
|
|
355
|
+
private hasOn;
|
|
356
|
+
constructor(opts?: ServerOpts);
|
|
357
|
+
listen(port: number, hostname?: string, backlog?: number, listeningListener?: () => void): void;
|
|
358
|
+
listen(port: number, hostname?: string, listeningListener?: () => void): void;
|
|
359
|
+
listen(port: number, backlog?: number, listeningListener?: () => void): void;
|
|
360
|
+
listen(port: number, listeningListener?: () => void): void;
|
|
361
|
+
listen(path: string, backlog?: number, listeningListener?: () => void): void;
|
|
362
|
+
listen(path: string, listeningListener?: () => void): void;
|
|
363
|
+
listen(handle: any, backlog?: number, listeningListener?: () => void): void;
|
|
364
|
+
listen(handle: any, listeningListener?: () => void): void;
|
|
365
|
+
setHandle(handle?: any): void;
|
|
366
|
+
/**
|
|
367
|
+
* get callback
|
|
368
|
+
* @returns
|
|
369
|
+
*/
|
|
370
|
+
createCallback(): (req: IncomingMessage, res: ServerResponse) => Promise<void>;
|
|
371
|
+
get handleServer(): any;
|
|
372
|
+
set handleServer(fn: any);
|
|
373
|
+
/**
|
|
374
|
+
* 兜底监听,当除开 `/api/router` 之外的请求,框架只监听一个api,所以有其他的请求都执行其他的监听
|
|
375
|
+
* @description 主要是为了兼容其他的监听
|
|
376
|
+
* @param listener
|
|
377
|
+
*/
|
|
378
|
+
on(listener: Listener | Listener[]): void;
|
|
379
|
+
get callback(): any;
|
|
380
|
+
get server(): http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/**
|
|
384
|
+
* get params and body
|
|
385
|
+
* @param req
|
|
386
|
+
* @param res
|
|
387
|
+
* @returns
|
|
388
|
+
*/
|
|
389
|
+
declare const handleServer: (req: IncomingMessage, res: ServerResponse) => Promise<{
|
|
390
|
+
token: string;
|
|
391
|
+
}>;
|
|
392
|
+
|
|
393
|
+
/** 自定义错误 */
|
|
394
|
+
declare class CustomError extends Error {
|
|
395
|
+
code?: number;
|
|
396
|
+
data?: any;
|
|
397
|
+
message: string;
|
|
398
|
+
tips?: string;
|
|
399
|
+
constructor(code?: number | string, message?: string, tips?: string);
|
|
400
|
+
static fromCode(code?: number): CustomError;
|
|
401
|
+
static fromErrorData(code?: number, data?: any): CustomError;
|
|
402
|
+
static parseError(e: CustomError): {
|
|
403
|
+
code: number;
|
|
404
|
+
data: any;
|
|
405
|
+
message: string;
|
|
406
|
+
tips: string;
|
|
407
|
+
};
|
|
408
|
+
parse(e?: CustomError): {
|
|
409
|
+
code: number;
|
|
410
|
+
data: any;
|
|
411
|
+
message: string;
|
|
412
|
+
tips: string;
|
|
413
|
+
};
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
type WsServerBaseOpts = {
|
|
417
|
+
wss?: WebSocketServer;
|
|
418
|
+
path?: string;
|
|
419
|
+
};
|
|
420
|
+
type ListenerFn = (message: {
|
|
421
|
+
data: Record<string, any>;
|
|
422
|
+
ws: WebSocket;
|
|
423
|
+
end: (data: any) => any;
|
|
424
|
+
}) => Promise<any>;
|
|
425
|
+
declare class WsServerBase {
|
|
426
|
+
wss: WebSocketServer;
|
|
427
|
+
path: string;
|
|
428
|
+
listeners: {
|
|
429
|
+
type: string;
|
|
430
|
+
listener: ListenerFn;
|
|
431
|
+
}[];
|
|
432
|
+
listening: boolean;
|
|
433
|
+
constructor(opts: WsServerBaseOpts);
|
|
434
|
+
setPath(path: string): void;
|
|
435
|
+
listen(): void;
|
|
436
|
+
addListener(type: string, listener: ListenerFn): void;
|
|
437
|
+
removeListener(type: string): void;
|
|
438
|
+
}
|
|
439
|
+
declare class WsServer extends WsServerBase {
|
|
440
|
+
server: Server;
|
|
441
|
+
constructor(server: Server, opts?: any);
|
|
442
|
+
initListener(): void;
|
|
443
|
+
listen(): void;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
type RouterHandle = (msg: {
|
|
447
|
+
path: string;
|
|
448
|
+
[key: string]: any;
|
|
449
|
+
}) => {
|
|
450
|
+
code: string;
|
|
451
|
+
data?: any;
|
|
452
|
+
message?: string;
|
|
453
|
+
[key: string]: any;
|
|
454
|
+
};
|
|
455
|
+
type AppOptions<T = {}> = {
|
|
456
|
+
router?: QueryRouter;
|
|
457
|
+
server?: Server;
|
|
458
|
+
/** handle msg 关联 */
|
|
459
|
+
routerHandle?: RouterHandle;
|
|
460
|
+
routerContext?: RouteContext<T>;
|
|
461
|
+
serverOptions?: {
|
|
462
|
+
path?: string;
|
|
463
|
+
cors?: Cors;
|
|
464
|
+
handle?: any;
|
|
465
|
+
};
|
|
466
|
+
io?: boolean;
|
|
467
|
+
ioOpts?: {
|
|
468
|
+
routerHandle?: RouterHandle;
|
|
469
|
+
routerContext?: RouteContext<T>;
|
|
470
|
+
path?: string;
|
|
471
|
+
};
|
|
472
|
+
};
|
|
473
|
+
declare class App<T = {}> {
|
|
474
|
+
router: QueryRouter;
|
|
475
|
+
server: Server;
|
|
476
|
+
io: WsServer;
|
|
477
|
+
constructor(opts?: AppOptions<T>);
|
|
478
|
+
listen(port: number, hostname?: string, backlog?: number, listeningListener?: () => void): void;
|
|
479
|
+
listen(port: number, hostname?: string, listeningListener?: () => void): void;
|
|
480
|
+
listen(port: number, backlog?: number, listeningListener?: () => void): void;
|
|
481
|
+
listen(port: number, listeningListener?: () => void): void;
|
|
482
|
+
listen(path: string, backlog?: number, listeningListener?: () => void): void;
|
|
483
|
+
listen(path: string, listeningListener?: () => void): void;
|
|
484
|
+
listen(handle: any, backlog?: number, listeningListener?: () => void): void;
|
|
485
|
+
listen(handle: any, listeningListener?: () => void): void;
|
|
486
|
+
use(path: string, fn: (ctx: any) => any, opts?: RouteOpts): void;
|
|
487
|
+
addRoute(route: Route): void;
|
|
488
|
+
add: (route: Route) => void;
|
|
489
|
+
Route: typeof Route;
|
|
490
|
+
route(opts: RouteOpts): Route;
|
|
491
|
+
route(path: string, key?: string): Route;
|
|
492
|
+
route(path: string, opts?: RouteOpts): Route;
|
|
493
|
+
route(path: string, key?: string, opts?: RouteOpts): Route;
|
|
494
|
+
call(message: {
|
|
495
|
+
path: string;
|
|
496
|
+
key: string;
|
|
497
|
+
payload?: any;
|
|
498
|
+
}, ctx?: RouteContext & {
|
|
499
|
+
[key: string]: any;
|
|
500
|
+
}): Promise<any>;
|
|
501
|
+
exportRoutes(): Route[];
|
|
502
|
+
importRoutes(routes: any[]): void;
|
|
503
|
+
importApp(app: App): void;
|
|
504
|
+
throw(code?: number | string, message?: string, tips?: string): void;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
export { App, Connect, CustomError, QueryConnect, QueryRouter, QueryRouterServer, Route, type RouteContext, type RouteOpts, type Rule, type Run, Server, createSchema, handleServer };
|
|
@@ -5652,6 +5652,9 @@ class Route {
|
|
|
5652
5652
|
this.data = data;
|
|
5653
5653
|
return this;
|
|
5654
5654
|
}
|
|
5655
|
+
throw(...args) {
|
|
5656
|
+
throw new CustomError(...args);
|
|
5657
|
+
}
|
|
5655
5658
|
}
|
|
5656
5659
|
class QueryRouter {
|
|
5657
5660
|
routes;
|
|
@@ -5873,6 +5876,7 @@ class QueryRouter {
|
|
|
5873
5876
|
ctx = ctx || {};
|
|
5874
5877
|
ctx.query = { ...ctx.query, ...query, ...payload };
|
|
5875
5878
|
ctx.state = {};
|
|
5879
|
+
ctx.throw = this.throw;
|
|
5876
5880
|
// put queryRouter to ctx
|
|
5877
5881
|
// TODO: 是否需要queryRouter,函数内部处理router路由执行,这应该是避免去内部去包含的功能过
|
|
5878
5882
|
ctx.queryRouter = this;
|
|
@@ -5900,6 +5904,22 @@ class QueryRouter {
|
|
|
5900
5904
|
return { code, data: body, message };
|
|
5901
5905
|
};
|
|
5902
5906
|
}
|
|
5907
|
+
exportRoutes() {
|
|
5908
|
+
return this.routes.map((r) => {
|
|
5909
|
+
return r;
|
|
5910
|
+
});
|
|
5911
|
+
}
|
|
5912
|
+
importRoutes(routes) {
|
|
5913
|
+
for (let route of routes) {
|
|
5914
|
+
this.add(route);
|
|
5915
|
+
}
|
|
5916
|
+
}
|
|
5917
|
+
importRouter(router) {
|
|
5918
|
+
this.importRoutes(router.routes);
|
|
5919
|
+
}
|
|
5920
|
+
throw(...args) {
|
|
5921
|
+
throw new CustomError(...args);
|
|
5922
|
+
}
|
|
5903
5923
|
}
|
|
5904
5924
|
/**
|
|
5905
5925
|
* QueryRouterServer
|
|
@@ -5914,6 +5934,59 @@ class QueryRouterServer extends QueryRouter {
|
|
|
5914
5934
|
setHandle(wrapperFn, ctx) {
|
|
5915
5935
|
this.handle = this.getHandle(this, wrapperFn, ctx);
|
|
5916
5936
|
}
|
|
5937
|
+
use(path, fn, opts) {
|
|
5938
|
+
const route = new Route(path, '', opts);
|
|
5939
|
+
route.run = fn;
|
|
5940
|
+
this.add(route);
|
|
5941
|
+
}
|
|
5942
|
+
addRoute(route) {
|
|
5943
|
+
this.add(route);
|
|
5944
|
+
}
|
|
5945
|
+
Route = Route;
|
|
5946
|
+
route(...args) {
|
|
5947
|
+
const [path, key, opts] = args;
|
|
5948
|
+
if (typeof path === 'object') {
|
|
5949
|
+
return new Route(path.path, path.key, path);
|
|
5950
|
+
}
|
|
5951
|
+
if (typeof path === 'string') {
|
|
5952
|
+
if (opts) {
|
|
5953
|
+
return new Route(path, key, opts);
|
|
5954
|
+
}
|
|
5955
|
+
if (key && typeof key === 'object') {
|
|
5956
|
+
return new Route(path, key?.key || '', key);
|
|
5957
|
+
}
|
|
5958
|
+
return new Route(path, key);
|
|
5959
|
+
}
|
|
5960
|
+
return new Route(path, key, opts);
|
|
5961
|
+
}
|
|
5962
|
+
async call(message, ctx) {
|
|
5963
|
+
return await this.parse(message, ctx);
|
|
5964
|
+
}
|
|
5965
|
+
async run({ path, key, payload }) {
|
|
5966
|
+
const handle = this.handle;
|
|
5967
|
+
const resultError = (error, code = 500) => {
|
|
5968
|
+
const r = {
|
|
5969
|
+
code: code,
|
|
5970
|
+
message: error,
|
|
5971
|
+
};
|
|
5972
|
+
return r;
|
|
5973
|
+
};
|
|
5974
|
+
try {
|
|
5975
|
+
const end = handle({ path, key, ...payload });
|
|
5976
|
+
return end;
|
|
5977
|
+
}
|
|
5978
|
+
catch (e) {
|
|
5979
|
+
if (e.code && typeof e.code === 'number') {
|
|
5980
|
+
return {
|
|
5981
|
+
code: e.code,
|
|
5982
|
+
message: e.message,
|
|
5983
|
+
};
|
|
5984
|
+
}
|
|
5985
|
+
else {
|
|
5986
|
+
return resultError('Router Server error');
|
|
5987
|
+
}
|
|
5988
|
+
}
|
|
5989
|
+
}
|
|
5917
5990
|
}
|
|
5918
5991
|
|
|
5919
5992
|
class Connect {
|
|
@@ -6167,46 +6240,6 @@ class Server {
|
|
|
6167
6240
|
}
|
|
6168
6241
|
}
|
|
6169
6242
|
|
|
6170
|
-
const Code400 = [
|
|
6171
|
-
{
|
|
6172
|
-
code: 400,
|
|
6173
|
-
msg: 'Bad Request',
|
|
6174
|
-
zn: '表示其他错误,就是4xx都无法描述的前端发生的错误',
|
|
6175
|
-
},
|
|
6176
|
-
{ code: 401, msg: 'Authentication', zn: '表示认证类型的错误' }, // token 无效 (无token, token无效, token 过期)
|
|
6177
|
-
{
|
|
6178
|
-
code: 403,
|
|
6179
|
-
msg: 'Authorization',
|
|
6180
|
-
zn: '表示授权的错误(认证和授权的区别在于:认证表示“识别前来访问的是谁”,而授权则是“赋予特定用户执行特定操作的权限”)',
|
|
6181
|
-
},
|
|
6182
|
-
{ code: 404, msg: 'Not Found', zn: '表示访问的数据不存在' },
|
|
6183
|
-
{
|
|
6184
|
-
code: 405,
|
|
6185
|
-
msg: 'Method Not Allowd',
|
|
6186
|
-
zn: '表示可以访问接口,但是使用的HTTP方法不允许',
|
|
6187
|
-
},
|
|
6188
|
-
];
|
|
6189
|
-
const ResultCode = [{ code: 200, msg: 'OK', zn: '请求成功。' }].concat(Code400);
|
|
6190
|
-
const Result = ({ code, msg, userTip, ...other }) => {
|
|
6191
|
-
const Code = ResultCode.find((item) => item.code === code);
|
|
6192
|
-
let _result = {
|
|
6193
|
-
code: code || Code?.code,
|
|
6194
|
-
msg: msg || Code?.msg,
|
|
6195
|
-
userTip: undefined,
|
|
6196
|
-
...other,
|
|
6197
|
-
};
|
|
6198
|
-
if (userTip) {
|
|
6199
|
-
_result.userTip = userTip;
|
|
6200
|
-
}
|
|
6201
|
-
return _result;
|
|
6202
|
-
};
|
|
6203
|
-
Result.success = (data) => {
|
|
6204
|
-
return {
|
|
6205
|
-
code: 200,
|
|
6206
|
-
data,
|
|
6207
|
-
};
|
|
6208
|
-
};
|
|
6209
|
-
|
|
6210
6243
|
const parseIfJson = (input) => {
|
|
6211
6244
|
try {
|
|
6212
6245
|
// 尝试解析 JSON
|
|
@@ -6412,6 +6445,18 @@ class App {
|
|
|
6412
6445
|
const router = this.router;
|
|
6413
6446
|
return await router.parse(message, ctx);
|
|
6414
6447
|
}
|
|
6448
|
+
exportRoutes() {
|
|
6449
|
+
return this.router.exportRoutes();
|
|
6450
|
+
}
|
|
6451
|
+
importRoutes(routes) {
|
|
6452
|
+
this.router.importRoutes(routes);
|
|
6453
|
+
}
|
|
6454
|
+
importApp(app) {
|
|
6455
|
+
this.importRoutes(app.exportRoutes());
|
|
6456
|
+
}
|
|
6457
|
+
throw(...args) {
|
|
6458
|
+
throw new CustomError(...args);
|
|
6459
|
+
}
|
|
6415
6460
|
}
|
|
6416
6461
|
|
|
6417
|
-
export { App, Connect, CustomError, QueryConnect, QueryRouter, QueryRouterServer,
|
|
6462
|
+
export { App, Connect, CustomError, QueryConnect, QueryRouter, QueryRouterServer, Route, ZodType as Schema, Server, createSchema, handleServer };
|