@kevisual/router 0.1.4 → 0.1.5

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.
@@ -0,0 +1,811 @@
1
+ import { EventEmitter } from 'eventemitter3';
2
+ import { z } from 'zod';
3
+ import * as http from 'node:http';
4
+ import { IncomingMessage, ServerResponse } from 'node:http';
5
+ import { IncomingMessage as IncomingMessage$1, ServerResponse as ServerResponse$1 } from 'http';
6
+ import { PluginInput, Hooks, Plugin } from '@opencode-ai/plugin';
7
+
8
+ type CustomErrorOptions = {
9
+ cause?: Error | string;
10
+ code?: number;
11
+ message?: string;
12
+ };
13
+ interface throwError {
14
+ throw(code?: number | string, message?: string): void;
15
+ throw(code?: number | string, opts?: CustomErrorOptions): void;
16
+ throw(opts?: CustomErrorOptions): void;
17
+ }
18
+
19
+ declare class MockProcess {
20
+ emitter?: EventEmitter;
21
+ process?: NodeJS.Process;
22
+ constructor(opts?: {
23
+ emitter?: EventEmitter;
24
+ isNode?: boolean;
25
+ });
26
+ send(data?: any, callback?: (err?: Error) => void): void;
27
+ exit(flag?: number): void;
28
+ on(fn: (msg?: any) => any): void;
29
+ desctroy(): void;
30
+ }
31
+
32
+ type RouterContextT = {
33
+ code?: number;
34
+ [key: string]: any;
35
+ };
36
+ type BuildRouteContext<M, U> = M extends {
37
+ args?: infer A;
38
+ } ? A extends z.ZodObject<any> ? RouteContext<{
39
+ args?: z.infer<A>;
40
+ }, U> : A extends Record<string, z.ZodTypeAny> ? RouteContext<{
41
+ args?: {
42
+ [K in keyof A]: z.infer<A[K]>;
43
+ };
44
+ }, U> : RouteContext<U> : RouteContext<U>;
45
+ type RouteContext<T = {
46
+ code?: number;
47
+ }, U extends SimpleObject = {}, S = {
48
+ [key: string]: any;
49
+ }> = {
50
+ /**
51
+ * 本地自己调用的时候使用,可以标识为当前自调用,那么 auth 就不许重复的校验
52
+ * 或者不需要登录的,直接调用
53
+ */
54
+ appId?: string;
55
+ query?: {
56
+ [key: string]: any;
57
+ };
58
+ args?: {
59
+ [key: string]: any;
60
+ };
61
+ /** return body */
62
+ body?: number | string | Object;
63
+ forward?: (response: {
64
+ code: number;
65
+ data?: any;
66
+ message?: any;
67
+ }) => void;
68
+ /** return code */
69
+ code?: number;
70
+ /** return msg */
71
+ message?: string;
72
+ /**
73
+ * 传递状态
74
+ */
75
+ state?: S;
76
+ /**
77
+ * 当前routerId
78
+ */
79
+ currentId?: string;
80
+ /**
81
+ * 当前路径
82
+ */
83
+ currentPath?: string;
84
+ /**
85
+ * 当前key
86
+ */
87
+ currentKey?: string;
88
+ /**
89
+ * 当前route
90
+ */
91
+ currentRoute?: Route;
92
+ /**
93
+ * 进度
94
+ */
95
+ progress?: [string, string][];
96
+ nextQuery?: {
97
+ [key: string]: any;
98
+ };
99
+ end?: boolean;
100
+ app?: QueryRouter;
101
+ error?: any;
102
+ /** 请求 route的返回结果,不解析body为data */
103
+ call?: (message: {
104
+ path: string;
105
+ key?: string;
106
+ payload?: any;
107
+ [key: string]: any;
108
+ } | {
109
+ id: string;
110
+ apyload?: any;
111
+ [key: string]: any;
112
+ }, ctx?: RouteContext & {
113
+ [key: string]: any;
114
+ }) => Promise<any>;
115
+ /** 请求 route的返回结果,解析了body为data,就类同于 query.post获取的数据*/
116
+ run?: (message: {
117
+ path: string;
118
+ key?: string;
119
+ payload?: any;
120
+ }, ctx?: RouteContext) => Promise<any>;
121
+ index?: number;
122
+ throw?: throwError['throw'];
123
+ /** 是否需要序列化, 使用JSON.stringify和JSON.parse */
124
+ needSerialize?: boolean;
125
+ } & T & U;
126
+ type SimpleObject = Record<string, any>;
127
+ type Run<T extends SimpleObject = {}> = (ctx: Required<RouteContext<T>>) => Promise<typeof ctx | null | void>;
128
+ type RunMessage = {
129
+ path?: string;
130
+ key?: string;
131
+ id?: string;
132
+ payload?: any;
133
+ };
134
+ type NextRoute = Pick<Route, 'id' | 'path' | 'key'>;
135
+ type RouteMiddleware = {
136
+ path?: string;
137
+ key?: string;
138
+ id?: string;
139
+ } | string;
140
+ type RouteOpts<U = {}, T = SimpleObject> = {
141
+ path?: string;
142
+ key?: string;
143
+ id?: string;
144
+ run?: Run<U>;
145
+ nextRoute?: NextRoute;
146
+ description?: string;
147
+ metadata?: T;
148
+ middleware?: RouteMiddleware[];
149
+ type?: 'route' | 'middleware' | 'compound';
150
+ isDebug?: boolean;
151
+ };
152
+ type DefineRouteOpts = Omit<RouteOpts, 'idUsePath' | 'nextRoute'>;
153
+ declare const pickValue: readonly ["path", "key", "id", "description", "type", "middleware", "metadata"];
154
+ type RouteInfo = Pick<Route, (typeof pickValue)[number]>;
155
+ /**
156
+ * @M 是 route的 metadate的类型,默认是 SimpleObject
157
+ * @U 是 RouteContext 里 state的类型
158
+ */
159
+ declare class Route<M extends SimpleObject = SimpleObject, U extends SimpleObject = SimpleObject> implements throwError {
160
+ /**
161
+ * 一级路径
162
+ */
163
+ path?: string;
164
+ /**
165
+ * 二级路径
166
+ */
167
+ key?: string;
168
+ id?: string;
169
+ run?: Run<BuildRouteContext<M, U>>;
170
+ nextRoute?: NextRoute;
171
+ description?: string;
172
+ metadata?: M;
173
+ middleware?: RouteMiddleware[];
174
+ type?: string;
175
+ /**
176
+ * 是否开启debug,开启后会打印错误信息
177
+ */
178
+ isDebug?: boolean;
179
+ constructor(path?: string, key?: string, opts?: RouteOpts);
180
+ prompt(description: string): this;
181
+ prompt(description: Function): this;
182
+ define<T extends {
183
+ [key: string]: any;
184
+ } = RouterContextT>(opts: DefineRouteOpts): this;
185
+ define<T extends {
186
+ [key: string]: any;
187
+ } = RouterContextT>(fn: Run<T & BuildRouteContext<M, U>>): this;
188
+ define<T extends {
189
+ [key: string]: any;
190
+ } = RouterContextT>(key: string, fn: Run<T & BuildRouteContext<M, U>>): this;
191
+ define<T extends {
192
+ [key: string]: any;
193
+ } = RouterContextT>(path: string, key: string, fn: Run<T & BuildRouteContext<M, U>>): this;
194
+ update(opts: DefineRouteOpts, onlyUpdateList?: string[]): this;
195
+ addTo(router: QueryRouter | {
196
+ add: (route: Route) => void;
197
+ [key: string]: any;
198
+ }, opts?: AddOpts): void;
199
+ throw(...args: any[]): void;
200
+ }
201
+ /**
202
+ * @parmas overwrite 是否覆盖已存在的route,默认true
203
+ */
204
+ type AddOpts = {
205
+ overwrite?: boolean;
206
+ };
207
+ declare class QueryRouter<T extends SimpleObject = SimpleObject> implements throwError {
208
+ appId: string;
209
+ routes: Route[];
210
+ maxNextRoute: number;
211
+ context?: RouteContext<T>;
212
+ constructor();
213
+ /**
214
+ * add route
215
+ * @param route
216
+ * @param opts
217
+ */
218
+ add(route: Route, opts?: AddOpts): void;
219
+ /**
220
+ * remove route by path and key
221
+ * @param route
222
+ */
223
+ remove(route: Route | {
224
+ path: string;
225
+ key?: string;
226
+ }): void;
227
+ /**
228
+ * remove route by id
229
+ * @param uniqueId
230
+ */
231
+ removeById(uniqueId: string): void;
232
+ /**
233
+ * 执行route
234
+ * @param path
235
+ * @param key
236
+ * @param ctx
237
+ * @returns
238
+ */
239
+ runRoute(path: string, key: string, ctx?: RouteContext<T>): Promise<RouteContext<T>>;
240
+ /**
241
+ * 第一次执行
242
+ * @param message
243
+ * @param ctx
244
+ * @returns
245
+ */
246
+ parse(message: {
247
+ path: string;
248
+ key?: string;
249
+ payload?: any;
250
+ }, ctx?: RouteContext<T> & {
251
+ [key: string]: any;
252
+ }): Promise<RouteContext<T, {}, {
253
+ [key: string]: any;
254
+ }>>;
255
+ /**
256
+ * 返回的数据包含所有的context的请求返回的内容,可做其他处理
257
+ * @param message
258
+ * @param ctx
259
+ * @returns
260
+ */
261
+ call(message: {
262
+ id?: string;
263
+ path?: string;
264
+ key?: string;
265
+ payload?: any;
266
+ }, ctx?: RouteContext<T> & {
267
+ [key: string]: any;
268
+ }): Promise<RouteContext<T, {}, {
269
+ [key: string]: any;
270
+ }> | {
271
+ code: number;
272
+ body: any;
273
+ message: string;
274
+ }>;
275
+ /**
276
+ * 请求 result 的数据
277
+ * @param message
278
+ * @param ctx
279
+ * @deprecated use run or call instead
280
+ * @returns
281
+ */
282
+ queryRoute(message: {
283
+ id?: string;
284
+ path: string;
285
+ key?: string;
286
+ payload?: any;
287
+ }, ctx?: RouteContext & {
288
+ [key: string]: any;
289
+ }): Promise<{
290
+ code: number;
291
+ data: any;
292
+ message: string;
293
+ }>;
294
+ /**
295
+ * Router Run获取数据
296
+ * @param message
297
+ * @param ctx
298
+ * @returns
299
+ */
300
+ run(message: {
301
+ id?: string;
302
+ path?: string;
303
+ key?: string;
304
+ payload?: any;
305
+ }, ctx?: RouteContext<T> & {
306
+ [key: string]: any;
307
+ }): Promise<{
308
+ code: number;
309
+ data: any;
310
+ message: string;
311
+ }>;
312
+ /**
313
+ * 设置上下文
314
+ * @description 这里的上下文是为了在handle函数中使用
315
+ * @param ctx
316
+ */
317
+ setContext(ctx: RouteContext): void;
318
+ getList(filter?: (route: Route) => boolean): RouteInfo[];
319
+ /**
320
+ * 获取handle函数, 这里会去执行parse函数
321
+ */
322
+ getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn, ctx?: RouteContext): (msg: {
323
+ id?: string;
324
+ path?: string;
325
+ key?: string;
326
+ [key: string]: any;
327
+ }, handleContext?: RouteContext<T>) => Promise<{
328
+ [key: string]: any;
329
+ code: string;
330
+ data?: any;
331
+ message?: string;
332
+ } | {
333
+ code: any;
334
+ data: any;
335
+ message: any;
336
+ } | {
337
+ code: number;
338
+ message: any;
339
+ data?: undefined;
340
+ }>;
341
+ exportRoutes(): Route<SimpleObject, SimpleObject>[];
342
+ importRoutes(routes: Route[]): void;
343
+ importRouter(router: QueryRouter): void;
344
+ throw(...args: any[]): void;
345
+ hasRoute(path: string, key?: string): Route<SimpleObject, SimpleObject>;
346
+ findRoute(opts?: {
347
+ path?: string;
348
+ key?: string;
349
+ id?: string;
350
+ }): Route<SimpleObject, SimpleObject>;
351
+ createRouteList(opts?: {
352
+ force?: boolean;
353
+ filter?: (route: Route) => boolean;
354
+ middleware?: string[];
355
+ }): void;
356
+ /**
357
+ * 等待程序运行, 获取到message的数据,就执行
358
+ * params 是预设参数
359
+ * emitter = process
360
+ * -- .exit
361
+ * -- .on
362
+ * -- .send
363
+ */
364
+ wait(params?: {
365
+ message: RunMessage;
366
+ }, opts?: {
367
+ mockProcess?: MockProcess;
368
+ timeout?: number;
369
+ getList?: boolean;
370
+ force?: boolean;
371
+ filter?: (route: Route) => boolean;
372
+ routeListMiddleware?: string[];
373
+ }): Promise<void>;
374
+ toJSONSchema: (args: any, opts?: {
375
+ mergeObject?: boolean;
376
+ override?: (opts: {
377
+ jsonSchema: any;
378
+ path: string[];
379
+ zodSchema: z.ZodTypeAny;
380
+ }) => void;
381
+ }) => {
382
+ [key: string]: any;
383
+ };
384
+ fromJSONSchema: <Merge extends boolean = false>(args?: any, opts?: {
385
+ mergeObject?: boolean;
386
+ }) => Merge extends true ? z.ZodObject<{
387
+ [key: string]: any;
388
+ }, z.core.$strip> : {
389
+ [key: string]: z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>;
390
+ };
391
+ }
392
+ type QueryRouterServerOpts<C extends SimpleObject = SimpleObject> = {
393
+ handleFn?: HandleFn;
394
+ context?: RouteContext<C>;
395
+ appId?: string;
396
+ initHandle?: boolean;
397
+ };
398
+ interface HandleFn<T = any> {
399
+ (msg: {
400
+ path: string;
401
+ [key: string]: any;
402
+ }, ctx?: any): {
403
+ code: string;
404
+ data?: any;
405
+ message?: string;
406
+ [key: string]: any;
407
+ };
408
+ (res: RouteContext<T>): any;
409
+ }
410
+ /**
411
+ * QueryRouterServer
412
+ * @description 移除server相关的功能,只保留router相关的功能,和http.createServer不相关,独立
413
+ * @template C 自定义 RouteContext 类型
414
+ */
415
+ declare class QueryRouterServer<C extends SimpleObject = SimpleObject> extends QueryRouter<C> {
416
+ appId: string;
417
+ handle: any;
418
+ context: RouteContext<C>;
419
+ constructor(opts?: QueryRouterServerOpts<C>);
420
+ setHandle(wrapperFn?: HandleFn, ctx?: RouteContext): void;
421
+ addRoute(route: Route, opts?: AddOpts): void;
422
+ Route: typeof Route;
423
+ route<M extends SimpleObject = SimpleObject>(opts: RouteOpts & {
424
+ metadata?: M;
425
+ }): Route<M, Required<RouteContext<C>>>;
426
+ route<M extends SimpleObject = SimpleObject>(path: string, opts?: RouteOpts & {
427
+ metadata?: M;
428
+ }): Route<M, Required<RouteContext<C>>>;
429
+ route<M extends SimpleObject = SimpleObject>(path: string, key?: string): Route<M, Required<RouteContext<C>>>;
430
+ route<M extends SimpleObject = SimpleObject>(path: string, key?: string, opts?: RouteOpts & {
431
+ metadata?: M;
432
+ }): Route<M, Required<RouteContext<C>>>;
433
+ prompt(description: string): Route<SimpleObject, SimpleObject>;
434
+ /**
435
+ * 调用了handle
436
+ * @param param0
437
+ * @returns
438
+ */
439
+ run(msg: {
440
+ id?: string;
441
+ path?: string;
442
+ key?: string;
443
+ payload?: any;
444
+ token?: string;
445
+ data?: any;
446
+ }, ctx?: Partial<RouteContext<C>>): Promise<any>;
447
+ runAction<T extends {
448
+ id?: string;
449
+ path?: string;
450
+ key?: string;
451
+ metadata?: {
452
+ args?: any;
453
+ };
454
+ } = {}>(api: T, payload: RunActionPayload<T>, ctx?: RouteContext<C>): Promise<any>;
455
+ }
456
+ /** JSON Schema 基本类型映射到 TypeScript 类型 */
457
+ type JsonSchemaTypeToTS<T> = T extends {
458
+ type: "string";
459
+ } ? string : T extends {
460
+ type: "boolean";
461
+ } ? boolean : T extends {
462
+ type: "number";
463
+ } ? number : T extends {
464
+ type: "integer";
465
+ } ? number : T extends {
466
+ type: "object";
467
+ } ? object : T extends {
468
+ type: "array";
469
+ } ? any[] : any;
470
+ /** 将 args shape(key -> JSON Schema 类型)转换为 payload 类型,支持 optional: true 的字段为可选 */
471
+ type ArgsShapeToPayload<T> = {
472
+ [K in keyof T as T[K] extends {
473
+ optional: true;
474
+ } ? never : K]: JsonSchemaTypeToTS<T[K]>;
475
+ } & {
476
+ [K in keyof T as T[K] extends {
477
+ optional: true;
478
+ } ? K : never]?: JsonSchemaTypeToTS<T[K]>;
479
+ };
480
+ /** 处理两种 args 格式:完整 JSON Schema(含 properties)或简单 key->type 映射 */
481
+ type ArgsToPayload<T> = T extends {
482
+ type: "object";
483
+ properties: infer P;
484
+ } ? ArgsShapeToPayload<P> : ArgsShapeToPayload<T>;
485
+ /** 从 API 定义中提取 metadata.args */
486
+ type ExtractArgs<T> = T extends {
487
+ metadata: {
488
+ args: infer A;
489
+ };
490
+ } ? A : {};
491
+ /** runAction 第二个参数的类型,根据第一个参数的 metadata.args 推断 */
492
+ type RunActionPayload<T> = ArgsToPayload<ExtractArgs<T>>;
493
+
494
+ type Cors = {
495
+ /**
496
+ * @default '*''
497
+ */
498
+ origin?: string | undefined;
499
+ };
500
+ type ServerOpts<T = {}> = {
501
+ /**path default `/api/router` */
502
+ path?: string;
503
+ /**handle Fn */
504
+ handle?: (msg?: {
505
+ path: string;
506
+ key?: string;
507
+ [key: string]: any;
508
+ }, ctx?: {
509
+ req: http.IncomingMessage;
510
+ res: http.ServerResponse;
511
+ }) => any;
512
+ cors?: Cors;
513
+ io?: boolean;
514
+ showConnected?: boolean;
515
+ } & T;
516
+ interface ServerType {
517
+ path?: string;
518
+ server?: any;
519
+ handle: ServerOpts['handle'];
520
+ setHandle(handle?: any): void;
521
+ listeners: Listener[];
522
+ listen(port: number, hostname?: string, backlog?: number, listeningListener?: () => void): void;
523
+ listen(port: number, hostname?: string, listeningListener?: () => void): void;
524
+ listen(port: number, backlog?: number, listeningListener?: () => void): void;
525
+ listen(port: number, listeningListener?: () => void): void;
526
+ listen(path: string, backlog?: number, listeningListener?: () => void): void;
527
+ listen(path: string, listeningListener?: () => void): void;
528
+ listen(handle: any, backlog?: number, listeningListener?: () => void): void;
529
+ listen(handle: any, listeningListener?: () => void): void;
530
+ /**
531
+ * 兜底监听,当除开 `/api/router` 之外的请求,框架只监听一个api,所以有其他的请求都执行其他的监听
532
+ * @description 主要是为了兼容其他的监听
533
+ * @param listener
534
+ */
535
+ on(listener: OnListener): void;
536
+ onWebSocket({ ws, message, pathname, token, id }: OnWebSocketOptions): void;
537
+ onWsClose<T = {}>(ws: WS<T>): void;
538
+ sendConnected<T = {}>(ws: WS<T>): void;
539
+ }
540
+ type OnWebSocketOptions<T = {}> = {
541
+ ws: WS<T>;
542
+ message: string | Buffer;
543
+ pathname: string;
544
+ token?: string;
545
+ /** data 的id提取出来 */
546
+ id?: string;
547
+ };
548
+ type WS<T = {}> = {
549
+ send: (data: any) => void;
550
+ close: (code?: number, reason?: string) => void;
551
+ /**
552
+ * ws 自己生成的一个id,主要是为了区分不同的ws连接,方便在onWebSocket中使用
553
+ */
554
+ wsId?: string;
555
+ data?: {
556
+ /**
557
+ * ws连接时的url,包含pathname和searchParams
558
+ */
559
+ url: URL;
560
+ /**
561
+ * ws连接时的pathname
562
+ */
563
+ pathname: string;
564
+ /**
565
+ * ws连接时的url中的token参数
566
+ */
567
+ token?: string;
568
+ /**
569
+ * ws连接时的url中的id参数.
570
+ */
571
+ id?: string;
572
+ /**
573
+ * 鉴权后的获取的信息
574
+ */
575
+ userApp?: string;
576
+ } & T;
577
+ };
578
+ type Listener = {
579
+ id?: string;
580
+ io?: boolean;
581
+ path?: string;
582
+ func: WebSocketListenerFun | HttpListenerFun;
583
+ /**
584
+ * @description 是否默认解析为 JSON,如果为 true,则 message 会被 JSON.parse 处理,默认是 true
585
+ */
586
+ json?: boolean;
587
+ };
588
+ type WebSocketListenerFun = (req: WebSocketReq, res: WebSocketRes) => Promise<void> | void;
589
+ type HttpListenerFun = (req: RouterReq, res: RouterRes) => Promise<void> | void;
590
+ type WebSocketReq<T = {}, U = Record<string, any>> = {
591
+ emitter?: EventEmitter;
592
+ ws: WS<T>;
593
+ data?: U;
594
+ message?: string | Buffer;
595
+ pathname?: string;
596
+ token?: string;
597
+ id?: string;
598
+ };
599
+ type WebSocketRes = {
600
+ end: (data: any) => void;
601
+ };
602
+ type ListenerFun = WebSocketListenerFun | HttpListenerFun;
603
+ type OnListener = Listener | ListenerFun | (Listener | ListenerFun)[];
604
+ type RouterReq<T = {}> = {
605
+ url: string;
606
+ method: string;
607
+ headers: Record<string, string>;
608
+ socket?: {
609
+ remoteAddress?: string;
610
+ remotePort?: number;
611
+ };
612
+ body?: string;
613
+ cookies?: Record<string, string>;
614
+ bun?: {
615
+ request: Bun.BunRequest;
616
+ server: Bun.Server<{}>;
617
+ resolve: (response: Response) => void;
618
+ };
619
+ on: (event: 'close', listener: Function) => void;
620
+ } & T;
621
+ type RouterRes<T = {}> = {
622
+ statusCode: number;
623
+ headersSent: boolean;
624
+ _headers: Record<string, string | string[]>;
625
+ _bodyChunks: any[];
626
+ writableEnded: boolean;
627
+ writeHead: (statusCode: number, headers?: Record<string, string>) => void;
628
+ setHeader: (name: string, value: string | string[]) => void;
629
+ cookie: (name: string, value: string, options?: any) => void;
630
+ write: (chunk: any) => void;
631
+ pipe: (stream: ReadableStream) => void;
632
+ end: (data?: any) => void;
633
+ } & T;
634
+
635
+ interface StringifyOptions {
636
+ /**
637
+ * Specifies a function that will be used to encode a [cookie-value](https://datatracker.ietf.org/doc/html/rfc6265#section-4.1.1).
638
+ * Since value of a cookie has a limited character set (and must be a simple string), this function can be used to encode
639
+ * a value into a string suited for a cookie's value, and should mirror `decode` when parsing.
640
+ *
641
+ * @default encodeURIComponent
642
+ */
643
+ encode?: (str: string) => string;
644
+ }
645
+ /**
646
+ * Set-Cookie object.
647
+ */
648
+ interface SetCookie {
649
+ /**
650
+ * Specifies the name of the cookie.
651
+ */
652
+ name: string;
653
+ /**
654
+ * Specifies the string to be the value for the cookie.
655
+ */
656
+ value: string | undefined;
657
+ /**
658
+ * Specifies the `number` (in seconds) to be the value for the [`Max-Age` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.2).
659
+ *
660
+ * The [cookie storage model specification](https://tools.ietf.org/html/rfc6265#section-5.3) states that if both `expires` and
661
+ * `maxAge` are set, then `maxAge` takes precedence, but it is possible not all clients by obey this,
662
+ * so if both are set, they should point to the same date and time.
663
+ */
664
+ maxAge?: number;
665
+ /**
666
+ * Specifies the `Date` object to be the value for the [`Expires` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.1).
667
+ * When no expiration is set, clients consider this a "non-persistent cookie" and delete it when the current session is over.
668
+ *
669
+ * The [cookie storage model specification](https://tools.ietf.org/html/rfc6265#section-5.3) states that if both `expires` and
670
+ * `maxAge` are set, then `maxAge` takes precedence, but it is possible not all clients by obey this,
671
+ * so if both are set, they should point to the same date and time.
672
+ */
673
+ expires?: Date;
674
+ /**
675
+ * Specifies the value for the [`Domain` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.3).
676
+ * When no domain is set, clients consider the cookie to apply to the current domain only.
677
+ */
678
+ domain?: string;
679
+ /**
680
+ * Specifies the value for the [`Path` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.4).
681
+ * When no path is set, the path is considered the ["default path"](https://tools.ietf.org/html/rfc6265#section-5.1.4).
682
+ */
683
+ path?: string;
684
+ /**
685
+ * Enables the [`HttpOnly` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.6).
686
+ * When enabled, clients will not allow client-side JavaScript to see the cookie in `document.cookie`.
687
+ */
688
+ httpOnly?: boolean;
689
+ /**
690
+ * Enables the [`Secure` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.5).
691
+ * When enabled, clients will only send the cookie back if the browser has an HTTPS connection.
692
+ */
693
+ secure?: boolean;
694
+ /**
695
+ * Enables the [`Partitioned` `Set-Cookie` attribute](https://tools.ietf.org/html/draft-cutler-httpbis-partitioned-cookies/).
696
+ * When enabled, clients will only send the cookie back when the current domain _and_ top-level domain matches.
697
+ *
698
+ * This is an attribute that has not yet been fully standardized, and may change in the future.
699
+ * This also means clients may ignore this attribute until they understand it. More information
700
+ * about can be found in [the proposal](https://github.com/privacycg/CHIPS).
701
+ */
702
+ partitioned?: boolean;
703
+ /**
704
+ * Specifies the value for the [`Priority` `Set-Cookie` attribute](https://tools.ietf.org/html/draft-west-cookie-priority-00#section-4.1).
705
+ *
706
+ * - `'low'` will set the `Priority` attribute to `Low`.
707
+ * - `'medium'` will set the `Priority` attribute to `Medium`, the default priority when not set.
708
+ * - `'high'` will set the `Priority` attribute to `High`.
709
+ *
710
+ * More information about priority levels can be found in [the specification](https://tools.ietf.org/html/draft-west-cookie-priority-00#section-4.1).
711
+ */
712
+ priority?: "low" | "medium" | "high";
713
+ /**
714
+ * Specifies the value for the [`SameSite` `Set-Cookie` attribute](https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-09#section-5.4.7).
715
+ *
716
+ * - `true` will set the `SameSite` attribute to `Strict` for strict same site enforcement.
717
+ * - `'lax'` will set the `SameSite` attribute to `Lax` for lax same site enforcement.
718
+ * - `'none'` will set the `SameSite` attribute to `None` for an explicit cross-site cookie.
719
+ * - `'strict'` will set the `SameSite` attribute to `Strict` for strict same site enforcement.
720
+ *
721
+ * More information about enforcement levels can be found in [the specification](https://tools.ietf.org/html/draft-ietf-httpbis-rfc6265bis-09#section-5.4.7).
722
+ */
723
+ sameSite?: boolean | "lax" | "strict" | "none";
724
+ }
725
+ /**
726
+ * Backward compatibility serialize options.
727
+ */
728
+ type SerializeOptions = StringifyOptions & Omit<SetCookie, "name" | "value">;
729
+
730
+ type CookieFn = (name: string, value: string, options?: SerializeOptions, end?: boolean) => void;
731
+ type HandleCtx = {
732
+ req: IncomingMessage & {
733
+ cookies: Record<string, string>;
734
+ };
735
+ res: ServerResponse & {
736
+ /**
737
+ * cookie 函数, end 参数用于设置是否立即设置到响应头,设置了后面的cookie再设置会覆盖前面的
738
+ */
739
+ cookie: CookieFn;
740
+ };
741
+ };
742
+
743
+ type ServerNodeOpts = ServerOpts<{
744
+ httpType?: 'http' | 'https' | 'http2';
745
+ httpsKey?: string;
746
+ httpsCert?: string;
747
+ }>;
748
+
749
+ type RouterHandle = (msg: {
750
+ path: string;
751
+ [key: string]: any;
752
+ }) => {
753
+ code: string;
754
+ data?: any;
755
+ message?: string;
756
+ [key: string]: any;
757
+ };
758
+ type AppOptions<T = {}> = {
759
+ router?: QueryRouterServer;
760
+ server?: ServerType;
761
+ /** handle msg 关联 */
762
+ routerHandle?: RouterHandle;
763
+ routerContext?: RouteContext<T>;
764
+ serverOptions?: ServerNodeOpts;
765
+ appId?: string;
766
+ };
767
+ type AppRouteContext<T> = HandleCtx & RouteContext<T> & {
768
+ app: App<T>;
769
+ };
770
+ /**
771
+ * 封装了 Router 和 Server 的 App 模块,处理http的请求和响应,内置了 Cookie 和 Token 和 res 的处理
772
+ * U - Route Context的扩展类型
773
+ */
774
+ declare class App<U = {}> extends QueryRouterServer<AppRouteContext<U>> {
775
+ appId: string;
776
+ router: QueryRouterServer;
777
+ server: ServerType;
778
+ context: AppRouteContext<U>;
779
+ constructor(opts?: AppOptions<U>);
780
+ listen(port: number, hostname?: string, backlog?: number, listeningListener?: () => void): void;
781
+ listen(port: number, hostname?: string, listeningListener?: () => void): void;
782
+ listen(port: number, backlog?: number, listeningListener?: () => void): void;
783
+ listen(port: number, listeningListener?: () => void): void;
784
+ listen(path: string, backlog?: number, listeningListener?: () => void): void;
785
+ listen(path: string, listeningListener?: () => void): void;
786
+ listen(handle: any, backlog?: number, listeningListener?: () => void): void;
787
+ listen(handle: any, listeningListener?: () => void): void;
788
+ Route: typeof Route;
789
+ static handleRequest(req: IncomingMessage$1, res: ServerResponse$1): Promise<{
790
+ cookies: Record<string, string>;
791
+ token: string;
792
+ }>;
793
+ onServerRequest(fn: (req: IncomingMessage$1, res: ServerResponse$1) => void): void;
794
+ }
795
+
796
+ declare const addCallFn: (app: App) => void;
797
+ declare const createRouterAgentPluginFn: (opts?: {
798
+ router?: App | QueryRouterServer;
799
+ query?: string;
800
+ hooks?: (plugin: PluginInput) => Promise<Hooks>;
801
+ }) => Plugin;
802
+ declare const usePluginInput: () => PluginInput;
803
+ /**
804
+ * 如果args是z.object类型,拆分第一个Object的属性,比如z.object({ name: z.string(), age: z.number() }),拆分成{name: z.string(), age: z.number()}
805
+ * 如果args是普通对象,直接返回
806
+ * @param args
807
+ * @returns
808
+ */
809
+ declare const extractArgs: (args: any) => any;
810
+
811
+ export { addCallFn, createRouterAgentPluginFn, extractArgs, usePluginInput };