Skip to content

Commit bbf4467

Browse files
committed
refactor: cleanup how named middleware are created and used
1 parent 16a6d65 commit bbf4467

23 files changed

+600
-949
lines changed

index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,3 @@ export { HttpException } from './src/exceptions/http_exception.js'
2222
export { AbortException } from './src/exceptions/abort_exception.js'
2323
export { RouteNotFoundException } from './src/exceptions/route_not_found.js'
2424
export { CannotLookupRouteException } from './src/exceptions/cannot_lookup_route.js'
25-
export { defineMiddleware, defineNamedMiddleware } from './src/define_middleware.js'

src/define_middleware.ts

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,55 @@
77
* file that was distributed with this source code.
88
*/
99

10-
import type { LazyImport } from './types/base.js'
11-
import type { MiddlewareAsClass } from './types/middleware.js'
10+
import { moduleImporter } from '@adonisjs/fold'
11+
import type { LazyImport, UnWrapLazyImport } from './types/base.js'
12+
import type {
13+
GetMiddlewareArgs,
14+
MiddlewareAsClass,
15+
ParsedGlobalMiddleware,
16+
} from './types/middleware.js'
1217

1318
/**
14-
* Define an array of midldeware to be used either by the server
15-
* or the router
19+
* Converts a middleware name and its lazy import to a factory function. The function
20+
* can than later be used to reference the middleware with different arguments
21+
* every time.
1622
*/
17-
export function defineMiddleware(list: LazyImport<MiddlewareAsClass>[]) {
18-
return list
23+
function middlewareReferenceBuilder(
24+
name: string | number | symbol,
25+
middleware: LazyImport<MiddlewareAsClass>
26+
) {
27+
const handler = moduleImporter(middleware, 'handle').toHandleMethod()
28+
return function (...args: any[]) {
29+
return {
30+
name,
31+
args: args[0],
32+
...handler,
33+
}
34+
}
1935
}
2036

2137
/**
22-
* Define an collection of named middleware. The name can be later be
23-
* referenced on the routes
38+
* Define an collection of named middleware. The collection gets converted
39+
* into a collection of factory functions. Calling the function returns
40+
* a reference to the executable middleware.
2441
*/
2542
export function defineNamedMiddleware<
26-
NamedMiddleware extends Record<string, LazyImport<MiddlewareAsClass>>
43+
NamedMiddleware extends Record<string | number | symbol, LazyImport<MiddlewareAsClass>>
2744
>(collection: NamedMiddleware) {
28-
return collection
45+
return Object.keys(collection).reduce(
46+
(result, key: keyof NamedMiddleware) => {
47+
result[key] = middlewareReferenceBuilder(key, collection[key])
48+
return result
49+
},
50+
{} as {
51+
[K in keyof NamedMiddleware]: <
52+
Args extends GetMiddlewareArgs<UnWrapLazyImport<NamedMiddleware[K]>>
53+
>(
54+
...args: Args
55+
) => {
56+
name: K
57+
args: Args[0]
58+
} & ParsedGlobalMiddleware
59+
}
60+
)
2961
}

src/middleware/store.ts

Lines changed: 0 additions & 94 deletions
This file was deleted.

src/router/brisk.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ import { Macroable } from '@poppinss/macroable'
1111
import type { Application } from '@adonisjs/application'
1212

1313
import { Route } from './route.js'
14-
import type { LazyImport } from '../types/base.js'
15-
import type { MiddlewareStore } from '../middleware/store.js'
16-
import type { MiddlewareAsClass } from '../types/middleware.js'
14+
import type { ParsedGlobalMiddleware } from '../types/middleware.js'
1715
import type { MakeUrlOptions, RouteFn, RouteMatchers } from '../types/route.js'
1816

1917
/**
@@ -25,9 +23,7 @@ import type { MakeUrlOptions, RouteFn, RouteMatchers } from '../types/route.js'
2523
*
2624
* Brisk routes are always registered under the `GET` HTTP method.
2725
*/
28-
export class BriskRoute<
29-
NamedMiddleware extends Record<string, LazyImport<MiddlewareAsClass>> = any
30-
> extends Macroable {
26+
export class BriskRoute extends Macroable {
3127
/**
3228
* Route pattern
3329
*/
@@ -44,26 +40,26 @@ export class BriskRoute<
4440
#app: Application<any, any>
4541

4642
/**
47-
* Middleware store to resolve middleware
43+
* Middleware registered on the router
4844
*/
49-
#middlewareStore: MiddlewareStore<NamedMiddleware>
45+
#routerMiddleware: ParsedGlobalMiddleware[]
5046

5147
/**
5248
* Reference to route instance. Set after `setHandler` is called
5349
*/
54-
route: null | Route<NamedMiddleware> = null
50+
route: null | Route = null
5551

5652
constructor(
5753
app: Application<any, any>,
58-
middlewareStore: MiddlewareStore<NamedMiddleware>,
54+
routerMiddleware: ParsedGlobalMiddleware[],
5955
options: {
6056
pattern: string
6157
globalMatchers: RouteMatchers
6258
}
6359
) {
6460
super()
6561
this.#app = app
66-
this.#middlewareStore = middlewareStore
62+
this.#routerMiddleware = routerMiddleware
6763
this.#pattern = options.pattern
6864
this.#globalMatchers = options.globalMatchers
6965
}
@@ -72,7 +68,7 @@ export class BriskRoute<
7268
* Set handler for the brisk route
7369
*/
7470
setHandler(handler: RouteFn): Route {
75-
this.route = new Route(this.#app, this.#middlewareStore, {
71+
this.route = new Route(this.#app, this.#routerMiddleware, {
7672
pattern: this.#pattern,
7773
globalMatchers: this.#globalMatchers,
7874
methods: ['GET', 'HEAD'],

src/router/group.ts

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@
88
*/
99

1010
import { Macroable } from '@poppinss/macroable'
11-
import type { MiddlewareStore } from '../middleware/store.js'
12-
import type { LazyImport, UnWrapLazyImport } from '../types/base.js'
1311
import type { RouteMatcher, StoreRouteMiddleware } from '../types/route.js'
14-
import type { GetMiddlewareArgs, MiddlewareAsClass, MiddlewareFn } from '../types/middleware.js'
12+
import type { MiddlewareFn, ParsedNamedMiddleware } from '../types/middleware.js'
1513

1614
import { Route } from './route.js'
1715
import { BriskRoute } from './brisk.js'
@@ -21,25 +19,14 @@ import { RouteResource } from './resource.js'
2119
* Group class exposes the API to take action on a group of routes.
2220
* The group routes must be pre-defined using the constructor.
2321
*/
24-
export class RouteGroup<
25-
NamedMiddleware extends Record<string, LazyImport<MiddlewareAsClass>> = any
26-
> extends Macroable {
22+
export class RouteGroup extends Macroable {
2723
/**
2824
* Array of middleware registered on the group.
2925
*/
3026
#middleware: StoreRouteMiddleware[] = []
3127

32-
/**
33-
* Middleware store to resolve middleware
34-
*/
35-
#middlewareStore: MiddlewareStore<NamedMiddleware>
36-
37-
constructor(
38-
public routes: (Route | RouteGroup | RouteResource | BriskRoute)[],
39-
middlewareStore: MiddlewareStore<NamedMiddleware>
40-
) {
28+
constructor(public routes: (Route | RouteGroup | RouteResource | BriskRoute)[]) {
4129
super()
42-
this.#middlewareStore = middlewareStore
4330
}
4431

4532
/**
@@ -226,12 +213,7 @@ export class RouteGroup<
226213
* }).middleware(['auth'])
227214
* ```
228215
*/
229-
middleware<Name extends keyof NamedMiddleware>(
230-
middleware: Name,
231-
...args: GetMiddlewareArgs<UnWrapLazyImport<NamedMiddleware[Name]>>
232-
): this
233-
middleware(middleware: MiddlewareFn): this
234-
middleware(middleware: keyof NamedMiddleware | MiddlewareFn, args?: any): this {
216+
middleware(middleware: MiddlewareFn | ParsedNamedMiddleware): this {
235217
/**
236218
* Register middleware with children. We share the group middleware
237219
* array by reference, therefore have to register it only for the
@@ -241,21 +223,7 @@ export class RouteGroup<
241223
this.routes.forEach((route) => this.#shareMiddlewareStackWithRoutes(route))
242224
}
243225

244-
/**
245-
* Resolve string based middleware
246-
*/
247-
if (typeof middleware === 'string') {
248-
this.#middleware.push(this.#middlewareStore.get(middleware, args))
249-
return this
250-
}
251-
252-
/**
253-
* Register function
254-
*/
255-
if (typeof middleware === 'function') {
256-
this.#middleware.push(middleware)
257-
}
258-
226+
this.#middleware.push(middleware)
259227
return this
260228
}
261229
}

0 commit comments

Comments
 (0)