Skip to content

Commit d141df9

Browse files
committed
feat(react): add custom fetch method to AsgardeoContext and update HttpRequestConfig usage
1 parent b50ea2b commit d141df9

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export interface HttpRequestInterface {
3636
enableRetrievingSignOutURLFromSession?: (config: SPACustomGrantConfig) => void;
3737
}
3838

39-
export interface HttpRequestConfig extends AxiosRequestConfig {
39+
export interface HttpRequestConfig extends AxiosRequestConfig, Omit<Request, 'headers' | 'method' | 'signal' | 'url'> {
4040
attachToken?: boolean;
4141
shouldEncodeToFormData?: boolean;
4242
shouldAttachIDPAccessToken?: boolean;

packages/react/src/AsgardeoReactClient.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ import {
3939
AllOrganizationsApiResponse,
4040
extractUserClaimsFromIdToken,
4141
TokenResponse,
42+
HttpRequestConfig,
43+
HttpResponse,
4244
} from '@asgardeo/browser';
4345
import AuthAPI from './__temp__/api';
4446
import getMeOrganizations from './api/getMeOrganizations';
@@ -347,6 +349,13 @@ class AsgardeoReactClient<T extends AsgardeoReactConfig = AsgardeoReactConfig> e
347349
'The signUp method with SignUpOptions is not implemented in the React client.',
348350
);
349351
}
352+
353+
async fetch(url: string, options?: HttpRequestConfig): Promise<HttpResponse<any>> {
354+
return this.asgardeo.httpRequest({
355+
url,
356+
...options,
357+
});
358+
}
350359
}
351360

352361
export default AsgardeoReactClient;

packages/react/src/contexts/Asgardeo/AsgardeoContext.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*/
1818

1919
import {Context, createContext} from 'react';
20-
import {Organization} from '@asgardeo/browser';
20+
import {HttpRequestConfig, HttpResponse, Organization} from '@asgardeo/browser';
2121
import AsgardeoReactClient from '../../AsgardeoReactClient';
2222

2323
/**
@@ -60,6 +60,13 @@ export type AsgardeoContextProps = {
6060
signUp: any;
6161
user: any;
6262
organization: Organization;
63+
/**
64+
* Custom fetch function to make HTTP requests.
65+
* @param url - The URL to fetch.
66+
* @param options - Optional configuration for the HTTP request.
67+
* @returns A promise that resolves to the HTTP response.
68+
*/
69+
fetch: (url: string, options?: HttpRequestConfig) => Promise<HttpResponse<any>>;
6370
};
6471

6572
/**
@@ -81,6 +88,7 @@ const AsgardeoContext: Context<AsgardeoContextProps | null> = createContext<null
8188
signOut: null,
8289
signUp: null,
8390
user: null,
91+
fetch: () => null,
8492
});
8593

8694
AsgardeoContext.displayName = 'AsgardeoContext';

packages/react/src/contexts/Asgardeo/AsgardeoProvider.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
getBrandingPreference,
3030
GetBrandingPreferenceConfig,
3131
BrandingPreference,
32+
HttpRequestConfig,
3233
} from '@asgardeo/browser';
3334
import {FC, RefObject, PropsWithChildren, ReactElement, useEffect, useMemo, useRef, useState, useCallback} from 'react';
3435
import AsgardeoContext from './AsgardeoContext';
@@ -390,6 +391,10 @@ const AsgardeoProvider: FC<PropsWithChildren<AsgardeoProviderProps>> = ({
390391
}));
391392
};
392393

394+
const fetch = async (url: string, options?: HttpRequestConfig): Promise<any> => {
395+
return asgardeo.fetch(url, options);
396+
};
397+
393398
return (
394399
<AsgardeoContext.Provider
395400
value={{
@@ -408,6 +413,7 @@ const AsgardeoProvider: FC<PropsWithChildren<AsgardeoProviderProps>> = ({
408413
signOut,
409414
signUp,
410415
user,
416+
fetch,
411417
}}
412418
>
413419
<I18nProvider preferences={preferences?.i18n}>

0 commit comments

Comments
 (0)