UNPKG

@anglr/rest

Version:

Angular module representing rest services

38 lines 1.83 kB
import { isPresent } from '@jscrpt/common'; import { of, map } from 'rxjs'; import { AdvancedCacheService } from '../services'; /** * Middleware that is used for storing and restoring response from advanced cache service */ export class AdvancedCacheMiddleware { //######################### public static properties ######################### /** * String identification of middleware */ static { this.id = 'AdvancedCacheMiddleware'; } //######################### public methods - implementation of RestMiddleware ######################### /** * Runs code that is defined for this rest middleware, in this method you can modify request and response * @param this - Method is bound to RESTClient * @param id - Unique id that identifies request method * @param target - Prototype of class that are decorators applied to * @param methodName - Name of method that is being modified * @param descriptor - Descriptor of method that is being modified * @param args - Array of arguments passed to called method * @param request - Http request that you can modify * @param next - Used for calling next middleware with modified request */ run(_id, _target, _methodName, descriptor, _args, request, next) { const $this = this; $this.ɵCache ??= this.injector.get(AdvancedCacheService, null); if (!$this.ɵCache) { return next(request); } const cachedResponse = $this.ɵCache.get(descriptor.key, request); if (isPresent(cachedResponse)) { return of(cachedResponse); } return next(request).pipe(map(response => $this.ɵCache?.add(descriptor.key, request, response, { validUntil: descriptor.validUntil }))); } } //# sourceMappingURL=advancedCache.middleware.js.map