I am facing an issue with my parent class, HTTPConnection
, which I intend to use as a network utility class in order to avoid redundant code. However, when attempting to utilize it, the file core.umd.js
throws an error stating
Uncaught ReferenceError: HTTPConnection is not defined
, even though the class is clearly defined. This has left me puzzled.
This is how my HTTPConnection
class looks:
import {Response, RequestOptionsArgs, Headers} from "@angular/http";
import {Observable} from "rxjs/Observable";
export class HTTPConnection {
protected static addAuthorizationHeader(token: string, reqOptionsArgs?: RequestOptionsArgs): RequestOptionsArgs {
if (reqOptionsArgs) {
reqOptionsArgs.headers.append('Authorization', token);
return reqOptionsArgs;
}
var headers = new Headers();
headers.append('Authorization', token);
return {headers: headers};
}
//This method is copied from the Angular 2 Developer Guide - HTTP Client
protected static extractData(res: Response) {
return res.json || {};
}
protected static handleError (error: any) {
let errMsg = "Error: " + (error.message) ? error.message : error.status ? `${error.status} - ${error.statusText}` : 'Server error';
console.error(errMsg);
return Observable.throw(errMsg);
}
}
Here is the service that extends the HTTPConnection
class:
import {Injectable} from "@angular/core";
import {Observable} from "rxjs/Observable";
import {Http, Response} from "@angular/http";
import {HTTPConnection} from "./http.connection";
import 'rxjs/add/operator/map';
@Injectable()
export class CoursesService extends HTTPConnection {
private token: string = "pew";
constructor(private _http: Http) {
super();
}
getCourses(): Observable<Response> {
return this._http.get('https://httpbin.org/get', HTTPConnection.addAuthorizationHeader(this.token))
.map(HTTPConnection.extractData)
.catch(HTTPConnection.handleError);
}
}
If anyone could offer some assistance with this issue, I would be extremely grateful.