标题Hub 节点 SDK
用于CaptionHub API的节点TypeScript SDK,其方法为CaptionHub API端点,响应类型,webhook类型和Timbra WebSocket API.
安装
您可以通过 npm 安装 SDK :
npm install @captionhub/captionhub-node-sdk
或者说
yarn add @captionhub/captionhub-node-sdk
与 API 交互
以下是如何使用CaptionHub SDK的一个简单的例子:
import { CaptionHub } from '@captionhub/captionhub-node-sdk';
const client = new CaptionHub('YOUR_API_KEY');
const projects = await client.projects.getProjects();
console.log('Projects:', projects);
如果 API 返回一个不可胜数的响应( 即 4xx 或 5xx 响应), 一个子类: CaptionHub.APIError 将扔出 :
try {
await client.projects.getProject('i-404');
} catch (err) {
if (err instanceof CaptionHub.APIError.NotFoundError) {
console.error(`API Error: ${err.status} - ${err.message}`);
console.error('Response data:', err.data);
} else if (err instanceof CaptionHub.APIError) {
console.error(`API Error: ${err.status} - ${err.message}`);
console.error('Response data:', err.data);
} else {
console.error('Unexpected error:', err);
}
}
API 响应类型
如果您只想要使用 SDK 中定义的类型, 您可以直接导入 :
import { type APITypes } from '@captionhub/captionhub-node-sdk';
async function main() {
const project: Partial<APITypes.Project> = {
name: 'Your project',
};
console.log('Project:', project);
}
Webhook 类型
使用 WebhookEvent 类型以强烈键入您的 webhook 处理器 :
import { type WebhookEvent } from '@captionhub/captionhub-node-sdk';
function handleWebhook(event: WebhookEvent) {
if (event.type === 'project.created') {
// Now `event.data.project` is strongly typed as `APITypes.Project`
console.log('Project created with ID:', event.data.project.id);
}
}
蒂姆布拉 WebSocket API
使用 Timbra 的 WebSocket API 通过 WebSocket 连接接收字幕 .
const captionhub = new CaptionHub("YOUR_API_KEY", "http://localhost");
captionhub.timbra.startFlow("example-timbra-project");
const connectionDetails = await captionhub.timbra.getConnectionDetails("example-timbra-project");
captionhub.timbra.subscribe({
connection: connectionDetails,
onCaption: (event: CaptionsEvent) => {
console.log("Received captions:", event);
}
});
见 标题事件 为有效载荷.
支助和反馈
提高支持票在 说明Hub 支持。 。 。 .