Inversifyjs: How to decorate external lib with @injectable()

Created on 29 Apr 2018  路  3Comments  路  Source: inversify/InversifyJS


I have an external library with a set of classes.
I want somehow decorate these classes in my module and make them injectable.

Possible Solution



Right now I do next:

import * as libClasses from 'externalLib'
import {injectable} from 'inversify';

Object.keys(libClasses).forEach((field)=> {
  const libClass = libClasses[field];
  const decorator = injectable();
  libClasses[field] = Reflect.decorate([decorator], libClass);
});

But it doesn't work.
So, the question is - how I can bind decorator programmatically?

Most helpful comment

use decorate, also from inversify.

import { decorate, injectable } from 'inversify'
decorate(injectable(), SomeClass)

All 3 comments

use decorate, also from inversify.

import { decorate, injectable } from 'inversify'
decorate(injectable(), SomeClass)

@rmvermeulen Thank you!

How do you inject the library when it's decorated?

Was this page helpful?
0 / 5 - 0 ratings