Skip to content

Restfit 1.0.5

Choose a tag to compare

@kyree-henry kyree-henry released this 26 Nov 17:04
· 13 commits to main since this release

New Feature: Response Interceptors

Added support for response interceptors that allow you to check responses and trigger actions based on conditions (headers, data, status, etc.).

Features:

  1. Global Response Interceptors

    • Configure interceptors at the service level via ApiServiceConfig
    • Applied to all requests made by the service
    • Similar to how resilience policies work
  2. Method-Specific Interceptors

    • Use @interceptor decorator for method-specific interceptors
    • Can chain multiple interceptors per method
    • Runs after global interceptors

Usage:

Global Interceptors:
const globalInterceptors: ResponseInterceptorConfig[] = [
{
handler: (response) => {
if (response.headers['x-rate-limit-remaining']) {
console.log('Rate limit:', response.headers['x-rate-limit-remaining']);
}
}
}
];

const service = createApiService(MyService, {
baseUrl: 'https://api.example.com',
responseInterceptors: globalInterceptors
});

Method-Specific Interceptors:
class MyService {
@get('/users')
@interceptor((response) => {
if (response.headers['x-something']) {
console.log('Custom header found!');
}
})
async getUsers(): Promise<User[]> {
return [];
}
}

Use Cases:

  • Monitor rate limit headers and trigger warnings
  • Check for custom headers and update app state
  • Analyze response data and trigger notifications
  • Track analytics based on response characteristics
  • Update cache strategies based on cache-control headers
  • Global logging or monitoring across all API calls

Breaking Changes:

None - This is a new feature addition.

Migration Guide:

No migration needed. This is a new feature that doesn't affect existing code.