UNPKG

angular-interceptors

Version:

Useful interceptors for [Angular](https://github.com/angular/angular)

206 lines (196 loc) 6.45 kB
import { HTTP_INTERCEPTORS } from '@angular/common/http'; import { Inject, Injectable, InjectionToken, Optional, NgModule } from '@angular/core'; import { interval, throwError } from 'rxjs'; import { catchError, finalize, publishReplay, refCount, take } from 'rxjs/operators'; /** * @fileoverview added by tsickle * @suppress {checkTypes} checked by tsc */ var /** @type {?} */ MAX_AGE_MS = new InjectionToken('maxAgeMs'); var CacheInterceptor = /** @class */ (function () { /** * @param {?} maxAgeMs */ function CacheInterceptor(maxAgeMs) { this.maxAgeMs = maxAgeMs; this.cache = new Map(); } /** * @param {?} req * @param {?} next * @return {?} */ CacheInterceptor.prototype.intercept = function (req, next) { var _this = this; if (!this.isCachable(req)) { return next.handle(req); } var /** @type {?} */ cachedResponse = this.cache.get(req.urlWithParams); if (cachedResponse) { return cachedResponse; } var /** @type {?} */ obs$ = next.handle(req).pipe(finalize(function () { return interval(_this.maxAgeMs) .pipe(take(1)) .subscribe(function () { return _this.cache.delete(req.urlWithParams); }); }), publishReplay(), refCount(), catchError(function (err) { return throwError(err); })); this.cache.set(req.urlWithParams, obs$); return obs$; }; /** * @param {?} req * @return {?} */ CacheInterceptor.prototype.isCachable = function (req) { return req.method === 'GET' && !req.headers.get('disable-cache'); }; return CacheInterceptor; }()); CacheInterceptor.decorators = [ { type: Injectable }, ]; /** @nocollapse */ CacheInterceptor.ctorParameters = function () { return [ { type: undefined, decorators: [{ type: Inject, args: [MAX_AGE_MS,] }, { type: Optional },] }, ]; }; var /** @type {?} */ CACHE_INTERCEPTOR_PROVIDER = { provide: HTTP_INTERCEPTORS, useClass: CacheInterceptor, multi: true }; /** * @fileoverview added by tsickle * @suppress {checkTypes} checked by tsc */ var CacheInterceptorModule = /** @class */ (function () { function CacheInterceptorModule() { } /** * Cache all HTTP `GET` requests. * @param {?=} maxAgeMs * @return {?} */ CacheInterceptorModule.forRoot = function (maxAgeMs) { if (maxAgeMs === void 0) { maxAgeMs = 5000; } return { ngModule: CacheInterceptorModule, providers: [CACHE_INTERCEPTOR_PROVIDER, { provide: MAX_AGE_MS, useValue: maxAgeMs }] }; }; return CacheInterceptorModule; }()); CacheInterceptorModule.decorators = [ { type: NgModule }, ]; /** * @fileoverview added by tsickle * @suppress {checkTypes} checked by tsc */ var EnsureHttpsInterceptor = /** @class */ (function () { function EnsureHttpsInterceptor() { this.cache = new Map(); } /** * @param {?} req * @param {?} next * @return {?} */ EnsureHttpsInterceptor.prototype.intercept = function (req, next) { return next.handle(req.clone({ url: req.url.replace('http://', 'https://') })); }; return EnsureHttpsInterceptor; }()); EnsureHttpsInterceptor.decorators = [ { type: Injectable }, ]; /** @nocollapse */ EnsureHttpsInterceptor.ctorParameters = function () { return []; }; var /** @type {?} */ ENSURE_HTTPS_INTERCEPTOR_PROVIDER = { provide: HTTP_INTERCEPTORS, useClass: EnsureHttpsInterceptor, multi: true }; /** * @fileoverview added by tsickle * @suppress {checkTypes} checked by tsc */ var EnsureHttpsInterceptorModule = /** @class */ (function () { function EnsureHttpsInterceptorModule() { } /** * Change `http://` to `https://` in HTTP request urls. * @return {?} */ EnsureHttpsInterceptorModule.forRoot = function () { return { ngModule: EnsureHttpsInterceptorModule, providers: [ENSURE_HTTPS_INTERCEPTOR_PROVIDER] }; }; return EnsureHttpsInterceptorModule; }()); EnsureHttpsInterceptorModule.decorators = [ { type: NgModule }, ]; /** * @fileoverview added by tsickle * @suppress {checkTypes} checked by tsc */ var /** @type {?} */ PREFIX = new InjectionToken('prefix'); var PrefixUrlInterceptor = /** @class */ (function () { /** * @param {?} prefix */ function PrefixUrlInterceptor(prefix) { this.prefix = prefix; } /** * @param {?} req * @param {?} next * @return {?} */ PrefixUrlInterceptor.prototype.intercept = function (req, next) { return next.handle(req.clone({ url: this.prefix + req.url })); }; return PrefixUrlInterceptor; }()); PrefixUrlInterceptor.decorators = [ { type: Injectable }, ]; /** @nocollapse */ PrefixUrlInterceptor.ctorParameters = function () { return [ { type: undefined, decorators: [{ type: Inject, args: [PREFIX,] },] }, ]; }; var /** @type {?} */ PREFIX_URL_INTERCEPTOR_PROVIDER = { provide: HTTP_INTERCEPTORS, useClass: PrefixUrlInterceptor, multi: true }; /** * @fileoverview added by tsickle * @suppress {checkTypes} checked by tsc */ var PrefixUrlInterceptorModule = /** @class */ (function () { function PrefixUrlInterceptorModule() { } /** * Prefix HTTP request urls. * @param {?} prefix * @return {?} */ PrefixUrlInterceptorModule.forRoot = function (prefix) { return { ngModule: PrefixUrlInterceptorModule, providers: [PREFIX_URL_INTERCEPTOR_PROVIDER, { provide: PREFIX, useValue: prefix }] }; }; return PrefixUrlInterceptorModule; }()); PrefixUrlInterceptorModule.decorators = [ { type: NgModule }, ]; /** * @fileoverview added by tsickle * @suppress {checkTypes} checked by tsc */ /** * @fileoverview added by tsickle * @suppress {checkTypes} checked by tsc */ export { CacheInterceptorModule, MAX_AGE_MS, EnsureHttpsInterceptorModule, PREFIX, PrefixUrlInterceptorModule, CACHE_INTERCEPTOR_PROVIDER as ɵb, CacheInterceptor as ɵa, ENSURE_HTTPS_INTERCEPTOR_PROVIDER as ɵf, EnsureHttpsInterceptor as ɵe, PREFIX_URL_INTERCEPTOR_PROVIDER as ɵd, PrefixUrlInterceptor as ɵc }; //# sourceMappingURL=angular-interceptors.js.map