海滩寄存器类型Script 库
Beach Depository TypeScript库从TypeScript提供方便访问Beach Depository API.
目录
安装
npm i -s @beach-depository/sdk
参考文献
该图书馆的全部参考资料可供查阅 这里。 。 。 .
使用量
以下列方式证明和使用客户端:
import { BeachApiClient } from "@beach-depository/sdk";
const client = new BeachApiClient({ token: "YOUR_TOKEN" });
await client.fboAccounts.create({
firstName: "firstName",
lastName: "lastName",
email: "email",
phone: "phone"
});
环境
此 SDK 允许您为 API 请求配置不同的环境 .
import { BeachApiClient, BeachApiEnvironment } from "@beach-depository/sdk";
const client = new BeachApiClient({
environment: BeachApiEnvironment.Sandbox,
});
请求和答复类型
SDK导出所有请求和响应类型为TypeScript接口. 只需用 以下命名空间:
import { BeachApi } from "@beach-depository/sdk";
const request: BeachApi.ListFboAccountsRequest = {
...
};
例外处理
当 API 返回一个非成功状态代码( 4xx 或 5xx 响应) 时, 以下错误的子类 将会被扔出.
import { BeachApiError } from "@beach-depository/sdk";
try {
await client.fboAccounts.create(...);
} catch (err) {
if (err instanceof BeachApiError) {
console.log(err.statusCode);
console.log(err.message);
console.log(err.body);
console.log(err.rawResponse);
}
}
高级
出口
这个SDK支持子包客户端的直接进口,它允许JavaScript捆绑器进行树摇并只包括被导入子包代码. 这导致捆绑大小要小得多.
import { OrganizationClient } from '@beach-depository/sdk/organization';
const client = new OrganizationClient({...});
其他信头
如果您希望作为请求的一部分发送额外的信头,请使用 headers 请求选项.
import { BeachApiClient } from "@beach-depository/sdk";
const client = new BeachApiClient({
...
headers: {
'X-Custom-Header': 'custom value'
}
});
const response = await client.fboAccounts.create(..., {
headers: {
'X-Custom-Header': 'custom value'
}
});
其它查询字符串参数
如果您希望作为请求的一部分发送额外的查询字符串参数,请使用 queryParams 请求选项.
const response = await client.fboAccounts.create(..., {
queryParams: {
'customQueryParamKey': 'custom query param value'
}
});
检索
SDK是用有指数回放的自动回放装置. 申请要等会再审 由于请求被认为可以重试,且重试的次数没有超过所配置的 重试限制( 默认: 2) .
重审哪个状态代码取决于 retryStatusCodes 生成器配置 :
legacy (当前默认值):在
- 408 (简体中文) (超时)
- 429个 (太多请求)
- 第5XX条 (所有服务器错误,包括500个)
recommended: 重试
- 408 (简体中文) (超时)
- 429个 (太多请求)
- 502 (中文(简体) ) (坏门)
- 第503条 (服务不可用)
- 504 (中文(简体) ) (Gateway超时)
使用 maxRetries 请求选项来配置此行为.
const response = await client.fboAccounts.create(..., {
maxRetries: 0 // override maxRetries at the request level
});
超时
SDK默认为60第二次超时. 使用 timeoutInSeconds 选项来配置此行为.
const response = await client.fboAccounts.create(..., {
timeoutInSeconds: 30 // override timeout to 30s
});
中止请求
SDK允许用户在任何时刻通过一个中止信号来中止请求.
const controller = new AbortController();
const response = await client.fboAccounts.create(..., {
abortSignal: controller.signal
});
controller.abort(); // aborts the request
访问原始响应数据
SDK通过下列方式提供原始响应数据,包括信头: .withRawResponse() 方法.
这个 .withRawResponse() 方法返回给一个带有 data (单位:千美元) rawResponse 属性.
const { data, rawResponse } = await client.fboAccounts.create(...).withRawResponse();
console.log(data);
console.log(rawResponse.headers['X-My-Header']);
日志
SDK支持记录. 您可以通过通过一个 logging 对象对客户端选项.
import { BeachApiClient, logging } from "@beach-depository/sdk";
const client = new BeachApiClient({
...
logging: {
level: logging.LogLevel.Debug, // defaults to logging.LogLevel.Info
logger: new logging.ConsoleLogger(), // defaults to ConsoleLogger
silent: false, // defaults to true, set to false to enable logging
}
});
这个 logging 对象可以具有以下属性:
level: 要使用的日志级别 。 默认为logging.LogLevel.Info。 。 。 .logger:要使用的伐木机. 默认为 alogging.ConsoleLogger。 。 。 .silent: 是否让伐木工安静. 默认为true。 。 。 .
这个 level 属性可以是下列值之一:
logging.LogLevel.Debuglogging.LogLevel.Infologging.LogLevel.Warnlogging.LogLevel.Error
要提供自定义日志, 您可以通过一个执行 logging.ILogger 接口.
自定义日志示例
举个例子,用流行的 winston 伐木图书馆.
import winston from 'winston';
const winstonLogger = winston.createLogger({...});
const logger: logging.ILogger = {
debug: (msg, ...args) => winstonLogger.debug(msg, ...args),
info: (msg, ...args) => winstonLogger.info(msg, ...args),
warn: (msg, ...args) => winstonLogger.warn(msg, ...args),
error: (msg, ...args) => winstonLogger.error(msg, ...args),
};
举个例子,用流行的 pino 伐木图书馆.
import pino from 'pino';
const pinoLogger = pino({...});
const logger: logging.ILogger = {
debug: (msg, ...args) => pinoLogger.debug(args, msg),
info: (msg, ...args) => pinoLogger.info(args, msg),
warn: (msg, ...args) => pinoLogger.warn(args, msg),
error: (msg, ...args) => pinoLogger.error(args, msg),
};
自定义抓取
SDK提供低级 fetch 将自定义的 HTTP 请求生成到当前
从SDK级配置中受益,如认证、重试、超时和记录.
这对调用SDK中尚未支持的API端点很有用.
const response = await client.fetch("/v1/custom/endpoint", {
method: "GET",
}, {
timeoutInSeconds: 30,
maxRetries: 3,
headers: {
"X-Custom-Header": "custom-value",
},
});
const data = await response.json();
运行时间相容性
SDK工作时间如下:
- 节点.js 18+
- 维塞尔
- 云花岗工人
- 德诺v1.25+
- 1.0+号舱
- 原生反应
捐款
虽然我们重视对SDK的开源贡献,但这个库是程序生成的. 直接给这个图书馆增加的代码 得转到我们一代人的代码上, 否则,它们将被覆盖 在下一次产生的释放。 随便打开公关吧 是一个概念的证明, 但知道我们将无法 合并它作为一个。 我们建议开放 先和我们讨论一个问题!
另一方面,对 " README " 的贡献总是非常受欢迎的!