Documents and folders with the TypeScript SDK
Add text, list, read and download documents, change them, and follow the work a write starts.
import { ByteRover } from "@byterover/sdk";
const client = new ByteRover();
const driveId = "drv_4h7k2m9q8r1t5v6w3x0y";Add a small text document
Text up to 256 KiB goes in one call. A file of any other kind goes through an upload. A document already at the path gets a new revision.
const { document, operation } = await client.documents.putText(driveId, {
path: "/policies/customer-refund-policy.md",
text: "# Refunds\n\n## European Union\n\nCustomers in the European Union may cancel an order within 14 days of delivery and receive a full refund.",
tags: ["policy"],
});Wait for the work a write started
A write returns an operation, which tracks the work of making the file searchable. client.operations.wait polls it, at the pace the API asks for, until it is done.
import { OperationFailedError, WaitTimeoutError } from "@byterover/sdk";
try {
const done = await client.operations.wait(operation.id, { timeout: 300_000 });
console.log(done.status);
} catch (error) {
if (error instanceof OperationFailedError) console.log(error.code, error.operation.error);
else if (error instanceof WaitTimeoutError) console.log("Still running; wait again for", error.operation_id);
else throw error;
}It waits 10 minutes by default. When the time runs out, the operation goes on; wait for it again by its ID. A poll that fails on the way, such as a 504 while a big file is processed, is asked again at the next poll.
List the documents of a drive
client.documents.list goes through every page for you in a for await loop. Without the loop, await gives you one page.
for await (const listed of client.documents.list(driveId, { folder: "/policies", sort: "updated", order: "desc" })) {
console.log(listed.path, listed.status);
}
const firstPage = await client.documents.list(driveId, { limit: 200 });
console.log(firstPage.documents.length, firstPage.next_cursor);folder lists only the documents directly in one folder, name matches part of a file name, and status (in_ingest or failed) finds the documents that aren't searchable yet. See Pagination and cancelling.
One document and its revisions:
const policy = await client.documents.retrieve(document.id);
const { revisions } = await client.documents.listRevisions(document.id);
console.log(policy.revision, revisions.length);Read the text
client.documents.read returns the text of one part of a document: a page, slide, sheet, heading_path or byte_range, exactly one of them. It's the next call after a search, with the result's document_id and one selector from its location, such as its page or heading_path. See Read a document.
const page = await client.documents.read("doc_7n4p1s8v2x5z9b3d6f0h", { page: 6 });
const section = await client.documents.read("doc_7n4p1s8v2x5z9b3d6f0h", {
heading_path: ["Refunds", "European Union"],
max_chars: 8_000,
});
console.log(page.text, section.truncated);Download the original file
client.documents.content streams the file as it was uploaded. Read it whole with bytes() or text(), or chunk by chunk with for await.
import { createWriteStream } from "node:fs";
import { Readable } from "node:stream";
import { pipeline } from "node:stream/promises";
const original = await client.documents.content("doc_7n4p1s8v2x5z9b3d6f0h", { revision: 3 });
await pipeline(Readable.from(original), createWriteStream("customer-refund-policy.pdf"));If the connection drops, or nothing arrives for the call's timeout, the SDK asks for the rest of the file with a Range request tied to the file's ETag. If the file changed in the meantime, the download fails instead of joining two versions. Pass range for part of a large file.
Change, copy and delete
const moved = await client.documents.update(document.id, {
path: "/policies/eu/customer-refund-policy.md",
tags: ["policy", "eu"],
expected_version: policy.version,
});
const copy = await client.documents.copy(document.id, { drive_id: driveId, path: "/archive/customer-refund-policy.md" });
const removal = await client.documents.delete(copy.document.id);
await client.operations.wait(removal.id);
console.log(moved.document.path);expected_version makes a change fail with 409 VERSION_MISMATCH if the document changed since you read it. move: true on a copy moves the document into another drive instead. To rename or move a document within its drive, change its path with documents.update(id, { path }); the API refuses a same-drive move with 400.
Folders
await client.folders.create(driveId, { path: "/policies/drafts" });
const { folders } = await client.folders.list(driveId);
await client.folders.move(driveId, { from: "/policies/drafts", to: "/policies/old-drafts" });
await client.folders.delete(driveId, { path: "/policies/old-drafts" });
console.log(folders.map((folder) => folder.path));folders.create also makes any parent folder that is missing, so /policies/drafts makes /policies too. folders.delete deletes a folder only when no document is in it or in a folder below it, and removes its empty subfolders with it; otherwise it throws a ConflictError with code FOLDER_NOT_EMPTY (409). Delete or move the documents first.