Fern 类型脚本库
Fern TypeScript库提供从TypeScript获取Fern API的方便通道.
目录
安装
npm i -s @fern-api/venus-api-sdk
参考文献
该图书馆的全部参考资料可供查阅 这里。 。 。 .
使用量
以下列方式证明和使用客户端:
import { FernVenusApiClient } from "@fern-api/venus-api-sdk";
const client = new FernVenusApiClient({ environment: "YOUR_BASE_URL", token: "YOUR_TOKEN" });
await client.apiKeys.create({
organizationId: "organizationId"
});
请求和答复类型
SDK导出所有请求和响应类型为TypeScript接口. 只需用 以下命名空间:
import { FernVenusApi } from "@fern-api/venus-api-sdk";
const request: FernVenusApi.CreateApiKeyRequest = {
...
};
例外处理
当 API 返回一个非成功状态代码( 4xx 或 5xx 响应) 时, 以下错误的子类 将会被扔出.
import { FernVenusApiError } from "@fern-api/venus-api-sdk";
try {
await client.apiKeys.create(...);
} catch (err) {
if (err instanceof FernVenusApiError) {
console.log(err.statusCode);
console.log(err.message);
console.log(err.body);
console.log(err.rawResponse);
}
}
高级
其他信头
如果您希望作为请求的一部分发送额外的信头,请使用 headers 请求选项.
import { FernVenusApiClient } from "@fern-api/venus-api-sdk";
const client = new FernVenusApiClient({
...
headers: {
'X-Custom-Header': 'custom value'
}
});
const response = await client.apiKeys.create(..., {
headers: {
'X-Custom-Header': 'custom value'
}
});
其它查询字符串参数
如果您希望作为请求的一部分发送额外的查询字符串参数,请使用 queryParams 请求选项.
const response = await client.apiKeys.create(..., {
queryParams: {
'customQueryParamKey': 'custom query param value'
}
});
检索
SDK是用有指数回放的自动回放装置. 申请要等会再审 由于请求被认为可以重试,且重试的次数没有超过所配置的 重试限制( 默认: 2) .
当返回以下任一 HTTP 状态代码时, 请求被视为可重试 :
- 408 (简体中文) (超时)
- 429个 (太多请求)
- 第5XX条 (内部服务器出错)
使用 maxRetries 请求选项来配置此行为.
const response = await client.apiKeys.create(..., {
maxRetries: 0 // override maxRetries at the request level
});
超时
SDK默认为60第二次超时. 使用 timeoutInSeconds 选项来配置此行为.
const response = await client.apiKeys.create(..., {
timeoutInSeconds: 30 // override timeout to 30s
});
中止请求
SDK允许用户在任何时刻通过一个中止信号来中止请求.
const controller = new AbortController();
const response = await client.apiKeys.create(..., {
abortSignal: controller.signal
});
controller.abort(); // aborts the request
访问原始响应数据
SDK通过下列方式提供原始响应数据,包括信头: .withRawResponse() 方法.
这个 .withRawResponse() 方法返回给一个带有 data (单位:千美元) rawResponse 属性.
const { data, rawResponse } = await client.apiKeys.create(...).withRawResponse();
console.log(data);
console.log(rawResponse.headers['X-My-Header']);
日志
SDK支持记录. 您可以通过通过一个 logging 对象对客户端选项.
import { FernVenusApiClient, logging } from "@fern-api/venus-api-sdk";
const client = new FernVenusApiClient({
...
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工作时间如下:
- 节点.js 18+
- 维塞尔
- 云花岗工人
- 德诺v1.25+
- 1.0+号舱
- 原生反应
自定义获取客户端
SDK为您提供了自定义 HTTP 客户端/ 获取功能的方法 。 如果你正在运行在一个 不支持的环境,这为您提供了一个打破玻璃并确保SDK工作的方法.
import { FernVenusApiClient } from "@fern-api/venus-api-sdk";
const client = new FernVenusApiClient({
...
fetcher: // provide your implementation here
});
捐款
虽然我们重视对SDK的开源贡献,但这个库是程序生成的. 直接给这个图书馆增加的代码 得转到我们一代人的代码上, 否则,它们将被覆盖 在下一次产生的释放。 随便打开公关吧 是一个概念的证明, 但知道我们将无法 合并它作为一个。 我们建议开放 先和我们讨论一个问题!
另一方面,对 " README " 的贡献总是非常受欢迎的!