@buttercup/dropbox-client

中文文档
2.2.0发布于 2023/11/11MITTypeScriptES Module近 30 天下载 2409

Dropbox client library

原始使用文档
文档目录9 节
  1. About
  2. Installation
  3. Usage
  4. Authorisation
  5. Client
  6. Compatibility Mode
  7. Custom Headers
  8. Fs
  9. Error Handling

Dropbox Client

Dropbox client library for Buttercup

Buttercup npm version Tests status

About

Dropbox is an integral part of the Buttercup platform as it's used by a huge amount of users to store all kinds of data - including Buttercup vault files. Having a functional, portable and reliable Dropbox client interface is critical to the platform's stability, and currently the official Dropbox SDK is lacking in terms of quality and stability.

This library is a barebones HTTP client that makes requests directly to Dropbox's HTTP API using a token (handled externally - this library will not be responsible for fetching them). The result is a tiny, portable script that is reliable and simple to understand. It uses fetch (cross-fetch) to perform requests, which will obviously work in a reproducible fassion across environments.

Installation

Simply run npm install @buttercup/dropbox-client --save to install.

The latest version (v2) requires an ESM environment to run. It is not available to standard CommonJS projects.

Usage

Authorisation

You can generate Dropbox authorisation URLs by using generateAuthorisationURL:

import { generateAuthorisationURL } from "@buttercup/dropbox-client";

const url = generateAuthorisationURL("client-id", "https://redir.example.com");
// open `url`

Client

Use the DropboxClient class to create a client interface:

import { generateAuthorisationURL } from "@buttercup/dropbox-client";

const client = new DropboxClient("my-token");

You can then use the client adapter to make requests like for directory contents:

client
    .getDirectoryContents("/Documents")
    .then(contents => {
        // [ {
        //     name: "My directory",
        //     path: "/Documents/My directory",
        //     type: "directory"
        // }, {
        //     name: "results.pdf",
        //     path: "/Documents/results.pdf",
        //     type: "file"
        // } ]
    });

You can also read and write files using getFileContents and putFileContents, respectively.

Compatibility Mode

You can enable compatibility mode for browser-based environments where CORS may break requests:

const client = new DropboxClient("my-token", { compat: true });

In some browser environments the "CORS hack" Content-Type header can fail, so this can be disabled by specifying false for the compatCorsHack property:

const client = new DropboxClient("my-token", {
    compat: true,
    compatCorsHack: false
});

Custom Headers

You can provide custom headers to all the requests the client makes by specifying the headers option:

const client = new DropboxClient("my-token", {
    headers: {
        // Disable the cache (works/necessary in some environments)
        "Cache-Control": "no-cache, no-store, max-age=0"
    }
});

Fs

An fs-like interface is also available:

import { generateAuthorisationURL } from "@buttercup/dropbox-client";

const client = new DropboxClient("my-token");

client.fs.readdir("/photos", (err, items) => {
    // array of file names
});

Error Handling

Errors while performing requests against the Dropbox API will be thrown wrapped in a Layerr error instance. It provides some extra properties with each error:

import { Layerr } from "layerr";

// ...

client.getDirectoryContents("/").catch((err) => {
    const {
        status,
        statusText,
        url
    } = Layerr.info(err);

    if (status === 403) {
        // ...
    }
});

版本与发布记录

@buttercup/dropbox-client 共发布 28 个版本,首次发布于 2018/11/16;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.2.0latest2023/11/112 年前
2.1.22023/07/163 年前
2.1.12023/04/253 年前
2.1.02023/02/093 年前
2.0.02022/12/263 年前
1.1.32022/09/044 年前

关于 @buttercup/dropbox-client 的常见问题

@buttercup/dropbox-client 是什么?

Dropbox client library

如何安装 @buttercup/dropbox-client?

在项目目录运行 npm install @buttercup/dropbox-client,或使用 pnpm add / yarn add / bun add。

@buttercup/dropbox-client 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@buttercup/dropbox-client 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@buttercup/dropbox-client 可以商用吗?

@buttercup/dropbox-client 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@buttercup/dropbox-client 的下载量有多少?

根据 npm 官方下载统计,@buttercup/dropbox-client 在 2026/10/09 – 2026/11/07 这 30 天内被下载 2,409 次,日均约 80 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@buttercup/dropbox-client 还在维护吗?

最新版本 2.2.0 发布于 2023/11/11(2 年前);至今共发布 28 个版本,其中过去 12 个月发布了 0 个;首次发布于 2018/11/16。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。