1 | import { Container } from './container.js';
|
2 | import { ContainerResolver } from './resolver.js';
|
3 | import type { ModuleHandler, ModuleCallable } from './types.js';
|
4 | /**
|
5 | * The moduleExpression module works around a very specific pattern we use
|
6 | * with AdonisJS, ie to bind modules as string.
|
7 | *
|
8 | * For example: With the router of AdonisJS, we can bind a controller to a route
|
9 | * as follows.
|
10 | *
|
11 | * ```ts
|
12 | * Route.get('users', '#controllers/users_controller.index')
|
13 | * ```
|
14 | *
|
15 | * Behind the scenes, we have to run following operations in order to call a
|
16 | * method on the users_controller class.
|
17 | *
|
18 | * - Dynamic import `#controllers/users_controller` module
|
19 | * - Check if the module has a default export.
|
20 | * - Create an instance of the default export class using the container.
|
21 | * - Call the `index` method on the controller class using the container.
|
22 | *
|
23 | * Router is just one example, we do this with event listeners, redis pub/sub
|
24 | * and so on.
|
25 | *
|
26 | * So, instead of writing all this parsing logic, we encapsulate it inside the
|
27 | * "moduleExpression" module.
|
28 | */
|
29 | export declare function moduleExpression(expression: string, parentURL: URL | string): {
|
30 | /**
|
31 | * Parses a module expression to extract the module import path
|
32 | * and the method to call on the default exported class.
|
33 | *
|
34 | * ```ts
|
35 | * moduleExpression('#controllers/users_controller').parse()
|
36 | * // ['#controllers/users_controller', 'handle']
|
37 | * ```
|
38 | *
|
39 | * With method
|
40 | * ```ts
|
41 | * moduleExpression('#controllers/users_controller.index').parse()
|
42 | * // ['#controllers/users_controller', 'index']
|
43 | * ```
|
44 | */
|
45 | parse(): [string, string];
|
46 | /**
|
47 | * Converts the module expression to a callable function. Invoking this
|
48 | * method run internally import the module, create a new instance of the
|
49 | * default export class using the container and invokes the method using
|
50 | * the container.
|
51 | *
|
52 | * You can create a callable function using the container instance as shown below
|
53 | *
|
54 | * ```ts
|
55 | * const fn = moduleExpression('#controllers/users_controller.index')
|
56 | * .toCallable(container)
|
57 | *
|
58 | * // Call the function and pass context to it
|
59 | * await fn(ctx)
|
60 | * ```
|
61 | *
|
62 | * Another option is to not pass the container at the time of creating
|
63 | * the callable function, but instead pass a resolver instance at
|
64 | * the time of calling the function
|
65 | *
|
66 | * ```ts
|
67 | * const fn = moduleExpression('#controllers/users_controller.index')
|
68 | * .toCallable()
|
69 | *
|
70 | * // Call the function and pass context to it
|
71 | * const resolver = container.createResolver()
|
72 | * await fn(resolver, ctx)
|
73 | * ```
|
74 | */
|
75 | toCallable<T extends Container<any> | ContainerResolver<any> | undefined = undefined, Args extends any[] = any[]>(container?: T): ModuleCallable<T, Args>;
|
76 | /**
|
77 | * Converts the module expression to an object with handle method. Invoking the
|
78 | * handle method run internally imports the module, create a new instance of
|
79 | * the default export class using the container and invokes the method using
|
80 | * the container.
|
81 | *
|
82 | * You can create a handle method object using the container instance as shown below
|
83 | *
|
84 | * ```ts
|
85 | * const handler = moduleExpression('#controllers/users_controller.index')
|
86 | * .toHandleMethod(container)
|
87 | *
|
88 | * // Call the function and pass context to it
|
89 | * await handler.handle(ctx)
|
90 | * ```
|
91 | *
|
92 | * Another option is to not pass the container at the time of creating
|
93 | * the handle method object, but instead pass a resolver instance at
|
94 | * the time of calling the function
|
95 | *
|
96 | * ```ts
|
97 | * const handler = moduleExpression('#controllers/users_controller.index')
|
98 | * .toHandleMethod()
|
99 | *
|
100 | * // Call the function and pass context to it
|
101 | * const resolver = container.createResolver()
|
102 | * await handler.handle(resolver, ctx)
|
103 | * ```
|
104 | */
|
105 | toHandleMethod<T extends Container<any> | ContainerResolver<any> | undefined = undefined, Args extends any[] = any[]>(container?: T): ModuleHandler<T, Args>;
|
106 | };
|