Skip to content

Commit

Permalink
refactor: assign names to wrapping methods created to invoke modules
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Dec 18, 2023
1 parent ffaaa48 commit ccf2852
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/module_caller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type { ModuleHandler, ModuleCallable, Constructor } from './types.js'

/**
* The moduleCaller works around a very specific pattern we use with
* AdonisJS, ie to constructor classes and call methods using the
* AdonisJS, ie to construct classes and call methods using the
* container.
*
* For example: Controllers of AdonisJS allows defining a controller
Expand Down Expand Up @@ -116,13 +116,15 @@ export function moduleCaller(target: Constructor<any>, method: string) {
>(container?: T): ModuleHandler<T, Args> {
if (container) {
return {
name: `${target.name}.${method}`,
async handle(...args: Args) {
return container.call(await container.make(target), method, args)
},
} as ModuleHandler<T, Args>
}

return {
name: `${target.name}.${method}`,
async handle(resolver: ContainerResolver<any> | Container<any>, ...args: Args) {
return resolver.call(await resolver.make(target), method, args)
},
Expand Down
2 changes: 2 additions & 0 deletions src/module_importer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ export function moduleImporter(

if (container) {
return {
name: importFn.name,
async handle(...args: Args) {
defaultExport = defaultExport || (await importDefault(importFn))
return container.call(await container.make(defaultExport), method, args)
Expand All @@ -144,6 +145,7 @@ export function moduleImporter(
}

return {
name: importFn.name,
async handle(resolver: ContainerResolver<any> | Container<any>, ...args: Args) {
defaultExport = defaultExport || (await importDefault(importFn))
return resolver.call(await resolver.make(defaultExport), method, args)
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,10 @@ export type ModuleCallable<T, Args extends any[]> = T extends undefined
*/
export type ModuleHandler<T, Args extends any[]> = T extends undefined
? {
name?: string
handle(resolver: ContainerResolver<any> | Container<any>, ...args: Args): Promise<any>
}
: {
name?: string
handle(...args: Args): Promise<any>
}

0 comments on commit ccf2852

Please sign in to comment.