AdonisJS API
    Preparing search index...

    Function moduleImporter

    • The moduleImporter module works around a very specific pattern we use with AdonisJS, ie to lazy load modules by wrapping import calls inside a callback.

      For example: Middleware of AdonisJS allows registering middleware as an array of import calls.

      Behind the scenes, we have to run following operations in order to call the handle method on the defined middleware:

      • Lazily call the registered callbacks to import the middleware.
      • Check if the module has a default export.
      • Create an instance of the default export class using the container.
      • Call the handle method on the middleware class using the container.

      Parameters

      • importFn: () => Promise<{ default: Constructor<any> }>

        Function that returns a promise with the module's default export

      • method: string

        The method name to call on the imported class instance

      Returns {
          toCallable<
              T extends
                  ContainerResolver<any>
                  | Container<any>
                  | undefined = undefined,
              Args extends any[] = any[],
          >(
              container?: T,
          ): ModuleCallable<T, Args>;
          toHandleMethod<
              T extends
                  ContainerResolver<any>
                  | Container<any>
                  | undefined = undefined,
              Args extends any[] = any[],
          >(
              container?: T,
          ): ModuleHandler<T, Args>;
      }

      • toCallable: function
        • Converts the module import function to a callable function. Invoking this method run internally import the module, create a new instance of the default export class using the container and invokes the method using the container.

          You can create a callable function using the container instance as shown below

          const fn = moduleImporter(() => import('#middleware/auth_middleware'), 'handle')
          .toCallable(container)

          // Call the function and pass context to it
          await fn(ctx)

          Another option is to not pass the container at the time of creating the callable function, but instead pass a resolver instance at the time of calling the function

          const fn = moduleImporter(() => import('#middleware/auth_middleware'), 'handle')
          .toCallable()

          // Call the function and pass context to it
          const resolver = container.createResolver()
          await fn(resolver, ctx)

          Type Parameters

          Parameters

          • Optionalcontainer: T

          Returns ModuleCallable<T, Args>

      • toHandleMethod: function
        • Converts the module import function to an object with handle method. Invoking the handle method run internally imports the module, create a new instance of the default export class using the container and invokes the method using the container.

          You can create a handle method object using the container instance as shown below

          const handler = moduleImporter(() => import('#middleware/auth_middleware'), 'handle')
          .toHandleMethod(container)

          // Call the function and pass context to it
          await handler.handle(ctx)

          Another option is to not pass the container at the time of creating the handle method object, but instead pass a resolver instance at the time of calling the function

          const handler = moduleImporter(() => import('#middleware/auth_middleware'), 'handle')
          .toHandleMethod()

          // Call the function and pass context to it
          const resolver = container.createResolver()
          await handler.handle(resolver, ctx)

          Type Parameters

          Parameters

          • Optionalcontainer: T

          Returns ModuleHandler<T, Args>

      defineMiddleware([
      () => import('#middleware/silent_auth')
      ])
      defineMiddleware({
      auth: () => import('#middleware/auth')
      })
      const callable = moduleImporter(() => import('#middleware/auth'), 'handle')
      .toCallable(container)

      await callable(ctx)