UNPKG

angular-interceptors

Version:

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

214 lines (209 loc) 7.77 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/common/http'), require('@angular/core'), require('rxjs'), require('rxjs/operators')) : typeof define === 'function' && define.amd ? define('angular-interceptors', ['exports', '@angular/common/http', '@angular/core', 'rxjs', 'rxjs/operators'], factory) : (factory((global['angular-interceptors'] = {}),global.ng.common.http,global.ng.core,null,global.Rx.Observable.prototype)); }(this, (function (exports,http,core,rxjs,operators) { 'use strict'; /** * @fileoverview added by tsickle * @suppress {checkTypes} checked by tsc */ var /** @type {?} */ MAX_AGE_MS = new core.InjectionToken('maxAgeMs'); var CacheInterceptor = (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(operators.finalize(function () { return rxjs.interval(_this.maxAgeMs) .pipe(operators.take(1)) .subscribe(function () { return _this.cache.delete(req.urlWithParams); }); }), operators.publishReplay(), operators.refCount(), operators.catchError(function (err) { return rxjs.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: core.Injectable }, ]; /** @nocollapse */ CacheInterceptor.ctorParameters = function () { return [ { type: undefined, decorators: [{ type: core.Inject, args: [MAX_AGE_MS,] }, { type: core.Optional },] }, ]; }; var /** @type {?} */ CACHE_INTERCEPTOR_PROVIDER = { provide: http.HTTP_INTERCEPTORS, useClass: CacheInterceptor, multi: true }; /** * @fileoverview added by tsickle * @suppress {checkTypes} checked by tsc */ var CacheInterceptorModule = (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: core.NgModule }, ]; /** * @fileoverview added by tsickle * @suppress {checkTypes} checked by tsc */ var EnsureHttpsInterceptor = (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: core.Injectable }, ]; /** @nocollapse */ EnsureHttpsInterceptor.ctorParameters = function () { return []; }; var /** @type {?} */ ENSURE_HTTPS_INTERCEPTOR_PROVIDER = { provide: http.HTTP_INTERCEPTORS, useClass: EnsureHttpsInterceptor, multi: true }; /** * @fileoverview added by tsickle * @suppress {checkTypes} checked by tsc */ var EnsureHttpsInterceptorModule = (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: core.NgModule }, ]; /** * @fileoverview added by tsickle * @suppress {checkTypes} checked by tsc */ var /** @type {?} */ PREFIX = new core.InjectionToken('prefix'); var PrefixUrlInterceptor = (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: core.Injectable }, ]; /** @nocollapse */ PrefixUrlInterceptor.ctorParameters = function () { return [ { type: undefined, decorators: [{ type: core.Inject, args: [PREFIX,] },] }, ]; }; var /** @type {?} */ PREFIX_URL_INTERCEPTOR_PROVIDER = { provide: http.HTTP_INTERCEPTORS, useClass: PrefixUrlInterceptor, multi: true }; /** * @fileoverview added by tsickle * @suppress {checkTypes} checked by tsc */ var PrefixUrlInterceptorModule = (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: core.NgModule }, ]; exports.CacheInterceptorModule = CacheInterceptorModule; exports.MAX_AGE_MS = MAX_AGE_MS; exports.EnsureHttpsInterceptorModule = EnsureHttpsInterceptorModule; exports.PREFIX = PREFIX; exports.PrefixUrlInterceptorModule = PrefixUrlInterceptorModule; exports.ɵb = CACHE_INTERCEPTOR_PROVIDER; exports.ɵa = CacheInterceptor; exports.ɵf = ENSURE_HTTPS_INTERCEPTOR_PROVIDER; exports.ɵe = EnsureHttpsInterceptor; exports.ɵd = PREFIX_URL_INTERCEPTOR_PROVIDER; exports.ɵc = PrefixUrlInterceptor; Object.defineProperty(exports, '__esModule', { value: true }); }))); //# sourceMappingURL=angular-interceptors.umd.js.map