Skip to content

Commit

Permalink
Add generic/typed userInfo for FusionAuthService
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeLo123 committed Jun 20, 2024
1 parent cda9268 commit 19356ea
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,21 @@ describe('FusionAuthService', () => {
it('Can be configured to automatically handle getting userInfo', (done: DoneFn) => {
mockIsLoggedIn();

const user = { email: '[email protected]' };
const user = {
email: '[email protected]',
customTrait: 'something special',
};
spyOn(window, 'fetch').and.resolveTo(
new Response(JSON.stringify(user), { status: 200 }),
);

const service = configureTestingModule(config);
const service: FusionAuthService<typeof user> =
configureTestingModule(config);

service.getUserInfoObservable().subscribe({
next: userInfo => {
expect(userInfo.email).toBe('[email protected]');
expect(userInfo.customTrait).toBe('something special');
done();
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { FUSIONAUTH_SERVICE_CONFIG } from './injectionToken';
@Injectable({
providedIn: 'root',
})
export class FusionAuthService {
export class FusionAuthService<T = UserInfo> {
private core: SDKCore;
private autoRefreshTimer?: NodeJS.Timeout;
private isLoggedInSubject: BehaviorSubject<boolean>;
Expand Down Expand Up @@ -74,11 +74,11 @@ export class FusionAuthService {
getUserInfoObservable(callbacks?: {
onBegin?: () => void;
onDone?: () => void;
}): Observable<UserInfo> {
}): Observable<T> {
callbacks?.onBegin?.();
return new Observable<UserInfo>(observer => {
return new Observable<T>(observer => {
this.core
.fetchUserInfo()
.fetchUserInfo<T>()
.then(userInfo => {
observer.next(userInfo);
})
Expand All @@ -99,8 +99,8 @@ export class FusionAuthService {
* Fetches userInfo from the 'me' endpoint.
* @throws {Error} - if an error occurred while fetching.
*/
async getUserInfo(): Promise<UserInfo> {
return await this.core.fetchUserInfo();
async getUserInfo<T>(): Promise<T> {
return await this.core.fetchUserInfo<T>();
}

/**
Expand Down

0 comments on commit 19356ea

Please sign in to comment.