@fraqjs/fraq 0.8.1 → 0.9.0
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 +5 -8
- package/dist/index.d.mts +32 -6
- package/dist/index.mjs +65 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -27,15 +27,12 @@ const ctx = Context.fromUrl('<替换成你的 Milky 协议端地址>', {
|
|
|
27
27
|
// accessToken: '<如果你的 Milky 协议端配置了 token,请在这里添加>',
|
|
28
28
|
});
|
|
29
29
|
|
|
30
|
-
ctx.router
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
},
|
|
35
|
-
execute(session, { content }) {
|
|
30
|
+
ctx.router
|
|
31
|
+
.command('echo')
|
|
32
|
+
.arg('content', param.greedy())
|
|
33
|
+
.execute((session, { content }) => {
|
|
36
34
|
session.reply(msg`You said: ${content}`);
|
|
37
|
-
}
|
|
38
|
-
});
|
|
35
|
+
});
|
|
39
36
|
|
|
40
37
|
ctx.start();
|
|
41
38
|
```
|
package/dist/index.d.mts
CHANGED
|
@@ -5506,11 +5506,10 @@ declare namespace param {
|
|
|
5506
5506
|
>;
|
|
5507
5507
|
}
|
|
5508
5508
|
//#endregion
|
|
5509
|
-
//#region src/routing/
|
|
5509
|
+
//#region src/routing/command.d.ts
|
|
5510
5510
|
type Pattern = Record<string, Parameter<any>>;
|
|
5511
5511
|
type ParamsOf<P extends Pattern> = { [K in keyof P]: P[K] extends Parameter<infer T> ? T : never };
|
|
5512
5512
|
type Executor<P extends Pattern> = (session: Session, params: ParamsOf<P>) => void | Promise<void>;
|
|
5513
|
-
type SessionPredicate = (session: Session) => boolean;
|
|
5514
5513
|
interface Command<P extends Pattern> {
|
|
5515
5514
|
name: string;
|
|
5516
5515
|
pattern: P;
|
|
@@ -5520,6 +5519,32 @@ interface RawPattern<P extends Pattern> {
|
|
|
5520
5519
|
pattern: P;
|
|
5521
5520
|
execute: Executor<P>;
|
|
5522
5521
|
}
|
|
5522
|
+
interface Session {
|
|
5523
|
+
raw: IncomingMessage;
|
|
5524
|
+
reply(segments: OutgoingSegment_ZodInput[]): Promise<void>;
|
|
5525
|
+
}
|
|
5526
|
+
declare class CommandBuilder<P extends Pattern = {}, S = Command<P>> {
|
|
5527
|
+
readonly name: string;
|
|
5528
|
+
private readonly sink;
|
|
5529
|
+
private readonly pattern;
|
|
5530
|
+
private executor?;
|
|
5531
|
+
constructor(name: string, sink?: (command: Command<P>) => S);
|
|
5532
|
+
arg<K extends string, T>(
|
|
5533
|
+
key: K,
|
|
5534
|
+
parameter: Parameter<T>,
|
|
5535
|
+
): CommandBuilder<
|
|
5536
|
+
P & { [K2 in K]: Parameter<T> },
|
|
5537
|
+
S extends Command<P>
|
|
5538
|
+
? Command<P & { [K2 in K]: Parameter<T> }>
|
|
5539
|
+
: S extends RawPattern<P>
|
|
5540
|
+
? RawPattern<P & { [K2 in K]: Parameter<T> }>
|
|
5541
|
+
: S
|
|
5542
|
+
>;
|
|
5543
|
+
execute(executor: Executor<P>): S;
|
|
5544
|
+
}
|
|
5545
|
+
//#endregion
|
|
5546
|
+
//#region src/routing/router.d.ts
|
|
5547
|
+
type SessionPredicate = (session: Session) => boolean;
|
|
5523
5548
|
type RouteEntry =
|
|
5524
5549
|
| {
|
|
5525
5550
|
type: 'command';
|
|
@@ -5563,14 +5588,12 @@ type RouteMatchResult =
|
|
|
5563
5588
|
rawPattern: RawPattern<Pattern>;
|
|
5564
5589
|
params: any;
|
|
5565
5590
|
};
|
|
5566
|
-
interface Session {
|
|
5567
|
-
raw: IncomingMessage;
|
|
5568
|
-
reply(segments: OutgoingSegment_ZodInput[]): Promise<void>;
|
|
5569
|
-
}
|
|
5570
5591
|
declare class Router {
|
|
5571
5592
|
private readonly entries;
|
|
5572
5593
|
private readonly groups;
|
|
5594
|
+
command(name: string): CommandBuilder;
|
|
5573
5595
|
command<P extends Pattern>(command: Command<P>): this;
|
|
5596
|
+
rawPattern(): CommandBuilder<{}, RawPattern<{}>>;
|
|
5574
5597
|
rawPattern<P extends Pattern>(rawPattern: RawPattern<P>): this;
|
|
5575
5598
|
group(name: string): Router;
|
|
5576
5599
|
filter(predicate: SessionPredicate): Router;
|
|
@@ -5594,7 +5617,9 @@ declare namespace filter {
|
|
|
5594
5617
|
function define(filter: Filter): Filter;
|
|
5595
5618
|
function allPass(): Filter;
|
|
5596
5619
|
function friend(...uinList: number[]): Filter;
|
|
5620
|
+
function allFriends(): Filter;
|
|
5597
5621
|
function group(...uinList: number[]): Filter;
|
|
5622
|
+
function allGroups(): Filter;
|
|
5598
5623
|
function admin(): Filter;
|
|
5599
5624
|
function or(...filters: Filter[]): Filter;
|
|
5600
5625
|
function and(...filters: Filter[]): Filter;
|
|
@@ -5782,6 +5807,7 @@ export {
|
|
|
5782
5807
|
ApiEndpoints,
|
|
5783
5808
|
Capturer,
|
|
5784
5809
|
Command,
|
|
5810
|
+
CommandBuilder,
|
|
5785
5811
|
Context,
|
|
5786
5812
|
ContextOptions,
|
|
5787
5813
|
ContextUrlOptions,
|
package/dist/index.mjs
CHANGED
|
@@ -64,6 +64,31 @@ function createMilkyClient(...params) {
|
|
|
64
64
|
} });
|
|
65
65
|
}
|
|
66
66
|
//#endregion
|
|
67
|
+
//#region src/routing/command.ts
|
|
68
|
+
var CommandBuilder = class {
|
|
69
|
+
name;
|
|
70
|
+
sink;
|
|
71
|
+
pattern = {};
|
|
72
|
+
executor;
|
|
73
|
+
constructor(name, sink = (command) => command) {
|
|
74
|
+
this.name = name;
|
|
75
|
+
this.sink = sink;
|
|
76
|
+
}
|
|
77
|
+
arg(key, parameter) {
|
|
78
|
+
this.pattern[key] = parameter;
|
|
79
|
+
return this;
|
|
80
|
+
}
|
|
81
|
+
execute(executor) {
|
|
82
|
+
this.executor = executor;
|
|
83
|
+
const command = {
|
|
84
|
+
name: this.name,
|
|
85
|
+
pattern: this.pattern,
|
|
86
|
+
execute: this.executor
|
|
87
|
+
};
|
|
88
|
+
return this.sink(command);
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
//#endregion
|
|
67
92
|
//#region src/routing/tokenizer.ts
|
|
68
93
|
var Tokenizer = class {
|
|
69
94
|
segments;
|
|
@@ -192,6 +217,10 @@ var Router = class Router {
|
|
|
192
217
|
entries = [];
|
|
193
218
|
groups = /* @__PURE__ */ new Map();
|
|
194
219
|
command(command) {
|
|
220
|
+
if (typeof command === "string") return new CommandBuilder(command, (builtCommand) => {
|
|
221
|
+
this.command(builtCommand);
|
|
222
|
+
return builtCommand;
|
|
223
|
+
});
|
|
195
224
|
this.validatePattern(command.pattern);
|
|
196
225
|
this.entries.push({
|
|
197
226
|
type: "command",
|
|
@@ -200,6 +229,10 @@ var Router = class Router {
|
|
|
200
229
|
return this;
|
|
201
230
|
}
|
|
202
231
|
rawPattern(rawPattern) {
|
|
232
|
+
if (!rawPattern) return new CommandBuilder("", (builtCommand) => {
|
|
233
|
+
this.rawPattern(builtCommand);
|
|
234
|
+
return builtCommand;
|
|
235
|
+
});
|
|
203
236
|
this.validatePattern(rawPattern.pattern, { rawPattern: true });
|
|
204
237
|
this.entries.push({
|
|
205
238
|
type: "rawPattern",
|
|
@@ -878,6 +911,17 @@ let filter;
|
|
|
878
911
|
};
|
|
879
912
|
}
|
|
880
913
|
_filter.friend = friend;
|
|
914
|
+
function allFriends() {
|
|
915
|
+
return {
|
|
916
|
+
message_receive: ({ data }) => data.message_scene === "friend",
|
|
917
|
+
message_recall: ({ data }) => data.message_scene === "friend",
|
|
918
|
+
peer_pin_change: ({ data }) => data.message_scene === "friend",
|
|
919
|
+
group_invitation: () => true,
|
|
920
|
+
friend_nudge: () => true,
|
|
921
|
+
friend_file_upload: () => true
|
|
922
|
+
};
|
|
923
|
+
}
|
|
924
|
+
_filter.allFriends = allFriends;
|
|
881
925
|
function group(...uinList) {
|
|
882
926
|
const uinSet = new Set(uinList);
|
|
883
927
|
return {
|
|
@@ -899,6 +943,26 @@ let filter;
|
|
|
899
943
|
};
|
|
900
944
|
}
|
|
901
945
|
_filter.group = group;
|
|
946
|
+
function allGroups() {
|
|
947
|
+
return {
|
|
948
|
+
message_receive: ({ data }) => data.message_scene === "group",
|
|
949
|
+
message_recall: ({ data }) => data.message_scene === "group",
|
|
950
|
+
peer_pin_change: ({ data }) => data.message_scene === "group",
|
|
951
|
+
group_join_request: () => true,
|
|
952
|
+
group_invited_join_request: () => true,
|
|
953
|
+
group_admin_change: () => true,
|
|
954
|
+
group_essence_message_change: () => true,
|
|
955
|
+
group_member_increase: () => true,
|
|
956
|
+
group_member_decrease: () => true,
|
|
957
|
+
group_name_change: () => true,
|
|
958
|
+
group_message_reaction: () => true,
|
|
959
|
+
group_mute: () => true,
|
|
960
|
+
group_whole_mute: () => true,
|
|
961
|
+
group_nudge: () => true,
|
|
962
|
+
group_file_upload: () => true
|
|
963
|
+
};
|
|
964
|
+
}
|
|
965
|
+
_filter.allGroups = allGroups;
|
|
902
966
|
function admin() {
|
|
903
967
|
return { message_receive: ({ data }) => data.message_scene === "group" && data.group_member.role !== "member" };
|
|
904
968
|
}
|
|
@@ -1199,4 +1263,4 @@ let param;
|
|
|
1199
1263
|
_param.segment = segment;
|
|
1200
1264
|
})(param || (param = {}));
|
|
1201
1265
|
//#endregion
|
|
1202
|
-
export { Context, Logger, Parameter, Router, combineLogHandlers, createMilkyClient, definePlugin, filter, implementsESNextDisposable, isDisposable, milkyPackageVersion, milkyVersion, msg, param, seg };
|
|
1266
|
+
export { CommandBuilder, Context, Logger, Parameter, Router, combineLogHandlers, createMilkyClient, definePlugin, filter, implementsESNextDisposable, isDisposable, milkyPackageVersion, milkyVersion, msg, param, seg };
|