丢弃框客户端
Buttercup 的 Dropbox 客户端库
关于
Dropbox是Buttercup平台的一个组成部分,因为它被大量用户用来存储各种数据——包括Buttercup金库文件. 拥有实用,可移植和可靠的Dropbox客户端接口对于平台的稳定性至关重要,目前官方的Dropbox SDK在质量和稳定性方面都缺乏.
这个库是一个赤骨 HTTP 客户端,可以直接向 Dropbox 的 HTTP API 发送请求,使用一个符号( 外部处理 - 这个库将不负责获取它们) 。 结果是一个小而可移植的脚本,可靠而简单易懂. 用这个 fetch (单位:千美元)横切)执行请求,这显然将在可复制的跨环境快活中发挥作用.
安装
随便跑 npm install @buttercup/dropbox-client --save 要安装.
最新版本(v2)要求 无害环境管理 要运行的环境 。 标准CommonJS项目无法使用.
使用量
授权
您可以使用 Dropbox 授权 URL 生成 generateAuthorisationURL数字 :
import { generateAuthorisationURL } from "@buttercup/dropbox-client";
const url = generateAuthorisationURL("client-id", "https://redir.example.com");
// open `url`
客户端
使用 DropboxClient 创建客户端界面的类 :
import { generateAuthorisationURL } from "@buttercup/dropbox-client";
const client = new DropboxClient("my-token");
然后,你可以使用 client 用于请求目录内容的适配器 :
client
.getDirectoryContents("/Documents")
.then(contents => {
// [ {
// name: "My directory",
// path: "/Documents/My directory",
// type: "directory"
// }, {
// name: "results.pdf",
// path: "/Documents/results.pdf",
// type: "file"
// } ]
});
您也可以使用 getFileContents 财务报告和审定财务报表 putFileContents分别是:.
兼容模式
您可以为基于浏览器的环境启用兼容模式, CORS 可以中断请求 :
const client = new DropboxClient("my-token", { compat: true });
在一些浏览器环境中,“ CORS hack” Content-Type 头头可能失败, 因此可以通过指定来禁用 false 联 合 国 compatCorsHack 属性 :
const client = new DropboxClient("my-token", {
compat: true,
compatCorsHack: false
});
自定义信头
您可以为客户端的所有请求提供自定义信头,具体方式是指定 headers 选项 :
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 数
一个 fs- 类似接口也可用 :
import { generateAuthorisationURL } from "@buttercup/dropbox-client";
const client = new DropboxClient("my-token");
client.fs.readdir("/photos", (err, items) => {
// array of file names
});
处理错误
执行针对 Dropbox API 的请求时出错将丢入包 Layerr 错误实例 。 它为每个错误提供了一些额外的属性:
import { Layerr } from "layerr";
// ...
client.getDirectoryContents("/").catch((err) => {
const {
status,
statusText,
url
} = Layerr.info(err);
if (status === 403) {
// ...
}
});