Node.js & TypeScript SDK
Node.js、Next.js、TypeScriptアプリケーション向け公式 @smallpict/sdk ドキュメント。
公式SmallPict Node.js SDKは、完全なTypeScript型定義、HMAC-SHA256自動署名、ストリーム処理、および自動再試行機能を提供します。
インストール
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. 画像バッファを最適化const inputBuffer = fs.readFileSync('hero.jpg');const result = await client.optimize(inputBuffer, { format: 'avif', quality: 80, maxWidth: 1920});
console.log(`最適化後URL: ${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 };}