@depoc/storefront-sdk
v1.2.0
Published
Official TypeScript/JavaScript SDK for Depoc shop storefronts — connect Next.js, Nuxt, Angular, Astro, and any JS frontend to api.depoc.ir
Maintainers
Readme
@depoc/storefront-sdk
Official TypeScript/JavaScript SDK for connecting shop storefronts to Depoc.
Works with Next.js, Nuxt, Angular, Astro, SvelteKit, Remix, and any environment with fetch (Node 18+, browsers, edge runtimes).
Install
npm install @depoc/storefront-sdk
# or
yarn add @depoc/storefront-sdk
# or
pnpm add @depoc/storefront-sdkQuick start
- Create an API key in Depoc Panel → Shops → [your shop] → API Keys
- Configure your storefront:
import { DepocStorefront } from '@depoc/storefront-sdk'
const depoc = new DepocStorefront({
apiKey: process.env.DEPOC_API_KEY!, // depoc_live_...
// Optional overrides (defaults shown):
// apiOrigin: 'https://api.depoc.ir',
// siteUrl: 'https://depoc.ir',
})
// Shop branding & enabled modules
const shop = await depoc.getShopContext()
console.log(shop.siteName, shop.modules)
// Catalog
const homepage = await depoc.getHomepage()
const product = await depoc.getProduct('industrial-pump-x200')
const results = await depoc.searchProducts('پمپ', { sortBy: 'price-low' })
// Checkout (guest checkout — cart is client-side)
const quote = await depoc.getShippingQuote(
depoc.cart.load().map((item) => ({
productId: item.productId,
quantity: item.quantity,
unitPrice: item.unitPrice,
}))
)
const order = await depoc.createOrder({
firstName: 'علی',
lastName: 'رضایی',
email: '[email protected]',
phone: '09123456789',
address: {
fullAddress: 'خیابان ولیعصر، پلاک ۱۰',
city: 'تهران',
province: 'تهران',
postalCode: '1234567890',
},
paymentMethod: 'card',
items: depoc.cart.load().map((item) => ({
productId: item.productId,
productName: item.productName,
productImage: item.productImage,
sku: item.sku,
quantity: item.quantity,
unitPrice: item.unitPrice,
})),
})
// Online payment (Zarinpal, etc.)
const payment = await depoc.initiatePayment({
orderId: order.id,
amount: order.total,
customerPhone: '09123456789',
})
window.location.href = payment.paymentUrl
// Or use the full Depoc-hosted flow (recommended):
await depoc.completeCardCheckout({ order, customerEmail: '[email protected]' })
// → redirects to Zarinpal → api.depoc.ir verifies → back to your shop /checkout/successDepoc-hosted payments
Payments are processed on Depoc servers (api.depoc.ir). Your shop never talks to Zarinpal directly.
Shop frontend api.depoc.ir Zarinpal
│ │ │
│ initiatePayment() │ │
│ ─────────────────────────► │ request payment │
│ ◄───────────────────────── │ ────────────────────────► │
│ paymentUrl │ │
│ ─────────────────────────────────────────────────────► │
│ │ ◄──── callback ────────── │
│ │ verify + update order │
│ ◄── redirect to shop ───── │ │
│ /checkout/success │ │- SDK calls
POST /checkout/payments/initiatewith your shopreturnUrl - Customer pays on Zarinpal
- Zarinpal redirects to
https://api.depoc.ir/api/v1/checkout/payments/callback - Depoc verifies payment, updates order status
- Customer is redirected to
{siteUrl}/checkout/success?orderId=...&payment=paid
// One-liner after order creation
await depoc.completeCardCheckout({ order, customerEmail: user.email })
// Manual control
const payment = await depoc.initiatePayment({
orderId: order.id,
amount: order.total,
returnUrl: 'https://myshop.com/order/success',
failureUrl: 'https://myshop.com/order/failed',
})
depoc.redirectToPaymentGateway(payment)
// Poll or verify (SPA / mobile)
const status = await depoc.getPaymentStatus(payment.id)
const result = await depoc.verifyPayment({ authority: '...', status: 'OK' })Framework examples
Next.js (App Router)
// lib/depoc.ts
import { DepocStorefront } from '@depoc/storefront-sdk'
export const depoc = new DepocStorefront({
apiKey: process.env.DEPOC_API_KEY!,
apiOrigin: process.env.DEPOC_API_ORIGIN,
})
// app/products/[slug]/page.tsx
export async function generateStaticParams() {
return depoc.getAllProductSlugs()
}Nuxt 3
// plugins/depoc.ts
import { DepocStorefront } from '@depoc/storefront-sdk'
export default defineNuxtPlugin(() => {
const config = useRuntimeConfig()
const depoc = new DepocStorefront({
apiKey: config.depocApiKey,
})
return { provide: { depoc } }
})Angular
// services/depoc.service.ts
import { Injectable } from '@angular/core'
import { DepocStorefront } from '@depoc/storefront-sdk'
import { environment } from '../environments/environment'
@Injectable({ providedIn: 'root' })
export class DepocService extends DepocStorefront {
constructor() {
super({ apiKey: environment.depocApiKey })
}
}Astro
---
import { DepocStorefront } from '@depoc/storefront-sdk'
const depoc = new DepocStorefront({
apiKey: import.meta.env.DEPOC_API_KEY,
})
const homepage = await depoc.getHomepage()
---API reference
DepocStorefront
| Method | Description |
|--------|-------------|
| getShopContext() | Branding, modules, profile (requires API key) |
| getHomepage() | Aggregated homepage data |
| getFeaturedProducts() | Featured product list |
| getBanners() | CMS banners |
| getCategories({ tree? }) | Category list or tree |
| getCategory(slug) | Single category |
| getCategoryProducts(slug, filters?) | Products in category |
| listProducts(options?) | Paginated product list |
| getProduct(slug) | Product detail |
| getProductSlugRedirect(slug) | Canonical slug redirect |
| searchProducts(query, filters?) | Full-text search |
| getAllProductSlugs() | All slugs for SSG |
| getAllCategorySlugs() | All category slugs for SSG |
| getShippingQuote(items) | Shipping cost quote |
| getShippingSettings() | Public shipping config |
| getPaymentMethods() | Enabled payment methods |
| createOrder(payload) | Place guest order |
| initiatePayment(params) | Start online payment (Depoc-hosted) |
| completeCardCheckout({ order }) | Create payment + redirect to gateway |
| verifyPayment({ authority, status }) | Verify payment on Depoc servers |
| getPaymentStatus(paymentId) | Poll payment status |
| getDepocPaymentCallbackUrl() | Depoc callback URL (for reference) |
| buildPaymentReturnUrls(paths?) | Shop success/failure URLs |
| getOrder(orderId) | Order status lookup |
| getPublicFileUrl(path) | Public file CDN URL |
| resolveAssetUrl(path) | Resolve relative image paths |
Storage helpers
depoc.cart.load() // CartItem[] from localStorage
depoc.cart.save([]) // Persist cart
depoc.orders.save(order)
depoc.orders.load()Configuration
| Option | Required | Default | Description |
|--------|----------|---------|-------------|
| apiKey | Yes | — | Shop API key (depoc_live_...) |
| apiOrigin | No | https://api.depoc.ir | API server origin |
| siteUrl | No | https://depoc.ir | Your shop website URL |
| fetch | No | globalThis.fetch | Custom fetch (Node, edge) |
| timeoutMs | No | 30000 | Request timeout |
| onError | No | — | Error callback |
| performance | No | see below | Dedupe, memo, ETag, retry, keepalive |
Performance
Existing method signatures stay the same. Opt into extra tuning when needed:
const depoc = new DepocStorefront({
apiKey: process.env.DEPOC_API_KEY!,
performance: {
dedupe: true, // concurrent identical GETs share one network call
memoTtlMs: 0, // global GET memo (0 = off; shop/homepage still use short defaults)
etag: true, // If-None-Match + 304 body reuse
retry: { retries: 2 }, // backoff on network / 5xx / 429
keepalive: true,
},
})
// Bootstrap warm (parallel)
await depoc.prefetchBootstrap()
// Stale cancellation (search-as-you-type)
const ac = depoc.staleController('search')
await depoc.searchProducts(query, {}, { signal: ac.signal })
// Client-side product batch
const products = await depoc.getProducts(['slug-a', 'slug-b'])Versioning
This package follows Semantic Versioning:
- MAJOR — breaking API changes
- MINOR — new features, backward compatible
- PATCH — bug fixes
Check SDK_VERSION export or package.json version. Pin in production:
"@depoc/storefront-sdk": "~1.2.0"Publishing (maintainers)
From monorepo root:
# Install dependencies
npm install
# Build
npm run sdk:build
# Version bump (changesets)
npm run changeset
npm run version-packages
# Publish to npm
npm run releaseLicense
MIT
