@fraqjs/fraq 0.7.0 → 0.8.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 -5
- package/dist/index.d.mts +16 -16
- package/dist/index.mjs +7 -15
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -27,15 +27,15 @@ const ctx = Context.fromUrl('<替换成你的 Milky 协议端地址>', {
|
|
|
27
27
|
// accessToken: '<如果你的 Milky 协议端配置了 token,请在这里添加>',
|
|
28
28
|
});
|
|
29
29
|
|
|
30
|
-
ctx.router.command(
|
|
31
|
-
'echo',
|
|
32
|
-
{
|
|
30
|
+
ctx.router.command({
|
|
31
|
+
name: 'echo',
|
|
32
|
+
pattern: {
|
|
33
33
|
content: param.greedy(),
|
|
34
34
|
},
|
|
35
|
-
(session, { content })
|
|
35
|
+
execute(session, { content }) {
|
|
36
36
|
session.reply(msg`You said: ${content}`);
|
|
37
37
|
},
|
|
38
|
-
);
|
|
38
|
+
});
|
|
39
39
|
|
|
40
40
|
ctx.start();
|
|
41
41
|
```
|
package/dist/index.d.mts
CHANGED
|
@@ -5505,21 +5505,21 @@ declare namespace param {
|
|
|
5505
5505
|
//#region src/routing/router.d.ts
|
|
5506
5506
|
type Pattern = Record<string, Parameter<any>>;
|
|
5507
5507
|
type ParamsOf<P extends Pattern> = { [K in keyof P]: P[K] extends Parameter<infer T> ? T : never };
|
|
5508
|
-
type
|
|
5508
|
+
type Executor<P extends Pattern> = (session: Session, params: ParamsOf<P>) => void | Promise<void>;
|
|
5509
5509
|
type SessionPredicate = (session: Session) => boolean;
|
|
5510
|
-
interface Command {
|
|
5510
|
+
interface Command<P extends Pattern> {
|
|
5511
5511
|
name: string;
|
|
5512
|
-
pattern:
|
|
5513
|
-
|
|
5512
|
+
pattern: P;
|
|
5513
|
+
execute: Executor<P>;
|
|
5514
5514
|
}
|
|
5515
|
-
interface RawPattern {
|
|
5516
|
-
pattern:
|
|
5517
|
-
|
|
5515
|
+
interface RawPattern<P extends Pattern> {
|
|
5516
|
+
pattern: P;
|
|
5517
|
+
execute: Executor<P>;
|
|
5518
5518
|
}
|
|
5519
5519
|
type RouteEntry =
|
|
5520
5520
|
| {
|
|
5521
5521
|
type: 'command';
|
|
5522
|
-
command: Command
|
|
5522
|
+
command: Command<Pattern>;
|
|
5523
5523
|
}
|
|
5524
5524
|
| {
|
|
5525
5525
|
type: 'group';
|
|
@@ -5533,30 +5533,30 @@ type RouteEntry =
|
|
|
5533
5533
|
}
|
|
5534
5534
|
| {
|
|
5535
5535
|
type: 'rawPattern';
|
|
5536
|
-
rawPattern: RawPattern
|
|
5536
|
+
rawPattern: RawPattern<Pattern>;
|
|
5537
5537
|
};
|
|
5538
5538
|
type RouteBranch =
|
|
5539
5539
|
| {
|
|
5540
5540
|
type: 'command';
|
|
5541
5541
|
path: string[];
|
|
5542
|
-
command: Command
|
|
5542
|
+
command: Command<Pattern>;
|
|
5543
5543
|
}
|
|
5544
5544
|
| {
|
|
5545
5545
|
type: 'rawPattern';
|
|
5546
5546
|
path: string[];
|
|
5547
|
-
rawPattern: RawPattern
|
|
5547
|
+
rawPattern: RawPattern<Pattern>;
|
|
5548
5548
|
};
|
|
5549
5549
|
type RouteMatchResult =
|
|
5550
5550
|
| {
|
|
5551
5551
|
type: 'command';
|
|
5552
5552
|
path: string[];
|
|
5553
|
-
command: Command
|
|
5553
|
+
command: Command<Pattern>;
|
|
5554
5554
|
params: any;
|
|
5555
5555
|
}
|
|
5556
5556
|
| {
|
|
5557
5557
|
type: 'rawPattern';
|
|
5558
5558
|
path: string[];
|
|
5559
|
-
rawPattern: RawPattern
|
|
5559
|
+
rawPattern: RawPattern<Pattern>;
|
|
5560
5560
|
params: any;
|
|
5561
5561
|
};
|
|
5562
5562
|
interface Session {
|
|
@@ -5566,8 +5566,8 @@ interface Session {
|
|
|
5566
5566
|
declare class Router {
|
|
5567
5567
|
private readonly entries;
|
|
5568
5568
|
private readonly groups;
|
|
5569
|
-
command<P extends Pattern>(
|
|
5570
|
-
rawPattern<P extends Pattern>(
|
|
5569
|
+
command<P extends Pattern>(command: Command<P>): this;
|
|
5570
|
+
rawPattern<P extends Pattern>(rawPattern: RawPattern<P>): this;
|
|
5571
5571
|
group(name: string): Router;
|
|
5572
5572
|
filter(predicate: SessionPredicate): Router;
|
|
5573
5573
|
routes(): RouteEntry[];
|
|
@@ -5781,8 +5781,8 @@ export {
|
|
|
5781
5781
|
ContextUrlOptions,
|
|
5782
5782
|
Disposable,
|
|
5783
5783
|
EventMap,
|
|
5784
|
+
Executor,
|
|
5784
5785
|
Filter,
|
|
5785
|
-
Handler,
|
|
5786
5786
|
Injection,
|
|
5787
5787
|
LogHandler,
|
|
5788
5788
|
LogLevel,
|
package/dist/index.mjs
CHANGED
|
@@ -164,25 +164,17 @@ var Tokenizer = class {
|
|
|
164
164
|
var Router = class Router {
|
|
165
165
|
entries = [];
|
|
166
166
|
groups = /* @__PURE__ */ new Map();
|
|
167
|
-
command(
|
|
168
|
-
const command = {
|
|
169
|
-
name,
|
|
170
|
-
pattern,
|
|
171
|
-
handler
|
|
172
|
-
};
|
|
167
|
+
command(command) {
|
|
173
168
|
this.entries.push({
|
|
174
169
|
type: "command",
|
|
175
170
|
command
|
|
176
171
|
});
|
|
177
172
|
return this;
|
|
178
173
|
}
|
|
179
|
-
rawPattern(
|
|
180
|
-
if (Object.keys(pattern).length === 0) throw new Error("Raw pattern must have at least one parameter.");
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
pattern,
|
|
184
|
-
handler
|
|
185
|
-
};
|
|
174
|
+
rawPattern(rawPattern) {
|
|
175
|
+
if (Object.keys(rawPattern.pattern).length === 0) throw new Error("Raw pattern must have at least one parameter.");
|
|
176
|
+
const firstKey = Object.keys(rawPattern.pattern)[0];
|
|
177
|
+
if (rawPattern.pattern[firstKey].capturer.typeInstruction.type === "literal") throw new Error("The first parameter of a raw pattern cannot be a literal, as it would conflict with command patterns.");
|
|
186
178
|
this.entries.push({
|
|
187
179
|
type: "rawPattern",
|
|
188
180
|
rawPattern
|
|
@@ -226,10 +218,10 @@ var Router = class Router {
|
|
|
226
218
|
if (match === void 0) return false;
|
|
227
219
|
switch (match.type) {
|
|
228
220
|
case "command":
|
|
229
|
-
await match.command.
|
|
221
|
+
await match.command.execute(session, match.params);
|
|
230
222
|
break;
|
|
231
223
|
case "rawPattern":
|
|
232
|
-
await match.rawPattern.
|
|
224
|
+
await match.rawPattern.execute(session, match.params);
|
|
233
225
|
break;
|
|
234
226
|
}
|
|
235
227
|
return true;
|