1 | import { Models } from './models';
|
2 | import { Log, Level } from 'ng2-logger/browser';
|
3 | import { Helpers } from 'ng2-logger/helper';
|
4 | import { RestHeaders } from './rest-headers';
|
5 | const log = Log.create('request-cache', Level.__NOTHING);
|
6 | export class RequestCache {
|
7 | constructor(response) {
|
8 | this.response = response;
|
9 | }
|
10 | static restoreFromLocalStorage() {
|
11 | if (Helpers.isNode) {
|
12 | return;
|
13 | }
|
14 | if (!RequestCache.isRestoredFromLocalStorage) {
|
15 | RequestCache.isRestoredFromLocalStorage = true;
|
16 | const data = localStorage.getItem(RequestCache.LOCAL_STORAGE_KEY);
|
17 | let requests = [];
|
18 | if (data) {
|
19 | try {
|
20 | requests = JSON.parse(data);
|
21 | }
|
22 | catch (error) {
|
23 | }
|
24 | const restored = requests.map(r => {
|
25 | let { sourceRequest, responseText, body, headers, circular, entity, isArray, cookies, statusCode } = r.response;
|
26 | r.response = new Models.HttpResponse(sourceRequest, responseText, RestHeaders.from(headers), statusCode, entity, circular, isArray);
|
27 | r = new RequestCache(r.response);
|
28 | r.response.rq = r;
|
29 | return r;
|
30 | });
|
31 | log.i('RESTORED FROM LOCAL STORAGE', restored);
|
32 | RequestCache.cached = restored;
|
33 | }
|
34 | }
|
35 | }
|
36 | static findBy(sourceRequest) {
|
37 | log.i('findby', sourceRequest);
|
38 | log.i('RequestCache.cached', RequestCache.cached);
|
39 | RequestCache.restoreFromLocalStorage();
|
40 | return RequestCache.cached.find(c => {
|
41 | const a = c.response.sourceRequest;
|
42 | const b = sourceRequest;
|
43 | return (a.isArray === b.isArray &&
|
44 | a.url === b.url &&
|
45 | a.method === b.method &&
|
46 | a.body === b.body);
|
47 | });
|
48 | }
|
49 | get containsCache() {
|
50 | RequestCache.restoreFromLocalStorage();
|
51 | return RequestCache.cached.includes(this);
|
52 | }
|
53 | persistsInLocalStorage() {
|
54 | localStorage.setItem(RequestCache.LOCAL_STORAGE_KEY, JSON.stringify(RequestCache.cached.map(r => {
|
55 | return {
|
56 | response: {
|
57 | sourceRequest: r.response.sourceRequest,
|
58 | responseText: r.response.responseText,
|
59 | headers: r.response.headers,
|
60 | statusCode: r.response.statusCode,
|
61 | entity: r.response.entity,
|
62 | circular: r.response.circular,
|
63 | isArray: r.response.isArray,
|
64 | }
|
65 | };
|
66 | })));
|
67 | }
|
68 | store() {
|
69 | RequestCache.restoreFromLocalStorage();
|
70 | if (!this.containsCache) {
|
71 | RequestCache.cached.push(this);
|
72 | this.persistsInLocalStorage();
|
73 | }
|
74 | else {
|
75 | console.log('already stored');
|
76 | }
|
77 | return this;
|
78 | }
|
79 | remove() {
|
80 | RequestCache.restoreFromLocalStorage();
|
81 | const index = RequestCache.cached.indexOf(this);
|
82 | if (index !== -1) {
|
83 | RequestCache.cached.splice(index, 1);
|
84 | this.persistsInLocalStorage();
|
85 | }
|
86 | else {
|
87 | console.log('already removed');
|
88 | }
|
89 | }
|
90 | }
|
91 | RequestCache.LOCAL_STORAGE_KEY = 'ng2restrequestcache';
|
92 | RequestCache.cached = [];
|
93 | RequestCache.isRestoredFromLocalStorage = false;
|
94 |
|
\ | No newline at end of file |