1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.CacheInterceptor = void 0;
|
4 | const tslib_1 = require("tslib");
|
5 | const rxjs_1 = require("rxjs");
|
6 | const operators_1 = require("rxjs/operators");
|
7 | const decorators_1 = require("../../decorators");
|
8 | const logger_service_1 = require("../../services/logger.service");
|
9 | const shared_utils_1 = require("../../utils/shared.utils");
|
10 | const cache_constants_1 = require("../cache.constants");
|
11 | const HTTP_ADAPTER_HOST = 'HttpAdapterHost';
|
12 | const REFLECTOR = 'Reflector';
|
13 | let CacheInterceptor = class CacheInterceptor {
|
14 | constructor(cacheManager, reflector) {
|
15 | this.cacheManager = cacheManager;
|
16 | this.reflector = reflector;
|
17 | this.allowedMethods = ['GET'];
|
18 | }
|
19 | async intercept(context, next) {
|
20 | var _a;
|
21 | const key = this.trackBy(context);
|
22 | const ttlValueOrFactory = (_a = this.reflector.get(cache_constants_1.CACHE_TTL_METADATA, context.getHandler())) !== null && _a !== void 0 ? _a : null;
|
23 | if (!key) {
|
24 | return next.handle();
|
25 | }
|
26 | try {
|
27 | const value = await this.cacheManager.get(key);
|
28 | if (!(0, shared_utils_1.isNil)(value)) {
|
29 | return (0, rxjs_1.of)(value);
|
30 | }
|
31 | const ttl = (0, shared_utils_1.isFunction)(ttlValueOrFactory)
|
32 | ? await ttlValueOrFactory(context)
|
33 | : ttlValueOrFactory;
|
34 | return next.handle().pipe((0, operators_1.tap)(async (response) => {
|
35 | const args = (0, shared_utils_1.isNil)(ttl) ? [key, response] : [key, response, { ttl }];
|
36 | try {
|
37 | await this.cacheManager.set(...args);
|
38 | }
|
39 | catch (err) {
|
40 | logger_service_1.Logger.error(`An error has occured when inserting "key: ${key}", "value: ${response}"`, 'CacheInterceptor');
|
41 | }
|
42 | }));
|
43 | }
|
44 | catch (_b) {
|
45 | return next.handle();
|
46 | }
|
47 | }
|
48 | trackBy(context) {
|
49 | const httpAdapter = this.httpAdapterHost.httpAdapter;
|
50 | const isHttpApp = httpAdapter && !!httpAdapter.getRequestMethod;
|
51 | const cacheMetadata = this.reflector.get(cache_constants_1.CACHE_KEY_METADATA, context.getHandler());
|
52 | if (!isHttpApp || cacheMetadata) {
|
53 | return cacheMetadata;
|
54 | }
|
55 | const request = context.getArgByIndex(0);
|
56 | if (!this.isRequestCacheable(context)) {
|
57 | return undefined;
|
58 | }
|
59 | return httpAdapter.getRequestUrl(request);
|
60 | }
|
61 | isRequestCacheable(context) {
|
62 | const req = context.switchToHttp().getRequest();
|
63 | return this.allowedMethods.includes(req.method);
|
64 | }
|
65 | };
|
66 | tslib_1.__decorate([
|
67 | (0, decorators_1.Optional)(),
|
68 | (0, decorators_1.Inject)(HTTP_ADAPTER_HOST),
|
69 | tslib_1.__metadata("design:type", Object)
|
70 | ], CacheInterceptor.prototype, "httpAdapterHost", void 0);
|
71 | CacheInterceptor = tslib_1.__decorate([
|
72 | (0, decorators_1.Injectable)(),
|
73 | tslib_1.__param(0, (0, decorators_1.Inject)(cache_constants_1.CACHE_MANAGER)),
|
74 | tslib_1.__param(1, (0, decorators_1.Inject)(REFLECTOR)),
|
75 | tslib_1.__metadata("design:paramtypes", [Object, Object])
|
76 | ], CacheInterceptor);
|
77 | exports.CacheInterceptor = CacheInterceptor;
|