1 | import { Container } from './container.js';
|
2 | import { ContainerResolver } from './resolver.js';
|
3 | import type { ModuleHandler, ModuleCallable, Constructor } from './types.js';
|
4 | /**
|
5 | * The moduleImporter module works around a very specific pattern we use
|
6 | * with AdonisJS, ie to lazy load modules by wrapping import calls inside
|
7 | * a callback.
|
8 | *
|
9 | * For example: Middleware of AdonisJS allows registering middleware as an
|
10 | * array of import calls.
|
11 | *
|
12 | * ```ts
|
13 | * defineMiddleware([
|
14 | * () => import('#middleware/silent_auth')
|
15 | * ])
|
16 | *
|
17 | * defineMiddleware({
|
18 | * auth: () => import('#middleware/auth')
|
19 | * })
|
20 | * ```
|
21 | *
|
22 | * Behind the scenes, we have to run following operations in order to call the
|
23 | * handle method on the defined middleware.
|
24 | *
|
25 | * - Lazily call the registered callbacks to import the middleware.
|
26 | * - Check if the module has a default export.
|
27 | * - Create an instance of the default export class using the container.
|
28 | * - Call the `handle` method on the middleware class using the container.
|
29 | */
|
30 | export declare function moduleImporter(importFn: () => Promise<{
|
31 | default: Constructor<any>;
|
32 | }>, method: string): {
|
33 | /**
|
34 | * Converts the module import function to a callable function. Invoking this
|
35 | * method run internally import the module, create a new instance of the
|
36 | * default export class using the container and invokes the method using
|
37 | * the container.
|
38 | *
|
39 | * You can create a callable function using the container instance as shown below
|
40 | *
|
41 | * ```ts
|
42 | * const fn = moduleImporter(() => import('#middleware/auth_middleware'), 'handle')
|
43 | * .toCallable(container)
|
44 | *
|
45 | * // Call the function and pass context to it
|
46 | * await fn(ctx)
|
47 | * ```
|
48 | *
|
49 | * Another option is to not pass the container at the time of creating
|
50 | * the callable function, but instead pass a resolver instance at
|
51 | * the time of calling the function
|
52 | *
|
53 | * ```ts
|
54 | * const fn = moduleImporter(() => import('#middleware/auth_middleware'), 'handle')
|
55 | * .toCallable()
|
56 | *
|
57 | * // Call the function and pass context to it
|
58 | * const resolver = container.createResolver()
|
59 | * await fn(resolver, ctx)
|
60 | * ```
|
61 | */
|
62 | toCallable<T extends Container<any> | ContainerResolver<any> | undefined = undefined, Args extends any[] = any[]>(container?: T): ModuleCallable<T, Args>;
|
63 | /**
|
64 | * Converts the module import function to an object with handle method. Invoking the
|
65 | * handle method run internally imports the module, create a new instance of
|
66 | * the default export class using the container and invokes the method using
|
67 | * the container.
|
68 | *
|
69 | * You can create a handle method object using the container instance as shown below
|
70 | *
|
71 | * ```ts
|
72 | * const handler = moduleImporter(() => import('#middleware/auth_middleware'), 'handle')
|
73 | * .toHandleMethod(container)
|
74 | *
|
75 | * // Call the function and pass context to it
|
76 | * await handler.handle(ctx)
|
77 | * ```
|
78 | *
|
79 | * Another option is to not pass the container at the time of creating
|
80 | * the handle method object, but instead pass a resolver instance at
|
81 | * the time of calling the function
|
82 | *
|
83 | * ```ts
|
84 | * const handler = moduleImporter(() => import('#middleware/auth_middleware'), 'handle')
|
85 | * .toHandleMethod()
|
86 | *
|
87 | * // Call the function and pass context to it
|
88 | * const resolver = container.createResolver()
|
89 | * await handler.handle(resolver, ctx)
|
90 | * ```
|
91 | */
|
92 | toHandleMethod<T extends Container<any> | ContainerResolver<any> | undefined = undefined, Args extends any[] = any[]>(container?: T): ModuleHandler<T, Args>;
|
93 | };
|