I'm looking to enhance an existing angular service (for example, Http
).
Requirements:
- The extension of the service should be done through angular's dependency injection
- It should be possible to extend the service multiple times using a pattern similar to the Decorator Pattern
- The resulting object should implement or extend the functionality of the
Http
service so that existing code remains compatible. The dependency injector should inject the "decorated"Http
. - A potential use case could involve an instance of
Http
embellished withSecureHttp
andLoggingHttp
.
What I've tried so far:
SecureHttp implements Http
didn't work because 1.Http
is not an interface and 2. it has protected members that require implementation.To extend
Http
:class SecureHttp extends Http { constructor(_backend: ConnectionBackend, _defaultOptions: RequestOptions, private _http: Http){ super(_backend, _defaultOptions); } get(url: string, options?: RequestOptionsArgs): Observable<Response> { console.log("This is secure Http."); return this._http.get(url, options); } ... }
I'm unsure how to set up the service so that
Http
is injected into theSecureHttp
class whileHttp
inSecureHttp
also gets injected byHttp
.
Am I on the right path?
Alternatively, are there other angular2 concepts that facilitate extending existing services that I may not be aware of?