1 | import { Container } from './container.js';
|
2 | import { ContainerResolver } from './resolver.js';
|
3 | import type { ModuleHandler, ModuleCallable, Constructor } from './types.js';
|
4 | /**
|
5 | * The moduleCaller works around a very specific pattern we use with
|
6 | * AdonisJS, ie to construct classes and call methods using the
|
7 | * container.
|
8 | *
|
9 | * For example: Controllers of AdonisJS allows defining a controller
|
10 | * as follows
|
11 | *
|
12 | * ```ts
|
13 | * route.get('/', [HomeController, 'index'])
|
14 | * ```
|
15 | *
|
16 | * Behind the scenes, we have to run following operations in order to call the
|
17 | * handle method on the defined middleware.
|
18 | *
|
19 | * - Create an instance of the controller class using the container.
|
20 | * - Call the method using the container. Hence having the ability to use
|
21 | * DI
|
22 | */
|
23 | export declare function moduleCaller(target: Constructor<any>, method: string): {
|
24 | /**
|
25 | * Converts the class reference to a callable function. Invoking this method
|
26 | * internally creates a new instance of the class using the container and
|
27 | * invokes the method using the container.
|
28 | *
|
29 | * You can create a callable function using the container instance as shown below
|
30 | *
|
31 | * ```ts
|
32 | * const fn = moduleCaller(HomeController, 'handle')
|
33 | * .toCallable(container)
|
34 | *
|
35 | * // Call the function and pass context to it
|
36 | * await fn(ctx)
|
37 | * ```
|
38 | *
|
39 | * Another option is to not pass the container at the time of creating
|
40 | * the callable function, but instead pass a resolver instance at
|
41 | * the time of calling the function
|
42 | *
|
43 | * ```ts
|
44 | * const fn = moduleCaller(HomeController, 'handle')
|
45 | * .toCallable()
|
46 | *
|
47 | * // Call the function and pass context to it
|
48 | * const resolver = container.createResolver()
|
49 | * await fn(resolver, ctx)
|
50 | * ```
|
51 | */
|
52 | toCallable<T extends Container<any> | ContainerResolver<any> | undefined = undefined, Args extends any[] = any[]>(container?: T): ModuleCallable<T, Args>;
|
53 | /**
|
54 | * Converts the class reference to an object with handle method. Invoking this
|
55 | * method internally creates a new instance of the class using the container
|
56 | * and invokes the method using the container.
|
57 | *
|
58 | * You can create a handle method object using the container instance as shown below
|
59 | *
|
60 | * ```ts
|
61 | * const handler = moduleCaller(HomeController, 'handle')
|
62 | * .toHandleMethod(container)
|
63 | *
|
64 | * // Call the function and pass context to it
|
65 | * await handler.handle(ctx)
|
66 | * ```
|
67 | *
|
68 | * Another option is to not pass the container at the time of creating
|
69 | * the handle method object, but instead pass a resolver instance at
|
70 | * the time of calling the function
|
71 | *
|
72 | * ```ts
|
73 | * const handler = moduleCaller(HomeController, 'handle')
|
74 | * .toHandleMethod()
|
75 | *
|
76 | * // Call the function and pass context to it
|
77 | * const resolver = container.createResolver()
|
78 | * await handler.handle(resolver, ctx)
|
79 | * ```
|
80 | */
|
81 | toHandleMethod<T extends Container<any> | ContainerResolver<any> | undefined = undefined, Args extends any[] = any[]>(container?: T): ModuleHandler<T, Args>;
|
82 | };
|