Behio Storefront SDK
Advanced

Events & Interceptors

SDK event system and request/response interceptors

Events

Subscribe to lifecycle events:

// Auth events
client.on('auth:login', (data) => { /* { email } */ });
client.on('auth:logout', () => { });
client.on('auth:token-refresh', () => { });
client.on('auth:token-refresh-failed', () => { });

// Cart events
client.on('cart:updated', (cart) => { });
client.on('cart:cleared', () => { });

// Order events
client.on('order:created', (order) => { });

// Network events
client.on('request', ({ method, path }) => { });
client.on('response', ({ method, path, status }) => { });
client.on('error', (error) => { });

// Rate limiting
client.on('rate-limit-warning', ({ remaining, reset }) => { });

Unsubscribe

const unsubscribe = client.on('cart:updated', handler);
// Later:
unsubscribe();

Request Interceptors

Modify requests before they are sent:

client.addRequestInterceptor(async (config) => {
  // Add custom header
  config.headers['X-Custom'] = 'value';
  
  // Log all requests
  console.log(`${config.method} ${config.url}`);
  
  return config;
});

Response Interceptors

Process responses after they are received:

client.addResponseInterceptor(async ({ status, data, headers }) => {
  // Analytics
  analytics.track('api_call', { status });
});

Rate Limit Info

const { remaining, reset } = client.getRateLimitInfo();
if (remaining !== null && remaining <= 5) {
  console.warn('Rate limit approaching');
}

On this page