UNPKG

@graphql-mesh/cache-cfw-kv

Version:
33 lines (32 loc) 1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); class CFWorkerKVCache { constructor(config) { this.kvNamespace = globalThis[config.namespace]; if (this.kvNamespace === undefined) { // We don't use mocks because they increase the bundle size. config.logger?.warn(`Make sure KV Namespace: ${config.namespace} exists.`); } } async get(key) { return this.kvNamespace?.get(key, 'json'); } async getKeysByPrefix(prefix) { const result = await this.kvNamespace?.list({ prefix, }); if (!result) { return []; } return result.keys.map(keyEntry => keyEntry.name); } async set(key, value, options) { return this.kvNamespace?.put(key, JSON.stringify(value), { expirationTtl: options?.ttl, }); } async delete(key) { return this.kvNamespace?.delete(key); } } exports.default = CFWorkerKVCache;