I have an angular-cli
generated project that I would like to use with a deployd backend. Deployd provides a script for accessing it's API's that can be loaded from http://<deployd-host>/dpd.js
. This creates a global dpd
object that can access the API from javascript global context (e.g. from Chrome dev tools console).
I would like to wrap this in an Angular2 service so I can inject a mock one for testing etc. The tasks are to load the script from the URL and then gain access to the global dpd
object. I've looked at this SO post but haven't been able to get the accepted answer to work. If I add the script manually to the document
object, I can't access the dpd object on window
.
Also, the URL that the script is loaded from will be different based on environments, e.g. http://localhost:3000/dpd.js
for local dev, http://dev.example.com/dpd.js
for staging and http://www.example.com/dpd.js
for production. So ideally I would be able to configure that in the service as well.
Looking for something like below to work.
@Injectable()
export class DpdService {
constructor() {
if (getEnvironmentSomeHow() == 'development') {
loadScriptFrom("http://localhost:3000/dpd.js");
} else {
loadScriptFrom("http://dev.example.com/dpd.js");
}
dpd = window.dpd;
}
public session(): Observable<Session> {
return Observable.fromPromise(dpd.sessions.get());
}
}