UNPKG

angular-interceptors

Version:

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

192 lines (182 loc) 5.4 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 */ const /** @type {?} */ MAX_AGE_MS = new InjectionToken('maxAgeMs'); class CacheInterceptor { /** * @param {?} maxAgeMs */ constructor(maxAgeMs) { this.maxAgeMs = maxAgeMs; this.cache = new Map(); } /** * @param {?} req * @param {?} next * @return {?} */ intercept(req, next) { if (!this.isCachable(req)) { return next.handle(req); } const /** @type {?} */ cachedResponse = this.cache.get(req.urlWithParams); if (cachedResponse) { return cachedResponse; } const /** @type {?} */ obs$ = next.handle(req).pipe(finalize(() => interval(this.maxAgeMs) .pipe(take(1)) .subscribe(() => this.cache.delete(req.urlWithParams))), publishReplay(), refCount(), catchError(err => throwError(err))); this.cache.set(req.urlWithParams, obs$); return obs$; } /** * @param {?} req * @return {?} */ isCachable(req) { return req.method === 'GET' && !req.headers.get('disable-cache'); } } CacheInterceptor.decorators = [ { type: Injectable }, ]; /** @nocollapse */ CacheInterceptor.ctorParameters = () => [ { type: undefined, decorators: [{ type: Inject, args: [MAX_AGE_MS,] }, { type: Optional },] }, ]; const /** @type {?} */ CACHE_INTERCEPTOR_PROVIDER = { provide: HTTP_INTERCEPTORS, useClass: CacheInterceptor, multi: true }; /** * @fileoverview added by tsickle * @suppress {checkTypes} checked by tsc */ class CacheInterceptorModule { /** * Cache all HTTP `GET` requests. * @param {?=} maxAgeMs * @return {?} */ static forRoot(maxAgeMs = 5000) { return { ngModule: CacheInterceptorModule, providers: [CACHE_INTERCEPTOR_PROVIDER, { provide: MAX_AGE_MS, useValue: maxAgeMs }] }; } } CacheInterceptorModule.decorators = [ { type: NgModule }, ]; /** * @fileoverview added by tsickle * @suppress {checkTypes} checked by tsc */ class EnsureHttpsInterceptor { constructor() { this.cache = new Map(); } /** * @param {?} req * @param {?} next * @return {?} */ intercept(req, next) { return next.handle(req.clone({ url: req.url.replace('http://', 'https://') })); } } EnsureHttpsInterceptor.decorators = [ { type: Injectable }, ]; /** @nocollapse */ EnsureHttpsInterceptor.ctorParameters = () => []; const /** @type {?} */ ENSURE_HTTPS_INTERCEPTOR_PROVIDER = { provide: HTTP_INTERCEPTORS, useClass: EnsureHttpsInterceptor, multi: true }; /** * @fileoverview added by tsickle * @suppress {checkTypes} checked by tsc */ class EnsureHttpsInterceptorModule { /** * Change `http://` to `https://` in HTTP request urls. * @return {?} */ static forRoot() { return { ngModule: EnsureHttpsInterceptorModule, providers: [ENSURE_HTTPS_INTERCEPTOR_PROVIDER] }; } } EnsureHttpsInterceptorModule.decorators = [ { type: NgModule }, ]; /** * @fileoverview added by tsickle * @suppress {checkTypes} checked by tsc */ const /** @type {?} */ PREFIX = new InjectionToken('prefix'); class PrefixUrlInterceptor { /** * @param {?} prefix */ constructor(prefix) { this.prefix = prefix; } /** * @param {?} req * @param {?} next * @return {?} */ intercept(req, next) { return next.handle(req.clone({ url: this.prefix + req.url })); } } PrefixUrlInterceptor.decorators = [ { type: Injectable }, ]; /** @nocollapse */ PrefixUrlInterceptor.ctorParameters = () => [ { type: undefined, decorators: [{ type: Inject, args: [PREFIX,] },] }, ]; const /** @type {?} */ PREFIX_URL_INTERCEPTOR_PROVIDER = { provide: HTTP_INTERCEPTORS, useClass: PrefixUrlInterceptor, multi: true }; /** * @fileoverview added by tsickle * @suppress {checkTypes} checked by tsc */ class PrefixUrlInterceptorModule { /** * Prefix HTTP request urls. * @param {?} prefix * @return {?} */ static forRoot(prefix) { return { ngModule: PrefixUrlInterceptorModule, providers: [PREFIX_URL_INTERCEPTOR_PROVIDER, { provide: PREFIX, useValue: prefix }] }; } } 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