UNPKG

fabric8-planner

Version:
132 lines 5.8 kB
var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); import { HttpBackend, HttpClient, HttpHeaders } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { throwError, timer } from 'rxjs'; import { flatMap, retryWhen } from 'rxjs/operators'; var HttpClientService = /** @class */ (function () { function HttpClientService(http) { this.http = http; } HttpClientService.prototype.setHeaders = function (options) { var headers = new HttpHeaders(); // This is a hack to forcefully define no extra header // to the request if (Object.keys(options).length && Object.keys(options)[0] === 'no-header') { return headers; } Object.entries(options).forEach(function (_a) { var key = _a[0], value = _a[1]; headers = headers.append(key, value); }); // this header is added to identify that it's a planner request to // the auth interceptoe. This header is removed in the interceptor headers = headers.append('planner-req', 'planner-req'); return headers; }; HttpClientService.prototype.requestRetryLogic = function (attempts) { console.log('retryWhen callback'); var count = 0; return attempts.pipe(flatMap(function (error) { if (error.status == 0) { // Server offline :: keep trying console.log('########### Now offline #############', error); return timer(++count * 1000); // TODO ng6: use timer from rxjs 6 } else if (error.status == 500 || error.status == 401) { // Server error :: Try 3 times then throw error return ++count >= 3 ? throwError(error) : timer(1000); } else { return throwError(error); } })); }; HttpClientService.prototype.get = function (url, options) { var _this = this; if (options === void 0) { options = {}; } console.log('GET request initiated'); console.log('URL - ', url); console.log('Options - ', options); return this.http.get(url, { headers: this.setHeaders(options) }) .pipe(retryWhen(function (attempts) { return _this.requestRetryLogic(attempts); })); }; HttpClientService.prototype.post = function (url, body, options) { var _this = this; if (options === void 0) { options = {}; } console.log('GET request initiated'); console.log('URL - ', url); console.log('Options - ', options); return this.http.post(url, body, { headers: this.setHeaders(options) }) .pipe(retryWhen(function (attempts) { return _this.requestRetryLogic(attempts); })); }; HttpClientService.prototype.patch = function (url, body, options) { var _this = this; if (options === void 0) { options = {}; } console.log('PATCH request initiated'); console.log('URL - ', url); console.log('Body - ', body); console.log('Options - ', options); return this.http.patch(url, body, { headers: this.setHeaders(options) }) .pipe(retryWhen(function (attempts) { return _this.requestRetryLogic(attempts); })); }; HttpClientService.prototype.delete = function (url) { var _this = this; console.log('DELETE request initiated'); console.log('URL - ', url); return this.http.delete(url, { responseType: 'text', headers: this.setHeaders({}) }) .pipe(retryWhen(function (attempts) { return _this.requestRetryLogic(attempts); })); }; HttpClientService.decorators = [ { type: Injectable }, ]; /** @nocollapse */ HttpClientService.ctorParameters = function () { return [ { type: HttpClient, }, ]; }; return HttpClientService; }()); export { HttpClientService }; /** * This is to bypass all interceptors * (mainly for detailPage on firefox to solve the redirection problem) * * Working of HttpHandler, HttpBackend according to Angular code documentation : * * HttpHandler Transforms an `HttpRequest` into a stream of `HttpEvent`s, one of which will likely be a * `HttpResponse`. * `HttpHandler` is injectable. When injected, the handler instance dispatches requests to the * first interceptor in the chain, which dispatches to the second, etc, eventually reaching the * `HttpBackend`. * In an `HttpInterceptor`, the `HttpHandler` parameter is the next interceptor in the chain. * * HttpBackend is the final `HttpHandler` which will dispatch the request via browser HTTP APIs to a backend * * When injected, `HttpBackend` dispatches requests directly to the backend, without going * through the interceptor chain. * */ var HttpBackendClient = /** @class */ (function (_super) { __extends(HttpBackendClient, _super); function HttpBackendClient(handler) { // inject the HttpBackend to // disptach the request directly to backend return _super.call(this, handler) || this; } HttpBackendClient.decorators = [ { type: Injectable }, ]; /** @nocollapse */ HttpBackendClient.ctorParameters = function () { return [ { type: HttpBackend, }, ]; }; return HttpBackendClient; }(HttpClient)); export { HttpBackendClient }; //# sourceMappingURL=http.service.js.map