38 lines
968 B
TypeScript
38 lines
968 B
TypeScript
|
import * as interfaces from './interfaces/index.js';
|
||
|
import * as plugins from './plugins.js';
|
||
|
|
||
|
declare var lokv: plugins.cloudflareTypes.KVNamespace;
|
||
|
|
||
|
/**
|
||
|
* an abstraction for the workerd KV store
|
||
|
*/
|
||
|
export class KVHandler {
|
||
|
private getSafeIdentifier(urlString: string) {
|
||
|
return encodeURI(urlString);
|
||
|
}
|
||
|
|
||
|
async getFromKv(keyIdentifier: string) {
|
||
|
const key = this.getSafeIdentifier(keyIdentifier);
|
||
|
const valueString = await lokv.get(key);
|
||
|
return valueString;
|
||
|
}
|
||
|
|
||
|
async putInKv(keyIdentifier: string, valueForStorage: string) {
|
||
|
const key = this.getSafeIdentifier(keyIdentifier);
|
||
|
const value = valueForStorage;
|
||
|
await lokv.put(key, value);
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* deletes a key/value from the cache
|
||
|
* @param keyIdentifier
|
||
|
*/
|
||
|
async deleteInKv(keyIdentifier: string) {
|
||
|
const cacheKey = this.getSafeIdentifier(keyIdentifier);
|
||
|
await lokv.delete(cacheKey);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export const kvHandlerInstance = new KVHandler();
|