Skip to content

Commit 0744686

Browse files
Refactor HttpClient to support instance-specific instances for multi-auth context
1 parent e873b8d commit 0744686

File tree

3 files changed

+41
-38
lines changed

3 files changed

+41
-38
lines changed

packages/browser/src/__legacy__/clients/main-thread-client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export const MainThreadClient = async (
8585

8686
let _getSignOutURLFromSessionStorage: boolean = false;
8787

88-
const _httpClient: HttpClientInstance = HttpClient.getInstance();
88+
const _httpClient: HttpClientInstance = HttpClient.getInstance(instanceID);
8989
let _isHttpHandlerEnabled: boolean = true;
9090
let _httpErrorCallback: (error: HttpError) => void | Promise<void>;
9191
let _httpFinishCallback: () => void;

packages/browser/src/__legacy__/http-client/clients/axios-http-client.ts

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ import {HttpClientInstance, HttpClientInterface, HttpClientStatic} from '../mode
4141
*/
4242
@staticDecorator<HttpClientStatic<HttpClientInstance>>()
4343
export class HttpClient implements HttpClientInterface<HttpRequestConfig, HttpResponse, HttpError> {
44-
private static axiosInstance: HttpClientInstance;
45-
private static clientInstance: HttpClient;
46-
private static isHandlerEnabled: boolean;
44+
private static instances: Map<number, HttpClientInstance> = new Map();
45+
private static clientInstances: Map<number, HttpClient> = new Map();
46+
private isHandlerEnabled: boolean = true;
4747
private attachToken: (request: HttpRequestConfig) => Promise<void> = () => Promise.resolve();
4848
private requestStartCallback: (request: HttpRequestConfig) => void = () => null;
4949
private requestSuccessCallback: (response: HttpResponse) => void = () => null;
@@ -66,48 +66,51 @@ export class HttpClient implements HttpClientInterface<HttpRequestConfig, HttpRe
6666
}
6767

6868
/**
69-
* Returns an aggregated instance of type `HttpInstance` of `HttpClient`.
69+
* Returns an instance-specific HttpClient instance.
70+
* Each instance ID gets its own axios instance and HttpClient to avoid state conflicts.
7071
*
71-
* @return {any}
72+
* @param instanceId - The instance ID for multi-auth context support. Defaults to 0.
73+
* @return {HttpClientInstance}
7274
*/
73-
public static getInstance(): HttpClientInstance {
74-
if (this.axiosInstance) {
75-
return this.axiosInstance;
75+
public static getInstance(instanceId: number = 0): HttpClientInstance {
76+
if (this.instances.has(instanceId)) {
77+
return this.instances.get(instanceId)!;
7678
}
7779

78-
this.axiosInstance = axios.create({
80+
const axiosInstance = axios.create({
7981
withCredentials: true,
80-
});
82+
}) as HttpClientInstance;
8183

82-
if (!this.clientInstance) {
83-
this.clientInstance = new HttpClient();
84-
}
84+
const clientInstance = new HttpClient();
85+
this.clientInstances.set(instanceId, clientInstance);
8586

8687
// Register request interceptor
87-
this.axiosInstance.interceptors.request.use(async request => await this.clientInstance.requestHandler(request as HttpRequestConfig));
88+
axiosInstance.interceptors.request.use(async request => await clientInstance.requestHandler(request as HttpRequestConfig));
8889

8990
// Register response interceptor
90-
this.axiosInstance.interceptors.response.use(
91-
response => this.clientInstance.successHandler(response),
92-
error => this.clientInstance.errorHandler(error),
91+
axiosInstance.interceptors.response.use(
92+
response => clientInstance.successHandler(response),
93+
error => clientInstance.errorHandler(error),
9394
);
9495

9596
// Add the missing helper methods from axios
96-
this.axiosInstance.all = axios.all;
97-
this.axiosInstance.spread = axios.spread;
97+
axiosInstance.all = axios.all;
98+
axiosInstance.spread = axios.spread;
9899

99100
// Add the init method from the `HttpClient` instance.
100-
this.axiosInstance.init = this.clientInstance.init;
101+
axiosInstance.init = clientInstance.init;
101102

102103
// Add the handler enabling & disabling methods to the instance.
103-
this.axiosInstance.enableHandler = this.clientInstance.enableHandler;
104-
this.axiosInstance.disableHandler = this.clientInstance.disableHandler;
105-
this.axiosInstance.disableHandlerWithTimeout = this.clientInstance.disableHandlerWithTimeout;
106-
this.axiosInstance.setHttpRequestStartCallback = this.clientInstance.setHttpRequestStartCallback;
107-
this.axiosInstance.setHttpRequestSuccessCallback = this.clientInstance.setHttpRequestSuccessCallback;
108-
this.axiosInstance.setHttpRequestErrorCallback = this.clientInstance.setHttpRequestErrorCallback;
109-
this.axiosInstance.setHttpRequestFinishCallback = this.clientInstance.setHttpRequestFinishCallback;
110-
return this.axiosInstance;
104+
axiosInstance.enableHandler = clientInstance.enableHandler;
105+
axiosInstance.disableHandler = clientInstance.disableHandler;
106+
axiosInstance.disableHandlerWithTimeout = clientInstance.disableHandlerWithTimeout;
107+
axiosInstance.setHttpRequestStartCallback = clientInstance.setHttpRequestStartCallback;
108+
axiosInstance.setHttpRequestSuccessCallback = clientInstance.setHttpRequestSuccessCallback;
109+
axiosInstance.setHttpRequestErrorCallback = clientInstance.setHttpRequestErrorCallback;
110+
axiosInstance.setHttpRequestFinishCallback = clientInstance.setHttpRequestFinishCallback;
111+
112+
this.instances.set(instanceId, axiosInstance);
113+
return axiosInstance;
111114
}
112115

113116
/**
@@ -134,7 +137,7 @@ export class HttpClient implements HttpClientInterface<HttpRequestConfig, HttpRe
134137

135138
request.startTimeInMs = new Date().getTime();
136139

137-
if (HttpClient.isHandlerEnabled) {
140+
if (this.isHandlerEnabled) {
138141
if (this.requestStartCallback && typeof this.requestStartCallback === 'function') {
139142
this.requestStartCallback(request);
140143
}
@@ -151,7 +154,7 @@ export class HttpClient implements HttpClientInterface<HttpRequestConfig, HttpRe
151154
* @return {HttpError}
152155
*/
153156
public errorHandler(error: HttpError): HttpError {
154-
if (HttpClient.isHandlerEnabled) {
157+
if (this.isHandlerEnabled) {
155158
if (this.requestErrorCallback && typeof this.requestErrorCallback === 'function') {
156159
this.requestErrorCallback(error);
157160
}
@@ -171,7 +174,7 @@ export class HttpClient implements HttpClientInterface<HttpRequestConfig, HttpRe
171174
* @return {HttpResponse}
172175
*/
173176
public successHandler(response: HttpResponse): HttpResponse {
174-
if (HttpClient.isHandlerEnabled) {
177+
if (this.isHandlerEnabled) {
175178
if (this.requestSuccessCallback && typeof this.requestSuccessCallback === 'function') {
176179
this.requestSuccessCallback(response);
177180
}
@@ -195,22 +198,22 @@ export class HttpClient implements HttpClientInterface<HttpRequestConfig, HttpRe
195198
isHandlerEnabled = true,
196199
attachToken: (request: HttpRequestConfig) => Promise<void>,
197200
): Promise<void> {
198-
HttpClient.isHandlerEnabled = isHandlerEnabled;
201+
this.isHandlerEnabled = isHandlerEnabled;
199202
this.attachToken = attachToken;
200203
}
201204

202205
/**
203206
* Enables the handler.
204207
*/
205208
public enableHandler(): void {
206-
HttpClient.isHandlerEnabled = true;
209+
this.isHandlerEnabled = true;
207210
}
208211

209212
/**
210213
* Disables the handler.
211214
*/
212215
public disableHandler(): void {
213-
HttpClient.isHandlerEnabled = false;
216+
this.isHandlerEnabled = false;
214217
}
215218

216219
/**
@@ -219,10 +222,10 @@ export class HttpClient implements HttpClientInterface<HttpRequestConfig, HttpRe
219222
* @param {number} timeout - Timeout in milliseconds.
220223
*/
221224
public disableHandlerWithTimeout(timeout: number = HttpClient.DEFAULT_HANDLER_DISABLE_TIMEOUT): void {
222-
HttpClient.isHandlerEnabled = false;
225+
this.isHandlerEnabled = false;
223226

224227
setTimeout(() => {
225-
HttpClient.isHandlerEnabled = true;
228+
this.isHandlerEnabled = true;
226229
}, timeout);
227230
}
228231

packages/browser/src/__legacy__/http-client/models/http-client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { HttpError, HttpResponse } from "../../models";
2323
* Http client interface with static functions.
2424
*/
2525
export interface HttpClientStatic<S> {
26-
getInstance(): S;
26+
getInstance(instanceId?: number): S;
2727
}
2828

2929
/**

0 commit comments

Comments
 (0)