adal-angular8
Version:
ADAL wrapper for Angular 8+
181 lines (180 loc) • 5.53 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;
};
Object.defineProperty(exports, "__esModule", { value: true });
var Observable_1 = require("rxjs/internal/Observable");
var core_1 = require("@angular/core");
var http_1 = require("@angular/common/http");
var operators_1 = require("rxjs/operators");
/**
*
*
* @export
* @class Adal8HTTPService
*/
var Adal8HTTPService = /** @class */ (function () {
/**
* Creates an instance of Adal8HTTPService.
* @param {HttpClient} http
* @param {Adal8Service} service
*
* @memberOf Adal8HTTPService
*/
function Adal8HTTPService(http, service) {
this.http = http;
this.service = service;
}
Adal8HTTPService_1 = Adal8HTTPService;
/**
*
*
* @static
* @param {HttpClient} http
* @param {Adal8Service} service
*
* @memberOf Adal8HTTPService
*/
Adal8HTTPService.factory = function (http, service) {
return new Adal8HTTPService_1(http, service);
};
/**
*
*
* @param {string} url
* @param {*} [options]
* @returns {Observable<any>}
*
* @memberOf Adal8HTTPService
*/
Adal8HTTPService.prototype.get = function (url, options) {
return this.sendRequest('get', url, options);
};
/**
*
*
* @param {string} url
* @param {*} body
* @param {*} [options]
* @returns {Observable<any>}
*
* @memberOf Adal8HTTPService
*/
Adal8HTTPService.prototype.post = function (url, body, options) {
options.body = body;
return this.sendRequest('post', url, options);
};
/**
*
*
* @param {string} url
* @param {*} [options]
* @returns {Observable<any>}
*
* @memberOf Adal8HTTPService
*/
Adal8HTTPService.prototype.delete = function (url, options) {
return this.sendRequest('delete', url, options);
};
/**
*
*
* @param {string} url
* @param {*} body
* @param {*} [options]
* @returns {Observable<any>}
*
* @memberOf Adal8HTTPService
*/
Adal8HTTPService.prototype.patch = function (url, body, options) {
options.body = body;
return this.sendRequest('patch', url, options);
};
/**
*
*
* @param {string} url
* @param {*} body
* @param {*} [options]
* @returns {Observable<any>}
*
* @memberOf Adal8HTTPService
*/
Adal8HTTPService.prototype.put = function (url, body, options) {
options.body = body;
return this.sendRequest('put', url, options);
};
/**
*
*
* @param {string} url
* @param {*} [options]
* @returns {Observable<any>}
*
* @memberOf Adal8HTTPService
*/
Adal8HTTPService.prototype.head = function (url, options) {
return this.sendRequest('head', url, options);
};
/**
*
*
* @private
* @param {string} method
* @param {string} url
* @param {RequestOptionsArgs} options
* @returns {Observable<string>}
*
* @memberOf Adal8HTTPService
*/
Adal8HTTPService.prototype.sendRequest = function (method, url, options) {
var _this = this;
var resource = this.service.getResourceForEndpoint(url);
var authenticatedCall;
if (resource) {
if (this.service.userInfo.authenticated) {
authenticatedCall = this.service.acquireToken(resource)
.pipe(operators_1.mergeMap(function (token) {
if (options.headers == null) {
options.headers = new http_1.HttpHeaders();
}
options.headers = options.headers.append('Authorization', 'Bearer ' + token);
return _this.http.request(method, url, options)
.pipe(operators_1.catchError(_this.handleError));
}));
}
else {
authenticatedCall = Observable_1.Observable.throw(new Error('User Not Authenticated.'));
}
}
else {
authenticatedCall = this.http.request(method, url, options)
.pipe(operators_1.catchError(this.handleError));
}
return authenticatedCall;
};
/**
*
*
* @private
* @param {*} error
* @returns
*
* @memberOf Adal8HTTPService
*/
Adal8HTTPService.prototype.handleError = function (error) {
// In a real world app, we might send the error to remote logging infrastructure
var errMsg = error.message || 'Server error';
console.error(JSON.stringify(error)); // log to console instead
return Observable_1.Observable.throw(error);
};
var Adal8HTTPService_1;
Adal8HTTPService = Adal8HTTPService_1 = __decorate([
core_1.Injectable()
], Adal8HTTPService);
return Adal8HTTPService;
}());
exports.Adal8HTTPService = Adal8HTTPService;