mirror of
https://github.com/datahub-project/datahub.git
synced 2025-08-03 14:57:53 +00:00
36 lines
903 B
TypeScript
36 lines
903 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;
|
||
|
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}`);
|
||
|
};
|