@fraqjs/fraq 0.8.2 → 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 CHANGED
@@ -27,15 +27,12 @@ const ctx = Context.fromUrl('<替换成你的 Milky 协议端地址>', {
27
27
  // accessToken: '<如果你的 Milky 协议端配置了 token,请在这里添加>',
28
28
  });
29
29
 
30
- ctx.router.command({
31
- name: 'echo',
32
- pattern: {
33
- content: param.greedy(),
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/router.d.ts
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;
@@ -5784,6 +5807,7 @@ export {
5784
5807
  ApiEndpoints,
5785
5808
  Capturer,
5786
5809
  Command,
5810
+ CommandBuilder,
5787
5811
  Context,
5788
5812
  ContextOptions,
5789
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",
@@ -1230,4 +1263,4 @@ let param;
1230
1263
  _param.segment = segment;
1231
1264
  })(param || (param = {}));
1232
1265
  //#endregion
1233
- 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 };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@fraqjs/fraq",
3
3
  "type": "module",
4
- "version": "0.8.2",
4
+ "version": "0.9.0",
5
5
  "description": "Milky Bot framework",
6
6
  "files": [
7
7
  "dist"