( binding: string | [LazyImport<Constructor<any>> | Constructor<any>, any?] )
| 51 | * ``` |
| 52 | */ |
| 53 | export async function parseBindingReference( |
| 54 | binding: string | [LazyImport<Constructor<any>> | Constructor<any>, any?] |
| 55 | ): Promise<{ moduleNameOrPath: string; method: string }> { |
| 56 | /** |
| 57 | * The binding reference is a magic string. It might not have method |
| 58 | * name attached to it. Therefore we split the string and attempt |
| 59 | * to find the method or use the default method name "handle". |
| 60 | */ |
| 61 | if (typeof binding === 'string') { |
| 62 | const tokens = binding.split('.') |
| 63 | if (tokens.length === 1) { |
| 64 | return { moduleNameOrPath: binding, method: 'handle' } |
| 65 | } |
| 66 | return { method: tokens.pop()!, moduleNameOrPath: tokens.join('.') } |
| 67 | } |
| 68 | |
| 69 | const [bindingReference, method] = binding |
| 70 | |
| 71 | /** |
| 72 | * Parsing the binding reference for dynamic imports and using its |
| 73 | * import value. |
| 74 | */ |
| 75 | const imports = [...(await parseImports(bindingReference.toString()))] |
| 76 | const importedModule = imports.find( |
| 77 | ($import) => $import.isDynamicImport && $import.moduleSpecifier.value |
| 78 | ) |
| 79 | if (importedModule) { |
| 80 | return { |
| 81 | moduleNameOrPath: importedModule.moduleSpecifier.value!, |
| 82 | method: method || 'handle', |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Otherwise using the name of the binding reference. |
| 88 | */ |
| 89 | return { |
| 90 | moduleNameOrPath: bindingReference.name, |
| 91 | method: method || 'handle', |
| 92 | } |
| 93 | } |
no test coverage detected
searching dependent graphs…