// All types here REFERENCE @cloudflare/workers-types — they don't redefine the bindings. // They ADD type parameters that the raw types lack. // --- Typed KV --- /** A KV namespace with typed values. Wraps KVNamespace from @cloudflare/workers-types. */ export interface TypedKVNamespace { /** The underlying untyped KV binding */ readonly raw: KVNamespace; get(key: string): Promise; getWithMetadata(key: string): Promise<{ value: T | null; metadata: M | null }>; put(key: string, value: T, options?: KVNamespacePutOptions): Promise; delete(key: string): Promise; list(options?: KVNamespaceListOptions): Promise>; } // --- Typed D1 --- /** Type-safe D1 query result */ export interface TypedD1Result { results: T[]; success: boolean; meta: D1Meta; error?: string; } /** D1 meta information */ export interface D1Meta { changed_db: boolean; changes: number; duration: number; last_row_id: number; rows_read: number; rows_written: number; size_after: number; } // --- Typed R2 --- /** R2 object with typed custom metadata */ export interface TypedR2Object = Record> { key: string; version: string; size: number; etag: string; httpEtag: string; uploaded: Date; httpMetadata?: R2HTTPMetadata; customMetadata: M; checksums: R2Checksums; } /** R2 get result — body + metadata */ export interface TypedR2ObjectBody = Record> extends TypedR2Object { body: ReadableStream; bodyUsed: boolean; arrayBuffer(): Promise; text(): Promise; json(): Promise; blob(): Promise; } // --- R2 supporting types --- export interface R2HTTPMetadata { contentType?: string; contentLanguage?: string; contentDisposition?: string; contentEncoding?: string; cacheControl?: string; cacheExpiry?: Date; } export interface R2Checksums { md5?: ArrayBuffer; sha1?: ArrayBuffer; sha256?: ArrayBuffer; sha384?: ArrayBuffer; sha512?: ArrayBuffer; } // --- Typed Queue --- /** A typed queue producer */ export interface TypedQueue { send(body: Body, options?: QueueSendOptions): Promise; sendBatch( messages: Iterable>, options?: QueueSendBatchOptions, ): Promise; } /** A typed message for sendBatch */ export interface TypedMessageSendRequest { body: Body; contentType?: QueueContentType; delaySeconds?: number; } /** A typed message received from a queue */ export interface TypedMessage { readonly id: string; readonly timestamp: Date; readonly body: Body; readonly attempts: number; ack(): void; retry(options?: QueueRetryOptions): void; } /** A typed message batch */ export interface TypedMessageBatch { readonly queue: string; readonly messages: readonly TypedMessage[]; ackAll(): void; retryAll(options?: QueueRetryOptions): void; } // --- Queue supporting types --- export interface QueueSendOptions { contentType?: QueueContentType; delaySeconds?: number; } export interface QueueSendBatchOptions { delaySeconds?: number; } export interface QueueRetryOptions { delaySeconds?: number; } export type QueueContentType = "text" | "bytes" | "json" | "v8"; // --- Typed DO Storage --- /** Typed Durable Object storage interface */ export interface TypedDurableObjectStorage { get(key: string): Promise; get(keys: string[]): Promise>; put(key: string, value: T): Promise; put(entries: Record): Promise; delete(key: string): Promise; delete(keys: string[]): Promise; deleteAll(): Promise; list(options?: DurableObjectStorageListOptions): Promise>; transaction(closure: (txn: TypedDurableObjectStorage) => Promise): Promise; getAlarm(): Promise; setAlarm(scheduledTime: number | Date): Promise; deleteAlarm(): Promise; } export interface DurableObjectStorageListOptions { start?: string; startAfter?: string; end?: string; prefix?: string; reverse?: boolean; limit?: number; allowConcurrency?: boolean; noCache?: boolean; } // --- KV supporting types needed by TypedKVNamespace --- export interface KVNamespacePutOptions { expiration?: number; expirationTtl?: number; metadata?: unknown; } export interface KVNamespaceListOptions { prefix?: string; limit?: number; cursor?: string; } export interface KVNamespaceListResult { keys: KVNamespaceListKey[]; list_complete: boolean; cursor?: string; cacheStatus: string | null; } export interface KVNamespaceListKey { name: string; expiration?: number; metadata?: T; }