Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@internxt/sdk",
"author": "Internxt <hello@internxt.com>",
"version": "1.12.2",
"version": "1.12.3",
"description": "An sdk for interacting with Internxt's services",
"repository": {
"type": "git",
Expand Down
30 changes: 29 additions & 1 deletion src/shared/http/client.ts
Original file line number Diff line number Diff line change
@@ -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<InternalAxiosRequestConfig>;
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) {
Expand All @@ -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();
}

Expand Down
Loading