地盖类型SDK
正式类型SDK 地面覆盖 API (英语).
所需资源
- 节点.js (>= 18.0.0)
- 阿宝 (>= 1.0.0)
- 德诺 页:1
- 一个地面封面 API 密钥和后端 ID
安装
# npm
npm install @groundcover/api-client
# yarn
yarn add @groundcover/api-client
# pnpm
pnpm add @groundcover/api-client
# bun
bun add @groundcover/api-client
# deno
deno add npm:@groundcover/api-client
快速启动
import { initClient } from '@groundcover/api-client';
import { metricsQuery } from '@groundcover/api-client';
// Initialize the client
// Uses GC_API_KEY and GC_BACKEND_ID from environment by default
const client = initClient();
async function run() {
const now = new Date();
const start = new Date(now.getTime() - 60 * 60 * 1000);
const result = await metricsQuery({
client,
body: {
Promql: 'up',
QueryType: 'instant',
Start: start.toISOString(),
End: now.toISOString(),
Step: '1m',
},
});
console.log(result.data);
}
run();
SDK提供 已键入 API 函数 作为主界面.
导入操作模块并使用 client数字 :
import { searchLogs } from '@groundcover/api-client';
const result = await searchLogs({
client,
body: {
start: start.toISOString(),
end: now.toISOString(),
query: '* | stats count(*)',
},
});
原始 HTTP 方法( 缩写)client.get(), (中文(简体) ), client.post()(a) 继续提供
高级使用大小写或端点尚未作为键入函数可用.
配置
SDK默认从环境变量中读取:
| 变量 | 说明 | 需求 |
|---|---|---|
GC_API_KEY | 您的地标 API 密钥 | 对 |
GC_BACKEND_ID | 您的后端标识 | 对 |
GC_BASE_URL | API 基础 URL( 默认 ): https://api.groundcover.com(中文(简体) ) | 没有 |
GC_TRACEPARENT | 用于跟踪的默认跟踪父头 | 没有 |
您也可以明确通过配置 :
const client = initClient({
apiKey: 'your-api-key',
backendId: 'your-backend-id',
baseUrl: 'https://api.groundcover.com',
timeout: 60000,
maxRetries: 5,
});
监视器( YAML)
监视端点返回YAML内容类型. SDK提供方便包装 getMonitorParsed 将 YAML 响应自动解析为 JavaScript 对象 :
import { getMonitorParsed, createMonitor, updateMonitor } from '@groundcover/api-client';
// Fetch and automatically parse YAML to a JS object
const { data: monitorObj, error } = await getMonitorParsed({ client, path: { id: 'monitor-id' } });
// Create or update using standard JS objects
// (Since JSON is a valid subset of YAML, the standard client correctly serializes your object)
await createMonitor({
client,
body: { name: 'My Monitor', type: 'logs' }
});
await updateMonitor({
client,
path: { id: 'monitor-id' },
body: updatedMonitorObj
});
处理错误
所有请求——既输入了API呼叫,又输入了原始的HTTP方法——返回 a error 如果请求失败,则在结果中标出对象:
import { getDashboard } from '@groundcover/api-client';
const result = await getDashboard({
client,
path: { id: 'nonexistent' }
});
if (result.error) {
console.error(`API error:`, result.error);
} else {
console.log(result.data);
}
重试行为
默认情况下, SDK 重试了 idepotent 请求 (GET, (中文(简体) ), HEAD, (中文(简体) ), OPTIONS, (中文(简体) ), TRACE在HTTP 429和503上带有指数后退和焦急:
- 默认重试: 3个
- 默认重试状态: 429 503 (中文(简体) )
- 备份: 以时速、 1分、 最大 30分表示
发展
已发布的客户端仍在Node.js 18+上运行. 使用的维特/维特工具链
页:1 pnpm test:unit 需要节点20.19+或22.12+(Vite 8 / Vitest 4.
cd sdk-typescript
pnpm install
# Run unit tests
pnpm test:unit
# Run e2e tests
pnpm test:e2e
# Run linting
pnpm lint
# Format code
pnpm format
许可证
Apache 2.0 — 见 自由主义。 。 。 .