100% type-safe SDK with IntelliSense, compile-time checking, and OpenAPI-generated types
The SDK provides 100% type safety with auto-generated types from OpenAPI specifications, complete IntelliSense support, and compile-time error prevention.
All types are automatically generated from the OpenAPI specification, ensuring they’re always in sync with the API:
Copy
// Types are generated from OpenAPI spec and always currentimport type { Product, Cart, Order, Customer, ApiResult} from '@commercengine/storefront-sdk';
// Type guards for runtime type checkingfunction isProduct(item: unknown): item is Product { return typeof item === 'object' && item !== null && 'id' in item && 'name' in item;}// Usage with type safetyconst data: unknown = await fetch('/some-endpoint');if (isProduct(data)) { // TypeScript knows data is Product console.log(data.name);}
// Full type safety and IntelliSenseconst result = await client.catalog.getProduct('product-id');if (result.success) { // ✅ TypeScript knows exact structure const product: Product = result.data; console.log(product.name); // ✅ Known to be string console.log(product.selling_price); // ✅ Known to be number console.log(product.images[0].url); // ✅ Safe array access} else { // ✅ Typed error handling console.error(result.error.code); // ✅ Known error structure}
Copy
// Full type safety and IntelliSenseconst result = await client.catalog.getProduct('product-id');if (result.success) { // ✅ TypeScript knows exact structure const product: Product = result.data; console.log(product.name); // ✅ Known to be string console.log(product.selling_price); // ✅ Known to be number console.log(product.images[0].url); // ✅ Safe array access} else { // ✅ Typed error handling console.error(result.error.code); // ✅ Known error structure}
Copy
// No type safety - all runtime errorsconst response = await fetch('/catalog/products/product-id');const data = await response.json();if (response.ok) { // ❌ No type information - could fail at runtime console.log(data.name); // Could be undefined console.log(data.selling_price); // Could be string instead of number console.log(data.images[0].url); // Could throw if images is empty} else { // ❌ Unknown error structure console.error(data.message); // Could be different property name}
// ❌ Avoid type assertionsconst product = data as Product;// ✅ Use type guards or SDK methodsconst result = await client.catalog.getProduct(id);if (result.success) { const product = result.data; // Already typed correctly}
Leverage Union Types
Handle different states with discriminated unions:
Find API usage errors during development, not production
Better Documentation
Types serve as always-current API documentation
Faster Development
IntelliSense speeds up development with autocomplete
Confident Refactoring
Change APIs with confidence using IDE refactoring tools
All SDK types are automatically generated from the OpenAPI specification, ensuring they’re always accurate and up-to-date with the actual API responses.