/** All application Axios imports belong here. New calls and axios.create() * clients inherit the instance's browser identity, proxy and timeout. */ import axios, { type AxiosInstance } from 'axios'; import { applyBrokerHttpDefaults, applyBrokerProxy } from './outbound-proxy'; import { getRequestUserAgent } from './request-user-agent'; const g = globalThis as typeof globalThis & { __configuredHttpClients?: WeakSet }; const configured = g.__configuredHttpClients ??= new WeakSet(); function configure(client: T): T { if (configured.has(client)) return client; applyBrokerHttpDefaults(client.defaults); // Builds import route modules but must not choose an instance's identity. if (process.env.NEXT_PHASE !== 'phase-production-build') { client.defaults.headers.common['User-Agent'] = getRequestUserAgent(); try { applyBrokerProxy(client.defaults); } catch (err) { console.error(`[proxy] could not apply proxy_url at load: ${err instanceof Error ? err.message : String(err)}`); } } client.interceptors.request.use((config) => { // Force replacement even for differently cased headers or `false`. // This also replaces a stale per-call UA without losing auth headers. config.headers.set('User-Agent', getRequestUserAgent(), true); return config; }, undefined, { synchronous: true }); // Axios copies defaults into child clients, but not interceptors. const create = client.create.bind(client); client.create = (options) => configure(create(options)); configured.add(client); return client; } export default configure(axios);