AngularJS: Inherited dependencies need to be duplicated?

Viewed 537

Using Angular 1.6 in combination with ES6-classes i ran into the following issue:

I wrote a service with some dependencies (surprise!)

class myService {

    /*@ngInject*/
    constructor($q){
      this.$q = $q;
      this.creationDate = new Date();
    }

    doStuff(data){
      return this.$q.when(data);
    }
}

angular.module('app').service('myService', myService)

However i got a build-target in which the service needed to be a bit fancier, so i extended it and used the extended service in that case instead:

class myFancyService extends myService{

    /*@ngInject*/
    constructor($q, $http){
      super($q);
      this.$http = $http;
    }

    doFancyStuff(data){
      console.log(this.creationDate);
      return this.doStuff(data)
          .then((stuff) => this.$http.post('api.myapp', stuff));
    }
}

angular.module('app').service('myService', myFancyService)

This works fine so far, but has a major drawback:

By calling super(dependencies), the dependencies of my base-class can't get injected automatically from @ngInject. Thus i need to be extremely aware that anytime i change the dependencies of myService, the dependencies of myFancyService (and any other potential future child-class) need to be changed as well.

I can not use Composition instead of Inheritance because myService is not registered as angular-service and thus can't be injected as dependency.

Question:

Is there a way to inject dependencies of the baseclass automatically anyways?

If not, is there at least a way to let my unittests remind me that i need to update the dependencies of myFancyService? I couldn't find a way yet to test with karma/jasmine if the arguments (or maybe just the number of arguments) of super($q) equal the (number of) arguments of the myService-constructor.

2 Answers
Related