|
14 | 14 | cache.match(new Request(`https://${this.prefix}/${encodeURIComponent(key)}`)) |
15 | 15 | .then(response => resolve(response ? true : false)) |
16 | 16 | }).catch(err => { |
17 | | - console.error('CacheDB Has Erorr:' + err); |
| 17 | + console.error(`CacheDB Has Erorr: Key "${key}" ${err}`); |
18 | 18 | reject(false) |
19 | 19 | }); |
20 | 20 | }) |
21 | 21 | } |
22 | 22 | this.read = async function (key, config) { |
23 | 23 | config = config || {}; |
24 | | - config.type = config.type || (this.config.auto ? "auto" : "text"); |
| 24 | + config.type = (config.type || (this.config.auto ? "auto" : "text")).toLowerCase(); |
25 | 25 | if (!await this.has(key)) { |
26 | | - if (config.default) { |
27 | | - await this.write(key,config.default, config); |
| 26 | + if (typeof config.default !== 'undefined') { |
| 27 | + await this.write(key, config.default, config); |
28 | 28 | return config.default; |
29 | 29 | } else { |
30 | | - console.error('CacheDB Read Erorr: Key not found'); |
| 30 | + console.error(`CacheDB Read Erorr: Key "${key}" not found and no default value provided.`); |
31 | 31 | return null; |
32 | 32 | } |
33 | 33 | } |
|
75 | 75 | resolve(response.text()); |
76 | 76 | return; |
77 | 77 | case 'boolean': |
78 | | - resolve(await response.text() == 1); |
| 78 | + resolve(await response.text() === "1"); |
79 | 79 | return; |
80 | 80 | default: |
81 | 81 | resolve(response.body); |
|
84 | 84 | }) |
85 | 85 | }) |
86 | 86 | .catch(err => { |
87 | | - console.error('CacheDB Read Erorr:' + err); |
| 87 | + console.error(`CacheDB Read Erorr: Key "${key}" ${err}`); |
88 | 88 | reject(null); |
89 | 89 | }); |
90 | 90 | }) |
|
93 | 93 |
|
94 | 94 | this.write = async function (key, value, config) { |
95 | 95 | config = config || {}; |
96 | | - config.type = config.type || (this.config.auto ? "auto" : "text"); |
| 96 | + config.type = (config.type || (this.config.auto ? "auto" : "text")).toLowerCase(); |
97 | 97 | if (config.type === 'auto') |
98 | 98 | config.type = ifObjectisJSON(value) ? 'json' : typeof value; |
99 | 99 | switch (config.type) { |
|
126 | 126 | headers: { 'Content-Type': config.content_type } |
127 | 127 | })).then(resolve(1)) |
128 | 128 | }).catch(err => { |
129 | | - console.error('CacheDB Write Erorr:' + err); |
| 129 | + console.error(`CacheDB Write Erorr: Key "${key}" ${err}`); |
130 | 130 | reject(0) |
131 | 131 | }); |
132 | 132 | }) |
|
139 | 139 | .then(resolve(true)) |
140 | 140 | }) |
141 | 141 | .catch(err => { |
142 | | - console.error('CacheDB Delete Erorr:' + err); |
| 142 | + console.error(`CacheDB Delete Erorr: Key "${key}" ${err}`); |
143 | 143 | reject(false) |
144 | 144 | }); |
145 | 145 | }) |
|
152 | 152 | resolve(keys.map(key => decodeURIComponent(key.url.split('/').pop()))); |
153 | 153 | }) |
154 | 154 | }).catch(err => { |
155 | | - console.error('CacheDB List Erorr:' + err); |
| 155 | + console.error(`CacheDB List Erorr: ${err}`); |
156 | 156 | reject([]) |
157 | 157 | }); |
158 | 158 | }) |
|
167 | 167 | })) |
168 | 168 | resolve(data); |
169 | 169 | }).catch(err => { |
170 | | - console.error('CacheDB All Erorr:' + err); |
| 170 | + console.error(`CacheDB All Erorr: ${err}`); |
171 | 171 | reject([]) |
172 | 172 | }); |
173 | 173 | }) |
|
181 | 181 | }) |
182 | 182 | resolve(true); |
183 | 183 | }).catch(err => { |
184 | | - console.error('CacheDB Clear Erorr:' + err); |
| 184 | + console.error(`CacheDB Clear Erorr: ${err}`); |
185 | 185 | reject(false) |
186 | 186 | }); |
187 | 187 | }) |
|
192 | 192 | this.clear().then(() => { |
193 | 193 | caches.delete(this.namespace).then(resolve(true)); |
194 | 194 | }).catch(err => { |
195 | | - console.error('CacheDB Destroy Erorr:' + err); |
| 195 | + console.error(`CacheDB Destroy Erorr: ${err}`); |
196 | 196 | reject(false) |
197 | 197 | }); |
198 | 198 | }) |
|
0 commit comments