UNPKG

staticql

Version:

Type-safe query engine for static content including Markdown, YAML, JSON, and more.

91 lines (90 loc) 2.64 kB
import { StorageRepository } from "./StorageRepository.js"; /** * R2-compatible bucket interface (Cloudflare Workers binding). */ export interface R2Bucket { put(key: string, value: string | ArrayBuffer | ReadableStream): Promise<void>; get(key: string): Promise<R2ObjectBody | null>; delete(key: string): Promise<void>; list(options?: { prefix?: string; }): Promise<R2Objects>; } /** * Represents an object retrieved from R2. */ export interface R2ObjectBody { body: ReadableStream; text(): Promise<string>; arrayBuffer(): Promise<ArrayBuffer>; } /** * Represents the result of listing objects in an R2 bucket. */ export interface R2Objects { objects: { key: string; }[]; } /** * R2Repository: A StorageRepository implementation for Cloudflare R2. */ export declare class R2Repository implements StorageRepository { private bucket; private prefix?; constructor(bucket: R2Bucket, prefix?: string | undefined); /** * Adds a namespace prefix to keys (if specified). */ private buildKey; /** * Lists file paths in the R2 bucket under a given prefix. * * Supports wildcard patterns by trimming after `*`. * * @param prefix - Path prefix or glob (e.g. "content/*.md"). * @returns Sorted list of matching object keys. */ listFiles(prefix: string): Promise<string[]>; /** * Reads the content of a file from R2. * * @param path - Key within the bucket. * @returns File content as string; empty string if not found. */ readFile(path: string): Promise<string>; /** * Opens a file as a ReadableStream from Cloudflare R2. * * @param path - Key within the bucket. * @returns ReadableStream of the file contents. * @throws Error if the object does not exist. */ openFileStream(path: string): Promise<ReadableStream>; /** * Writes data to the R2 bucket. * * @param path - Key to write. * @param data - Content to write. */ writeFile(path: string, data: string): Promise<void>; /** * Checks if the specified file exists in the R2 bucket. * * @param path - Key to check. * @returns `true` if it exists, `false` otherwise. */ exists(path: string): Promise<boolean>; /** * Deletes the specified file from the R2 bucket. * * @param path - Key to delete. */ removeFile(path: string): Promise<void>; /** * Deletes the specified file from the R2 bucket. * * @param path - Key to delete. */ removeDir(path: string): Promise<void>; }