npm.io
0.1.0 • Published yesterday

@devstorage/node

Licence
MIT
Version
0.1.0
Deps
0
Size
52 kB
Vulns
0
Weekly
0

@devstorage/node

Official DevStorage SDK. Works in Node.js 18+, browsers, and edge runtimes.

npm install @devstorage/node

Quick start

import { DevStorage } from "@devstorage/node";
import { readFile } from "node:fs/promises";

const ds = new DevStorage({
  apiKey: process.env.DSK_API_KEY!,
});

const buffer = await readFile("./photo.jpg");

const file = await ds.upload(buffer, {
  name: "photo.jpg",
  mimeType: "image/jpeg",
  metadata: { orderId: "ord_123" },
});

console.log(file.url); // presigned R2 URL
console.log(file.id);  // "fil_01HXYZ..."

API

new DevStorage(options)
Option Type Description
apiKey string (required) Project API key (dsk_live_...) from your dashboard.
baseUrl string Override the API host. Defaults to https://api.devstorage.io.
fetch typeof fetch Inject a custom fetch implementation (useful for tests).
ds.upload(body, options) → Promise<UploadedFile>

Uploads a file directly to Cloudflare R2 via a presigned URL, then confirms the upload server-side.

body accepts Buffer, Uint8Array, ArrayBuffer, or Blob.

Option Type Description
name string (required) File name shown in your dashboard.
mimeType string (required) Content type sent to R2 and recorded against the file.
metadata Record<string, string> Arbitrary key/value pairs returned with the file.
signal AbortSignal Cancel the upload (and the underlying PUT) at any point.

If the PUT to R2 fails the SDK calls /v1/upload/abort for you before throwing.

ds.files.list(options?) → Promise<{ files, nextCursor, hasMore }>

Cursor-paginated. Pass the previous response's nextCursor to fetch the next page.

ds.files.get(id) → Promise<FileRecord>

Returns the file metadata plus a fresh presigned url.

ds.files.delete(id) → Promise<void>

Removes the file from R2 and decrements your storage usage.

Errors

All non-2xx API responses throw DevStorageError:

import { DevStorage, DevStorageError } from "@devstorage/node";

try {
  await ds.upload(buf, { name: "x", mimeType: "image/png" });
} catch (err) {
  if (err instanceof DevStorageError) {
    console.error(err.status, err.message);
  }
}