UNPKG

@variablesoftware/mock-kv

Version:

🎛️🏷️✨ Mock KV Namespace for testing Cloudflare Workers

27 lines (26 loc) 959 B
import logface from "@variablesoftware/logface"; export function getWithMetadataHandler(data) { return async (key, opts) => { const entry = data[key]; const now = Date.now(); logface.debug('getWithMetadata("%s") → checking entry', key); if (!entry || (entry.expiresAt !== undefined && entry.expiresAt < now)) { logface.debug('getWithMetadata("%s") → expired or missing', key); delete data[key]; return null; } const raw = entry.value; let value = raw; if (opts?.type === "json") { try { value = JSON.parse(raw); logface.debug('getWithMetadata("%s") → parsed JSON', key); } catch { logface.warn('getWithMetadata("%s") → invalid JSON', key); value = null; } } return { value, metadata: entry.metadata ?? null }; }; }