Utility functions for parsing binding references from various formats including magic strings, class references, and lazy import references. These utilities are particularly useful for parsing route controller bindings, event listener bindings, and other string-based references in AdonisJS applications.
Parses binding references from multiple formats including magic strings, class references, and lazy import functions.
/**
* Parses binding references from magic strings, class references, and lazy import references
* @param binding - String reference or array with class/lazy import and optional method
* @returns Promise resolving to parsed module path and method name
*/
function parseBindingReference(
binding: string | [LazyImport<Constructor<any>> | Constructor<any>, any?]
): Promise<{ moduleNameOrPath: string; method: string }>;Usage Examples:
import { parseBindingReference } from "@adonisjs/fold";
// Magic strings
const result1 = await parseBindingReference('#controllers/home_controller');
// Returns: { moduleNameOrPath: '#controllers/home_controller', method: 'handle' }
const result2 = await parseBindingReference('#controllers/home_controller.index');
// Returns: { moduleNameOrPath: '#controllers/home_controller', method: 'index' }
const result3 = await parseBindingReference('#controllers/home.controller.index');
// Returns: { moduleNameOrPath: '#controllers/home.controller', method: 'index' }
// Class reference
class HomeController {
handle() { /* implementation */ }
index() { /* implementation */ }
}
const result4 = await parseBindingReference([HomeController]);
// Returns: { moduleNameOrPath: 'HomeController', method: 'handle' }
const result5 = await parseBindingReference([HomeController, 'index']);
// Returns: { moduleNameOrPath: 'HomeController', method: 'index' }
// Lazy import reference
const HomeControllerImport = () => import('#controllers/home_controller');
const result6 = await parseBindingReference([HomeControllerImport]);
// Returns: { moduleNameOrPath: '#controllers/home_controller', method: 'handle' }
const result7 = await parseBindingReference([HomeControllerImport, 'index']);
// Returns: { moduleNameOrPath: '#controllers/home_controller', method: 'index' }The function supports magic strings with the following formats:
#controllers/home_controller → defaults to handle method#controllers/home_controller.index → uses index method#controllers/home.controller.index → handles dotted module namesThe function accepts an array with:
handle)For lazy imports, the function:
moduleNameOrPathhandletype LazyImport<T> = () => Promise<{ default: T }>;
type Constructor<T = {}> = new (...args: any[]) => T;