napcon 0.0.9

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/README.md ADDED
@@ -0,0 +1,16 @@
1
+ # NapCon
2
+
3
+ A lightweight TypeScript library for [NapCat](https://github.com/NapNeko/NapCatQQ) and [OneBot v11](https://11.onebot.dev).
4
+
5
+ > **Note**: This library is currently in active development. APIs are subject to change.
6
+
7
+ ## Features
8
+
9
+ - **Ultra-lightweight**: Zero dependencies, ~1.7KB gzipped.
10
+ - **Simple API**: Connect to the server with a single `open(...)` method.
11
+ - **TypeScript First**: Comprehensive type definitions for events and methods.
12
+ - **Rich Utilities**: Built-in message builders, guards, and helper functions.
13
+
14
+ ## Example
15
+
16
+ Check out [examples/main.ts](https://github.com/suisanka/napcon/blob/main/examples/main.ts) for usage examples.
@@ -0,0 +1,669 @@
1
+ //#region src/types/common.d.ts
2
+ type Dispose = {} & (() => void);
3
+ type MaybePromiseLike<T> = T | PromiseLike<T>;
4
+ type MaybeArray<T> = T | T[];
5
+ //#endregion
6
+ //#region src/types/message.d.ts
7
+ type ProtocolMessageSegment = ProtocolMessageRecvSegment | ProtocolMessageSendSegment;
8
+ type ProtocolMessageRecvSegment = ProtocolMessageTextSegment | ProtocolMessageAtSegment | ProtocolMessageReplySegment | ProtocolMessageFaceSegment | ProtocolMessageDiceRecvSegment | ProtocolMessageRpsRecvSegment | ProtocolMessagePokeRecvSegment | ProtocolMessageImageRecvSegment | ProtocolMessageRecordRecvSegment | ProtocolMessageVideoRecvSegment | ProtocolMessageFileRecvSegment | ProtocolMessageJsonRecvSegment | ProtocolMessageMusicRecvSegment | ProtocolMessageForwardRecvSegment | ProtocolMessageMarkdownSegment | ProtocolMessageMiniAppSegment | ProtocolMessageLocationSegment | ProtocolMessageXmlSegment | ProtocolMessageOnlineFileSegment | ProtocolMessageFlashTransferSegment;
9
+ type ProtocolMessageSendSegment = ProtocolMessageTextSegment | ProtocolMessageAtSegment | ProtocolMessageReplySegment | ProtocolMessageFaceSegment | ProtocolMessageMfaceSendSegment | ProtocolMessageDiceSendSegment | ProtocolMessageRpsSendSegment | ProtocolMessagePokeSendSegment | ProtocolMessageImageSendSegment | ProtocolMessageRecordSendSegment | ProtocolMessageVideoSendSegment | ProtocolMessageFileSendSegment | ProtocolMessageJsonSendSegment | ProtocolMessageMusicSendSegment | ProtocolMessageContactSendSegment | ProtocolMessageMarkdownSegment | ProtocolMessageMiniAppSegment | ProtocolMessageLocationSegment | ProtocolMessageXmlSegment | ProtocolMessageOnlineFileSegment | ProtocolMessageFlashTransferSegment;
10
+ interface ProtocolMessageTextSegment {
11
+ type: 'text';
12
+ data: {
13
+ text: string;
14
+ };
15
+ }
16
+ interface ProtocolMessageMarkdownSegment {
17
+ type: 'markdown';
18
+ data: {
19
+ content: string;
20
+ };
21
+ }
22
+ interface ProtocolMessageAtSegment {
23
+ type: 'at';
24
+ data: {
25
+ qq: string;
26
+ name?: string;
27
+ };
28
+ }
29
+ interface ProtocolMessageReplySegment {
30
+ type: 'reply';
31
+ data: {
32
+ id: string;
33
+ };
34
+ }
35
+ interface ProtocolMessageFaceSegment {
36
+ type: 'face';
37
+ data: {
38
+ id: string;
39
+ };
40
+ }
41
+ interface ProtocolMessageMfaceSendSegment {
42
+ type: 'mface';
43
+ data: {
44
+ emoji_id: string;
45
+ emoji_package_id: string;
46
+ key: string;
47
+ summary?: string;
48
+ };
49
+ }
50
+ interface ProtocolMessageDiceRecvSegment {
51
+ type: 'dice';
52
+ data: {
53
+ result: '1' | '2' | '3' | '4' | '5' | '6';
54
+ };
55
+ }
56
+ interface ProtocolMessageDiceSendSegment {
57
+ type: 'dice';
58
+ data: Record<string, never>;
59
+ }
60
+ interface ProtocolMessageRpsRecvSegment {
61
+ type: 'rps';
62
+ data: {
63
+ result: '1' | '2' | '3';
64
+ };
65
+ }
66
+ interface ProtocolMessageRpsSendSegment {
67
+ type: 'rps';
68
+ data: Record<string, never>;
69
+ }
70
+ interface ProtocolMessagePokeRecvSegment {
71
+ type: 'poke';
72
+ data: {
73
+ type: string;
74
+ id: string;
75
+ };
76
+ }
77
+ interface ProtocolMessagePokeSendSegment {
78
+ type: 'poke';
79
+ data: {
80
+ type: string;
81
+ id: string;
82
+ };
83
+ }
84
+ interface ProtocolMessageImageRecvSegment {
85
+ type: 'image';
86
+ data: {
87
+ file: string;
88
+ name?: string;
89
+ summary?: string;
90
+ sub_type?: number;
91
+ file_id?: string;
92
+ url?: string;
93
+ path?: string;
94
+ file_size?: number;
95
+ file_unique?: string;
96
+ };
97
+ }
98
+ interface ProtocolMessageImageSendSegment {
99
+ type: 'image';
100
+ data: {
101
+ file: string;
102
+ name?: string;
103
+ summary?: string;
104
+ sub_type?: number;
105
+ };
106
+ }
107
+ interface ProtocolMessageContactSendSegment {
108
+ type: 'contact';
109
+ data: {
110
+ type: 'qq' | 'group';
111
+ id: string;
112
+ };
113
+ }
114
+ interface ProtocolMessageRecordRecvSegment {
115
+ type: 'record';
116
+ data: {
117
+ file: string;
118
+ url?: string;
119
+ path?: string;
120
+ file_id?: string;
121
+ file_size?: number;
122
+ file_unique?: string;
123
+ };
124
+ }
125
+ interface ProtocolMessageRecordSendSegment {
126
+ type: 'record';
127
+ data: {
128
+ file: string;
129
+ name?: string;
130
+ };
131
+ }
132
+ interface ProtocolMessageVideoRecvSegment {
133
+ type: 'video';
134
+ data: {
135
+ file: string;
136
+ url?: string;
137
+ path?: string;
138
+ file_id?: string;
139
+ file_size?: number;
140
+ file_unique?: string;
141
+ };
142
+ }
143
+ interface ProtocolMessageVideoSendSegment {
144
+ type: 'video';
145
+ data: {
146
+ file: string;
147
+ name?: string;
148
+ thumb?: string;
149
+ };
150
+ }
151
+ interface ProtocolMessageFileRecvSegment {
152
+ type: 'file';
153
+ data: {
154
+ file: string;
155
+ path?: string;
156
+ url?: string;
157
+ file_id?: string;
158
+ file_size?: number;
159
+ file_unique?: string;
160
+ };
161
+ }
162
+ interface ProtocolMessageFileSendSegment {
163
+ type: 'file';
164
+ data: {
165
+ file: string;
166
+ name?: string;
167
+ };
168
+ }
169
+ interface ProtocolMessageJsonRecvSegment {
170
+ type: 'json';
171
+ data: {
172
+ data: string | object;
173
+ };
174
+ }
175
+ interface ProtocolMessageJsonSendSegment {
176
+ type: 'json';
177
+ data: {
178
+ data: string | object;
179
+ };
180
+ }
181
+ interface ProtocolMessageMusicRecvSegment {
182
+ type: 'music';
183
+ data: {
184
+ type?: 'qq' | '163' | 'kugou' | 'kuwo' | 'migu' | 'custom';
185
+ id?: string;
186
+ url?: string;
187
+ audio?: string;
188
+ title?: string;
189
+ image?: string;
190
+ singer?: string;
191
+ };
192
+ }
193
+ interface ProtocolMessageMusicSendSegment {
194
+ type: 'music';
195
+ data: {
196
+ type: 'qq' | '163' | 'kugou' | 'kuwo' | 'migu' | 'custom';
197
+ id?: string;
198
+ url?: string;
199
+ audio?: string;
200
+ title?: string;
201
+ image?: string;
202
+ singer?: string;
203
+ };
204
+ }
205
+ interface ProtocolMessageForwardRecvSegment {
206
+ type: 'forward';
207
+ data: {
208
+ id: string;
209
+ content?: ProtocolMessageEvent;
210
+ };
211
+ }
212
+ type ProtocolMessageNode = ProtocolMessageRefNode | ProtocolMessageContentNode;
213
+ interface ProtocolMessageRefNode {
214
+ type: 'node';
215
+ data: {
216
+ id: string;
217
+ content?: ProtocolMessageRecvSegment[];
218
+ };
219
+ }
220
+ interface ProtocolMessageContentNode {
221
+ type: 'node';
222
+ data: {
223
+ user_id: string;
224
+ nickname: string;
225
+ id?: string;
226
+ content: ProtocolMessageSegment[];
227
+ };
228
+ }
229
+ interface ProtocolMessageMiniAppSegment {
230
+ type: 'miniapp';
231
+ data: {
232
+ data: string;
233
+ };
234
+ }
235
+ interface ProtocolMessageLocationSegment {
236
+ type: 'location';
237
+ data: {
238
+ lat: string | number;
239
+ lon: string | number;
240
+ title?: string;
241
+ content?: string;
242
+ };
243
+ }
244
+ interface ProtocolMessageXmlSegment {
245
+ type: 'xml';
246
+ data: {
247
+ data: string;
248
+ };
249
+ }
250
+ interface ProtocolMessageOnlineFileSegment {
251
+ type: 'onlinefile';
252
+ data: {
253
+ msgId: string;
254
+ elementId: string;
255
+ fileName: string;
256
+ fileSize: string;
257
+ isDir: boolean;
258
+ };
259
+ }
260
+ interface ProtocolMessageFlashTransferSegment {
261
+ type: 'flashtransfer';
262
+ data: {
263
+ fileSetId: string;
264
+ };
265
+ }
266
+ //#endregion
267
+ //#region src/types/event.d.ts
268
+ type ProtocolEventPostTypes = 'meta_event' | 'notice' | 'request' | 'message' | 'message_sent';
269
+ interface ProtocolEventCommon {
270
+ time: number;
271
+ self_id: number;
272
+ post_type?: string;
273
+ sub_type?: string;
274
+ [x: string]: unknown;
275
+ }
276
+ interface ProtocolMetaEventCommon extends ProtocolEventCommon {
277
+ post_type: 'meta_event';
278
+ }
279
+ interface ProtocolMetaHeartbeatEvent extends ProtocolMetaEventCommon {
280
+ meta_event_type: 'heartbeat';
281
+ status: {
282
+ online?: boolean;
283
+ good: boolean;
284
+ };
285
+ interval: number;
286
+ }
287
+ type ProtocolMetaLifecycleEventSubTypes = 'enable' | 'disable' | 'connect';
288
+ interface ProtocolMetaLifecycleEvent extends ProtocolMetaEventCommon {
289
+ meta_event_type: 'lifecycle';
290
+ sub_type: ProtocolMetaLifecycleEventSubTypes;
291
+ }
292
+ type ProtocolMetaEvent = ProtocolMetaHeartbeatEvent | ProtocolMetaLifecycleEvent;
293
+ interface ProtocolNoticeEventCommon extends ProtocolEventCommon {
294
+ post_type: 'notice';
295
+ }
296
+ interface ProtocolNoticeGroupEventCommon extends ProtocolNoticeEventCommon {
297
+ group_id: number;
298
+ user_id: number;
299
+ }
300
+ interface ProtocolNoticeFriendAddEvent extends ProtocolNoticeEventCommon {
301
+ notice_type: 'friend_add';
302
+ user_id: number;
303
+ }
304
+ interface ProtocolNoticeFriendRecallEvent extends ProtocolNoticeEventCommon {
305
+ notice_type: 'friend_recall';
306
+ user_id: number;
307
+ message_id: number;
308
+ }
309
+ type ProtocolNoticeGroupIncreaseEventSubTypes = 'approve' | 'invite';
310
+ interface ProtocolNoticeGroupIncreaseEvent extends ProtocolNoticeGroupEventCommon {
311
+ notice_type: 'group_increase';
312
+ operator_id: number;
313
+ sub_type: ProtocolNoticeGroupIncreaseEventSubTypes;
314
+ }
315
+ type ProtocolNoticeGroupDecreaseEventSubTypes = 'leave' | 'kick' | 'kick_me' | 'disband';
316
+ interface ProtocolNoticeGroupDecreaseEvent extends ProtocolNoticeGroupEventCommon {
317
+ notice_type: 'group_decrease';
318
+ operator_id: number;
319
+ sub_type: ProtocolNoticeGroupDecreaseEventSubTypes;
320
+ }
321
+ type ProtocolNoticeGroupAdminEventSubTypes = 'set' | 'unset';
322
+ interface ProtocolNoticeGroupAdminEvent extends ProtocolNoticeGroupEventCommon {
323
+ notice_type: 'group_admin';
324
+ sub_type: ProtocolNoticeGroupAdminEventSubTypes;
325
+ }
326
+ type ProtocolNoticeGroupBanEventSubTypes = 'ban' | 'lift_ban';
327
+ interface ProtocolNoticeGroupBanEvent extends ProtocolNoticeGroupEventCommon {
328
+ notice_type: 'group_ban';
329
+ operator_id: number;
330
+ sub_type: ProtocolNoticeGroupBanEventSubTypes;
331
+ duration: number;
332
+ }
333
+ interface ProtocolNoticeGroupUploadFile {
334
+ id: string;
335
+ name: string;
336
+ size: number;
337
+ busid: number;
338
+ }
339
+ interface ProtocolNoticeGroupUploadEvent extends ProtocolNoticeGroupEventCommon {
340
+ notice_type: 'group_upload';
341
+ file: ProtocolNoticeGroupUploadFile;
342
+ }
343
+ interface ProtocolNoticeGroupRecallEvent extends ProtocolNoticeGroupEventCommon {
344
+ notice_type: 'group_recall';
345
+ operator_id: number;
346
+ message_id: number;
347
+ }
348
+ interface ProtocolNoticeGroupCardEvent extends ProtocolNoticeGroupEventCommon {
349
+ notice_type: 'group_card';
350
+ card_new: string;
351
+ card_old: string;
352
+ }
353
+ interface ProtocolNoticeNotifyGroupNameEvent extends ProtocolNoticeGroupEventCommon {
354
+ notice_type: 'notify';
355
+ sub_type: 'group_name';
356
+ name_new: string;
357
+ }
358
+ interface ProtocolNoticeNotifyGroupTitleEvent extends ProtocolNoticeGroupEventCommon {
359
+ notice_type: 'notify';
360
+ sub_type: 'title';
361
+ title: string;
362
+ }
363
+ type ProtocolNoticeGroupEssenceEventSubTypes = 'add' | 'delete';
364
+ interface ProtocolNoticeGroupEssenceEvent extends ProtocolNoticeGroupEventCommon {
365
+ notice_type: 'essence';
366
+ message_id: number;
367
+ sender_id: number;
368
+ operator_id: number;
369
+ sub_type: ProtocolNoticeGroupEssenceEventSubTypes;
370
+ }
371
+ interface ProtocolMessageEmojiLike {
372
+ emoji_id: string;
373
+ count: number;
374
+ }
375
+ interface ProtocolNoticeGroupMessageEmojiLikeEvent extends ProtocolNoticeGroupEventCommon {
376
+ notice_type: 'group_msg_emoji_like';
377
+ message_id: number;
378
+ likes: ProtocolMessageEmojiLike[];
379
+ }
380
+ interface ProtocolNoticeNotifyPokeEventCommon extends ProtocolNoticeEventCommon {
381
+ notice_type: 'notify';
382
+ sub_type: 'poke';
383
+ target_id: number;
384
+ user_id: number;
385
+ [x: string]: any;
386
+ }
387
+ interface ProtocolNoticeNotifyGroupPokeEvent extends ProtocolNoticeNotifyPokeEventCommon {
388
+ group_id: number;
389
+ raw_info: any;
390
+ }
391
+ interface ProtocolNoticeNotifyFriendPokeEvent extends ProtocolNoticeNotifyPokeEventCommon {
392
+ sender_id: number;
393
+ raw_info: any;
394
+ }
395
+ type ProtocolNoticeNotifyPokeEvent = ProtocolNoticeNotifyGroupPokeEvent | ProtocolNoticeNotifyFriendPokeEvent;
396
+ interface ProtocolNoticeNotifyProfileLikeEvent extends ProtocolNoticeEventCommon {
397
+ notice_type: 'notify';
398
+ sub_type: 'profile_like';
399
+ operator_id: number;
400
+ operator_nick: string;
401
+ times: number;
402
+ time: number;
403
+ }
404
+ interface ProtocolNoticeNotifyInputStatusEvent extends ProtocolNoticeEventCommon {
405
+ notice_type: 'notify';
406
+ sub_type: 'input_status';
407
+ status_text: string;
408
+ event_type: number;
409
+ user_id: number;
410
+ group_id?: number;
411
+ }
412
+ interface ProtocolNoticeBotOfflineEvent extends ProtocolNoticeEventCommon {
413
+ notice_type: 'bot_offline';
414
+ user_id: number;
415
+ tag: string;
416
+ message: string;
417
+ }
418
+ type ProtocolNoticeNotifyEvent = ProtocolNoticeNotifyGroupNameEvent | ProtocolNoticeNotifyGroupTitleEvent | ProtocolNoticeNotifyProfileLikeEvent | ProtocolNoticeNotifyInputStatusEvent | ProtocolNoticeNotifyPokeEvent;
419
+ type ProtocolNoticeGroupEvent = ProtocolNoticeGroupIncreaseEvent | ProtocolNoticeGroupDecreaseEvent | ProtocolNoticeGroupAdminEvent | ProtocolNoticeGroupBanEvent | ProtocolNoticeGroupUploadEvent | ProtocolNoticeGroupCardEvent | ProtocolNoticeGroupEssenceEvent | ProtocolNoticeGroupMessageEmojiLikeEvent | ProtocolNoticeGroupRecallEvent;
420
+ type ProtocolNoticeFriendEvent = ProtocolNoticeFriendAddEvent | ProtocolNoticeFriendRecallEvent;
421
+ type ProtocolNoticeEvent = ProtocolNoticeNotifyEvent | ProtocolNoticeGroupEvent | ProtocolNoticeFriendEvent;
422
+ interface ProtocolRequestEventCommon {
423
+ post_type: 'request';
424
+ request_type: 'friend' | 'group';
425
+ }
426
+ interface ProtocolFriendRequestEvent extends ProtocolRequestEventCommon {
427
+ request_type: 'friend';
428
+ user_id: number;
429
+ comment: string;
430
+ flag: string;
431
+ }
432
+ interface ProtocolGroupRequestEvent extends ProtocolRequestEventCommon {
433
+ request_type: 'group';
434
+ user_id: number;
435
+ comment: string;
436
+ flag: string;
437
+ sub_type: string;
438
+ }
439
+ type ProtocolRequestEvent = ProtocolFriendRequestEvent | ProtocolGroupRequestEvent;
440
+ interface ProtocolMessageEventCommon extends ProtocolEventCommon {
441
+ post_type: 'message';
442
+ message_id: number;
443
+ message_type: 'private' | 'group';
444
+ user_id: number;
445
+ message: string | ProtocolMessageRecvSegment[];
446
+ message_format: 'string' | 'array';
447
+ font: string;
448
+ raw_message: string;
449
+ }
450
+ type ProtocolMessagePrivateEventSubTypes = 'friend' | 'group' | 'other';
451
+ interface ProtocolMessagePrivateEvent extends ProtocolMessageEventCommon {
452
+ message_type: 'private';
453
+ user_id: number;
454
+ sub_type: ProtocolMessagePrivateEventSubTypes;
455
+ sender: {
456
+ user_id: number;
457
+ nickname: string;
458
+ sex: 'male' | 'female' | 'unknown';
459
+ age: number;
460
+ area?: string;
461
+ };
462
+ }
463
+ interface ProtocolMessageGroupEvent extends ProtocolMessageEventCommon {
464
+ message_type: 'group';
465
+ group_id: number;
466
+ anonymous?: any;
467
+ sender: {
468
+ user_id: number;
469
+ nickname: string;
470
+ card: string;
471
+ role: 'owner' | 'admin' | 'member';
472
+ title: string;
473
+ level: string;
474
+ sex: 'male' | 'female' | 'unknown';
475
+ age: number;
476
+ area: string;
477
+ };
478
+ }
479
+ type ProtocolMessageEvent = ProtocolMessagePrivateEvent | ProtocolMessageGroupEvent;
480
+ interface ProtocolMessageSentEvent extends Omit<ProtocolMessageEventCommon, 'post_type'> {
481
+ post_type: 'message_sent';
482
+ target_id: number;
483
+ }
484
+ type ProtocolEvent = ProtocolMetaEvent | ProtocolNoticeEvent | ProtocolRequestEvent | ProtocolMessageEvent | ProtocolMessageSentEvent;
485
+ type _GenerateProtocolEventNames<T extends Record<string | number, any>, O extends string = ''> = { [K in Exclude<keyof T, '_'>]: K extends string | number ? T[K] extends Record<'_', any> ? `${O}${K}` | _GenerateProtocolEventNames<T[K], `${O}${K}.`> : `${O}${K}` : never }[Exclude<keyof T, '_'>];
486
+ interface ProtocolEventNamePaths {
487
+ meta: {
488
+ _: ProtocolMetaEvent;
489
+ heartbeat: ProtocolMetaHeartbeatEvent;
490
+ lifecycle: {
491
+ _: ProtocolMetaLifecycleEvent;
492
+ enable: ProtocolMetaLifecycleEvent;
493
+ disable: ProtocolMetaLifecycleEvent;
494
+ connect: ProtocolMetaLifecycleEvent;
495
+ };
496
+ };
497
+ notice: {
498
+ _: ProtocolNoticeEvent;
499
+ group: {
500
+ _: ProtocolNoticeGroupEvent;
501
+ recall: ProtocolNoticeGroupRecallEvent;
502
+ increase: ProtocolNoticeGroupIncreaseEvent;
503
+ decrease: ProtocolNoticeGroupDecreaseEvent;
504
+ admin: {
505
+ _: ProtocolNoticeGroupAdminEvent;
506
+ set: ProtocolNoticeGroupAdminEvent;
507
+ unset: ProtocolNoticeGroupAdminEvent;
508
+ };
509
+ ban: ProtocolNoticeGroupBanEvent;
510
+ upload: ProtocolNoticeGroupUploadEvent;
511
+ card: ProtocolNoticeGroupCardEvent;
512
+ essence: {
513
+ _: ProtocolNoticeGroupEssenceEvent;
514
+ add: ProtocolNoticeGroupEssenceEvent;
515
+ delete: ProtocolNoticeGroupEssenceEvent;
516
+ };
517
+ reaction: ProtocolNoticeGroupMessageEmojiLikeEvent;
518
+ };
519
+ bot: {
520
+ _: ProtocolNoticeBotOfflineEvent;
521
+ offline: ProtocolNoticeBotOfflineEvent;
522
+ };
523
+ notify: {
524
+ _: ProtocolNoticeNotifyEvent;
525
+ group_name: ProtocolNoticeNotifyGroupNameEvent;
526
+ group_title: ProtocolNoticeNotifyGroupTitleEvent;
527
+ poke: ProtocolNoticeNotifyPokeEvent;
528
+ profile_like: ProtocolNoticeNotifyProfileLikeEvent;
529
+ input_status: ProtocolNoticeNotifyInputStatusEvent;
530
+ };
531
+ friend: {
532
+ _: ProtocolNoticeFriendEvent;
533
+ add: ProtocolNoticeFriendAddEvent;
534
+ recall: ProtocolNoticeFriendRecallEvent;
535
+ };
536
+ };
537
+ request: {
538
+ _: ProtocolRequestEvent;
539
+ friend: ProtocolFriendRequestEvent;
540
+ group: ProtocolGroupRequestEvent;
541
+ };
542
+ message: {
543
+ _: ProtocolMessageEvent;
544
+ private: {
545
+ _: ProtocolMessagePrivateEvent;
546
+ friend: ProtocolMessagePrivateEvent;
547
+ group: ProtocolMessagePrivateEvent;
548
+ other: ProtocolMessagePrivateEvent;
549
+ };
550
+ group: ProtocolMessageGroupEvent;
551
+ sent: ProtocolMessageSentEvent;
552
+ };
553
+ }
554
+ type _GetProtocolEventByName<K extends string, T extends Record<string | number, any>> = K extends `${infer O}.${infer R}` ? _GetProtocolEventByName<R, T[O]> : K extends keyof T ? T[K] extends Record<'_', any> ? T[K]['_'] : T[K] : never;
555
+ type ProtocolEventHandlers = { [T in ProtocolEventNames]: [event: _GetProtocolEventByName<T, ProtocolEventNamePaths>, connection: Connection] };
556
+ type ProtocolEventNames = _GenerateProtocolEventNames<ProtocolEventNamePaths>;
557
+ //#endregion
558
+ //#region src/types/protocol.d.ts
559
+ interface ProtocolRequest<T = any> {
560
+ action: string;
561
+ params: T;
562
+ echo?: string;
563
+ stream?: 'normal-action' | 'stream-action';
564
+ }
565
+ interface ProtocolStreamDataMessage {
566
+ type: 'stream';
567
+ data_type: 'data_chunk';
568
+ data: string;
569
+ }
570
+ interface ProtocolStreamCompleteMessage<T> {
571
+ type: 'response';
572
+ data_type: 'data_complete';
573
+ data: T;
574
+ }
575
+ interface ProtocolStreamErrorMessage {
576
+ type: 'error';
577
+ data_type: 'error';
578
+ }
579
+ type ProtocolStreamMessage<T = any> = ProtocolStreamDataMessage | ProtocolStreamCompleteMessage<T> | ProtocolStreamErrorMessage;
580
+ type ProtocolReplyStream<T = any> = ProtocolReply<ProtocolStreamMessage<T>>;
581
+ type ProtocolReplyStreamResponse<T = any> = ProtocolReplyOk<ProtocolStreamCompleteMessage<T>>;
582
+ type ProtocolReadableStream = ReadableStream<ProtocolStreamDataMessage>;
583
+ interface ProtocolReplyOk<T = any> {
584
+ status: 'ok';
585
+ retcode: number;
586
+ data: T;
587
+ echo?: string;
588
+ message: string;
589
+ wording: string;
590
+ stream: 'normal-action' | 'stream-action';
591
+ }
592
+ interface ProtocolReplyFailed {
593
+ status: 'failed';
594
+ retcode: number;
595
+ data?: null;
596
+ message: string;
597
+ wording: string;
598
+ echo?: string;
599
+ stream: 'normal-action' | 'stream-action';
600
+ }
601
+ type ProtocolReply<T = any> = ProtocolReplyOk<T> | ProtocolReplyFailed;
602
+ //#endregion
603
+ //#region src/types/pubsub.d.ts
604
+ interface SubHalf<T extends Record<string, any[]>> {
605
+ on<const K extends keyof T>(type: K, ...listeners: Array<(...args: T[K]) => void>): Dispose;
606
+ }
607
+ interface OffHalf<T extends Record<string, any> = Record<string, any>> {
608
+ off: (type: keyof T, ...listeners: Array<(...args: any[]) => any>) => void;
609
+ }
610
+ interface PubHalf<T extends Record<string, any> = Record<string, any>> {
611
+ emit: <const K extends keyof T>(type: K, ...args: any[]) => void;
612
+ }
613
+ type SubOff<T extends Record<string, any[]> = Record<string, any[]>> = SubHalf<T> & OffHalf<T>;
614
+ type PubSub<T extends Record<string, any[]> = Record<string, any[]>> = PubHalf<T> & SubHalf<T>;
615
+ type PubSubOff<T extends Record<string, any[]> = Record<string, any[]>> = PubSub<T> & OffHalf<T>;
616
+ //#endregion
617
+ //#region src/types/transport.d.ts
618
+ interface TransportEventHandlers {
619
+ message: (data: any) => void;
620
+ open: () => void;
621
+ error: (error: any) => void;
622
+ close: () => void;
623
+ }
624
+ type ReadyState = 0 | 1 | 2 | 3;
625
+ interface TransportCommon {
626
+ readonly readyState: ReadyState;
627
+ send: (data: string | ArrayBuffer) => void;
628
+ close: () => void;
629
+ }
630
+ interface Transport extends TransportCommon {
631
+ addEventListener: ((type: 'message', listener: (event: {
632
+ data: any;
633
+ }) => void) => void) & ((type: 'open', listener: () => void) => void) & ((type: 'error', listener: (error: any) => void) => void) & ((type: 'close', listener: () => void) => void);
634
+ removeEventListener: (type: string, listener: (...args: any[]) => any) => void;
635
+ }
636
+ //#endregion
637
+ //#region src/types/connection.d.ts
638
+ interface ConnectionBasicEventHandlers {
639
+ 'connection.connected': [transport: Transport, connection: Connection];
640
+ 'connection.disconnected': [transport: Transport, connection: Connection];
641
+ 'connection.reconnect': [transport: Transport, connection: Connection];
642
+ 'connection.request': [event: ProtocolRequest, connection: Connection];
643
+ 'connection.reply': [event: ProtocolReply, connection: Connection];
644
+ 'connection.event': [event: ProtocolEvent, connection: Connection];
645
+ 'connection.error': [error: any, connection: Connection];
646
+ 'connection.reply.stream': [data: ProtocolReply, connection: Connection];
647
+ }
648
+ type ConnectionEventHandlers = Omit<ConnectionBasicEventHandlers, never>;
649
+ type ConnectionPubSub = PubSubOff<ConnectionEventHandlers>;
650
+ type ConnectionStreamResult<R = any> = [stream: ProtocolReadableStream, result: Promise<ProtocolStreamCompleteMessage<R>>];
651
+ interface Connection extends ConnectionPubSub {
652
+ readonly transport: Transport;
653
+ connect: () => Promise<void>;
654
+ close: () => void;
655
+ request: (<const P, const R = any>(method: string, args: P, stream?: false) => Promise<R>) & (<const P, const R = any>(method: string, args: P, stream: true) => Promise<ConnectionStreamResult<R>>);
656
+ }
657
+ interface OpenConnectionOptions {
658
+ transport: (token: string) => MaybePromiseLike<Transport>;
659
+ token?: string;
660
+ timeout?: number;
661
+ reconnect: false | {
662
+ interval: number;
663
+ attempts: 'always' | number;
664
+ };
665
+ }
666
+ type OpenConnection = (options: OpenConnectionOptions) => Connection;
667
+ //#endregion
668
+ export { ProtocolNoticeFriendEvent as $, ProtocolMessageRecordRecvSegment as $t, ProtocolEventCommon as A, ProtocolMessageContactSendSegment as At, ProtocolMessageGroupEvent as B, ProtocolMessageImageSendSegment as Bt, ProtocolReplyStreamResponse as C, ProtocolNoticeNotifyInputStatusEvent as Ct, ProtocolStreamErrorMessage as D, ProtocolRequestEvent as Dt, ProtocolStreamDataMessage as E, ProtocolNoticeNotifyProfileLikeEvent as Et, ProtocolFriendRequestEvent as F, ProtocolMessageFileRecvSegment as Ft, ProtocolMetaEventCommon as G, ProtocolMessageMfaceSendSegment as Gt, ProtocolMessagePrivateEventSubTypes as H, ProtocolMessageJsonSendSegment as Ht, ProtocolGroupRequestEvent as I, ProtocolMessageFileSendSegment as It, ProtocolMetaLifecycleEventSubTypes as J, ProtocolMessageMusicSendSegment as Jt, ProtocolMetaHeartbeatEvent as K, ProtocolMessageMiniAppSegment as Kt, ProtocolMessageEmojiLike as L, ProtocolMessageFlashTransferSegment as Lt, ProtocolEventNamePaths as M, ProtocolMessageDiceRecvSegment as Mt, ProtocolEventNames as N, ProtocolMessageDiceSendSegment as Nt, ProtocolStreamMessage as O, ProtocolRequestEventCommon as Ot, ProtocolEventPostTypes as P, ProtocolMessageFaceSegment as Pt, ProtocolNoticeFriendAddEvent as Q, ProtocolMessagePokeSendSegment as Qt, ProtocolMessageEvent as R, ProtocolMessageForwardRecvSegment as Rt, ProtocolReplyStream as S, ProtocolNoticeNotifyGroupTitleEvent as St, ProtocolStreamCompleteMessage as T, ProtocolNoticeNotifyPokeEventCommon as Tt, ProtocolMessageSentEvent as U, ProtocolMessageLocationSegment as Ut, ProtocolMessagePrivateEvent as V, ProtocolMessageJsonRecvSegment as Vt, ProtocolMetaEvent as W, ProtocolMessageMarkdownSegment as Wt, ProtocolNoticeEvent as X, ProtocolMessageOnlineFileSegment as Xt, ProtocolNoticeBotOfflineEvent as Y, ProtocolMessageNode as Yt, ProtocolNoticeEventCommon as Z, ProtocolMessagePokeRecvSegment as Zt, SubOff as _, ProtocolNoticeGroupUploadFile as _t, ConnectionStreamResult as a, ProtocolMessageRpsSendSegment as an, ProtocolNoticeGroupCardEvent as at, ProtocolReplyFailed as b, ProtocolNoticeNotifyGroupNameEvent as bt, ReadyState as c, ProtocolMessageTextSegment as cn, ProtocolNoticeGroupEssenceEvent as ct, TransportEventHandlers as d, ProtocolMessageXmlSegment as dn, ProtocolNoticeGroupEventCommon as dt, ProtocolMessageRecordSendSegment as en, ProtocolNoticeFriendRecallEvent as et, OffHalf as f, Dispose as fn, ProtocolNoticeGroupIncreaseEvent as ft, SubHalf as g, ProtocolNoticeGroupUploadEvent as gt, PubSubOff as h, ProtocolNoticeGroupRecallEvent as ht, ConnectionPubSub as i, ProtocolMessageRpsRecvSegment as in, ProtocolNoticeGroupBanEventSubTypes as it, ProtocolEventHandlers as j, ProtocolMessageContentNode as jt, ProtocolEvent as k, ProtocolMessageAtSegment as kt, Transport as l, ProtocolMessageVideoRecvSegment as ln, ProtocolNoticeGroupEssenceEventSubTypes as lt, PubSub as m, MaybePromiseLike as mn, ProtocolNoticeGroupMessageEmojiLikeEvent as mt, ConnectionBasicEventHandlers as n, ProtocolMessageRefNode as nn, ProtocolNoticeGroupAdminEventSubTypes as nt, OpenConnection as o, ProtocolMessageSegment as on, ProtocolNoticeGroupDecreaseEvent as ot, PubHalf as p, MaybeArray as pn, ProtocolNoticeGroupIncreaseEventSubTypes as pt, ProtocolMetaLifecycleEvent as q, ProtocolMessageMusicRecvSegment as qt, ConnectionEventHandlers as r, ProtocolMessageReplySegment as rn, ProtocolNoticeGroupBanEvent as rt, OpenConnectionOptions as s, ProtocolMessageSendSegment as sn, ProtocolNoticeGroupDecreaseEventSubTypes as st, Connection as t, ProtocolMessageRecvSegment as tn, ProtocolNoticeGroupAdminEvent as tt, TransportCommon as u, ProtocolMessageVideoSendSegment as un, ProtocolNoticeGroupEvent as ut, ProtocolReadableStream as v, ProtocolNoticeNotifyEvent as vt, ProtocolRequest as w, ProtocolNoticeNotifyPokeEvent as wt, ProtocolReplyOk as x, ProtocolNoticeNotifyGroupPokeEvent as xt, ProtocolReply as y, ProtocolNoticeNotifyFriendPokeEvent as yt, ProtocolMessageEventCommon as z, ProtocolMessageImageRecvSegment as zt };
669
+ //# sourceMappingURL=connection-CBr_CAbl.d.mts.map