adminjs-nestjs icon indicating copy to clipboard operation
adminjs-nestjs copied to clipboard

extend request or context to resolve nestjs providers

Open Inoir opened this issue 3 years ago • 0 comments

currently its kinda impossible to resolve nestjs stuff inside i.e handler of actions. would be nice if this would be implemented.

my current workaround for this is to bind a moduleref on the current req obj inside the admin loader:

export class AdminLoader extends AbstractLoader {
  public constructor(private readonly moduleRef: ModuleRef) {
    super()
  }

  register(
    admin: AdminJS,
    httpAdapter: AbstractHttpAdapter<any, any, any>,
    options: AdminModuleOptions
  ) {
    .... lot other stuff

    app.use(options.adminJsOptions.rootPath, function admin(req, res, next) {
      req["resolver"] = moduleRef

      return router(req, res, next)
    })
   }
}

so on every request i can simply do:

const roleService = request.resolver.get(RoleService, {
  strict: false,
})

If there is already a way implemented, would be nice to point this out in the documentation.

Inoir avatar Jan 25 '23 03:01 Inoir