Skip to content

Commit 9dc00e9

Browse files
committed
修复类型错误
1 parent e0e3025 commit 9dc00e9

2 files changed

Lines changed: 15 additions & 15 deletions

File tree

index.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,20 @@
1414
cache.match(new Request(`https://${this.prefix}/${encodeURIComponent(key)}`))
1515
.then(response => resolve(response ? true : false))
1616
}).catch(err => {
17-
console.error('CacheDB Has Erorr:' + err);
17+
console.error(`CacheDB Has Erorr: Key "${key}" ${err}`);
1818
reject(false)
1919
});
2020
})
2121
}
2222
this.read = async function (key, config) {
2323
config = config || {};
24-
config.type = config.type || (this.config.auto ? "auto" : "text");
24+
config.type = (config.type || (this.config.auto ? "auto" : "text")).toLowerCase();
2525
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);
2828
return config.default;
2929
} 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.`);
3131
return null;
3232
}
3333
}
@@ -75,7 +75,7 @@
7575
resolve(response.text());
7676
return;
7777
case 'boolean':
78-
resolve(await response.text() == 1);
78+
resolve(await response.text() === "1");
7979
return;
8080
default:
8181
resolve(response.body);
@@ -84,7 +84,7 @@
8484
})
8585
})
8686
.catch(err => {
87-
console.error('CacheDB Read Erorr:' + err);
87+
console.error(`CacheDB Read Erorr: Key "${key}" ${err}`);
8888
reject(null);
8989
});
9090
})
@@ -93,7 +93,7 @@
9393

9494
this.write = async function (key, value, config) {
9595
config = config || {};
96-
config.type = config.type || (this.config.auto ? "auto" : "text");
96+
config.type = (config.type || (this.config.auto ? "auto" : "text")).toLowerCase();
9797
if (config.type === 'auto')
9898
config.type = ifObjectisJSON(value) ? 'json' : typeof value;
9999
switch (config.type) {
@@ -126,7 +126,7 @@
126126
headers: { 'Content-Type': config.content_type }
127127
})).then(resolve(1))
128128
}).catch(err => {
129-
console.error('CacheDB Write Erorr:' + err);
129+
console.error(`CacheDB Write Erorr: Key "${key}" ${err}`);
130130
reject(0)
131131
});
132132
})
@@ -139,7 +139,7 @@
139139
.then(resolve(true))
140140
})
141141
.catch(err => {
142-
console.error('CacheDB Delete Erorr:' + err);
142+
console.error(`CacheDB Delete Erorr: Key "${key}" ${err}`);
143143
reject(false)
144144
});
145145
})
@@ -152,7 +152,7 @@
152152
resolve(keys.map(key => decodeURIComponent(key.url.split('/').pop())));
153153
})
154154
}).catch(err => {
155-
console.error('CacheDB List Erorr:' + err);
155+
console.error(`CacheDB List Erorr: ${err}`);
156156
reject([])
157157
});
158158
})
@@ -167,7 +167,7 @@
167167
}))
168168
resolve(data);
169169
}).catch(err => {
170-
console.error('CacheDB All Erorr:' + err);
170+
console.error(`CacheDB All Erorr: ${err}`);
171171
reject([])
172172
});
173173
})
@@ -181,7 +181,7 @@
181181
})
182182
resolve(true);
183183
}).catch(err => {
184-
console.error('CacheDB Clear Erorr:' + err);
184+
console.error(`CacheDB Clear Erorr: ${err}`);
185185
reject(false)
186186
});
187187
})
@@ -192,7 +192,7 @@
192192
this.clear().then(() => {
193193
caches.delete(this.namespace).then(resolve(true));
194194
}).catch(err => {
195-
console.error('CacheDB Destroy Erorr:' + err);
195+
console.error(`CacheDB Destroy Erorr: ${err}`);
196196
reject(false)
197197
});
198198
})

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@chenyfan/cache-db",
3-
"version": "1.1.5",
3+
"version": "1.1.6",
44
"description": "A common Key/Value database working on both DOM/WebWorker with auto type convert, based on CacheStorage.",
55
"main": "./dist/index.min.js",
66
"type": "module",

0 commit comments

Comments
 (0)