Schematic Cloudflare Node
Cloudflare KV cache adapter for Schematic SDK.
Installation
npm install @schematichq/schematic-typescript-cloudflare
Usage
With Schematic SDK
import { SchematicClient } from "@schematichq/schematic-typescript-node";
import { CloudflareKVCache } from "@schematichq/schematic-typescript-cloudflare";
export default {
async fetch(request, env, ctx) {
const cache = new CloudflareKVCache<boolean>(env.MY_KV_NAMESPACE, {
ttl: 1000 * 60 * 60,
});
const schematic = new SchematicClient({
apiKey: env.SCHEMATIC_API_KEY,
cacheProviders: {
flagChecks: [cache],
}});
}
};
API
KVNamespace Interface
interface KVNamespace {
get<T>(key: string, type: 'json'): Promise<T | null>;
put(key: string, value: string, options?: { expirationTtl?: number }): Promise<void>;
delete(key: string): Promise<void>;
list(options?: { prefix?: string }): Promise<{ keys: Array<{ name: string }> }>;
}
The interface matches the Cloudflare Workers KV API, including support for typed get<T>() operations for enhanced type safety.
CloudflareKVCache
class CloudflareKVCache<T> {
constructor(
kvNamespace: KVNamespace,
options?: {
keyPrefix?: string; // Default: 'schematic:'
ttl?: number; // Default: 5000ms
}
);
get(key: string): Promise<T | undefined>;
set(key: string, value: T, ttlOverride?: number): Promise<void>;
delete(key: string): Promise<void>;
deleteAllExcept(keysToKeep: string[]): Promise<void>;
clear(): Promise<void>;
}
License
MIT