From 1aa8f676014ff31c4abea77ded4cb8f97a34f8c9 Mon Sep 17 00:00:00 2001 From: Ramon Candel Segura Date: Wed, 4 Feb 2026 09:57:34 +0100 Subject: [PATCH] add support for custom interceptors in HttpClient --- package.json | 2 +- src/shared/http/client.ts | 30 +++++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index f72ecf9..70088d2 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@internxt/sdk", "author": "Internxt ", - "version": "1.12.2", + "version": "1.12.3", "description": "An sdk for interacting with Internxt's services", "repository": { "type": "git", diff --git a/src/shared/http/client.ts b/src/shared/http/client.ts index 1931c25..f4d6cef 100644 --- a/src/shared/http/client.ts +++ b/src/shared/http/client.ts @@ -1,12 +1,30 @@ -import axios, { Axios, AxiosError, AxiosResponse, CancelToken } from 'axios'; +import axios, { Axios, AxiosError, AxiosResponse, CancelToken, InternalAxiosRequestConfig } from 'axios'; import AppError from '../types/errors'; import { Headers, Parameters, RequestCanceler, URL, UnauthorizedCallback } from './types'; export { RequestCanceler } from './types'; +export interface CustomInterceptor { + request?: { + onFulfilled?: ( + config: InternalAxiosRequestConfig, + ) => InternalAxiosRequestConfig | Promise; + onRejected?: (error: unknown) => unknown; + }; + response?: { + onFulfilled?: (response: AxiosResponse) => AxiosResponse; + onRejected?: (error: unknown) => unknown; + }; +} + export class HttpClient { private readonly axios: Axios; private readonly unauthorizedCallback: UnauthorizedCallback; + static globalInterceptors: CustomInterceptor[] = []; + + static setGlobalInterceptors(interceptors: CustomInterceptor[]): void { + HttpClient.globalInterceptors = interceptors; + } public static create(baseURL: URL, unauthorizedCallback?: UnauthorizedCallback) { if (unauthorizedCallback === undefined) { @@ -20,6 +38,16 @@ export class HttpClient { baseURL: baseURL, }); this.unauthorizedCallback = unauthorizedCallback; + + HttpClient.globalInterceptors.forEach((interceptor) => { + if (interceptor.request) { + this.axios.interceptors.request.use(interceptor.request.onFulfilled, interceptor.request.onRejected); + } + if (interceptor.response) { + this.axios.interceptors.response.use(interceptor.response.onFulfilled, interceptor.response.onRejected); + } + }); + this.initializeMiddleware(); }