UNPKG

@graphql-mesh/cache-cfw-kv

Version:
38 lines (37 loc) 1.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const promise_helpers_1 = require("@whatwg-node/promise-helpers"); class CFWorkerKVCache { constructor(config) { if (typeof config.namespace === 'string') { this.kvNamespace = globalThis[config.namespace]; } else { this.kvNamespace = 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.`); } } get(key) { return this.kvNamespace?.get(key, 'json'); } getKeysByPrefix(prefix) { return (0, promise_helpers_1.handleMaybePromise)(() => this.kvNamespace?.list({ prefix }), result => { if (!result) { return []; } return result.keys.map(keyEntry => keyEntry.name); }); } set(key, value, options) { return this.kvNamespace?.put(key, JSON.stringify(value), { expirationTtl: options?.ttl, }); } delete(key) { return (0, promise_helpers_1.handleMaybePromise)(() => this.kvNamespace?.delete(key), () => true, () => false); } } exports.default = CFWorkerKVCache;