Skip to content

Commit 95dca61

Browse files
doc: improvements
1 parent 233e4f1 commit 95dca61

File tree

3 files changed

+124
-30
lines changed

3 files changed

+124
-30
lines changed

src/index.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import type {
3131
SelectMenuMiddleware,
3232
CommandMiddleware,
3333
CommandbuilderType,
34+
GenericMiddleware,
3435
} from "./interactionRouter/internal.js";
3536

3637
import type { MessageMentionOptions } from "discord.js";
@@ -135,7 +136,8 @@ class Client extends HttpInteractionServer {
135136
*
136137
* Receives raw {@link APIInteraction} payload and response object.
137138
*
138-
* @param fns - Middleware functions to execute.
139+
* @param fns - {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function | Async} functions. See {@link GenericMiddleware} for callback parameters.
140+
*
139141
*/
140142

141143
middleware(...fns: GeneralMiddleware[]) {
@@ -145,7 +147,7 @@ class Client extends HttpInteractionServer {
145147
/**
146148
* Adds global middleware for unknown interactions.
147149
*
148-
* @param fns - Functions executed when no handler matches an interaction.
150+
* @param fns - Async Functions executed when no handler matches an interaction. See {@link UnknownMiddleware} for callback parameters.
149151
*/
150152
unknown(...fns: UnknownMiddleware[]) {
151153
this.router._unknownInteraction.push(...fns);
@@ -172,17 +174,19 @@ class Client extends HttpInteractionServer {
172174
/**
173175
* Registers a command with its associated middleware.
174176
*
177+
* @param commandbuilder - Function returning a {@link SlashCommandBuilder}.
178+
* @param fns {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function | Async} functions. See {@link GenericMiddleware} for callback parameters.
179+
* @returns {@link AutoCompleteKeyBuilder} for autocomplete options.
175180
* @example
181+
*
176182
* ```ts
177183
* router.command(
178-
* (builder) => builder.setName("Ping!").setDescription("Returns Pong!"),
179-
* async (interaction) => interaction.reply({ content: "pong!" })
184+
* (builder) =>
185+
* builder.setName("Ping!").setDescription("Returns Pong!"),
186+
* pongHandle
180187
* );
181188
* ```
182189
*
183-
* @param commandbuilder - Function returning a {@link SlashCommandBuilder}.
184-
* @param fns - Middleware functions for the command.
185-
* @returns An {@link AutoCompleteKeyBuilder} for autocomplete options.
186190
*/
187191
command(commandbuilder: CommandbuilderType, ...fns: CommandMiddleware[]) {
188192
this.tryAsync(fns);
@@ -199,6 +203,7 @@ class Client extends HttpInteractionServer {
199203
/**
200204
* Registers a button interaction with its associated middleware.
201205
*
206+
* @param fns {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function | Async} functions. See {@link GenericMiddleware} for callback parameters.
202207
* @example
203208
* ```ts
204209
* router.button("custom_button_id", buttonMiddleware);
@@ -212,6 +217,7 @@ class Client extends HttpInteractionServer {
212217
/**
213218
* Registers a modal interaction with its associated middleware.
214219
*
220+
* @param fns {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function | Async} functions. See {@link GenericMiddleware} for callback parameters.
215221
* @example
216222
* ```ts
217223
* router.modal("custom_modal_id", modalMiddleware);
@@ -225,6 +231,7 @@ class Client extends HttpInteractionServer {
225231
/**
226232
* Registers a role select interaction with its associated middleware.
227233
*
234+
* @param fns {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function | Async} functions. See {@link GenericMiddleware} for callback parameters.
228235
* @example
229236
* ```ts
230237
* router.roleSelect("roleSelectName", roleSelectMiddleware);
@@ -238,6 +245,7 @@ class Client extends HttpInteractionServer {
238245
/**
239246
* Registers a user select interaction with its associated middleware.
240247
*
248+
* @param fns {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function | Async} functions. See {@link GenericMiddleware} for callback parameters.
241249
* @example
242250
* ```ts
243251
* router.userSelect("userSelectName", userSelectMiddleware);
@@ -250,6 +258,7 @@ class Client extends HttpInteractionServer {
250258
/**
251259
* Registers a string select interaction with its associated middleware.
252260
*
261+
* @param fns {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function | Async} functions. See {@link GenericMiddleware} for callback parameters.
253262
* @example
254263
* ```ts
255264
* router.stringSelect("stringSelectName", stringSelectMiddleware);
@@ -263,6 +272,7 @@ class Client extends HttpInteractionServer {
263272
/**
264273
* Registers a channel select interaction with its associated middleware.
265274
*
275+
* @param fns {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function | Async} functions. See {@link GenericMiddleware} for callback parameters.
266276
* @example
267277
* ```ts
268278
* router.channelSelect("channelSelectName", channelSelectMiddleware);
@@ -276,6 +286,7 @@ class Client extends HttpInteractionServer {
276286
/**
277287
* Registers a mentionable select interaction with its associated middleware.
278288
*
289+
* @param fns {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function | Async} functions. See {@link GenericMiddleware} for callback parameters.
279290
* @example
280291
* ```ts
281292
* router.mentionableSelect("mentionableSelectName", mentionableSelectMiddleware);
@@ -288,7 +299,8 @@ class Client extends HttpInteractionServer {
288299

289300
/**
290301
* Registers an autocomplete interaction with its associated middleware.
291-
*
302+
*
303+
* @param fns {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function | Async} functions. See {@link GenericMiddleware} for callback parameters.
292304
* @example
293305
* ```ts
294306
* const githubQuery = router.command(
@@ -326,6 +338,7 @@ class Client extends HttpInteractionServer {
326338
/**
327339
* Registers a user context menu interaction with its associated middleware.
328340
*
341+
* @param fns {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function | Async} functions. See {@link GenericMiddleware} for callback parameters.
329342
* @example
330343
* ```ts
331344
* router.userContextMenu("userContextMenuId", userContextMenuMiddleware);
@@ -339,6 +352,7 @@ class Client extends HttpInteractionServer {
339352
/**
340353
* Registers a message context menu interaction with its associated middleware.
341354
*
355+
* @param fns {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function | Async} functions. See {@link GenericMiddleware} for callback parameters.
342356
* @example
343357
* ```ts
344358
* router.messageContextMenu("messageContextMenu", messageContextMenuMiddleware);

src/interactionRouter/index.ts

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ import { SlashCommandBuilder } from "@discordjs/builders";
2424
*
2525
* @example
2626
* ```ts
27-
* import { InteractionRouter } from 'discord.https/router';
27+
* // utility/ping.js
28+
*
29+
* import { InteractionRouter } from 'discord.https/router';
2830
* const router = new InteractionRouter();
2931
* export deafult router.command(
3032
* (builder) => builder.setName("Ping!").setDescription("Returns Pong!"),
@@ -88,7 +90,7 @@ class InteractionRouter {
8890
*
8991
* This does **not** affect other routers or collectors.
9092
*
91-
* @param fns Async middleware functions.
93+
* @param fns Async middleware functions. See {@link GenericMiddleware} for callback parameters
9294
*/
9395

9496
middleware(...fns: GeneralMiddleware[]) {
@@ -108,7 +110,7 @@ class InteractionRouter {
108110
* ```
109111
*
110112
* @param commandbuilder - Function returning a {@link SlashCommandBuilder}.
111-
* @param fns - Middleware functions for the command.
113+
* @param fns {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function | Async} functions. See {@link GenericMiddleware} for callback parameters.
112114
* @returns An {@link AutoCompleteKeyBuilder} for autocomplete options.
113115
*/
114116
command(commandbuilder: CommandbuilderType, ...fns: CommandMiddleware[]) {
@@ -126,6 +128,7 @@ class InteractionRouter {
126128
/**
127129
* Registers a button interaction with its associated middleware.
128130
*
131+
* @param fns {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function | Async} functions. See {@link GenericMiddleware} for callback parameters.
129132
* @example
130133
* ```ts
131134
* router.button("custom_button_id", buttonMiddleware);
@@ -139,6 +142,7 @@ class InteractionRouter {
139142
/**
140143
* Registers a modal interaction with its associated middleware.
141144
*
145+
* @param fns {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function | Async} functions. See {@link GenericMiddleware} for callback parameters.
142146
* @example
143147
* ```ts
144148
* router.modal("custom_modal_id", modalMiddleware);
@@ -152,6 +156,7 @@ class InteractionRouter {
152156
/**
153157
* Registers a role select interaction with its associated middleware.
154158
*
159+
* @param fns {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function | Async} functions. See {@link GenericMiddleware} for callback parameters.
155160
* @example
156161
* ```ts
157162
* router.roleSelect("roleSelectName", roleSelectMiddleware);
@@ -165,6 +170,7 @@ class InteractionRouter {
165170
/**
166171
* Registers a user select interaction with its associated middleware.
167172
*
173+
* @param fns {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function | Async} functions. See {@link GenericMiddleware} for callback parameters.
168174
* @example
169175
* ```ts
170176
* router.userSelect("userSelectName", userSelectMiddleware);
@@ -177,6 +183,7 @@ class InteractionRouter {
177183
/**
178184
* Registers a string select interaction with its associated middleware.
179185
*
186+
* @param fns {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function | Async} functions. See {@link GenericMiddleware} for callback parameters.
180187
* @example
181188
* ```ts
182189
* router.stringSelect("stringSelectName", stringSelectMiddleware);
@@ -190,6 +197,7 @@ class InteractionRouter {
190197
/**
191198
* Registers a channel select interaction with its associated middleware.
192199
*
200+
* @param fns {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function | Async} functions. See {@link GenericMiddleware} for callback parameters.
193201
* @example
194202
* ```ts
195203
* router.channelSelect("channelSelectName", channelSelectMiddleware);
@@ -203,6 +211,7 @@ class InteractionRouter {
203211
/**
204212
* Registers a mentionable select interaction with its associated middleware.
205213
*
214+
* @param fns {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function | Async} functions. See {@link GenericMiddleware} for callback parameters.
206215
* @example
207216
* ```ts
208217
* router.mentionableSelect("mentionableSelectName", mentionableSelectMiddleware);
@@ -216,18 +225,19 @@ class InteractionRouter {
216225
/**
217226
* Registers an autocomplete interaction with its associated middleware.
218227
*
228+
* @param fns {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function | Async} functions. See {@link GenericMiddleware} for callback parameters.
219229
* @example
220230
* ```ts
221231
* const githubQuery = router.command(
222232
* (builder) =>
223233
* builder
224-
* .setName("weather") // The command name
225-
* .setDescription("QuQuery weather information!") // The command description
234+
* .setName("weather")
235+
* .setDescription("Query weather information!")
226236
* .addStringOption(option =>
227237
* option
228-
* .setName("city") // Option name
229-
* .setDescription("City to get the weather for") // Option description
230-
* .setAutocomplete(true) // Enable autocomplete for this option
238+
* .setName("city")
239+
* .setDescription("City to get the weather for")
240+
* .setAutocomplete(true)
231241
* ),
232242
* (interaction) => handler
233243
* );
@@ -249,6 +259,7 @@ class InteractionRouter {
249259
/**
250260
* Registers a user context menu interaction with its associated middleware.
251261
*
262+
* @param fns {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function | Async} functions. See {@link GenericMiddleware} for callback parameters.
252263
* @example
253264
* ```ts
254265
* router.userContextMenu("userContextMenuId", userContextMenuMiddleware);
@@ -262,6 +273,7 @@ class InteractionRouter {
262273
/**
263274
* Registers a message context menu interaction with its associated middleware.
264275
*
276+
* @param fns {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function | Async} functions. See {@link GenericMiddleware} for callback parameters.
265277
* @example
266278
* ```ts
267279
* router.messageContextMenu("messageContextMenu", messageContextMenuMiddleware);
@@ -296,19 +308,25 @@ class InteractionRouter {
296308
/**
297309
* Collector for InteractionRouter.
298310
*
299-
* @example
311+
*
312+
* @example
313+
*
314+
* Example: Organizing multiple interaction routes with a collector.
315+
*
300316
* ```ts
301-
* import { InteractionRouter, InteractionRouterCollector } from 'discord.https/router';
317+
* import { InteractionRouterCollector } from "discord.https/router";
302318
*
303-
* const router = new InteractionRouter()
304-
* router.command(
305-
* (builder) => builder.setName("Ping!").setDescription("Returns Pong!")
306-
* (interaction) => interaction.reply({
307-
* content: "pong!"
308-
* })
309-
* )
310-
* // Register routes
311-
* InteractionRouterCollector.register(router);
319+
* // Recommend using PascalCase and ending with the 'Route' suffix
320+
* // for variable naming convention
321+
*
322+
* import PingRoute from "./ping.js";
323+
* import GithubRoute from "./github.js";
324+
*
325+
*
326+
* export default new InteractionRouterCollector().register(
327+
* PingRoute,
328+
* GithubRoute
329+
* );
312330
* ```
313331
*/
314332

src/interactionRouter/internal.ts

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,37 @@ export interface Context {
6060
| APIContextMenuInteraction;
6161
}
6262

63+
/**
64+
* A middleware function executed during an HTTP interaction lifecycle.
65+
*
66+
* @typeParam T - The specific interaction type handled by the middleware.
67+
* Defaults to {@link Context.resolvedInteraction | Context["resolvedInteraction"]}.
68+
*/
6369
export type GenericMiddleware<T = Context["resolvedInteraction"]> = (
70+
/**
71+
* The resolved interaction for this request.
72+
*/
6473
interaction: T,
74+
75+
/**
76+
* A read-only {@link REST} client instance.
77+
* Use this to perform Discord REST API calls safely.
78+
*/
6579
client: Readonly<REST>,
80+
81+
/**
82+
* A function you can call to immediately stop further
83+
* middleware execution and end the request.
84+
*/
6685
flush: () => never,
67-
// `res` will be removed in the near future, as the structure is complete.
86+
87+
/**
88+
* The underlying {@link HttpAdapterSererResponse} object.
89+
*
90+
* This will be removed in the near future, as the structure is complete.
91+
* This will only be available for unknown middleware
92+
* (see {@link UnknownMiddleware}).
93+
*/
6894
res: HttpAdapterSererResponse
6995
) => Promise<void>;
7096

@@ -114,12 +140,35 @@ export type ContextMenuMiddleware =
114140
| MenuContextMenuMiddleware;
115141

116142
// export type UnknownMiddleware = GenericMiddleware<DiscordHttpsAPIInteraction>;
143+
144+
/**
145+
* A middleware type for interactions that the library
146+
* does not explicitly cover.
147+
*
148+
* @typeParam T - The Discord HTTPS API interaction type.
149+
* Defaults to {@link DiscordHttpsAPIInteraction}.
150+
*/
117151
export type UnknownMiddleware<T = DiscordHttpsAPIInteraction> = (
152+
/**
153+
* The raw Discord HTTPS API interaction.
154+
*/
118155
interaction: T,
156+
/**
157+
* A read-only {@link REST} client instance.
158+
*/
119159
client: Readonly<REST>,
160+
/**
161+
* Ends the middleware chain immediately.
162+
* Calling `flush()` stops further middleware and never returns.
163+
*/
120164
flush: () => never,
121-
// `res` should be provided to unknown middlewares only, as `UnknownMiddleware` is meant to be used
122-
// only when the library doesn't cover that specific interaction.
165+
/**
166+
* The underlying {@link HttpAdapterSererResponse} object.
167+
*
168+
* `res` should be provided to unknown middlewares only,
169+
* as {@link UnknownMiddleware} is meant to be used when the
170+
* library does not yet cover a specific interaction.
171+
*/
123172
res: HttpAdapterSererResponse
124173
) => Promise<void>;
125174

@@ -169,6 +218,19 @@ interface UnknownRoute {
169218

170219
type ResolvedRoute = KnownRoute | UnknownRoute;
171220

221+
/**
222+
*
223+
* @internal
224+
*
225+
* Manages registration and routing of Discord interaction middlewares.
226+
*
227+
* The `InteractionRouterManager` stores middleware stacks for
228+
* different Discord interaction types (commands, buttons, selects,
229+
* context menus, etc.) and provides a registry for both known and
230+
* unknown routes.
231+
*
232+
*/
233+
172234
class InteractionRouterManager {
173235
middlewares: GenericMiddleware<DiscordHttpsInteraction>[] = [];
174236
_unknownInteraction: GenericMiddleware<DiscordHttpsAPIInteraction>[] = [];

0 commit comments

Comments
 (0)