Resilience — retry, chunking, helpers
Automatic retry
By default, the SDK retries 3 times on:
- HTTP
408,429,500,502,503,504 - network errors:
ECONNRESET,ETIMEDOUT,ECONNREFUSED,ENETUNREACH,ENOTFOUND,EAI_AGAIN
with an exponential backoff (1s → 2s → 4s, jitter ±500 ms, cap 10s).
javascript
// Customize
const client = SpmClient.getClient('v1', apiKey, {
retry: {
maxRetries: 5,
backoffBaseMs: 2000,
backoffCapMs: 30000,
retryableStatus: [429, 502, 503, 504],
},
});
// Disable
const client = SpmClient.getClient('v1', apiKey, { retry: false });When all attempts fail, the envelope returns ok: false with error.retryable and error.attempts:
javascript
const res = await SpmOrders.bulkSave(client, orders);
if (!res.ok && res.error.retryable) {
// retry later / queue it
}Optional chunking
Pass a large array: the SDK splits it into batches aligned with the API limit and aggregates the results.
javascript
const hugeArray = Array(500).fill().map((_, i) => ({ /* customer data */ }));
const res = await SpmCustomers.bulkSave(client, hugeArray, { chunk: true });
console.log(res.data.sent_count); // aggregated total
console.log(res.data.chunks); // per-chunk detail (10 × 50)By default, chunkSize equals the API-aligned value:
| Resource | CHUNK_SIZE |
|---|---|
SpmCustomDataRecords | 20 |
SpmCustomers, SpmCustomersAddresses, SpmProducts, SpmProductsImages, SpmProductsVariations, SpmOrders, SpmVouchers | 50 |
SpmCustomersGroups, SpmProductsCategories, SpmProductsManufacturers, SpmOrdersCarriers, SpmOrdersStatuses | 100 |
javascript
await SpmProducts.bulkSave(client, items, { chunk: true, chunkSize: 25 }); // override
console.log(SpmCustomers.CHUNK_SIZE); // 50Shared helpers
javascript
const { SpmHelpers } = require('@shopimind/sdk-shopimind');
SpmHelpers.chunk(array, 50);
// → [[...50], [...50], ...]
SpmHelpers.mergeResponses([envelope1, envelope2, ...]);
// → aggregated envelope: data.sent_count, data.rejected_count, data.chunks[]
SpmHelpers.extractCounts(envelope);
// → { sent: N, rejected: M, failed: K }
SpmHelpers.isRetryable(error);
// → true if the error/envelope is retryable (429 / 5xx / network)
SpmHelpers.formatError(envelope);
// → { message, code, statusCode, retryable, attempts, details }
// safe to serialize for structured logsRobust synchronization pattern
Combine bulkSave({ chunk: true }) (splitting + aggregation) with the built-in retry and SpmHelpers.formatError for actionable logs — you get a sync resilient to load spikes and transient outages without writing a retry loop.