Skip to content

Commit f5a6ee0

Browse files
Refactor AsgardeoSPAClient and HTTP utility for multi-instance support
1 parent 868f9e2 commit f5a6ee0

11 files changed

Lines changed: 67 additions & 54 deletions

File tree

packages/browser/src/__legacy__/client.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,6 @@ export class AsgardeoSPAClient {
223223
* ```
224224
*
225225
* @memberof AsgardeoSPAClient
226-
*
227-
* @preserve
228226
*/
229227
public static hasInstance(id: number = 0): boolean {
230228
return this._instances.has(id);
@@ -247,11 +245,15 @@ export class AsgardeoSPAClient {
247245
* ```
248246
*
249247
* @memberof AsgardeoSPAClient
250-
*
251-
* @preserve
252248
*/
253249
public static destroyInstance(id: number = 0): boolean {
254-
return this._instances.delete(id);
250+
const instance = this._instances.get(id);
251+
if (instance) {
252+
// Clean up the instance's session data before removing it
253+
instance.clearSession();
254+
return this._instances.delete(id);
255+
}
256+
return false;
255257
}
256258

257259
/**
@@ -267,8 +269,6 @@ export class AsgardeoSPAClient {
267269
* ```
268270
*
269271
* @memberof AsgardeoSPAClient
270-
*
271-
* @preserve
272272
*/
273273
public static getInstanceKeys(): number[] {
274274
return Array.from(this._instances.keys());
@@ -284,10 +284,12 @@ export class AsgardeoSPAClient {
284284
* ```
285285
*
286286
* @memberof AsgardeoSPAClient
287-
*
288-
* @preserve
289287
*/
290288
public static destroyAllInstances(): void {
289+
// Clean up each instance's session data before clearing
290+
this._instances.forEach((instance) => {
291+
instance.clearSession();
292+
});
291293
this._instances.clear();
292294
}
293295

packages/browser/src/utils/http.ts

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,51 @@
2020
import {AsgardeoSPAClient} from '../__legacy__/client';
2121

2222
/**
23-
* HTTP utility for making requests using the AsgardeoSPAClient instance.
23+
* Creates an HTTP utility for making requests using a specific AsgardeoSPAClient instance.
24+
*
25+
* @param instanceId - Optional instance ID for multi-instance support. Defaults to 0.
26+
* @returns An object with request and requestAll methods bound to the specified instance.
2427
*
2528
* @remarks
26-
* This utility provides methods to make single or multiple HTTP requests.
29+
* This utility provides methods to make single or multiple HTTP requests for a specific instance.
30+
*
31+
* @example
32+
* ```typescript
33+
* // Use default instance
34+
* const httpClient = http();
35+
*
36+
* // Use specific instance
37+
* const httpInstance1 = http(1);
38+
* const httpInstance2 = http(2);
39+
* ```
2740
*/
28-
const http: {
41+
export const http = (instanceId: number = 0): {
2942
request: typeof AsgardeoSPAClient.prototype.httpRequest;
3043
requestAll: typeof AsgardeoSPAClient.prototype.httpRequestAll;
31-
} = {
32-
/**
33-
* Makes a single HTTP request using the AsgardeoSPAClient instance.
34-
*
35-
* @param config - The HTTP request configuration object.
36-
* @returns A promise resolving to the HTTP response.
37-
*/
38-
request: AsgardeoSPAClient.getInstance().httpRequest.bind(AsgardeoSPAClient.getInstance()),
44+
} => {
45+
const client = AsgardeoSPAClient.getInstance(instanceId);
46+
47+
return {
48+
/**
49+
* Makes a single HTTP request using the AsgardeoSPAClient instance.
50+
*
51+
* @param config - The HTTP request configuration object.
52+
* @returns A promise resolving to the HTTP response.
53+
*/
54+
request: client.httpRequest.bind(client),
3955

40-
/**
41-
* Makes multiple HTTP requests in parallel using the AsgardeoSPAClient instance.
42-
*
43-
* @param configs - An array of HTTP request configuration objects.
44-
* @returns A promise resolving to an array of HTTP responses.
45-
*/
46-
requestAll: AsgardeoSPAClient.getInstance().httpRequestAll.bind(AsgardeoSPAClient.getInstance()),
56+
/**
57+
* Makes multiple HTTP requests in parallel using the AsgardeoSPAClient instance.
58+
*
59+
* @param configs - An array of HTTP request configuration objects.
60+
* @returns A promise resolving to an array of HTTP responses.
61+
*/
62+
requestAll: client.httpRequestAll.bind(client),
63+
};
4764
};
4865

49-
export default http;
66+
/**
67+
* Default HTTP utility using instance 0.
68+
* For multi-instance support, use http(instanceId) instead.
69+
*/
70+
export default http();

packages/javascript/src/__legacy__/client.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,6 @@ export class AsgardeoAuthClient<T> {
250250
authRequestConfig['client_secret'] = configData.clientSecret;
251251
}
252252

253-
authRequestConfig['state'] = `instance_${this.getInstanceId()}-${configData.clientId}`;
254-
255253
const authorizeRequestParams: Map<string, string> = getAuthorizeRequestUrlParams(
256254
{
257255
clientId: configData.clientId,

packages/react/src/api/createOrganization.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,8 @@ const createOrganization = async ({
9898
...requestConfig
9999
}: CreateOrganizationConfig): Promise<Organization> => {
100100
const defaultFetcher = async (url: string, config: RequestInit): Promise<Response> => {
101-
const httpClient: HttpInstance = AsgardeoSPAClient.getInstance(instanceId).httpRequest.bind(
102-
AsgardeoSPAClient.getInstance(instanceId),
103-
);
101+
const client = AsgardeoSPAClient.getInstance(instanceId);
102+
const httpClient: HttpInstance = client.httpRequest.bind(client);
104103
const response: HttpResponse<any> = await httpClient({
105104
data: config.body ? JSON.parse(config.body as string) : undefined,
106105
headers: config.headers as Record<string, string>,

packages/react/src/api/getAllOrganizations.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,8 @@ const getAllOrganizations = async ({
9090
...requestConfig
9191
}: GetAllOrganizationsConfig): Promise<AllOrganizationsApiResponse> => {
9292
const defaultFetcher = async (url: string, config: RequestInit): Promise<Response> => {
93-
const httpClient: HttpInstance = AsgardeoSPAClient.getInstance(instanceId).httpRequest.bind(
94-
AsgardeoSPAClient.getInstance(instanceId),
95-
);
93+
const client = AsgardeoSPAClient.getInstance(instanceId);
94+
const httpClient: HttpInstance = client.httpRequest.bind(client);
9695
const response: HttpResponse<any> = await httpClient({
9796
headers: config.headers as Record<string, string>,
9897
method: config.method || 'GET',

packages/react/src/api/getMeOrganizations.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,8 @@ const getMeOrganizations = async ({
9494
...requestConfig
9595
}: GetMeOrganizationsConfig): Promise<Organization[]> => {
9696
const defaultFetcher = async (url: string, config: RequestInit): Promise<Response> => {
97-
const httpClient: HttpInstance = AsgardeoSPAClient.getInstance(instanceId).httpRequest.bind(
98-
AsgardeoSPAClient.getInstance(instanceId),
99-
);
97+
const client = AsgardeoSPAClient.getInstance(instanceId);
98+
const httpClient: HttpInstance = client.httpRequest.bind(client);
10099
const response: HttpResponse<any> = await httpClient({
101100
headers: config.headers as Record<string, string>,
102101
method: config.method || 'GET',

packages/react/src/api/getOrganization.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,8 @@ const getOrganization = async ({
8686
...requestConfig
8787
}: GetOrganizationConfig): Promise<OrganizationDetails> => {
8888
const defaultFetcher = async (url: string, config: RequestInit): Promise<Response> => {
89-
const httpClient: HttpInstance = AsgardeoSPAClient.getInstance(instanceId).httpRequest.bind(
90-
AsgardeoSPAClient.getInstance(instanceId),
91-
);
89+
const client = AsgardeoSPAClient.getInstance(instanceId);
90+
const httpClient: HttpInstance = client.httpRequest.bind(client);
9291
const response: HttpResponse<any> = await httpClient({
9392
headers: config.headers as Record<string, string>,
9493
method: config.method || 'GET',

packages/react/src/api/getSchemas.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,8 @@ export interface GetSchemasConfig extends Omit<BaseGetSchemasConfig, 'fetcher'>
8080
*/
8181
const getSchemas = async ({fetcher, instanceId = 0, ...requestConfig}: GetSchemasConfig): Promise<Schema[]> => {
8282
const defaultFetcher = async (url: string, config: RequestInit): Promise<Response> => {
83-
const httpClient: HttpInstance = AsgardeoSPAClient.getInstance(instanceId).httpRequest.bind(
84-
AsgardeoSPAClient.getInstance(instanceId),
85-
);
83+
const client = AsgardeoSPAClient.getInstance(instanceId);
84+
const httpClient: HttpInstance = client.httpRequest.bind(client);
8685
const response: HttpResponse<any> = await httpClient({
8786
headers: config.headers as Record<string, string>,
8887
method: config.method || 'GET',

packages/react/src/api/getScim2Me.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,8 @@ export interface GetScim2MeConfig extends Omit<BaseGetScim2MeConfig, 'fetcher'>
8080
*/
8181
const getScim2Me = async ({fetcher, instanceId = 0, ...requestConfig}: GetScim2MeConfig): Promise<User> => {
8282
const defaultFetcher = async (url: string, config: RequestInit): Promise<Response> => {
83-
const httpClient: HttpInstance = AsgardeoSPAClient.getInstance(instanceId).httpRequest.bind(
84-
AsgardeoSPAClient.getInstance(instanceId),
85-
);
83+
const client = AsgardeoSPAClient.getInstance(instanceId);
84+
const httpClient: HttpInstance = client.httpRequest.bind(client);
8685
const response: HttpResponse<any> = await httpClient({
8786
headers: config.headers as Record<string, string>,
8887
method: config.method || 'GET',

packages/react/src/api/updateMeProfile.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,8 @@ export interface UpdateMeProfileConfig extends Omit<BaseUpdateMeProfileConfig, '
6868
*/
6969
const updateMeProfile = async ({fetcher, instanceId = 0, ...requestConfig}: UpdateMeProfileConfig): Promise<User> => {
7070
const defaultFetcher = async (url: string, config: RequestInit): Promise<Response> => {
71-
const httpClient: HttpInstance = AsgardeoSPAClient.getInstance(instanceId).httpRequest.bind(
72-
AsgardeoSPAClient.getInstance(instanceId),
73-
);
71+
const client = AsgardeoSPAClient.getInstance(instanceId);
72+
const httpClient: HttpInstance = client.httpRequest.bind(client);
7473
const response: HttpResponse<any> = await httpClient({
7574
data: config.body ? JSON.parse(config.body as string) : undefined,
7675
headers: config.headers as Record<string, string>,

0 commit comments

Comments
 (0)