@avallon-labs/ sdk (英语)
Avallon API 的 SDK 类型脚本 。 从 OpenAPI 自动生成, 带有自定义认证工具 .
安装
npm install @avallon-labs/sdk
对于反应/ SWR 钩:
npm install @avallon-labs/sdk swr react
使用量
配置
import { configure } from "@avallon-labs/sdk";
configure({
baseUrl: "https://api.avallon.ai",
apiKey: "your-api-key",
// OR for user auth:
getAccessToken: () => localStorage.getItem("access_token"),
});
响应信封
端点返回 API { data, message } 邮包-有效载荷
开始 data添加 meta.total 这样你就可以在没有电话的情况下拨号
第二个请求。 (204个终点) getPartnerJwks 返回 a
符合标准的JWKS,因此也没有封装. )
const { data, meta } = await listArtifacts({ count: 25, offset: 50 });
console.log(`showing ${data.length} of ${meta.total}`);
API 调用( 程序获取)
import { listVoiceAgents, createVoiceAgent } from "@avallon-labs/sdk";
// List agents
const { data } = await listVoiceAgents();
console.log(data.agents);
// Create agent
const { data: created } = await createVoiceAgent({ agent_name: "My Agent" });
API 呼叫( 反应 SWR Hooks)
import { useListVoiceAgents, useCreateVoiceAgent } from "@avallon-labs/sdk";
function AgentsList() {
const { data, error, isLoading } = useListVoiceAgents();
if (isLoading) return <Spinner />;
if (error) return <Error message={error.message} />;
return <ul>{data.data.agents.map(a => <li key={a.id}>{a.agent_name}</li>)}</ul>;
}
function CreateAgent() {
const { trigger, isMutating } = useCreateVoiceAgent();
return (
<button onClick={() => trigger({ agent_name: "New Agent" })} disabled={isMutating}>
Create
</button>
);
}
OAuth 流量
import { startOAuthFlow, getStoredVerifier, clearStoredVerifier, signIn } from "@avallon-labs/sdk";
// 1. Start OAuth (redirects user)
const { url } = await startOAuthFlow({
provider: "google",
redirectUri: "https://app.example.com/callback",
});
window.location.href = url;
// 2. After redirect, exchange code for tokens
const verifier = getStoredVerifier();
const response = await signIn({
type: "oauth",
code: new URLSearchParams(location.search).get("code"),
verifier,
});
clearStoredVerifier();
// 3. Store tokens and configure SDK
const { access_token, refresh_token, expires_at } = response.data;
localStorage.setItem("access_token", access_token);
configure({ getAccessToken: () => localStorage.getItem("access_token") });
处理错误
import { AvallonError } from "@avallon-labs/sdk";
try {
await createVoiceAgent({ agent_name: "" });
} catch (e) {
if (e instanceof AvallonError) {
if (e.isValidationError()) {
console.log("Validation errors:", e.errors);
} else if (e.isUnauthorized()) {
// Redirect to login
}
}
}
发展
# Regenerate SDK from OpenAPI (from repo root)
npm run sdk:gen
# Build
npm run build
结构
src/ # Handwritten code
index.ts # Main exports
fetcher.ts # Fetch wrapper with auth
auth.ts # OAuth PKCE utilities
generated/ # Auto-generated (do not edit)
endpoints/ # API functions + SWR hooks
models/ # Request/response types