UNPKG

@akanass/ng-universal-transfer-http

Version:

TransferHttpCacheModule installs a Http interceptor that avoids duplicate HttpClient requests on the client

1,048 lines (1,042 loc) 33.9 kB
import { InjectionToken, Injectable, Optional, Inject, ApplicationRef, PLATFORM_ID, NgModule } from '@angular/core'; import { HttpResponse, HttpHeaders, HttpErrorResponse, HTTP_INTERCEPTORS } from '@angular/common/http'; import { makeStateKey, TransferState, BrowserTransferStateModule } from '@angular/platform-browser'; import { isPlatformServer } from '@angular/common'; import createHash from 'create-hash'; import { stringify } from 'flatted/esm'; import { of, merge, from, throwError } from 'rxjs'; import { flatMap, filter, tap, first, map, defaultIfEmpty, toArray } from 'rxjs/operators'; /** * @fileoverview added by tsickle * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** @type {?} */ var NG_UNIVERSAL_TRANSFER_HTTP_CONFIG = new InjectionToken('ng_universal_transfer_http_config'); /** * @fileoverview added by tsickle * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ var TransferHttpCacheConfigService = /** @class */ (function () { /** * Class constructor */ function TransferHttpCacheConfigService(_transferHttpCacheConfig) { this._transferHttpCacheConfig = _transferHttpCacheConfig; this._config = { prodMode: true }; if (this._transferHttpCacheConfig !== null) { Object.assign(this._config, this._transferHttpCacheConfig); } } Object.defineProperty(TransferHttpCacheConfigService.prototype, "config", { /** * Returns private property _config */ get: /** * Returns private property _config * @return {?} */ function () { return this._config; }, enumerable: true, configurable: true }); TransferHttpCacheConfigService.decorators = [ { type: Injectable } ]; /** @nocollapse */ TransferHttpCacheConfigService.ctorParameters = function () { return [ { type: undefined, decorators: [{ type: Optional }, { type: Inject, args: [NG_UNIVERSAL_TRANSFER_HTTP_CONFIG,] }] } ]; }; return TransferHttpCacheConfigService; }()); /** * @fileoverview added by tsickle * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ var TransferHttpCacheInterceptor = /** @class */ (function () { /** * Class constructor */ function TransferHttpCacheInterceptor(_appRef, _transferState, _platformId, _configService) { this._appRef = _appRef; this._transferState = _transferState; this._platformId = _platformId; this._configService = _configService; this._id = 0; this._serverStateDataStoreKey = makeStateKey('server_state_data'); this._lastIdStoreKey = makeStateKey('server_state_last_id'); this._isCacheActivatedStoreKey = makeStateKey('is_cache_activated'); this._initCacheProcess(); } /** * Initialize cache process */ /** * Initialize cache process * @private * @return {?} */ TransferHttpCacheInterceptor.prototype._initCacheProcess = /** * Initialize cache process * @private * @return {?} */ function () { var _this = this; // initialize cache flag for the current platform of(of(isPlatformServer(this._platformId))) .pipe(flatMap((/** * @param {?} isServer * @return {?} */ function (isServer) { return merge(isServer .pipe(filter((/** * @param {?} _ * @return {?} */ function (_) { return !!_; })), tap((/** * @param {?} _ * @return {?} */ function (_) { return _this._isCacheActivated = _; })), tap((/** * @param {?} _ * @return {?} */ function (_) { return _this._transferState.set(_this._isCacheActivatedStoreKey, _); }))), isServer .pipe(filter((/** * @param {?} _ * @return {?} */ function (_) { return !_; })), tap((/** * @param {?} _ * @return {?} */ function (_) { return _this._transferState.hasKey(_this._isCacheActivatedStoreKey) ? _this._isCacheActivated = _this._transferState.get(_this._isCacheActivatedStoreKey, true) : _this._isCacheActivated = _; })))); })), flatMap((/** * @return {?} */ function () { // Stop using the cache if the application has stabilized, indicating initial rendering is complete // or if we are in development mode. return merge(of(_this._configService.config.prodMode) .pipe(filter((/** * @param {?} _ * @return {?} */ function (_) { return !_; })), tap((/** * @return {?} */ function () { return console.log('TransferHttpCacheModule is in the development mode. ' + 'Enable the production mode with Server Side Rendering.'); }))), _this._appRef.isStable .pipe(filter((/** * @param {?} _ * @return {?} */ function (_) { return !!_; })))) .pipe(first()); }))).subscribe((/** * @return {?} */ function () { return _this._isCacheActivated = false; })); }; /** * Interceptor process */ /** * Interceptor process * @param {?} req * @param {?} next * @return {?} */ TransferHttpCacheInterceptor.prototype.intercept = /** * Interceptor process * @param {?} req * @param {?} next * @return {?} */ function (req, next) { var _this = this; return of(of(this._isCacheActivated)) .pipe(flatMap((/** * @param {?} isCacheActivated * @return {?} */ function (isCacheActivated) { return merge(isCacheActivated .pipe(filter((/** * @param {?} _ * @return {?} */ function (_) { return !_; })), flatMap((/** * @return {?} */ function () { return next.handle(req) .pipe(tap((/** * @return {?} */ function () { return _this._cleanServerState(); }))); }))), isCacheActivated .pipe(filter((/** * @param {?} _ * @return {?} */ function (_) { return !!_; })), flatMap((/** * @return {?} */ function () { return _this._transferStateProcess(req, next); })))); }))); }; /** * Function to clean all data in server state */ /** * Function to clean all data in server state * @private * @return {?} */ TransferHttpCacheInterceptor.prototype._cleanServerState = /** * Function to clean all data in server state * @private * @return {?} */ function () { var _this = this; merge(this._getLastId(false) .pipe(tap((/** * @return {?} */ function () { return _this._transferState.remove(_this._lastIdStoreKey); }))), this._getServerStateData(false) .pipe(tap((/** * @param {?} _ * @return {?} */ function (_) { return _.forEach((/** * @param {?} __ * @return {?} */ function (__) { return _this._transferState.remove(makeStateKey(_this._createHash(__.reqKey + "_" + __.id))); })); })), tap((/** * @return {?} */ function () { return _this._transferState.remove(_this._serverStateDataStoreKey); }))), of(of(this._transferState.hasKey(this._isCacheActivatedStoreKey))) .pipe(flatMap((/** * @param {?} hasKey * @return {?} */ function (hasKey) { return hasKey .pipe(filter((/** * @param {?} _ * @return {?} */ function (_) { return !!_; })), tap((/** * @return {?} */ function () { return _this._transferState.remove(_this._isCacheActivatedStoreKey); }))); })))) .subscribe((/** * @return {?} */ function () { return undefined; }), (/** * @param {?} e * @return {?} */ function (e) { throw (e); })); }; /** * Transfer state process */ /** * Transfer state process * @private * @param {?} req * @param {?} next * @return {?} */ TransferHttpCacheInterceptor.prototype._transferStateProcess = /** * Transfer state process * @private * @param {?} req * @param {?} next * @return {?} */ function (req, next) { var _this = this; return this._createKey(req) .pipe(flatMap((/** * @param {?} storeKey * @return {?} */ function (storeKey) { return of(of(_this._transferState.hasKey(storeKey))) .pipe(flatMap((/** * @param {?} hasKey * @return {?} */ function (hasKey) { return merge(_this._hasKeyProcess(hasKey, storeKey), _this._hasNotKeyProcess(req, next, hasKey, storeKey)); }))); }))); }; /** * Creates transfer state key's store */ /** * Creates transfer state key's store * @private * @param {?} req * @return {?} */ TransferHttpCacheInterceptor.prototype._createKey = /** * Creates transfer state key's store * @private * @param {?} req * @return {?} */ function (req) { var _this = this; this._id++; return of(of(isPlatformServer(this._platformId))) .pipe(flatMap((/** * @param {?} isServer * @return {?} */ function (isServer) { return merge(isServer .pipe(filter((/** * @param {?} _ * @return {?} */ function (_) { return !!_; })), flatMap((/** * @return {?} */ function () { return _this._serverKey(req); }))), isServer .pipe(filter((/** * @param {?} _ * @return {?} */ function (_) { return !_; })), flatMap((/** * @return {?} */ function () { return _this._clientKey(req); })))); }))); }; /** * Function to get state data and create client key for current request */ /** * Function to get state data and create client key for current request * @private * @param {?} req * @return {?} */ TransferHttpCacheInterceptor.prototype._clientKey = /** * Function to get state data and create client key for current request * @private * @param {?} req * @return {?} */ function (req) { var _this = this; return this._requestFormatted(req) .pipe(map((/** * @param {?} _ * @return {?} */ function (_) { return _this._createHash(stringify(_)); })), flatMap((/** * @param {?} reqKey * @return {?} */ function (reqKey) { return _this._getServerStateData() .pipe(flatMap((/** * @param {?} _ * @return {?} */ function (_) { return from(_); })), filter((/** * @param {?} _ * @return {?} */ function (_) { return _.reqKey === reqKey; })), defaultIfEmpty(undefined), flatMap((/** * @param {?} _ * @return {?} */ function (_) { return !!_ ? of(_) : throwError(new Error('Request missing in server state data')); })), flatMap((/** * @param {?} serverState * @return {?} */ function (serverState) { return _this._getLastId() .pipe(flatMap((/** * @param {?} _ * @return {?} */ function (_) { return !_ || _this._id > _ ? throwError(new Error('Wrong id for server state data')) : of(_this._id); })), map((/** * @param {?} _ * @return {?} */ function (_) { return _ === serverState.id ? _ : serverState.id; })), map((/** * @param {?} id * @return {?} */ function (id) { return _this._createHash(reqKey + "_" + id); })), map((/** * @param {?} key * @return {?} */ function (key) { return makeStateKey(key); }))); }))); }))); }; /** * Function to get last id from server */ /** * Function to get last id from server * @private * @param {?=} _throwError * @return {?} */ TransferHttpCacheInterceptor.prototype._getLastId = /** * Function to get last id from server * @private * @param {?=} _throwError * @return {?} */ function (_throwError) { var _this = this; if (_throwError === void 0) { _throwError = true; } return of(this._lastIdStoreKey) .pipe(flatMap((/** * @param {?} storeKey * @return {?} */ function (storeKey) { return of(of(_this._transferState.hasKey(storeKey))) .pipe(flatMap((/** * @param {?} hasKey * @return {?} */ function (hasKey) { return merge(hasKey .pipe(filter((/** * @param {?} _ * @return {?} */ function (_) { return !_; })), flatMap((/** * @return {?} */ function () { return of(_throwError) .pipe(filter((/** * @param {?} __ * @return {?} */ function (__) { return !!__; })), flatMap((/** * @return {?} */ function () { return throwError(new Error('Missing server state last id')); }))); }))), hasKey .pipe(filter((/** * @param {?} _ * @return {?} */ function (_) { return !!_; })), map((/** * @return {?} */ function () { return _this._transferState.get(storeKey, 0); })))); }))); }))); }; /** * Function to get server state data */ /** * Function to get server state data * @private * @param {?=} _throwError * @return {?} */ TransferHttpCacheInterceptor.prototype._getServerStateData = /** * Function to get server state data * @private * @param {?=} _throwError * @return {?} */ function (_throwError) { var _this = this; if (_throwError === void 0) { _throwError = true; } return of(this._serverStateDataStoreKey) .pipe(flatMap((/** * @param {?} storeKey * @return {?} */ function (storeKey) { return of(of(_this._transferState.hasKey(storeKey))) .pipe(flatMap((/** * @param {?} hasKey * @return {?} */ function (hasKey) { return merge(hasKey .pipe(filter((/** * @param {?} _ * @return {?} */ function (_) { return !_; })), flatMap((/** * @return {?} */ function () { return of(_throwError) .pipe(filter((/** * @param {?} __ * @return {?} */ function (__) { return !!__; })), flatMap((/** * @return {?} */ function () { return throwError(new Error('Missing server state data')); }))); }))), hasKey .pipe(filter((/** * @param {?} _ * @return {?} */ function (_) { return !!_; })), map((/** * @return {?} */ function () { return _this._transferState.get(storeKey, (/** @type {?} */ ([]))); })))); }))); }))); }; /** * Function to create server key and store state data for current request */ /** * Function to create server key and store state data for current request * @private * @param {?} req * @return {?} */ TransferHttpCacheInterceptor.prototype._serverKey = /** * Function to create server key and store state data for current request * @private * @param {?} req * @return {?} */ function (req) { var _this = this; return this._requestFormatted(req) .pipe(map((/** * @param {?} _ * @return {?} */ function (_) { return _this._createHash(stringify(_)); })), tap((/** * @param {?} reqKey * @return {?} */ function (reqKey) { return _this._storeServerStateData(reqKey); })), map((/** * @param {?} reqKey * @return {?} */ function (reqKey) { return _this._createHash(reqKey + "_" + _this._id); })), map((/** * @param {?} key * @return {?} */ function (key) { return makeStateKey(key); }))); }; /** * Function to store server state data */ /** * Function to store server state data * @private * @param {?} reqKey * @return {?} */ TransferHttpCacheInterceptor.prototype._storeServerStateData = /** * Function to store server state data * @private * @param {?} reqKey * @return {?} */ function (reqKey) { var _this = this; of(this._serverStateDataStoreKey) .pipe(flatMap((/** * @param {?} storeKey * @return {?} */ function (storeKey) { return of(of(_this._transferState.hasKey(storeKey))) .pipe(flatMap((/** * @param {?} hasKey * @return {?} */ function (hasKey) { return merge(hasKey .pipe(filter((/** * @param {?} _ * @return {?} */ function (_) { return !_; })), map((/** * @return {?} */ function () { return (/** @type {?} */ ([])); }))), hasKey .pipe(filter((/** * @param {?} _ * @return {?} */ function (_) { return !!_; })), map((/** * @return {?} */ function () { return _this._transferState.get(storeKey, (/** @type {?} */ ([]))); })), flatMap((/** * @param {?} serverStateData * @return {?} */ function (serverStateData) { return !!serverStateData.find((/** * @param {?} _ * @return {?} */ function (_) { return _.reqKey === reqKey; })) ? throwError(new Error('Request already stored in server state data')) : of(serverStateData); })))); })), tap((/** * @return {?} */ function () { return _this._transferState.set(_this._lastIdStoreKey, _this._id); }))); }))) .subscribe((/** * @param {?} serverStateData * @return {?} */ function (serverStateData) { return _this._transferState.set(_this._serverStateDataStoreKey, serverStateData.concat({ id: _this._id, reqKey: reqKey })); }), (/** * @param {?} e * @return {?} */ function (e) { throw (e); })); }; /** * Process when key exists in transfer state */ /** * Process when key exists in transfer state * @private * @param {?} hasKey * @param {?} storeKey * @return {?} */ TransferHttpCacheInterceptor.prototype._hasKeyProcess = /** * Process when key exists in transfer state * @private * @param {?} hasKey * @param {?} storeKey * @return {?} */ function (hasKey, storeKey) { var _this = this; return hasKey .pipe(filter((/** * @param {?} _ * @return {?} */ function (_) { return !!_; })), map((/** * @return {?} */ function () { return of(_this._transferState.get(storeKey, (/** @type {?} */ ({})))); })), flatMap((/** * @param {?} obs * @return {?} */ function (obs) { return merge(obs .pipe(filter((/** * @param {?} _ * @return {?} */ function (_) { return _.status < 400; })), map((/** * @param {?} response * @return {?} */ function (response) { return new HttpResponse({ body: response.body, headers: new HttpHeaders(response.headers), status: response.status, statusText: response.statusText, url: response.url, }); }))), obs .pipe(filter((/** * @param {?} _ * @return {?} */ function (_) { return _.status >= 400; })), flatMap((/** * @param {?} response * @return {?} */ function (response) { return throwError(new HttpErrorResponse({ error: response.error, headers: new HttpHeaders(response.headers), status: response.status, statusText: response.statusText, url: response.url, })); })))); }))); }; /** * Process when key doesn't exist in transfer state */ /** * Process when key doesn't exist in transfer state * @private * @param {?} req * @param {?} next * @param {?} hasKey * @param {?} storeKey * @return {?} */ TransferHttpCacheInterceptor.prototype._hasNotKeyProcess = /** * Process when key doesn't exist in transfer state * @private * @param {?} req * @param {?} next * @param {?} hasKey * @param {?} storeKey * @return {?} */ function (req, next, hasKey, storeKey) { var _this = this; return hasKey .pipe(filter((/** * @param {?} _ * @return {?} */ function (_) { return !_; })), flatMap((/** * @return {?} */ function () { return next.handle(req) .pipe(tap((/** * @param {?} event * @return {?} */ function (event) { return of(event) .pipe(filter((/** * @param {?} evt * @return {?} */ function (evt) { return evt instanceof HttpResponse; }))) .subscribe((/** * @param {?} evt * @return {?} */ function (evt) { return _this._transferState.set(storeKey, { body: evt.body, headers: _this._getHeadersMap(evt.headers), status: evt.status, statusText: evt.statusText, // tslint:disable-next-line:no-non-null-assertion url: (/** @type {?} */ (evt.url)), }); })); }), (/** * @param {?} error * @return {?} */ function (error) { return of(error) .pipe(filter((/** * @param {?} err * @return {?} */ function (err) { return err instanceof HttpErrorResponse; }))) .subscribe((/** * @param {?} err * @return {?} */ function (err) { return _this._transferState.set(storeKey, { error: err.error, headers: _this._getHeadersMap(err.headers), status: err.status, statusText: err.statusText, // tslint:disable-next-line:no-non-null-assertion url: (/** @type {?} */ (err.url)), }); })); }))); }))); }; /** * Creates Headers Map */ /** * Creates Headers Map * @private * @param {?} headers * @return {?} */ TransferHttpCacheInterceptor.prototype._getHeadersMap = /** * Creates Headers Map * @private * @param {?} headers * @return {?} */ function (headers) { // tslint:disable-next-line:no-non-null-assertion return headers.keys().reduce((/** * @param {?} acc * @param {?} curr * @return {?} */ function (acc, curr) { var _a; return Object.assign(acc, (_a = {}, _a[curr] = (/** @type {?} */ (headers.getAll(curr))), _a)); }), {}); }; /** * Function to create sha256 hash */ /** * Function to create sha256 hash * @private * @param {?} data * @return {?} */ TransferHttpCacheInterceptor.prototype._createHash = /** * Function to create sha256 hash * @private * @param {?} data * @return {?} */ function (data) { return createHash('sha256').update(data).digest('hex'); }; /** * Returns HttpRequest with value of header inside url & urlWithParams */ /** * Returns HttpRequest with value of header inside url & urlWithParams * @private * @param {?} req * @param {?} headerName * @return {?} */ TransferHttpCacheInterceptor.prototype._replaceWithHeader = /** * Returns HttpRequest with value of header inside url & urlWithParams * @private * @param {?} req * @param {?} headerName * @return {?} */ function (req, headerName) { var _this = this; return of(of(this._getHeadersMap(req.headers)[headerName])) .pipe(flatMap((/** * @param {?} obs * @return {?} */ function (obs) { return merge(obs.pipe(filter((/** * @param {?} _ * @return {?} */ function (_) { return !!_ && !!_.length; })), map((/** * @param {?} _ * @return {?} */ function (_) { return of(_[_.length - 1]); })), flatMap((/** * @param {?} o * @return {?} */ function (o) { return merge(o.pipe(filter((/** * @param {?} _ * @return {?} */ function (_) { return !!_; })), flatMap((/** * @param {?} headerValue * @return {?} */ function (headerValue) { return merge(_this._formatUrlWithHeaderValue(req.url, headerValue), _this._formatUrlWithHeaderValue(req.urlWithParams, headerValue)).pipe(toArray(), map((/** * @param {?} _ * @return {?} */ function (_) { return (/** @type {?} */ (Object.assign({}, req, { url: _[0], urlWithParams: _[1] }))); }))); }))), o.pipe(filter((/** * @param {?} _ * @return {?} */ function (_) { return !_; })), flatMap((/** * @return {?} */ function () { return throwError(new Error("Missing header '" + headerName + "' value inside request to generate state key")); })))); }))), obs.pipe(filter((/** * @param {?} _ * @return {?} */ function (_) { return !_ || !_.length; })), flatMap((/** * @return {?} */ function () { return throwError(new Error("Missing header '" + headerName + "' value inside request to generate state key")); })))); }))); }; /** * Replace url with header value */ /** * Replace url with header value * @private * @param {?} url * @param {?} headerValue * @return {?} */ TransferHttpCacheInterceptor.prototype._formatUrlWithHeaderValue = /** * Replace url with header value * @private * @param {?} url * @param {?} headerValue * @return {?} */ function (url, headerValue) { return of(url) .pipe(map((/** * @param {?} _ * @return {?} */ function (_) { return _.split('://')[1].split('/'); })), map((/** * @param {?} _ * @return {?} */ function (_) { return _.map((/** * @param {?} s * @param {?} i * @return {?} */ function (s, i) { return i === 0 ? headerValue : s; })); })), map((/** * @param {?} _ * @return {?} */ function (_) { return _.join('/'); }))); }; /** * Returns the good request object to create hash */ /** * Returns the good request object to create hash * @private * @param {?} req * @return {?} */ TransferHttpCacheInterceptor.prototype._requestFormatted = /** * Returns the good request object to create hash * @private * @param {?} req * @return {?} */ function (req) { var _this = this; return of(of(this._configService.config.headerNameToOverrideUrlInKeyCachingGeneration)) .pipe(flatMap((/** * @param {?} obs * @return {?} */ function (obs) { return merge(obs.pipe(filter((/** * @param {?} _ * @return {?} */ function (_) { return !!_; })), flatMap((/** * @param {?} _ * @return {?} */ function (_) { return _this._replaceWithHeader(req, _); }))), obs.pipe(filter((/** * @param {?} _ * @return {?} */ function (_) { return !_; })), map((/** * @return {?} */ function () { return req; })))); }))); }; TransferHttpCacheInterceptor.decorators = [ { type: Injectable } ]; /** @nocollapse */ TransferHttpCacheInterceptor.ctorParameters = function () { return [ { type: ApplicationRef }, { type: TransferState }, { type: undefined, decorators: [{ type: Inject, args: [PLATFORM_ID,] }] }, { type: TransferHttpCacheConfigService } ]; }; return TransferHttpCacheInterceptor; }()); /** * @fileoverview added by tsickle * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ var TransferHttpCacheModule = /** @class */ (function () { function TransferHttpCacheModule() { } /** * @param {?} config * @return {?} */ TransferHttpCacheModule.withConfig = /** * @param {?} config * @return {?} */ function (config) { return { ngModule: TransferHttpCacheModule, providers: [{ provide: NG_UNIVERSAL_TRANSFER_HTTP_CONFIG, useValue: config }] }; }; TransferHttpCacheModule.decorators = [ { type: NgModule, args: [{ imports: [BrowserTransferStateModule], providers: [ TransferHttpCacheConfigService, { provide: HTTP_INTERCEPTORS, useClass: TransferHttpCacheInterceptor, multi: true }, ], },] } ]; return TransferHttpCacheModule; }()); export { TransferHttpCacheModule, TransferHttpCacheConfigService as ɵa, NG_UNIVERSAL_TRANSFER_HTTP_CONFIG as ɵb, TransferHttpCacheInterceptor as ɵc }; //# sourceMappingURL=akanass-ng-universal-transfer-http.js.map