discord-embed-router 0.1.0 → 0.2.2

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
@@ -1,3 +1,3 @@
1
1
  # discord-embed-router
2
2
 
3
- A url-based router for discord.js interaction messages
3
+ A url path based router for discord.js interaction messages
package/dist/index.d.mts CHANGED
@@ -1,19 +1,23 @@
1
1
  import { ParamData, Path, MatchFunction, MatchResult, TokenData } from 'path-to-regexp';
2
- import { Interaction, InteractionEditReplyOptions, ButtonInteraction, InteractionReplyOptions, ButtonBuilder } from 'discord.js';
2
+ import { Interaction, InteractionEditReplyOptions, ButtonInteraction, AnySelectMenuInteraction, InteractionReplyOptions, ButtonBuilder, APIButtonComponent, StringSelectMenuOptionBuilder, StringSelectMenuBuilder, StringSelectMenuComponentData, APIStringSelectComponent, RestOrArray, ChannelSelectMenuBuilder, ChannelSelectMenuComponentData, APIChannelSelectComponent, RoleSelectMenuBuilder, RoleSelectMenuComponentData, APIRoleSelectComponent, UserSelectMenuBuilder, UserSelectMenuComponentData, APIUserSelectComponent } from 'discord.js';
3
3
 
4
- type State<P extends ParamData> = MatchResult<P> & {
5
- searchParams: URLSearchParams;
4
+ type State<L, P extends ParamData = ParamData> = MatchResult<P> & {
5
+ embedRouter: EmbedRouter<L>;
6
+ locals?: L | undefined;
7
+ query: URLSearchParams;
6
8
  };
7
9
  type RouteResponse = InteractionEditReplyOptions;
8
- type RouteHandler<P extends ParamData> = (interaction: Interaction, state: State<P>) => RouteResponse;
9
- type CompiledRoute<P extends ParamData> = {
10
+ type RouteHandler<L, P extends ParamData = ParamData> = (interaction: Interaction, state: State<L, P>) => Promise<RouteResponse> | RouteResponse;
11
+ type Method = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
12
+ type CompiledRoute<L, P extends ParamData = ParamData> = {
13
+ method: Method;
10
14
  path: Path[];
11
15
  matchFunction: MatchFunction<P>;
12
- handler: RouteHandler<P>;
16
+ handler: RouteHandler<L, P>;
13
17
  };
14
- type ResolvedRoute<P extends ParamData> = {
15
- state: State<P>;
16
- handler: RouteHandler<P>;
18
+ type ResolvedRoute<L, P extends ParamData = ParamData> = {
19
+ state: State<L, P>;
20
+ handler: RouteHandler<L, P>;
17
21
  };
18
22
 
19
23
  type TakeIdentifier<T extends string, Name extends string = ""> = T extends `${infer Char}${infer Rest}` ? Char extends "/" | ":" | "*" | "{" | "}" ? [Name, T] : TakeIdentifier<Rest, `${Name}${Char}`> : [Name, T];
@@ -28,49 +32,97 @@ type ExtractParams<T extends string | TokenData, Depth extends string = ""> = T
28
32
  [K in Name]?: string;
29
33
  }) & ExtractParams<After, Depth> : ExtractParams<Rest, Depth> : Char extends "{" ? ExtractParams<Rest, `${Depth}1`> : Char extends "}" ? ExtractParams<Rest, Depth extends `1${infer Rest2}` ? Rest2 : Depth> : ExtractParams<Rest, Depth> : object : never;
30
34
 
31
- declare class EmbedRouter {
32
- private static usedIdentifiers;
33
- private static generateUniqueIdentifier;
34
- private idPrefix;
35
+ declare class EmbedRouter<L> {
36
+ #private;
35
37
  getIdPrefix(): string;
36
- private routes;
37
38
  /**
38
39
  *
40
+ * @param name the name to give the router. ensures buttons stay connected across restarts
39
41
  * @param idPrefix the prefix for RouteButtonBuilder customIds
40
42
  */
41
- constructor(idPrefix?: string);
43
+ constructor({ name, idPrefix, }?: {
44
+ name?: string | undefined;
45
+ idPrefix?: string | undefined;
46
+ });
42
47
  /**
43
48
  * Registers a path with the router
44
49
  *
45
50
  * @param routePath path to match with
46
51
  * @param handler function that generates the message when a path is matched
47
52
  */
48
- on<P extends Path = Path>(routePath: P | P[], handler: RouteHandler<ExtractParams<P>>): void;
53
+ get<P extends Path = Path>(routePath: P | P[], handler: RouteHandler<L, ExtractParams<P>>): void;
54
+ /**
55
+ * Registers a path with the router
56
+ *
57
+ * @param routePath path to match with
58
+ * @param handler function that generates the message when a path is matched
59
+ */
60
+ post<P extends Path = Path>(routePath: P | P[], handler: RouteHandler<L, ExtractParams<P>>): void;
61
+ /**
62
+ * Registers a path with the router
63
+ *
64
+ * @param routePath path to match with
65
+ * @param handler function that generates the message when a path is matched
66
+ */
67
+ put<P extends Path = Path>(routePath: P | P[], handler: RouteHandler<L, ExtractParams<P>>): void;
68
+ /**
69
+ * Registers a path with the router
70
+ *
71
+ * @param routePath path to match with
72
+ * @param handler function that generates the message when a path is matched
73
+ */
74
+ patch<P extends Path = Path>(routePath: P | P[], handler: RouteHandler<L, ExtractParams<P>>): void;
75
+ /**
76
+ * Registers a path with the router
77
+ *
78
+ * @param routePath path to match with
79
+ * @param handler function that generates the message when a path is matched
80
+ */
81
+ delete<P extends Path = Path>(routePath: P | P[], handler: RouteHandler<L, ExtractParams<P>>): void;
49
82
  /**
50
83
  * Adds a subrouter to the router
51
84
  *
52
85
  * @param routePath path of the router
53
86
  * @param embedRouter router to add at the path
54
87
  */
55
- use<P extends Path = Path>(routePath: P, embedRouter: EmbedRouter): void;
88
+ use<P extends Path = Path>(routePath: P, embedRouter: EmbedRouter<L>): void;
56
89
  /**
57
90
  * Must be attached to "interactionCreate" event for RouteButtonBuilder to work
58
91
  *
59
92
  * @param interaction interactions from "interactionCreate" (filter for ButtonInteractions)
60
93
  */
61
- listener(interaction: ButtonInteraction): Promise<void>;
94
+ listener(interaction: ButtonInteraction | AnySelectMenuInteraction, locals?: L | undefined): Promise<void>;
62
95
  /**
63
96
  * Connect or update an interaction message to a path
64
97
  *
65
98
  * @param interaction interaction to connect to
66
99
  * @param path path to route the interaction to
100
+ * @param method method to send to route
67
101
  * @param flags optional discord flags to send with message (only allowed on first reply)
68
102
  */
69
- dispatch<P extends Path = Path>(interaction: Interaction, path: P, flags?: InteractionReplyOptions["flags"]): Promise<void>;
70
- private resolve;
103
+ dispatch<P extends Path = Path>({ interaction, method, path, flags, locals, }: {
104
+ interaction: Interaction;
105
+ method?: Method;
106
+ path: P;
107
+ flags?: InteractionReplyOptions["flags"] | undefined;
108
+ locals?: L | undefined;
109
+ }): Promise<void>;
71
110
  }
72
111
 
73
- declare class RouteButtonBuilder extends ButtonBuilder {
112
+ type SetOptions<P extends Path> = {
113
+ method?: Method;
114
+ path: P;
115
+ query?: ConstructorParameters<typeof URLSearchParams>[0] | undefined;
116
+ };
117
+
118
+ declare class RouteButtonBuilder<L> extends ButtonBuilder {
119
+ #private;
120
+ /**
121
+ *
122
+ * @param embedRouter the router you want to route with
123
+ * @param data the data to construct a component out of
124
+ */
125
+ constructor(embedRouter: EmbedRouter<L>, data?: Omit<Partial<APIButtonComponent>, "custom_id" | "url"> | undefined);
74
126
  /**
75
127
  * Not supported for RouteButtonBuilder
76
128
  *
@@ -79,19 +131,202 @@ declare class RouteButtonBuilder extends ButtonBuilder {
79
131
  */
80
132
  setURL(): this;
81
133
  /**
82
- * Not supported for RouteButtonBuilder (setTo uses customId)
134
+ * Not supported for RouteButtonBuilder (use setTo)
135
+ *
136
+ * @remarks
137
+ * @param
138
+ */
139
+ setCustomId(): this;
140
+ /**
141
+ * Sets the path to route to when clicked
142
+ *
143
+ * @param path the path to route to
144
+ * @param query any query parameters you want to add
145
+ * @param method method to send to route
146
+ */
147
+ setTo<P extends Path>({ method, path, query }: SetOptions<P>): this;
148
+ /**
149
+ * Sets the path to route to when clicked
150
+ *
151
+ * @param path the path to route to
152
+ */
153
+ setTo<P extends Path>(path: P): this;
154
+ }
155
+
156
+ declare class RouteStringSelectMenuOptionBuilder extends StringSelectMenuOptionBuilder {
157
+ /**
158
+ * Not supported for RouteStringSelectMenuOptionBuilder (use setTo)
159
+ *
160
+ * @remarks
161
+ * @param
162
+ */
163
+ setValue(): this;
164
+ /**
165
+ * Sets the path to route to when clicked
166
+ *
167
+ * @param path the path to route to
168
+ * @param query any query parameters you want to add
169
+ * @param method method to send to route
170
+ */
171
+ setTo<P extends Path>({ method, path, query }: SetOptions<P>): this;
172
+ /**
173
+ * Sets the path to route to when clicked
174
+ *
175
+ * @param path the path to route to
176
+ */
177
+ setTo<P extends Path>(path: P): this;
178
+ }
179
+
180
+ declare class RouteStringSelectMenuBuilder<L> extends StringSelectMenuBuilder {
181
+ #private;
182
+ /**
183
+ *
184
+ * @param embedRouter the router you want to route with
185
+ * @param path the path to redirect to, :to or *to in path will be replaced with the selected user's id
186
+ * @param query any query parameters you want to add, :to will be replaced with the selected user's id
187
+ * @param data the data to construct a component out of
188
+ */
189
+ constructor(embedRouter: EmbedRouter<L>, data?: Partial<StringSelectMenuComponentData | APIStringSelectComponent> | undefined);
190
+ /**
191
+ * Not supported for RouteStringSelectMenuBuilder (customId set from embedRouter)
83
192
  *
84
193
  * @remarks
85
194
  * @param
86
195
  */
87
196
  setCustomId(): this;
197
+ /**
198
+ * Not supported for RouteStringSelectMenuBuilder (use addTos)
199
+ *
200
+ * @remarks
201
+ * @param
202
+ */
203
+ addOptions(): this;
204
+ /**
205
+ * Not supported for RouteStringSelectMenuBuilder (use setTos)
206
+ *
207
+ * @remarks
208
+ * @param
209
+ */
210
+ setOptions(): this;
211
+ /**
212
+ * Adds route select menu options to builder
213
+ *
214
+ * @param tos the list of route select menu options
215
+ */
216
+ addTos(...tos: RestOrArray<RouteStringSelectMenuOptionBuilder | ConstructorParameters<typeof RouteStringSelectMenuOptionBuilder>[0]>): this;
217
+ /**
218
+ * Sets route select menu options to builder
219
+ *
220
+ * @param tos the list of route select menu options
221
+ */
222
+ setTos(...tos: RestOrArray<RouteStringSelectMenuOptionBuilder | ConstructorParameters<typeof RouteStringSelectMenuOptionBuilder>[0]>): this;
223
+ /**
224
+ * Sets the pattern to redirect to (Required)
225
+ *
226
+ * @param path the path to redirect to, :to or *to in path will be replaced with the selected user's id
227
+ * @param query any query parameters you want to add, :to will be replaced with the selected user's id
228
+ * @param method method to send to route
229
+ */
230
+ setPattern<P extends Path>({ method, path, query, }: SetOptions<P>): this;
231
+ /**
232
+ * Sets the path to route to when clicked
233
+ *
234
+ * @param path the path to route to
235
+ */
236
+ setPattern<P extends Path>(path: P): this;
237
+ }
238
+
239
+ declare class RouteChannelSelectMenuBuilder<L> extends ChannelSelectMenuBuilder {
240
+ #private;
241
+ /**
242
+ *
243
+ * @param embedRouter the router you want to route with
244
+ * @param data the data to construct a component out of
245
+ */
246
+ constructor(embedRouter: EmbedRouter<L>, data?: Partial<ChannelSelectMenuComponentData | APIChannelSelectComponent> | undefined);
247
+ /**
248
+ * Not supported for RouteChannelSelectMenuBuilder (customId set from embedRouter)
249
+ *
250
+ * @remarks
251
+ * @param
252
+ */
253
+ setCustomId(): this;
254
+ /**
255
+ * Sets the pattern to redirect to (Required)
256
+ *
257
+ * @param path the path to redirect to, :channelId in path will be replaced with the selected user's id
258
+ * @param query any query parameters you want to add, :channelId will be replaced with the selected user's id
259
+ * @param method method to send to route
260
+ */
261
+ setPattern<P extends Path>({ method, path, query, }: SetOptions<P>): this;
262
+ /**
263
+ * Sets the path to route to when clicked
264
+ *
265
+ * @param path the path to route to
266
+ */
267
+ setPattern<P extends Path>(path: P): this;
268
+ }
269
+
270
+ declare class RouteRoleSelectMenuBuilder<L> extends RoleSelectMenuBuilder {
271
+ #private;
272
+ /**
273
+ *
274
+ * @param embedRouter the router you want to route with
275
+ * @param data the data to construct a component out of
276
+ */
277
+ constructor(embedRouter: EmbedRouter<L>, data?: Partial<RoleSelectMenuComponentData | APIRoleSelectComponent> | undefined);
278
+ /**
279
+ * Not supported for RouteRoleSelectMenuBuilder (customId set from embedRouter)
280
+ *
281
+ * @remarks
282
+ * @param
283
+ */
284
+ setCustomId(): this;
285
+ /**
286
+ * Sets the pattern to redirect to (Required)
287
+ *
288
+ * @param path the path to redirect to, :roleId in path will be replaced with the selected user's id
289
+ * @param query any query parameters you want to add, :roleId will be replaced with the selected user's id
290
+ * @param method method to send to route
291
+ */
292
+ setPattern<P extends Path>({ method, path, query, }: SetOptions<P>): this;
293
+ /**
294
+ * Sets the path to route to when clicked
295
+ *
296
+ * @param path the path to route to
297
+ */
298
+ setPattern<P extends Path>(path: P): this;
299
+ }
300
+
301
+ declare class RouteUserSelectMenuBuilder<L> extends UserSelectMenuBuilder {
302
+ #private;
303
+ /**
304
+ *
305
+ * @param embedRouter the router you want to route with
306
+ * @param data the data to construct a component out of
307
+ */
308
+ constructor(embedRouter: EmbedRouter<L>, data?: Partial<UserSelectMenuComponentData | APIUserSelectComponent> | undefined);
309
+ /**
310
+ * Not supported for RouteUserSelectMenuBuilder (customId set from embedRouter)
311
+ *
312
+ * @remarks
313
+ * @param
314
+ */
315
+ setCustomId(): this;
316
+ /**
317
+ * Sets the pattern to redirect to (Required)
318
+ *
319
+ * @param path the path to redirect to, :userId in path will be replaced with the selected user's id
320
+ * @param query any query parameters you want to add, :userId will be replaced with the selected user's id
321
+ * @param method method to send to route
322
+ */
323
+ setPattern<P extends Path>({ method, path, query, }: SetOptions<P>): this;
88
324
  /**
89
325
  * Sets the path to route to when clicked
90
326
  *
91
327
  * @param path the path to route to
92
- * @param idPrefix the prefix to add before the custom_id
93
328
  */
94
- setTo<P extends Path>(embedRouter: EmbedRouter, path: P): void;
329
+ setPattern<P extends Path>(path: P): this;
95
330
  }
96
331
 
97
- export { type CompiledRoute, EmbedRouter, type ResolvedRoute, RouteButtonBuilder, type RouteHandler, type RouteResponse, type State };
332
+ export { type CompiledRoute, EmbedRouter, type Method, type ResolvedRoute, RouteButtonBuilder, RouteChannelSelectMenuBuilder, type RouteHandler, type RouteResponse, RouteRoleSelectMenuBuilder, RouteStringSelectMenuBuilder, RouteStringSelectMenuOptionBuilder, RouteUserSelectMenuBuilder, type State };