0.3.8 • Published 5 months ago
@commercengine/storefront-sdk v0.3.8
Storefront SDK
TypeScript SDK for the CommerceEngine Storefront API.
Installation
npm install @commercengine/storefront-sdk
Or with yarn:
yarn add @commercengine/storefront-sdk
Or with pnpm:
pnpm add @commercengine/storefront-sdk
Usage
Initializing the SDK
You can initialize the SDK in just a few lines:
import StorefrontSDK, { Environment } from "@commercengine/storefront-sdk";
// Initialize with environment and store ID
const sdk = new StorefrontSDK({
storeId: "your-store-id",
environment: Environment.Staging, // or Environment.Production
});
// Or with a custom base URL (if needed)
const customSdk = new StorefrontSDK({
storeId: "your-store-id",
baseUrl: "https://custom-api.example.com",
});
Authentication
The SDK supports various authentication methods:
// Anonymous authentication
const { access_token, refresh_token, user } =
await sdk.auth.getAnonymousToken();
// Phone login
const { otp_token, otp_action } = await sdk.auth.loginWithPhone("9876543210");
// Email login
const { otp_token, otp_action } = await sdk.auth.loginWithEmail(
"user@example.com"
);
// Verify OTP
const { user, access_token, refresh_token } = await sdk.auth.verifyOtp(
"123456",
otp_token,
"login"
);
// Password login
const { user, access_token, refresh_token } = await sdk.auth.loginWithPassword({
email: "user@example.com",
password: "your-password",
});
// Registration
const { user, access_token, refresh_token } = await sdk.auth.registerWithPhone({
phone: "9876543210",
email: "user@example.com",
first_name: "John",
last_name: "Doe",
});
// Token management
sdk.setToken(access_token); // Set token for all clients
sdk.clearToken(); // Clear token from all clients
Using the API Clients
The SDK provides dedicated clients for different API sections:
// Catalog client example (product listing)
const products = await sdk.catalog.listProducts();
// Cart client example (add to cart)
const cart = await sdk.cart.addToCart("product-id", 1);
Advanced Configuration
You can configure timeout and other options:
const sdk = new StorefrontSDK({
storeId: "your-store-id",
environment: Environment.Production,
timeout: 5000, // 5 second timeout
token: "existing-token", // Initialize with an existing token
});