战车类型脚本库
Chariot TypeScript库提供从TypeScript获取Chariot API的方便通道.
安装
npm i -s @chariot-giving/typescript-sdk
使用量
以下列方式证明和使用客户端:
import { ChariotClient } from "@chariot-giving/typescript-sdk";
const client = new ChariotClient({ token: "YOUR_TOKEN" });
await client.nonprofits.create({
user: {
email: "[email protected]",
},
ein: "043567500",
});
请求和响应类型
SDK导出所有请求和响应类型为TypeScript接口. 只需用 以下命名空间:
import { Chariot } from "@chariot-giving/typescript-sdk";
const request: Chariot.NonprofitsCreateRequest = {
...
};
例外处理
当 API 返回一个非成功状态代码( 4xx 或 5xx 响应) 时, 以下错误的子类 将会被扔出.
import { ChariotError } from "@chariot-giving/typescript-sdk";
try {
await client.nonprofits.create(...);
} catch (err) {
if (err instanceof ChariotError) {
console.log(err.statusCode);
console.log(err.message);
console.log(err.body);
}
}
自动放置
列表终点为相接点. SDK提供传动器,让你 只需绕过项目 :
const result = await client.events.list();
for await (const event of result) {
console.log(event);
}
您也可以逐行逐页排列:
let page = await client.events.list();
for (const event of page.data) {
console.log(event);
}
或手动:
while (page.hasNextPage()) {
page = page.getNextPage();
// ...
}
高级
检索
SDK是用有指数回放的自动回放装置. 申请要等会再审 由于请求被认为可以收回,且重试次数没有超过所配置的 重试限制( 默认: 2) .
如果返回以下任一 HTTP 状态代码,则请求被视为可收回:
- 408 (简体中文) (超时)
- 429个 (太多请求)
- 第5XX条 (内部服务器出错)
使用 maxRetries 请求选项来配置此行为.
const response = await client.nonprofits.create(..., {
maxRetries: 0 // override maxRetries at the request level
});
超时
SDK默认为60第二次超时. 使用 timeoutInSeconds 选项来配置此行为.
const response = await client.nonprofits.create(..., {
timeoutInSeconds: 30 // override timeout to 30s
});
中止请求
SDK允许用户在任何时刻通过一个中止信号来中止请求.
const controller = new AbortController();
const response = await client.nonprofits.create(..., {
abortSignal: controller.signal
});
controller.abort(); // aborts the request
运行时间相容性
SDK 默认为 node-fetch 但是如果存在, 将会使用全局获取客户端 。 SDK的工作在以下几个方面
运行时间 :
- 节点.js 18+
- 维塞尔
- 云花岗工人
- 德诺v1.25+
- 1.0+号舱
- 原生反应
自定义获取客户端
SDK 为您自定义 HTTP 客户端/ 获取函数提供了一种方法 。 如果你正在运行在一个 不支持的环境,这为您提供了一个打破玻璃并确保SDK工作的方法.
import { ChariotClient } from "@chariot-giving/typescript-sdk";
const client = new ChariotClient({
...
fetcher: // provide your implementation here
});
捐款
虽然我们重视对SDK的开源贡献,但这个库是程序生成的. 直接给这个图书馆增加的代码 得转到我们一代人的代码上, 否则,它们将被覆盖 在下一次产生的释放。 随便打开公关吧 是一个概念的证明, 但知道我们将无法 合并它作为一个。 我们建议开放 先和我们讨论一个问题!
另一方面,对 " README " 的贡献总是非常受欢迎的!