@tech-arelius/api-client
Version:
Configurable HTTP client with builder pattern for Node.js/TypeScript
25 lines (23 loc) • 1.04 kB
text/typescript
import { InternalAxiosRequestConfig } from 'axios';
import { SessionProvider } from '../session/SessionProvider';
import { Interceptor } from './Interceptor';
/**
* Creates an authorization interceptor that automatically adds the Authorization header
* @param sessionProvider - The session provider to use for getting the authorization token
* @returns An interceptor that adds the Authorization header to requests
*/
export function authorizationInterceptor(sessionProvider: SessionProvider): Interceptor {
return {
onRequest: async (config: InternalAxiosRequestConfig): Promise<InternalAxiosRequestConfig> => {
try {
const authHeader = await sessionProvider.getAuthorizationHeader();
if (authHeader) {
config.headers.set('Authorization', authHeader);
}
} catch (error) {
console.warn('Failed to add authorization header:', error);
}
return config;
}
};
}