Node.js & TypeScript SDK
官方 @smallpict/sdk 深度文档,适用于 Node.js、Next.js 与现代 TypeScript 全栈应用。
SmallPict 官方 Node.js SDK 提供完备的 TypeScript 强类型定义、全自动 HMAC-SHA256 请求签名、Stream 流式处理支持与内置容错重试。
安装方式
Shell
npm install @smallpict/sdk# 或bun add @smallpict/sdk# 或pnpm add @smallpict/sdk快速上手示例
TypeScript
import { SmallPict } from '@smallpict/sdk';import fs from 'node:fs';
const client = new SmallPict({ apiKey: process.env.SMALLPICT_API_KEY!, secretKey: process.env.SMALLPICT_SECRET_KEY!, retryConfig: { maxRetries: 3, baseDelayMs: 500 }});
// 1. 优化图片文件 Bufferconst inputBuffer = fs.readFileSync('hero.jpg');const result = await client.optimize(inputBuffer, { format: 'avif', quality: 80, maxWidth: 1920});
console.log(`优化后 CDN 地址: ${result.url}`);console.log(`节省空间: ${result.savingsPercentage}% (共节约 ${result.bytesSaved} 字节)`);
// 2. 检查月度剩余配额const quota = await client.getQuota();console.log(`配额用量: ${quota.bytesUsed} / ${quota.quotaLimitBytes} 字节`);
// 3. 即时刷新边缘 CDN 缓存await client.purgeCdn([result.url]);客户端配置选项定义
TypeScript
interface ClientOptions { apiKey: string; secretKey: string; baseUrl?: string; // 默认值: https://api.smallpict.app/v1 timeoutMs?: number; // 默认值: 30000 (30 秒) fallbackMode?: 'throw' | 'passthrough'; // 默认值: 'throw' retryConfig?: { maxRetries?: number; // 默认重试: 3 次 baseDelayMs?: number; // 默认初始延迟: 500ms };}