Officiële Node.js en TypeScript SDK
Pakket @smallpict/sdk voor Node.js, Next.js, Express en TypeScript-toepassingen.
De officiële Node.js SDK biedt volledige TypeScript-ondersteuning, ondersteuning voor datastromen (streams) en compatibiliteit met Node.js 18+, Next.js en edge runtimes.
Installatie
Shell
npm install @smallpict/sdk# of via pnpmpnpm add @smallpict/sdk# of via yarnyarn add @smallpict/sdkCompleet toepassingsvoorbeeld
TypeScript
import { SmallPictClient } from "@smallpict/sdk";import fs from "fs";
// 1. Initialiseer de client met inloggegevensconst client = new SmallPictClient({ apiKey: process.env.SMALLPICT_API_KEY!, secretKey: process.env.SMALLPICT_SECRET_KEY!, maxRetries: 3, timeoutMs: 10000});
async function main() { try { // 2. Optimaliseer lokaal bestand via Buffer const imageBuffer = fs.readFileSync("./assets/hero.jpg");
const result = await client.optimize({ file: imageBuffer, filename: "hero.jpg", format: "avif", quality: 80, maxWidth: 1920 });
console.log("Optimalisatie geslaagd:"); console.log("- CDN-URL:", result.url); console.log("- Besparing:", result.savingsPercentage + "%"); console.log("- Bestandsgrootte:", result.optimizedSize, "bytes");
// 3. Resterend quotum opvragen const quota = await client.getQuota(); console.log("- Beschikbaar tegoed:", quota.remaining, "/", quota.total);
} catch (error) { console.error("Fout tijdens optimalisatie:", error); }}
main();