ng-http-caching 15.2.4 → 16.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/esm2022/lib/ng-http-caching-interceptor.service.mjs +72 -0
- package/esm2022/lib/ng-http-caching.module.mjs +43 -0
- package/esm2022/lib/ng-http-caching.service.mjs +331 -0
- package/{esm2020 → esm2022}/lib/storage/ng-http-caching-browser-storage.mjs +122 -122
- package/{esm2020 → esm2022}/lib/storage/ng-http-caching-local-storage.mjs +6 -6
- package/{esm2020 → esm2022}/lib/storage/ng-http-caching-memory-storage.mjs +26 -26
- package/{esm2020 → esm2022}/lib/storage/ng-http-caching-session-storage.mjs +6 -6
- package/{esm2020 → esm2022}/lib/storage/ng-http-caching-storage.interface.mjs +1 -1
- package/{esm2020 → esm2022}/ng-http-caching.mjs +4 -4
- package/{esm2020 → esm2022}/public-api.mjs +11 -11
- package/{fesm2020 → fesm2022}/ng-http-caching.mjs +579 -578
- package/fesm2022/ng-http-caching.mjs.map +1 -0
- package/index.d.ts +5 -5
- package/lib/ng-http-caching-interceptor.service.d.ts +15 -15
- package/lib/ng-http-caching.module.d.ts +9 -9
- package/lib/ng-http-caching.service.d.ts +214 -213
- package/lib/storage/ng-http-caching-browser-storage.d.ts +18 -18
- package/lib/storage/ng-http-caching-local-storage.d.ts +4 -4
- package/lib/storage/ng-http-caching-memory-storage.d.ts +12 -12
- package/lib/storage/ng-http-caching-session-storage.d.ts +4 -4
- package/lib/storage/ng-http-caching-storage.interface.d.ts +31 -31
- package/package.json +7 -13
- package/public-api.d.ts +8 -8
- package/esm2020/lib/ng-http-caching-interceptor.service.mjs +0 -71
- package/esm2020/lib/ng-http-caching.module.mjs +0 -42
- package/esm2020/lib/ng-http-caching.service.mjs +0 -329
- package/fesm2015/ng-http-caching.mjs +0 -606
- package/fesm2015/ng-http-caching.mjs.map +0 -1
- package/fesm2020/ng-http-caching.mjs.map +0 -1
|
@@ -1,606 +0,0 @@
|
|
|
1
|
-
import { HttpResponse, HTTP_INTERCEPTORS, HttpHeaders, HttpParams, HttpRequest } from '@angular/common/http';
|
|
2
|
-
import * as i0 from '@angular/core';
|
|
3
|
-
import { InjectionToken, VERSION, isDevMode, Injectable, Inject, Optional, NgModule } from '@angular/core';
|
|
4
|
-
import { scheduled, asapScheduler } from 'rxjs';
|
|
5
|
-
import { tap, finalize, shareReplay } from 'rxjs/operators';
|
|
6
|
-
|
|
7
|
-
class NgHttpCachingMemoryStorage {
|
|
8
|
-
constructor() {
|
|
9
|
-
this.store = new Map();
|
|
10
|
-
}
|
|
11
|
-
get size() {
|
|
12
|
-
return this.store.size;
|
|
13
|
-
}
|
|
14
|
-
clear() {
|
|
15
|
-
this.store.clear();
|
|
16
|
-
}
|
|
17
|
-
delete(key) {
|
|
18
|
-
return this.store.delete(key);
|
|
19
|
-
}
|
|
20
|
-
forEach(callbackfn) {
|
|
21
|
-
return this.store.forEach(callbackfn);
|
|
22
|
-
}
|
|
23
|
-
get(key) {
|
|
24
|
-
return this.store.get(key);
|
|
25
|
-
}
|
|
26
|
-
has(key) {
|
|
27
|
-
return this.store.has(key);
|
|
28
|
-
}
|
|
29
|
-
set(key, value) {
|
|
30
|
-
this.store.set(key, value);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const NG_HTTP_CACHING_CONFIG = new InjectionToken('ng-http-caching.config');
|
|
35
|
-
var NgHttpCachingStrategy;
|
|
36
|
-
(function (NgHttpCachingStrategy) {
|
|
37
|
-
/**
|
|
38
|
-
* All request are cacheable if HTTP method is into `allowedMethod`
|
|
39
|
-
*/
|
|
40
|
-
NgHttpCachingStrategy["ALLOW_ALL"] = "ALLOW_ALL";
|
|
41
|
-
/**
|
|
42
|
-
* Only the request with `X-NG-HTTP-CACHING-ALLOW-CACHE` header are cacheable if HTTP method is into `allowedMethod`
|
|
43
|
-
*/
|
|
44
|
-
NgHttpCachingStrategy["DISALLOW_ALL"] = "DISALLOW_ALL";
|
|
45
|
-
})(NgHttpCachingStrategy || (NgHttpCachingStrategy = {}));
|
|
46
|
-
var NgHttpCachingHeaders;
|
|
47
|
-
(function (NgHttpCachingHeaders) {
|
|
48
|
-
/**
|
|
49
|
-
* Request is cacheable if HTTP method is into `allowedMethod`
|
|
50
|
-
*/
|
|
51
|
-
NgHttpCachingHeaders["ALLOW_CACHE"] = "X-NG-HTTP-CACHING-ALLOW-CACHE";
|
|
52
|
-
/**
|
|
53
|
-
* Request isn't cacheable
|
|
54
|
-
*/
|
|
55
|
-
NgHttpCachingHeaders["DISALLOW_CACHE"] = "X-NG-HTTP-CACHING-DISALLOW-CACHE";
|
|
56
|
-
/**
|
|
57
|
-
* Specific cache lifetime for the request
|
|
58
|
-
*/
|
|
59
|
-
NgHttpCachingHeaders["LIFETIME"] = "X-NG-HTTP-CACHING-LIFETIME";
|
|
60
|
-
/**
|
|
61
|
-
* You can tag multiple request by adding this header with the same tag and
|
|
62
|
-
* using `NgHttpCachingService.clearCacheByTag(tag: string)` for delete all the tagged request
|
|
63
|
-
*/
|
|
64
|
-
NgHttpCachingHeaders["TAG"] = "X-NG-HTTP-CACHING-TAG";
|
|
65
|
-
})(NgHttpCachingHeaders || (NgHttpCachingHeaders = {}));
|
|
66
|
-
const NgHttpCachingHeadersList = Object.values(NgHttpCachingHeaders);
|
|
67
|
-
const NG_HTTP_CACHING_SECOND_IN_MS = 1000;
|
|
68
|
-
const NG_HTTP_CACHING_MINUTE_IN_MS = NG_HTTP_CACHING_SECOND_IN_MS * 60;
|
|
69
|
-
const NG_HTTP_CACHING_HOUR_IN_MS = NG_HTTP_CACHING_MINUTE_IN_MS * 60;
|
|
70
|
-
const NG_HTTP_CACHING_DAY_IN_MS = NG_HTTP_CACHING_HOUR_IN_MS * 24;
|
|
71
|
-
const NG_HTTP_CACHING_WEEK_IN_MS = NG_HTTP_CACHING_DAY_IN_MS * 7;
|
|
72
|
-
const NG_HTTP_CACHING_MONTH_IN_MS = NG_HTTP_CACHING_DAY_IN_MS * 30;
|
|
73
|
-
const NG_HTTP_CACHING_YEAR_IN_MS = NG_HTTP_CACHING_DAY_IN_MS * 365;
|
|
74
|
-
const NgHttpCachingConfigDefault = {
|
|
75
|
-
store: new NgHttpCachingMemoryStorage(),
|
|
76
|
-
lifetime: NG_HTTP_CACHING_HOUR_IN_MS,
|
|
77
|
-
version: VERSION.major,
|
|
78
|
-
allowedMethod: ['GET', 'HEAD'],
|
|
79
|
-
cacheStrategy: NgHttpCachingStrategy.ALLOW_ALL,
|
|
80
|
-
};
|
|
81
|
-
class NgHttpCachingService {
|
|
82
|
-
constructor(config) {
|
|
83
|
-
this.queue = new Map();
|
|
84
|
-
this.gcLock = false;
|
|
85
|
-
this.devMode = isDevMode();
|
|
86
|
-
if (config) {
|
|
87
|
-
this.config = Object.assign(Object.assign({}, NgHttpCachingConfigDefault), config);
|
|
88
|
-
}
|
|
89
|
-
else {
|
|
90
|
-
this.config = Object.assign({}, NgHttpCachingConfigDefault);
|
|
91
|
-
}
|
|
92
|
-
// start cache clean
|
|
93
|
-
this.runGc();
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* Return the config
|
|
97
|
-
*/
|
|
98
|
-
getConfig() {
|
|
99
|
-
return this.config;
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* Return the queue map
|
|
103
|
-
*/
|
|
104
|
-
getQueue() {
|
|
105
|
-
return this.queue;
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* Return the cache store
|
|
109
|
-
*/
|
|
110
|
-
getStore() {
|
|
111
|
-
return this.config.store;
|
|
112
|
-
}
|
|
113
|
-
/**
|
|
114
|
-
* Return response from cache
|
|
115
|
-
*/
|
|
116
|
-
getFromCache(req) {
|
|
117
|
-
const key = this.getKey(req);
|
|
118
|
-
const cached = this.config.store.get(key);
|
|
119
|
-
if (!cached) {
|
|
120
|
-
return undefined;
|
|
121
|
-
}
|
|
122
|
-
if (this.isExpired(cached)) {
|
|
123
|
-
this.clearCacheByKey(key);
|
|
124
|
-
return undefined;
|
|
125
|
-
}
|
|
126
|
-
return this.deepFreeze(cached.response);
|
|
127
|
-
}
|
|
128
|
-
/**
|
|
129
|
-
* Add response to cache
|
|
130
|
-
*/
|
|
131
|
-
addToCache(req, res) {
|
|
132
|
-
const entry = {
|
|
133
|
-
url: req.urlWithParams,
|
|
134
|
-
response: res,
|
|
135
|
-
request: req,
|
|
136
|
-
addedTime: Date.now(),
|
|
137
|
-
version: this.config.version,
|
|
138
|
-
};
|
|
139
|
-
if (this.isValid(entry)) {
|
|
140
|
-
const key = this.getKey(req);
|
|
141
|
-
this.config.store.set(key, entry);
|
|
142
|
-
return true;
|
|
143
|
-
}
|
|
144
|
-
return false;
|
|
145
|
-
}
|
|
146
|
-
/**
|
|
147
|
-
* Delete response from cache
|
|
148
|
-
*/
|
|
149
|
-
deleteFromCache(req) {
|
|
150
|
-
const key = this.getKey(req);
|
|
151
|
-
return this.clearCacheByKey(key);
|
|
152
|
-
}
|
|
153
|
-
/**
|
|
154
|
-
* Clear the cache
|
|
155
|
-
*/
|
|
156
|
-
clearCache() {
|
|
157
|
-
this.config.store.clear();
|
|
158
|
-
}
|
|
159
|
-
/**
|
|
160
|
-
* Clear the cache by key
|
|
161
|
-
*/
|
|
162
|
-
clearCacheByKey(key) {
|
|
163
|
-
return this.config.store.delete(key);
|
|
164
|
-
}
|
|
165
|
-
/**
|
|
166
|
-
* Clear the cache by regex
|
|
167
|
-
*/
|
|
168
|
-
clearCacheByRegex(regex) {
|
|
169
|
-
this.config.store.forEach((_, key) => {
|
|
170
|
-
if (regex.test(key)) {
|
|
171
|
-
this.clearCacheByKey(key);
|
|
172
|
-
}
|
|
173
|
-
});
|
|
174
|
-
}
|
|
175
|
-
/**
|
|
176
|
-
* Clear the cache by TAG
|
|
177
|
-
*/
|
|
178
|
-
clearCacheByTag(tag) {
|
|
179
|
-
this.config.store.forEach((entry, key) => {
|
|
180
|
-
const tagHeader = entry.request.headers.get(NgHttpCachingHeaders.TAG);
|
|
181
|
-
if (tagHeader && tagHeader.split(',').includes(tag)) {
|
|
182
|
-
this.clearCacheByKey(key);
|
|
183
|
-
}
|
|
184
|
-
});
|
|
185
|
-
}
|
|
186
|
-
/**
|
|
187
|
-
* Run garbage collector (delete expired cache entry)
|
|
188
|
-
*/
|
|
189
|
-
runGc() {
|
|
190
|
-
if (this.gcLock) {
|
|
191
|
-
return false;
|
|
192
|
-
}
|
|
193
|
-
this.gcLock = true;
|
|
194
|
-
this.config.store.forEach((entry, key) => {
|
|
195
|
-
if (this.isExpired(entry)) {
|
|
196
|
-
this.clearCacheByKey(key);
|
|
197
|
-
}
|
|
198
|
-
});
|
|
199
|
-
this.gcLock = false;
|
|
200
|
-
return true;
|
|
201
|
-
}
|
|
202
|
-
/**
|
|
203
|
-
* Return true if cache entry is expired
|
|
204
|
-
*/
|
|
205
|
-
isExpired(entry) {
|
|
206
|
-
// if user provide custom method, use it
|
|
207
|
-
if (typeof this.config.isExpired === 'function') {
|
|
208
|
-
const result = this.config.isExpired(entry);
|
|
209
|
-
// if result is undefined, normal behaviour is provided
|
|
210
|
-
if (result !== undefined) {
|
|
211
|
-
return result;
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
// config/default lifetime
|
|
215
|
-
let lifetime = this.config.lifetime;
|
|
216
|
-
// request has own lifetime
|
|
217
|
-
const headerLifetime = entry.request.headers.get(NgHttpCachingHeaders.LIFETIME);
|
|
218
|
-
if (headerLifetime) {
|
|
219
|
-
lifetime = +headerLifetime;
|
|
220
|
-
}
|
|
221
|
-
// never expire if 0
|
|
222
|
-
if (lifetime === 0) {
|
|
223
|
-
return false;
|
|
224
|
-
}
|
|
225
|
-
// wrong lifetime
|
|
226
|
-
if (lifetime < 0 || isNaN(lifetime)) {
|
|
227
|
-
throw new Error('lifetime must be greater than or equal 0');
|
|
228
|
-
}
|
|
229
|
-
return entry.addedTime + lifetime < Date.now();
|
|
230
|
-
}
|
|
231
|
-
/**
|
|
232
|
-
* Return true if cache entry is valid for store in the cache
|
|
233
|
-
*/
|
|
234
|
-
isValid(entry) {
|
|
235
|
-
// if user provide custom method, use it
|
|
236
|
-
if (typeof this.config.isValid === 'function') {
|
|
237
|
-
const result = this.config.isValid(entry);
|
|
238
|
-
// if result is undefined, normal behaviour is provided
|
|
239
|
-
if (result !== undefined) {
|
|
240
|
-
return result;
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
// different version
|
|
244
|
-
if (this.config.version !== entry.version) {
|
|
245
|
-
return false;
|
|
246
|
-
}
|
|
247
|
-
return true;
|
|
248
|
-
}
|
|
249
|
-
/**
|
|
250
|
-
* Return true if the request is cacheable
|
|
251
|
-
*/
|
|
252
|
-
isCacheable(req) {
|
|
253
|
-
// if user provide custom method, use it
|
|
254
|
-
if (typeof this.config.isCacheable === 'function') {
|
|
255
|
-
const result = this.config.isCacheable(req);
|
|
256
|
-
// if result is undefined, normal behaviour is provided
|
|
257
|
-
if (result !== undefined) {
|
|
258
|
-
return result;
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
// request has disallow cache header
|
|
262
|
-
if (req.headers.has(NgHttpCachingHeaders.DISALLOW_CACHE)) {
|
|
263
|
-
return false;
|
|
264
|
-
}
|
|
265
|
-
// strategy is disallow all...
|
|
266
|
-
if (this.config.cacheStrategy === NgHttpCachingStrategy.DISALLOW_ALL) {
|
|
267
|
-
// request isn't allowed if come without allow header
|
|
268
|
-
if (!req.headers.has(NgHttpCachingHeaders.ALLOW_CACHE)) {
|
|
269
|
-
return false;
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
// if allowed method is only ALL, allow all http methos
|
|
273
|
-
if (this.config.allowedMethod.length === 1) {
|
|
274
|
-
if (this.config.allowedMethod[0] === 'ALL') {
|
|
275
|
-
return true;
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
// request is allowed if method is in allowedMethod
|
|
279
|
-
return this.config.allowedMethod.indexOf(req.method) !== -1;
|
|
280
|
-
}
|
|
281
|
-
/**
|
|
282
|
-
* Return the cache key.
|
|
283
|
-
* Default key is http method plus url with query parameters, eg.:
|
|
284
|
-
* `GET@https://github.com/nigrosimone/ng-http-caching`
|
|
285
|
-
*/
|
|
286
|
-
getKey(req) {
|
|
287
|
-
// if user provide custom method, use it
|
|
288
|
-
if (typeof this.config.getKey === 'function') {
|
|
289
|
-
const result = this.config.getKey(req);
|
|
290
|
-
// if result is undefined, normal behaviour is provided
|
|
291
|
-
if (result !== undefined) {
|
|
292
|
-
return result;
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
// default key is req.method plus url with query parameters
|
|
296
|
-
return req.method + '@' + req.urlWithParams;
|
|
297
|
-
}
|
|
298
|
-
/**
|
|
299
|
-
* Return observable from cache
|
|
300
|
-
*/
|
|
301
|
-
getFromQueue(req) {
|
|
302
|
-
const key = this.getKey(req);
|
|
303
|
-
const cached = this.queue.get(key);
|
|
304
|
-
if (!cached) {
|
|
305
|
-
return undefined;
|
|
306
|
-
}
|
|
307
|
-
return cached;
|
|
308
|
-
}
|
|
309
|
-
/**
|
|
310
|
-
* Add observable to cache
|
|
311
|
-
*/
|
|
312
|
-
addToQueue(req, obs) {
|
|
313
|
-
const key = this.getKey(req);
|
|
314
|
-
this.queue.set(key, obs);
|
|
315
|
-
}
|
|
316
|
-
/**
|
|
317
|
-
* Delete observable from cache
|
|
318
|
-
*/
|
|
319
|
-
deleteFromQueue(req) {
|
|
320
|
-
const key = this.getKey(req);
|
|
321
|
-
return this.queue.delete(key);
|
|
322
|
-
}
|
|
323
|
-
/**
|
|
324
|
-
* Recursively Object.freeze simple Javascript structures consisting of plain objects, arrays, and primitives.
|
|
325
|
-
* Make the data immutable.
|
|
326
|
-
* @returns immutable object
|
|
327
|
-
*/
|
|
328
|
-
deepFreeze(object) {
|
|
329
|
-
// No freezing in production (for better performance).
|
|
330
|
-
if (!this.devMode || !object || typeof object !== 'object') {
|
|
331
|
-
return object;
|
|
332
|
-
}
|
|
333
|
-
// When already frozen, we assume its children are frozen (for better performance).
|
|
334
|
-
// This should be true if you always use `deepFreeze` to freeze objects.
|
|
335
|
-
//
|
|
336
|
-
// Note that Object.isFrozen will also return `true` for primitives (numbers,
|
|
337
|
-
// strings, booleans, undefined, null), so there is no need to check for
|
|
338
|
-
// those explicitly.
|
|
339
|
-
if (Object.isFrozen(object)) {
|
|
340
|
-
return object;
|
|
341
|
-
}
|
|
342
|
-
// At this point we know that we're dealing with either an array or plain object, so
|
|
343
|
-
// just freeze it and recurse on its values.
|
|
344
|
-
Object.freeze(object);
|
|
345
|
-
Object.keys(object).forEach(key => this.deepFreeze(object[key]));
|
|
346
|
-
return object;
|
|
347
|
-
}
|
|
348
|
-
}
|
|
349
|
-
NgHttpCachingService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.5", ngImport: i0, type: NgHttpCachingService, deps: [{ token: NG_HTTP_CACHING_CONFIG, optional: true }], target: i0.ɵɵFactoryTarget.Injectable });
|
|
350
|
-
NgHttpCachingService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.2.5", ngImport: i0, type: NgHttpCachingService });
|
|
351
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.5", ngImport: i0, type: NgHttpCachingService, decorators: [{
|
|
352
|
-
type: Injectable
|
|
353
|
-
}], ctorParameters: function () {
|
|
354
|
-
return [{ type: undefined, decorators: [{
|
|
355
|
-
type: Inject,
|
|
356
|
-
args: [NG_HTTP_CACHING_CONFIG]
|
|
357
|
-
}, {
|
|
358
|
-
type: Optional
|
|
359
|
-
}] }];
|
|
360
|
-
} });
|
|
361
|
-
|
|
362
|
-
/**
|
|
363
|
-
* Fix for https://github.com/ReactiveX/rxjs/issues/7241
|
|
364
|
-
*/
|
|
365
|
-
function* _of(value) {
|
|
366
|
-
yield value;
|
|
367
|
-
}
|
|
368
|
-
class NgHttpCachingInterceptorService {
|
|
369
|
-
constructor(cacheService) {
|
|
370
|
-
this.cacheService = cacheService;
|
|
371
|
-
}
|
|
372
|
-
intercept(req, next) {
|
|
373
|
-
// run garbage collector
|
|
374
|
-
this.cacheService.runGc();
|
|
375
|
-
// Don't cache if it's not cacheable
|
|
376
|
-
if (!this.cacheService.isCacheable(req)) {
|
|
377
|
-
return this.sendRequest(req, next);
|
|
378
|
-
}
|
|
379
|
-
// Checked if there is pending response for this request
|
|
380
|
-
const cachedObservable = this.cacheService.getFromQueue(req);
|
|
381
|
-
if (cachedObservable) {
|
|
382
|
-
// console.log('cachedObservable',cachedObservable);
|
|
383
|
-
return cachedObservable;
|
|
384
|
-
}
|
|
385
|
-
// Checked if there is cached response for this request
|
|
386
|
-
const cachedResponse = this.cacheService.getFromCache(req);
|
|
387
|
-
if (cachedResponse) {
|
|
388
|
-
// console.log('cachedResponse');
|
|
389
|
-
return scheduled(_of(cachedResponse.clone()), asapScheduler);
|
|
390
|
-
}
|
|
391
|
-
// If the request of going through for first time
|
|
392
|
-
// then let the request proceed and cache the response
|
|
393
|
-
// console.log('sendRequest', req);
|
|
394
|
-
const shared = this.sendRequest(req, next).pipe(tap(event => {
|
|
395
|
-
if (event instanceof HttpResponse) {
|
|
396
|
-
this.cacheService.addToCache(req, event.clone());
|
|
397
|
-
}
|
|
398
|
-
}), finalize(() => {
|
|
399
|
-
// delete pending request
|
|
400
|
-
this.cacheService.deleteFromQueue(req);
|
|
401
|
-
}), shareReplay());
|
|
402
|
-
// add pending request to queue for cache parallell request
|
|
403
|
-
this.cacheService.addToQueue(req, shared);
|
|
404
|
-
return shared;
|
|
405
|
-
}
|
|
406
|
-
/**
|
|
407
|
-
* Send http request (next handler)
|
|
408
|
-
*/
|
|
409
|
-
sendRequest(req, next) {
|
|
410
|
-
let cloned = req.clone();
|
|
411
|
-
// trim custom headers before send request
|
|
412
|
-
NgHttpCachingHeadersList.forEach(ngHttpCachingHeaders => {
|
|
413
|
-
if (cloned.headers.has(ngHttpCachingHeaders)) {
|
|
414
|
-
cloned = cloned.clone({ headers: cloned.headers.delete(ngHttpCachingHeaders) });
|
|
415
|
-
}
|
|
416
|
-
});
|
|
417
|
-
return next.handle(cloned);
|
|
418
|
-
}
|
|
419
|
-
}
|
|
420
|
-
NgHttpCachingInterceptorService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.5", ngImport: i0, type: NgHttpCachingInterceptorService, deps: [{ token: NgHttpCachingService }], target: i0.ɵɵFactoryTarget.Injectable });
|
|
421
|
-
NgHttpCachingInterceptorService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "15.2.5", ngImport: i0, type: NgHttpCachingInterceptorService });
|
|
422
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.5", ngImport: i0, type: NgHttpCachingInterceptorService, decorators: [{
|
|
423
|
-
type: Injectable
|
|
424
|
-
}], ctorParameters: function () { return [{ type: NgHttpCachingService }]; } });
|
|
425
|
-
|
|
426
|
-
class NgHttpCachingModule {
|
|
427
|
-
static forRoot(ngHttpCachingConfig) {
|
|
428
|
-
return {
|
|
429
|
-
ngModule: NgHttpCachingModule,
|
|
430
|
-
providers: [
|
|
431
|
-
{
|
|
432
|
-
provide: NG_HTTP_CACHING_CONFIG,
|
|
433
|
-
useValue: ngHttpCachingConfig,
|
|
434
|
-
},
|
|
435
|
-
],
|
|
436
|
-
};
|
|
437
|
-
}
|
|
438
|
-
}
|
|
439
|
-
NgHttpCachingModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "15.2.5", ngImport: i0, type: NgHttpCachingModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
|
|
440
|
-
NgHttpCachingModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "15.2.5", ngImport: i0, type: NgHttpCachingModule });
|
|
441
|
-
NgHttpCachingModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "15.2.5", ngImport: i0, type: NgHttpCachingModule, providers: [
|
|
442
|
-
NgHttpCachingService,
|
|
443
|
-
{
|
|
444
|
-
provide: HTTP_INTERCEPTORS,
|
|
445
|
-
useClass: NgHttpCachingInterceptorService,
|
|
446
|
-
multi: true,
|
|
447
|
-
},
|
|
448
|
-
] });
|
|
449
|
-
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "15.2.5", ngImport: i0, type: NgHttpCachingModule, decorators: [{
|
|
450
|
-
type: NgModule,
|
|
451
|
-
args: [{
|
|
452
|
-
providers: [
|
|
453
|
-
NgHttpCachingService,
|
|
454
|
-
{
|
|
455
|
-
provide: HTTP_INTERCEPTORS,
|
|
456
|
-
useClass: NgHttpCachingInterceptorService,
|
|
457
|
-
multi: true,
|
|
458
|
-
},
|
|
459
|
-
]
|
|
460
|
-
}]
|
|
461
|
-
}] });
|
|
462
|
-
|
|
463
|
-
const KEY_PREFIX = 'NgHttpCaching::';
|
|
464
|
-
const serializeRequest = (req) => {
|
|
465
|
-
const request = req.clone(); // Make a clone, useful for doing destructive things
|
|
466
|
-
return JSON.stringify({
|
|
467
|
-
headers: Object.fromEntries(// Just a helper to make this into an object, not really required but makes the output nicer
|
|
468
|
-
request.headers.keys().map(// Get all of the headers
|
|
469
|
-
(key) => [key, request.headers.getAll(key)] // Get all of the corresponding values for the headers
|
|
470
|
-
)),
|
|
471
|
-
method: request.method,
|
|
472
|
-
url: request.url,
|
|
473
|
-
params: Object.fromEntries(// Just a helper to make this into an object, not really required but makes the output nicer
|
|
474
|
-
request.headers.keys().map(// Get all of the headers
|
|
475
|
-
(key) => [key, request.headers.getAll(key)] // Get all of the corresponding values for the headers
|
|
476
|
-
)),
|
|
477
|
-
withCredentials: request.withCredentials,
|
|
478
|
-
respnseType: request.responseType,
|
|
479
|
-
body: request.serializeBody() // Serialize the body, all well and good since we are working on a clone
|
|
480
|
-
});
|
|
481
|
-
};
|
|
482
|
-
const serializeResponse = (res) => {
|
|
483
|
-
const response = res.clone();
|
|
484
|
-
return JSON.stringify({
|
|
485
|
-
headers: Object.fromEntries(// Just a helper to make this into an object, not really required but makes the output nicer
|
|
486
|
-
response.headers.keys().map(// Get all of the headers
|
|
487
|
-
(key) => [key, response.headers.getAll(key)] // Get all of the corresponding values for the headers
|
|
488
|
-
)),
|
|
489
|
-
status: response.status,
|
|
490
|
-
statusText: response.statusText,
|
|
491
|
-
url: response.url,
|
|
492
|
-
body: response.body // Serialize the body, all well and good since we are working on a clone
|
|
493
|
-
});
|
|
494
|
-
};
|
|
495
|
-
const deserializeRequest = (req) => {
|
|
496
|
-
const request = JSON.parse(req);
|
|
497
|
-
const headers = new HttpHeaders(request.headers);
|
|
498
|
-
const params = new HttpParams(); // Probably some way to make this a one-liner, but alas, there are no good docs
|
|
499
|
-
for (const parameter in request.params) {
|
|
500
|
-
request.params[parameter].forEach((paramValue) => params.append(parameter, paramValue));
|
|
501
|
-
}
|
|
502
|
-
return new HttpRequest(request.method, request.url, request.body, {
|
|
503
|
-
headers,
|
|
504
|
-
params,
|
|
505
|
-
responseType: request.responseType,
|
|
506
|
-
withCredentials: request.withCredentials
|
|
507
|
-
});
|
|
508
|
-
};
|
|
509
|
-
const deserializeResponse = (res) => {
|
|
510
|
-
const response = JSON.parse(res);
|
|
511
|
-
return new HttpResponse({
|
|
512
|
-
url: response.url,
|
|
513
|
-
headers: new HttpHeaders(response.headers),
|
|
514
|
-
body: response.body,
|
|
515
|
-
status: response.status,
|
|
516
|
-
statusText: response.statusText,
|
|
517
|
-
});
|
|
518
|
-
};
|
|
519
|
-
class NgHttpCachingBrowserStorage {
|
|
520
|
-
constructor(storage) {
|
|
521
|
-
this.storage = storage;
|
|
522
|
-
}
|
|
523
|
-
get size() {
|
|
524
|
-
let count = 0;
|
|
525
|
-
for (let i = 0, e = this.storage.length; i < e; i++) {
|
|
526
|
-
const key = this.storage.key(i);
|
|
527
|
-
if (key && key.startsWith(KEY_PREFIX)) {
|
|
528
|
-
count++;
|
|
529
|
-
}
|
|
530
|
-
}
|
|
531
|
-
return count;
|
|
532
|
-
}
|
|
533
|
-
clear() {
|
|
534
|
-
for (let i = 0, e = this.storage.length; i < e; i++) {
|
|
535
|
-
const key = this.storage.key(i);
|
|
536
|
-
if (key && key.startsWith(KEY_PREFIX)) {
|
|
537
|
-
this.storage.removeItem(key);
|
|
538
|
-
}
|
|
539
|
-
}
|
|
540
|
-
}
|
|
541
|
-
delete(key) {
|
|
542
|
-
this.storage.removeItem(KEY_PREFIX + key);
|
|
543
|
-
return true;
|
|
544
|
-
}
|
|
545
|
-
forEach(callbackfn) {
|
|
546
|
-
// iterate this.storage
|
|
547
|
-
const lenPrefix = KEY_PREFIX.length;
|
|
548
|
-
for (let i = 0, e = this.storage.length; i < e; i++) {
|
|
549
|
-
const key = this.storage.key(i);
|
|
550
|
-
if (key && key.startsWith(KEY_PREFIX)) {
|
|
551
|
-
const value = this.get(key.substring(lenPrefix));
|
|
552
|
-
if (value) {
|
|
553
|
-
callbackfn(value, key);
|
|
554
|
-
}
|
|
555
|
-
}
|
|
556
|
-
}
|
|
557
|
-
}
|
|
558
|
-
get(key) {
|
|
559
|
-
const item = this.storage.getItem(KEY_PREFIX + key);
|
|
560
|
-
if (item) {
|
|
561
|
-
const parsedItem = JSON.parse(item);
|
|
562
|
-
return {
|
|
563
|
-
url: parsedItem.url,
|
|
564
|
-
response: deserializeResponse(parsedItem.response),
|
|
565
|
-
request: deserializeRequest(parsedItem.request),
|
|
566
|
-
addedTime: parsedItem.addedTime,
|
|
567
|
-
version: parsedItem.version
|
|
568
|
-
};
|
|
569
|
-
}
|
|
570
|
-
return undefined;
|
|
571
|
-
}
|
|
572
|
-
has(key) {
|
|
573
|
-
return this.storage.getItem(KEY_PREFIX + key) !== undefined;
|
|
574
|
-
}
|
|
575
|
-
set(key, value) {
|
|
576
|
-
this.storage.setItem(KEY_PREFIX + key, JSON.stringify({
|
|
577
|
-
url: value.url,
|
|
578
|
-
response: serializeResponse(value.response),
|
|
579
|
-
request: serializeRequest(value.request),
|
|
580
|
-
addedTime: value.addedTime
|
|
581
|
-
}));
|
|
582
|
-
}
|
|
583
|
-
}
|
|
584
|
-
|
|
585
|
-
class NgHttpCachingLocalStorage extends NgHttpCachingBrowserStorage {
|
|
586
|
-
constructor() {
|
|
587
|
-
super(localStorage);
|
|
588
|
-
}
|
|
589
|
-
}
|
|
590
|
-
|
|
591
|
-
class NgHttpCachingSessionStorage extends NgHttpCachingBrowserStorage {
|
|
592
|
-
constructor() {
|
|
593
|
-
super(sessionStorage);
|
|
594
|
-
}
|
|
595
|
-
}
|
|
596
|
-
|
|
597
|
-
/*
|
|
598
|
-
* Public API Surface of ng-http-caching
|
|
599
|
-
*/
|
|
600
|
-
|
|
601
|
-
/**
|
|
602
|
-
* Generated bundle index. Do not edit.
|
|
603
|
-
*/
|
|
604
|
-
|
|
605
|
-
export { NG_HTTP_CACHING_CONFIG, NG_HTTP_CACHING_DAY_IN_MS, NG_HTTP_CACHING_HOUR_IN_MS, NG_HTTP_CACHING_MINUTE_IN_MS, NG_HTTP_CACHING_MONTH_IN_MS, NG_HTTP_CACHING_SECOND_IN_MS, NG_HTTP_CACHING_WEEK_IN_MS, NG_HTTP_CACHING_YEAR_IN_MS, NgHttpCachingBrowserStorage, NgHttpCachingConfigDefault, NgHttpCachingHeaders, NgHttpCachingHeadersList, NgHttpCachingInterceptorService, NgHttpCachingLocalStorage, NgHttpCachingMemoryStorage, NgHttpCachingModule, NgHttpCachingService, NgHttpCachingSessionStorage, NgHttpCachingStrategy, deserializeRequest, deserializeResponse, serializeRequest, serializeResponse };
|
|
606
|
-
//# sourceMappingURL=ng-http-caching.mjs.map
|