-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
447 lines (396 loc) · 13.4 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
interface HaSeulLocals {
[key: string]: any;
[key: number]: any;
}
interface HaSeulRequest {
err?: Error,
originalContent: string,
originalUrl: string,
params?: object,
locals: HaSeulLocals,
}
interface HaSeulSearchResults {
prefix: string,
route: string | null,
content: string,
}
type HaSeulCallbackFunction<Message> = ({
message,
userInput,
route,
err,
content,
prefix,
done,
next,
req
}: {
userInput: string,
route: string | null,
message?: Message,
err: Error | undefined,
content: string,
prefix: string,
done: (err?: Error) => void,
next: (err?: Error) => void,
req: HaSeulRequest,
}) => void
class HaSeul<Message = any> {
private routes: {
type: string,
url: string | null,
middlewares: (HaSeul<Message> | HaSeulCallbackFunction<Message>)[]
}[];
private settings: {
prefix?: string[],
'json spaces'?: string | number,
'case sensitive routing'?: boolean,
[key: string]: any
};
constructor() {
this.routes = [];
this.settings = {
prefix: [],
'json spaces': 2,
'case sensitive routing': false
};
}
/**
* Returns the content of a message, if the prefix (and optional route) matches the user's message.
* @param content The message content that the user has provided
* @param route The route name that needs to be removed from the resulting content
*/
getContentIfMatched(content: string, route: string | null): HaSeulSearchResults | null {
const contentToCheck = this.get('case sensitive routing') ? content.trim() : content.toLowerCase().trim();
const prefixes = this.get('prefix');
let foundPrefix = '';
let contentWithoutPrefix = content.trim();
let contentWithoutPrefixToCheck = contentToCheck.trim();
if (prefixes.length > 0 && prefixes.every(prefix => prefix !== '')) {
let found = false;
for (const prefix of this.get('prefix')) {
const prefixToCheck = this.get('case sensitive routing') ? prefix : prefix.toLowerCase();
// If the content doesn't start with the prefix, ignore.
if (contentToCheck.startsWith(prefixToCheck)) {
found = true;
foundPrefix = prefix;
contentWithoutPrefix = content.trim().substring(prefixToCheck.length).trim();
contentWithoutPrefixToCheck = contentToCheck.substring(prefixToCheck.length).trim();
break;
}
}
if (!found) return null;
}
// If a route isn't specified, just send the content
if (route === null) return {
prefix: foundPrefix,
route: null,
content: contentWithoutPrefix,
};
// If the content doesn't start with the route, ignore.
const routeToCheck = this.get('case sensitive routing') ? route.trim() : route.toLowerCase().trim();
if (!contentWithoutPrefixToCheck.startsWith(routeToCheck)) return null;
return {
prefix: foundPrefix,
route: route,
content: contentWithoutPrefix.substring(routeToCheck.length).trim(),
};
}
/**
* Set the prefixes the router will respond to.
* @param option `prefix`
* @param value The prefix that the router should react to.
*/
set(option: 'prefix', value: string[]): HaSeul<Message>
/**
* Set the prefix of the router.
* @deprecated Please use an array of strings instead.
* @param option `prefix`
* @param value The prefixes that the router should react to.
*/
set(option: 'prefix', value: string): HaSeul<Message>
/**
* Set the case sensitivity of routing.
* @param option `case sensitive routing`
* @param value Whether or not the router should be case sensitive or not.
*/
set(option: 'case sensitive routing', value: boolean): HaSeul<Message>
/**
* Set the white space that is used when converting an object to JSON.
* @param option `json spaces`
* @param value A number for the number of spaces to indent JSON objects by, or a string to use as the indenting character.
* @beta
*/
set(option: 'json spaces', value: string | number): HaSeul<Message>
/**
* Sets the value of `option` to `value`.
* You can store anything, but some options can configure the properties of the router.
* @param option The name of the option
* @param value The value that will be assigned to this option
*/
set(option: string, value: any): HaSeul<Message> {
// TODO: Remove this line soon
if (option === 'prefix' && typeof value === 'string') value = [value];
this.settings[option] = value;
return this;
}
/**
* Obtain the current prefix of the router.
* @param option `prefix`
*/
get(option: 'prefix'): string[]
/**
* Obtain whether or not the router is case sensitive or not.
* @param option `case sensitive routing`
*/
get(option: 'case sensitive routing'): boolean
/**
* Obtain the delimiter used to generate JSON objects.
* @param option `json spaces`
*/
get(option: 'json spaces'): string | number
/**
* Retrieve the value of a setting
* @param option The name of the option
*/
get(option: string) {
return this.settings[option]
}
/**
* Create an error handler which matches all commands
*
* You can set up the handler by placing it at the bottom to catch all errors which are created by routers.
* ```typescript
* const router = new HaSeul<Message>();
*
* router
* .command('help', ({ next }) => {
* next(new Error('An error occured while processing the HELP command!'))
* })
* .error(({ err, message }) => {
* console.log(err)
* message.channel.createMessage('An error occurred: ' + err)
* })
* ```
*
* @param middleware Middleware that will be executed in order whenever an error is caught by the router
*/
error(...middleware: (HaSeul<Message> | HaSeulCallbackFunction<Message>)[]): HaSeul<Message>;
/**
* Create an error handler which matches a command
* @param url The command that must be matched in order for this route to be executed
* @param middleware Middleware that will be executed in order whenever an error is caught by the router
*/
error(url: string, ...middleware: (HaSeul<Message> | HaSeulCallbackFunction<Message>)[]): HaSeul<Message>;
error(x: any, ...y: (HaSeul<Message> | HaSeulCallbackFunction<Message>)[]): HaSeul<Message> {
return this.createRoute('error', x, ...y);
}
/**
* Create a handler which matches all commands
* @param middleware Middleware that will be executed in order whenever the route is executed
*/
command(...middleware: (HaSeul<Message> | HaSeulCallbackFunction<Message>)[]): HaSeul<Message>;
/**
* Create a handler which matches a command
* @param url The command that must be matched in order for this route to be executed
* @param middleware Middleware that will be executed in order whenever the route is executed
*/
command(url: string, ...middleware: (HaSeul<Message> | HaSeulCallbackFunction<Message>)[]): HaSeul<Message>;
command(x: any, ...y: (HaSeul<Message> | HaSeulCallbackFunction<Message>)[]): HaSeul<Message> {
return this.createRoute('command', x, ...y);
}
/**
* Create a handler which matches all commands
* @param routeType The type of route to create.
* @param middleware Middleware that will be executed in order whenever the route is executed
* @private
*/
createRoute(routeType: string, ...middleware: (HaSeul<Message> | HaSeulCallbackFunction<Message>)[]): HaSeul<Message>;
/**
* Create a handler which matches a command
* @param routeType The type of route to create.
* @param url The command that must be matched in order for this route to be executed
* @param middleware Middleware that will be executed in order whenever the route is executed
* @private
*/
createRoute(routeType: string, url: string, ...middleware: (HaSeul<Message> | HaSeulCallbackFunction<Message>)[]): HaSeul<Message>;
createRoute(routeType: string, x: any, ...y: (HaSeul<Message> | HaSeulCallbackFunction<Message>)[]): HaSeul<Message> {
const middlewares: (HaSeul<Message> | HaSeulCallbackFunction<Message>)[] = [];
let url = null;
if (typeof x === 'function') {
middlewares.push(x);
} else if (x instanceof HaSeul) {
middlewares.push(x);
} else if (typeof x === 'string') {
url = x;
}
middlewares.push(...y);
this.routes.push({
type: routeType,
url,
middlewares: middlewares
.map((middleware) => {
if (middleware instanceof HaSeul) {
middleware.set('prefix', [])
}
return middleware
})
});
return this;
}
/**
* Pass a message into the router
* @param userInput The content of a message from a user
* @param message The object from your client API
*/
route(userInput: string, message?: Message): Promise<any> {
return this.executeRouter({
userInput,
message
})
}
/**
* Pass a message into the router, and define a starting point for where the router should look at.
* @param userInput The content of a message from a user
* @param message The object from your client API that you would like to pass around to routers and middleware
* @param existingReq The request object
* @param routeNumber The route number - Refers to the route to look at in the routes array.
* @param middlewareNumber The middleware number - Refers to the middleware array found in each route.
* @private
*/
private executeRouter({
userInput,
message,
existingReq,
routeNumber = 0,
middlewareNumber = 0,
}: {
userInput: string,
message?: Message,
existingReq?: HaSeulRequest,
routeNumber?: number,
middlewareNumber?: number,
}): Promise<void> {
return new Promise((resolve) => {
let req: HaSeulRequest;
if (existingReq) {
req = existingReq;
} else {
req = {
originalContent: userInput,
originalUrl: userInput,
locals: {},
}
}
const route = this.routes[routeNumber];
// If a route was not found, we've ran out of routes.
if (!route) resolve();
const nextRoute = () => {
resolve(this.executeRouter({
userInput,
message,
existingReq: req,
routeNumber: routeNumber + 1,
}));
}
const nextMiddleware = () => {
resolve(this.executeRouter({
userInput,
message,
existingReq: req,
routeNumber: routeNumber,
middlewareNumber: middlewareNumber + 1,
}));
}
// If this route is not an error handler, go to the next route
if (req.err && route.type !== 'error') {
nextRoute()
return;
}
// If this route is an error handler, but there's no error, go to the next route
if (!req.err && route.type === 'error') {
nextRoute()
return;
}
const match = this.getContentIfMatched(userInput, route.url)
// If the route is matched, try to execute the middleware
if (match) {
const middleware = route.middlewares[middlewareNumber];
if (typeof middleware === 'function') {
try {
middleware({
userInput,
message,
req,
err: req.err,
route: route.url,
content: match.content,
prefix: match.prefix,
done: (err?: Error): void => {
if (err) {
req.err = err;
// If this is an error router, go to deeper middleware
if (route.type === 'error') {
nextMiddleware();
} else {
// Otherwise, go to the next route in search for an error router.
nextRoute();
}
} else {
resolve()
}
},
next: (err?: Error): void => {
if (err) {
req.err = err;
// If this is an error router, go to deeper middleware
if (route.type === 'error') {
nextMiddleware();
} else {
// Otherwise, go to the next route in search for an error router.
nextRoute();
}
} else {
// If this is an error router, skip this route
if (route.type === 'error') {
nextRoute();
} else {
// Otherwise, go to deeper middleware
nextMiddleware()
}
}
}
})
} catch (err) {
req.err = err;
// If this is an error router, go to deeper middleware
if (route.type === 'error') {
nextMiddleware();
} else {
// Otherwise, go to the next route in search for an error router.
nextRoute();
}
}
} else if (middleware instanceof HaSeul) {
// Do the middleware.
middleware.executeRouter({
userInput: match.content,
message,
existingReq: req,
})
.then(() => {
// After routing, go to the next middleware
nextMiddleware();
})
} else {
// There is no more middleware. Go to the next router.
nextRoute();
}
} else {
// The route didn't match. Go to the next router.
nextRoute();
}
});
}
}
export default HaSeul;