mirror of
https://github.com/datahub-project/datahub.git
synced 2025-08-01 05:47:17 +00:00
37 lines
926 B
TypeScript
37 lines
926 B
TypeScript
import { getApiRoot, ApiStatus } from '@datahub/utils/api/shared';
|
|
import { getJSON } from '@datahub/utils/api/fetcher';
|
|
|
|
const currentUserUrl = `${getApiRoot()}/user/me`;
|
|
|
|
/**
|
|
* Describes the interface for the user json returned
|
|
* from the current user endpoint
|
|
*/
|
|
export interface IUser {
|
|
departmentNum: number;
|
|
email: string;
|
|
id: number;
|
|
name: string;
|
|
userName: string;
|
|
pictureLink: string;
|
|
userSetting: null | {
|
|
defaultWatch: string;
|
|
detailDefaultView: string;
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Requests the currently logged in user and if the response is ok,
|
|
* returns the user, otherwise throws
|
|
*/
|
|
export const currentUser = async (): Promise<IUser> => {
|
|
const response = (await getJSON({ url: currentUserUrl })) as { user: IUser; status: ApiStatus };
|
|
const { status = ApiStatus.FAILED, user } = response;
|
|
|
|
if (status === ApiStatus.OK) {
|
|
return user;
|
|
}
|
|
|
|
throw new Error(`Exception: ${status}`);
|
|
};
|