@akveo/nga-auth
Version:
@akveo/nga-auth
87 lines • 3.94 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { Injectable, Injector } from '@angular/core';
import { Headers, Http } from '@angular/http';
import { Subject } from 'rxjs/Subject';
import 'rxjs/add/operator/publish';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/switchMap';
import 'rxjs/add/operator/catch';
import { NgaAuthService } from './auth.service';
var NgaSecuredHttp = (function () {
function NgaSecuredHttp(injector, http) {
this.injector = injector;
this.http = http;
this.onErrorSubject = new Subject();
}
NgaSecuredHttp.prototype.get = function (url, options) {
var _this = this;
if (options === void 0) { options = {}; }
return this.doRequest(this.prepareOptions(options)
.switchMap(function (newOptions) { return _this.http.get(url, newOptions); }));
};
NgaSecuredHttp.prototype.post = function (url, body, options) {
var _this = this;
if (options === void 0) { options = {}; }
return this.doRequest(this.prepareOptions(options)
.switchMap(function (newOptions) { return _this.http.post(url, body, newOptions); }));
};
NgaSecuredHttp.prototype.put = function (url, body, options) {
var _this = this;
if (options === void 0) { options = {}; }
return this.doRequest(this.prepareOptions(options)
.switchMap(function (newOptions) { return _this.http.put(url, body, newOptions); }));
};
NgaSecuredHttp.prototype.delete = function (url, options) {
var _this = this;
if (options === void 0) { options = {}; }
return this.doRequest(this.prepareOptions(options)
.switchMap(function (newOptions) { return _this.http.delete(url, newOptions); }));
};
NgaSecuredHttp.prototype.onRequestError = function () {
return this.onErrorSubject.publish().refCount();
};
NgaSecuredHttp.prototype.prepareOptions = function (options) {
var _this = this;
if (options === void 0) { options = {}; }
return this.authService.getToken()
.map(function (token) {
options.headers = options.headers || new Headers();
_this.addAuthTokenHeader(options.headers, token);
return options;
});
};
NgaSecuredHttp.prototype.doRequest = function (requestObservable) {
var _this = this;
return requestObservable
.catch(function (err) {
_this.onErrorSubject.next(err);
throw err;
});
};
NgaSecuredHttp.prototype.addAuthTokenHeader = function (headers, token) {
headers.append('Authorization', "Bearer " + token);
};
Object.defineProperty(NgaSecuredHttp.prototype, "authService", {
get: function () {
return this.injector.get(NgaAuthService);
},
enumerable: true,
configurable: true
});
return NgaSecuredHttp;
}());
NgaSecuredHttp = __decorate([
Injectable(),
__metadata("design:paramtypes", [Injector,
Http])
], NgaSecuredHttp);
export { NgaSecuredHttp };
//# sourceMappingURL=secured-http.js.map