Skip to content

Commit 954b673

Browse files
committed
feat: remove skip array rules in favor of filterFn in routes scanner
The filter fn is more flexible can be controlled directly via tuyau
1 parent 2c6bc0d commit 954b673

File tree

2 files changed

+15
-19
lines changed

2 files changed

+15
-19
lines changed

src/code_scanners/routes_scanner/main.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
type RoutesListItem,
2323
type ScannedController,
2424
type RoutesScannerRules,
25+
type RoutesScannerFilterFn,
2526
} from '../../types/code_scanners.ts'
2627

2728
/**
@@ -41,6 +42,8 @@ import {
4142
* const scannedRoutes = scanner.getScannedRoutes()
4243
*/
4344
export class RoutesScanner {
45+
#filter: RoutesScannerFilterFn = () => false
46+
4447
/**
4548
* The root of the application from where we will resolve
4649
* paths.
@@ -103,7 +106,6 @@ export class RoutesScanner {
103106
* The rules to apply when scanning routes
104107
*/
105108
rules: RoutesScannerRules = {
106-
skip: [],
107109
request: {},
108110
response: {},
109111
}
@@ -119,15 +121,9 @@ export class RoutesScanner {
119121
this.pathsResolver = new PathsResolver(appRoot)
120122

121123
rulesCollection.forEach((rules) => {
122-
this.rules.skip = this.rules.skip.concat(rules.skip)
123124
Object.assign(this.rules.request, rules.request)
124125
Object.assign(this.rules.response, rules.response)
125126
})
126-
127-
/**
128-
* Removing duplicates
129-
*/
130-
this.rules.skip = Array.from(new Set([...this.rules.skip]))
131127
}
132128

133129
/**
@@ -303,7 +299,7 @@ export class RoutesScanner {
303299
* name for the route. There could be chances where one controller+method
304300
* combination is bound to multiple routes.
305301
*/
306-
const routeName =
302+
route.name =
307303
route.name ??
308304
new StringBuilder(controller.name)
309305
.removeSuffix('Controller')
@@ -315,7 +311,7 @@ export class RoutesScanner {
315311
* Skip route when its name is within the array of
316312
* skip routes
317313
*/
318-
if (this.rules.skip.includes(routeName)) {
314+
if (this.#filter(route)) {
319315
return
320316
}
321317

@@ -324,13 +320,13 @@ export class RoutesScanner {
324320
* and request if these values are not provided via rules.
325321
*/
326322
const scannedRoute: ScannedRoute = {
327-
name: routeName,
323+
name: route.name,
328324
domain: route.domain,
329325
methods: route.methods,
330326
pattern: route.pattern,
331327
tokens: route.tokens,
332-
request: this.rules.request[routeName],
333-
response: this.rules.response[routeName],
328+
request: this.rules.request[route.name],
329+
response: this.rules.response[route.name],
334330
controller,
335331
}
336332

@@ -365,7 +361,7 @@ export class RoutesScanner {
365361
* Skip route when it has a name and also part of
366362
* skip array
367363
*/
368-
if (route.name && this.rules.skip.includes(route.name)) {
364+
if (route.name && this.#filter(route)) {
369365
debug('route skipped route: %O, rules: %O', route, this.rules)
370366
return
371367
}

src/types/code_scanners.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,12 @@ export type RoutesListItem = {
197197
tokens: { val: string; old: string; type: 0 | 1 | 2 | 3; end: string }[]
198198
}
199199

200+
/**
201+
* The filter fn to exclude routes from being processed. Return true
202+
* to filter out (aka exclude) the route
203+
*/
204+
export type RoutesScannerFilterFn = (route: RoutesListItem) => boolean
205+
200206
/**
201207
* Configuration rules accepted by the routes scanner to customize
202208
* the scanning behavior and override type inference.
@@ -219,12 +225,6 @@ export type RoutesListItem = {
219225
* }
220226
*/
221227
export type RoutesScannerRules = {
222-
/**
223-
* An array of route names or controller+method paths to skip from processing.
224-
* Useful for excluding routes that don't need type generation.
225-
*/
226-
skip: string[]
227-
228228
/**
229229
* Define custom response types for specific routes by their name
230230
* or controller+method path. Overrides automatic type inference.

0 commit comments

Comments
 (0)