@kevisual/router 0.0.56 → 0.0.58

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,369 @@
1
+ import { Plugin } from '@opencode-ai/plugin';
2
+
3
+ type RouterContextT = {
4
+ code?: number;
5
+ [key: string]: any;
6
+ };
7
+ type RouteContext<T = {
8
+ code?: number;
9
+ }, S = any> = {
10
+ /**
11
+ * 本地自己调用的时候使用,可以标识为当前自调用,那么 auth 就不许重复的校验
12
+ * 或者不需要登录的,直接调用
13
+ */
14
+ appId?: string;
15
+ query?: {
16
+ [key: string]: any;
17
+ };
18
+ /** return body */
19
+ body?: number | string | Object;
20
+ forward?: (response: {
21
+ code: number;
22
+ data?: any;
23
+ message?: any;
24
+ }) => void;
25
+ /** return code */
26
+ code?: number;
27
+ /** return msg */
28
+ message?: string;
29
+ state?: S;
30
+ /**
31
+ * 当前路径
32
+ */
33
+ currentPath?: string;
34
+ /**
35
+ * 当前key
36
+ */
37
+ currentKey?: string;
38
+ /**
39
+ * 当前route
40
+ */
41
+ currentRoute?: Route;
42
+ /**
43
+ * 进度
44
+ */
45
+ progress?: [string, string][];
46
+ nextQuery?: {
47
+ [key: string]: any;
48
+ };
49
+ end?: boolean;
50
+ app?: QueryRouter;
51
+ error?: any;
52
+ /** 请求 route的返回结果,不解析body为data */
53
+ call?: (message: {
54
+ path: string;
55
+ key?: string;
56
+ payload?: any;
57
+ [key: string]: any;
58
+ } | {
59
+ id: string;
60
+ apyload?: any;
61
+ [key: string]: any;
62
+ }, ctx?: RouteContext & {
63
+ [key: string]: any;
64
+ }) => Promise<any>;
65
+ /** 请求 route的返回结果,解析了body为data,就类同于 query.post获取的数据*/
66
+ run?: (message: {
67
+ path: string;
68
+ key?: string;
69
+ payload?: any;
70
+ }, ctx?: RouteContext & {
71
+ [key: string]: any;
72
+ }) => Promise<any>;
73
+ index?: number;
74
+ throw?: (code?: number | string, message?: string, tips?: string) => void;
75
+ /** 是否需要序列化, 使用JSON.stringify和JSON.parse */
76
+ needSerialize?: boolean;
77
+ } & T;
78
+ type SimpleObject = Record<string, any>;
79
+ type Run<T extends SimpleObject = {}> = (ctx: RouteContext<T>) => Promise<typeof ctx | null | void>;
80
+ type NextRoute = Pick<Route, 'id' | 'path' | 'key'>;
81
+ type RouteMiddleware = {
82
+ path: string;
83
+ key?: string;
84
+ id?: string;
85
+ } | string;
86
+ type RouteOpts<U = {}, T = SimpleObject> = {
87
+ path?: string;
88
+ key?: string;
89
+ id?: string;
90
+ run?: Run<U>;
91
+ nextRoute?: NextRoute;
92
+ description?: string;
93
+ metadata?: T;
94
+ middleware?: RouteMiddleware[];
95
+ type?: 'route' | 'middleware';
96
+ /**
97
+ * $#$ will be used to split path and key
98
+ */
99
+ idUsePath?: boolean;
100
+ /**
101
+ * id 合并的分隔符,默认为 $#$
102
+ */
103
+ delimiter?: string;
104
+ isDebug?: boolean;
105
+ };
106
+ type DefineRouteOpts = Omit<RouteOpts, 'idUsePath' | 'nextRoute'>;
107
+ declare const pickValue: readonly ["path", "key", "id", "description", "type", "middleware", "metadata"];
108
+ type RouteInfo = Pick<Route, (typeof pickValue)[number]>;
109
+ declare class Route<U = {
110
+ [key: string]: any;
111
+ }, T extends SimpleObject = SimpleObject> {
112
+ /**
113
+ * 一级路径
114
+ */
115
+ path?: string;
116
+ /**
117
+ * 二级路径
118
+ */
119
+ key?: string;
120
+ id?: string;
121
+ run?: Run;
122
+ nextRoute?: NextRoute;
123
+ description?: string;
124
+ metadata?: T;
125
+ middleware?: RouteMiddleware[];
126
+ type?: string;
127
+ data?: any;
128
+ /**
129
+ * 是否开启debug,开启后会打印错误信息
130
+ */
131
+ isDebug?: boolean;
132
+ constructor(path?: string, key?: string, opts?: RouteOpts);
133
+ prompt(description: string): this;
134
+ prompt(description: Function): this;
135
+ define<T extends {
136
+ [key: string]: any;
137
+ } = RouterContextT>(opts: DefineRouteOpts): this;
138
+ define<T extends {
139
+ [key: string]: any;
140
+ } = RouterContextT>(fn: Run<T & U>): this;
141
+ define<T extends {
142
+ [key: string]: any;
143
+ } = RouterContextT>(key: string, fn: Run<T & U>): this;
144
+ define<T extends {
145
+ [key: string]: any;
146
+ } = RouterContextT>(path: string, key: string, fn: Run<T & U>): this;
147
+ update(opts: DefineRouteOpts, checkList?: string[]): this;
148
+ addTo(router: QueryRouter | {
149
+ add: (route: Route) => void;
150
+ [key: string]: any;
151
+ }): void;
152
+ setData(data: any): this;
153
+ throw(code?: number | string, message?: string, tips?: string): void;
154
+ }
155
+ declare class QueryRouter {
156
+ appId: string;
157
+ routes: Route[];
158
+ maxNextRoute: number;
159
+ context?: RouteContext;
160
+ constructor();
161
+ add(route: Route): void;
162
+ /**
163
+ * remove route by path and key
164
+ * @param route
165
+ */
166
+ remove(route: Route | {
167
+ path: string;
168
+ key?: string;
169
+ }): void;
170
+ /**
171
+ * remove route by id
172
+ * @param uniqueId
173
+ */
174
+ removeById(unique: string): void;
175
+ /**
176
+ * 执行route
177
+ * @param path
178
+ * @param key
179
+ * @param ctx
180
+ * @returns
181
+ */
182
+ runRoute(path: string, key: string, ctx?: RouteContext): any;
183
+ /**
184
+ * 第一次执行
185
+ * @param message
186
+ * @param ctx
187
+ * @returns
188
+ */
189
+ parse(message: {
190
+ path: string;
191
+ key?: string;
192
+ payload?: any;
193
+ }, ctx?: RouteContext & {
194
+ [key: string]: any;
195
+ }): Promise<any>;
196
+ /**
197
+ * 返回的数据包含所有的context的请求返回的内容,可做其他处理
198
+ * @param message
199
+ * @param ctx
200
+ * @returns
201
+ */
202
+ call(message: {
203
+ id?: string;
204
+ path?: string;
205
+ key?: string;
206
+ payload?: any;
207
+ }, ctx?: RouteContext & {
208
+ [key: string]: any;
209
+ }): Promise<any>;
210
+ /**
211
+ * 请求 result 的数据
212
+ * @param message
213
+ * @param ctx
214
+ * @deprecated use run or call instead
215
+ * @returns
216
+ */
217
+ queryRoute(message: {
218
+ id?: string;
219
+ path: string;
220
+ key?: string;
221
+ payload?: any;
222
+ }, ctx?: RouteContext & {
223
+ [key: string]: any;
224
+ }): Promise<{
225
+ code: any;
226
+ data: any;
227
+ message: any;
228
+ }>;
229
+ /**
230
+ * Router Run获取数据
231
+ * @param message
232
+ * @param ctx
233
+ * @returns
234
+ */
235
+ run(message: {
236
+ id?: string;
237
+ path?: string;
238
+ key?: string;
239
+ payload?: any;
240
+ }, ctx?: RouteContext & {
241
+ [key: string]: any;
242
+ }): Promise<{
243
+ code: any;
244
+ data: any;
245
+ message: any;
246
+ }>;
247
+ /**
248
+ * 设置上下文
249
+ * @description 这里的上下文是为了在handle函数中使用
250
+ * @param ctx
251
+ */
252
+ setContext(ctx: RouteContext): void;
253
+ getList(filter?: (route: Route) => boolean): RouteInfo[];
254
+ /**
255
+ * 获取handle函数, 这里会去执行parse函数
256
+ */
257
+ getHandle<T = any>(router: QueryRouter, wrapperFn?: HandleFn<T>, ctx?: RouteContext): (msg: {
258
+ id?: string;
259
+ path?: string;
260
+ key?: string;
261
+ [key: string]: any;
262
+ }, handleContext?: RouteContext) => Promise<{
263
+ [key: string]: any;
264
+ code: string;
265
+ data?: any;
266
+ message?: string;
267
+ } | {
268
+ code: any;
269
+ data: any;
270
+ message: any;
271
+ } | {
272
+ code: number;
273
+ message: any;
274
+ data?: undefined;
275
+ }>;
276
+ exportRoutes(): Route<{
277
+ [key: string]: any;
278
+ }, SimpleObject>[];
279
+ importRoutes(routes: Route[]): void;
280
+ importRouter(router: QueryRouter): void;
281
+ throw(code?: number | string, message?: string, tips?: string): void;
282
+ hasRoute(path: string, key?: string): Route<{
283
+ [key: string]: any;
284
+ }, SimpleObject>;
285
+ findRoute(opts?: {
286
+ path?: string;
287
+ key?: string;
288
+ id?: string;
289
+ }): Route<{
290
+ [key: string]: any;
291
+ }, SimpleObject>;
292
+ createRouteList(force?: boolean, filter?: (route: Route) => boolean): void;
293
+ /**
294
+ * 等待程序运行, 获取到message的数据,就执行
295
+ *
296
+ * emitter = process
297
+ * -- .exit
298
+ * -- .on
299
+ * -- .send
300
+ */
301
+ wait(params?: {
302
+ path?: string;
303
+ key?: string;
304
+ payload?: any;
305
+ }, opts?: {
306
+ emitter?: any;
307
+ timeout?: number;
308
+ getList?: boolean;
309
+ force?: boolean;
310
+ filter?: (route: Route) => boolean;
311
+ }): Promise<void>;
312
+ }
313
+ type QueryRouterServerOpts = {
314
+ handleFn?: HandleFn;
315
+ context?: RouteContext;
316
+ appId?: string;
317
+ };
318
+ interface HandleFn<T = any> {
319
+ (msg: {
320
+ path: string;
321
+ [key: string]: any;
322
+ }, ctx?: any): {
323
+ code: string;
324
+ data?: any;
325
+ message?: string;
326
+ [key: string]: any;
327
+ };
328
+ (res: RouteContext<T>): any;
329
+ }
330
+ /**
331
+ * QueryRouterServer
332
+ * @description 移除server相关的功能,只保留router相关的功能,和http.createServer不相关,独立
333
+ */
334
+ declare class QueryRouterServer extends QueryRouter {
335
+ appId: string;
336
+ handle: any;
337
+ constructor(opts?: QueryRouterServerOpts);
338
+ setHandle(wrapperFn?: HandleFn, ctx?: RouteContext): void;
339
+ use(path: string, fn: (ctx: any) => any, opts?: RouteOpts): void;
340
+ addRoute(route: Route): void;
341
+ Route: typeof Route;
342
+ route(opts: RouteOpts): Route<Required<RouteContext>>;
343
+ route(path: string, key?: string): Route<Required<RouteContext>>;
344
+ route(path: string, opts?: RouteOpts): Route<Required<RouteContext>>;
345
+ route(path: string, key?: string, opts?: RouteOpts): Route<Required<RouteContext>>;
346
+ prompt(description: string): Route<Required<RouteContext>>;
347
+ prompt(description: Function): Route<Required<RouteContext>>;
348
+ /**
349
+ * 调用了handle
350
+ * @param param0
351
+ * @returns
352
+ */
353
+ run(msg: {
354
+ id?: string;
355
+ path?: string;
356
+ key?: string;
357
+ payload?: any;
358
+ }, ctx?: RouteContext & {
359
+ [key: string]: any;
360
+ }): Promise<any>;
361
+ }
362
+
363
+ declare const addCallFn: (app: QueryRouterServer) => void;
364
+ declare const createRouterAgentPluginFn: (opts?: {
365
+ router?: QueryRouter;
366
+ query?: string;
367
+ }) => Plugin;
368
+
369
+ export { addCallFn, createRouterAgentPluginFn };