Skip to content
smallPict

Node.js & TypeScript SDK

Tài liệu chính thức gói @smallpict/sdk dành cho các ứng dụng Node.js, Next.js và TypeScript.

SDK Node.js chính thức của SmallPict cung cấp đầy đủ định nghĩa kiểu TypeScript, tự động tạo chữ ký yêu cầu HMAC-SHA256, hỗ trợ xử lý luồng (Stream) và tích hợp sẵn cơ chế thử lại tự động.


Cài đặt

Shell
npm install @smallpict/sdk# hoặcbun add @smallpict/sdk# hoặcpnpm add @smallpict/sdk

Ví dụ bắt đầu nhanh

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. Tối ưu hóa một Buffer hình ảnhconst inputBuffer = fs.readFileSync('hero.jpg');const result = await client.optimize(inputBuffer, {  format: 'avif',  quality: 80,  maxWidth: 1920});
console.log(`URL tối ưu trên CDN: ${result.url}`);console.log(`Tiết kiệm: ${result.savingsPercentage}% (giảm ${result.bytesSaved} bytes)`);
// 2. Kiểm tra hạn ngạch tháng hiện tạiconst quota = await client.getQuota();console.log(`Hạn ngạch đã dùng: ${quota.bytesUsed} / ${quota.quotaLimitBytes} bytes`);
// 3. Xóa bộ nhớ đệm CDNawait client.purgeCdn([result.url]);

Các tùy chọn cấu hình Client

TypeScript
interface ClientOptions {  apiKey: string;  secretKey: string;  baseUrl?: string;           // Mặc định: https://api.smallpict.app/v1  timeoutMs?: number;         // Mặc định: 30000 (30 giây)  fallbackMode?: 'throw' | 'passthrough'; // Mặc định: 'throw'  retryConfig?: {    maxRetries?: number;      // Mặc định: 3 lần    baseDelayMs?: number;     // Mặc định độ trễ khởi đầu: 500ms  };}