Skip to content

Commit

Permalink
feat: throw if HMR is enabled and controller is directly imported
Browse files Browse the repository at this point in the history
  • Loading branch information
RomainLanz committed Jun 20, 2024
1 parent 06a28ee commit 70e6695
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/exceptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ export const E_CANNOT_LOOKUP_ROUTE = createError<[routeIdentifier: string]>(
500
)

export const E_DIRECT_CONTROLLER_IMPORT = createError<[controllerName: string]>(
'Cannot import controller "%s" directly with HMR enabled. Make sure to lazy import it',
'E_DIRECT_CONTROLLER_IMPORT',
500
)

export const E_HTTP_EXCEPTION = class HttpException extends Exception {
body: any
static code = 'E_HTTP_EXCEPTION'
Expand Down
6 changes: 6 additions & 0 deletions src/router/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { moduleCaller, moduleImporter } from '@adonisjs/fold'

import { execute } from './executor.js'
import { dropSlash } from '../helpers.js'
import { E_DIRECT_CONTROLLER_IMPORT } from '../exceptions.js'
import type { Constructor, LazyImport, OneOrMore } from '../types/base.js'

import type {
Expand Down Expand Up @@ -162,6 +163,11 @@ export class Route<Controller extends Constructor<any> = any> extends Macroable
* The first item of the tuple is a class constructor
*/
if (is.class(handler[0])) {
// @ts-expect-error - Dynamic property added by hot-hook
if (import.meta.hot) {
throw new E_DIRECT_CONTROLLER_IMPORT([handler[0].name])
}

return {
reference: handler,
...moduleCaller(handler[0], (handler[1] || 'handle') as string).toHandleMethod(),
Expand Down

0 comments on commit 70e6695

Please sign in to comment.