From d25cae1b389458bc4f33c0213f68fe461f4e0b75 Mon Sep 17 00:00:00 2001 From: Ian <52504170+ibacher@users.noreply.github.com> Date: Fri, 8 Apr 2022 09:51:05 -0400 Subject: [PATCH] (feat) Unify session types (#387) --- .../esm-login-app/src/CurrentUserContext.tsx | 22 +--- .../translations/en.json | 8 +- .../src/root.component.tsx | 5 +- .../src/shared-api-objects/current-user.ts | 48 ++++---- packages/framework/esm-api/src/types/fetch.ts | 8 +- .../esm-api/src/types/openmrs-resource.ts | 22 ---- .../esm-api/src/types/user-resource.ts | 21 ++-- packages/framework/esm-framework/docs/API.md | 57 ++++++---- .../docs/interfaces/LoggedInUser.md | 24 ++-- .../interfaces/LoggedInUserFetchResponse.md | 4 +- .../esm-framework/docs/interfaces/Person.md | 6 +- .../docs/interfaces/Privilege.md | 8 +- .../esm-framework/docs/interfaces/Role.md | 6 +- .../esm-framework/docs/interfaces/Session.md | 92 +++++++++++++++ .../docs/interfaces/SessionLocation.md | 6 +- .../docs/interfaces/SessionUser.md | 92 --------------- .../docs/interfaces/UnauthenticatedUser.md | 52 --------- .../esm-framework/docs/interfaces/User.md | 107 ------------------ .../esm-react-utils/src/useSessionUser.tsx | 26 ++--- 19 files changed, 213 insertions(+), 401 deletions(-) create mode 100644 packages/framework/esm-framework/docs/interfaces/Session.md delete mode 100644 packages/framework/esm-framework/docs/interfaces/SessionUser.md delete mode 100644 packages/framework/esm-framework/docs/interfaces/UnauthenticatedUser.md delete mode 100644 packages/framework/esm-framework/docs/interfaces/User.md diff --git a/packages/apps/esm-login-app/src/CurrentUserContext.tsx b/packages/apps/esm-login-app/src/CurrentUserContext.tsx index 58d0aef34..06ed1fd6f 100644 --- a/packages/apps/esm-login-app/src/CurrentUserContext.tsx +++ b/packages/apps/esm-login-app/src/CurrentUserContext.tsx @@ -1,5 +1,5 @@ import React, { useContext, useState, useMemo, useEffect } from "react"; -import { getCurrentUser } from "@openmrs/esm-framework"; +import { getCurrentUser, LoggedInUser } from "@openmrs/esm-framework"; const CurrentUser = React.createContext({ current: undefined, @@ -16,26 +16,6 @@ export interface User { setCurrent(user: LoggedInUser): void; } -export interface ResourceRef { - uuid: string; - display: string; - links: Array; -} - -export interface LoggedInUser { - uuid: string; - display: string; - username: string; - systemId: string; - userProperties: any; - person: ResourceRef; - privileges: Array; - roles: Array; - retired: boolean; - locale: string; - allowedLocales: Array; -} - export function useCurrentUser() { const value = useContext(CurrentUser); return value.current; diff --git a/packages/apps/esm-offline-tools-app/translations/en.json b/packages/apps/esm-offline-tools-app/translations/en.json index be7965bce..5edcc5b95 100644 --- a/packages/apps/esm-offline-tools-app/translations/en.json +++ b/packages/apps/esm-offline-tools-app/translations/en.json @@ -19,8 +19,8 @@ "offlineActionsTableAction": "Action", "offlineActionsTableCreatedOn": "Date & Time", "offlineActionsTableDeleteAction": "Delete action", - "offlineActionsTableDeleteActions": "Delete {count} actions", - "offlineActionsTableDeleteActions_plural": "Delete {count} actions", + "offlineActionsTableDeleteActions_one": "Delete {count} actions", + "offlineActionsTableDeleteActions_other": "Delete {count} actions", "offlineActionsTableError": "Error", "offlineActionsTablePatient": "Patient", "offlineActionsUpdateOfflinePatients": "Update offline patients", @@ -32,7 +32,5 @@ "offlinePatientSyncDetailsFallbackErrorMessage": "Unknown error.", "offlinePatientSyncDetailsHeader": "Offline patient details", "offlineReady": "Offline Ready", - "offlineToolsAppMenuLink": "Offline tools", - "offlineActionsTableDeleteActions_one": "Delete {count} actions", - "offlineActionsTableDeleteActions_other": "Delete {count} actions" + "offlineToolsAppMenuLink": "Offline tools" } diff --git a/packages/apps/esm-primary-navigation-app/src/root.component.tsx b/packages/apps/esm-primary-navigation-app/src/root.component.tsx index 222735b70..e66f0a83c 100644 --- a/packages/apps/esm-primary-navigation-app/src/root.component.tsx +++ b/packages/apps/esm-primary-navigation-app/src/root.component.tsx @@ -11,7 +11,7 @@ export interface RootProps {} const Root: React.FC = () => { const [user, setUser] = useState(null); const [userSession, setUserSession] = useState(null); - const [allowedLocales, setAllowedLocales] = useState(); + const [allowedLocales, setAllowedLocales] = useState | null>(); const logout = useCallback(() => setUser(false), []); const openmrsSpaBase = window["getOpenmrsSpaBase"](); @@ -19,7 +19,8 @@ const Root: React.FC = () => { const currentUserSub = getSynchronizedCurrentUser({ includeAuthStatus: true, }).subscribe((response) => { - setAllowedLocales(response["allowedLocales"]); + setAllowedLocales(response.allowedLocales); + if (response.authenticated) { setUser(response.user); } else { diff --git a/packages/framework/esm-api/src/shared-api-objects/current-user.ts b/packages/framework/esm-api/src/shared-api-objects/current-user.ts index fc5715518..e1d87ba97 100644 --- a/packages/framework/esm-api/src/shared-api-objects/current-user.ts +++ b/packages/framework/esm-api/src/shared-api-objects/current-user.ts @@ -2,17 +2,18 @@ import { Observable, ReplaySubject } from "rxjs"; import { filter, map, tap, mergeAll } from "rxjs/operators"; import { openmrsFetch, sessionEndpoint } from "../openmrs-fetch"; -import { - LoggedInUserData, +import type { LoggedInUser, CurrentUserWithResponseOption, - UnauthenticatedUser, CurrentUserWithoutResponseOption, CurrentUserOptions, SessionLocation, + Privilege, + Role, + Session, } from "../types"; -const userSubject = new ReplaySubject>(1); +const userSubject = new ReplaySubject>(1); let lastFetchTimeMillis = 0; /** @@ -56,13 +57,13 @@ let lastFetchTimeMillis = 0; function getCurrentUser(): Observable; function getCurrentUser( opts: CurrentUserWithResponseOption -): Observable; +): Observable; function getCurrentUser( opts: CurrentUserWithoutResponseOption ): Observable; function getCurrentUser( opts: CurrentUserOptions = { includeAuthStatus: false } -): Observable { +): Observable { if (lastFetchTimeMillis < Date.now() - 1000 * 60) { refetchCurrentUser(); } @@ -72,31 +73,32 @@ function getCurrentUser( tap(setUserLanguage), map((r) => (opts.includeAuthStatus ? r : r.user)), filter(Boolean) - ) as Observable; + ) as Observable; } -function setUserLanguage(data: LoggedInUserData) { - if (data?.user?.userProperties?.defaultLocale) { - const locale = data.user.userProperties.defaultLocale; - const htmlLang = document.documentElement.getAttribute("lang"); +export { getCurrentUser }; + +function setUserLanguage(data: Session) { + const locale = data?.user?.userProperties?.defaultLocale ?? data.locale; + const htmlLang = document.documentElement.getAttribute("lang"); - if (locale !== htmlLang) { - document.documentElement.setAttribute("lang", locale); - } + if (locale !== htmlLang) { + document.documentElement.setAttribute("lang", locale); } } -function userHasPrivilege(requiredPrivilege: string, user: LoggedInUser) { +function userHasPrivilege( + requiredPrivilege: string, + user: { privileges: Array } +) { return user.privileges.find((p) => requiredPrivilege === p.display); } -function isSuperUser(user: LoggedInUser) { +function isSuperUser(user: { roles: Array }) { const superUserRole = "System Developer"; return user.roles.find((role) => role.display === superUserRole); } -export { getCurrentUser }; - /** * The `refetchCurrentUser` function causes a network request to redownload * the user. All subscribers to the current user will be notified of the @@ -124,7 +126,10 @@ export function refetchCurrentUser() { ); } -export function userHasAccess(requiredPrivilege: string, user: LoggedInUser) { +export function userHasAccess( + requiredPrivilege: string, + user: { privileges: Array; roles: Array } +) { return userHasPrivilege(requiredPrivilege, user) || isSuperUser(user); } @@ -140,8 +145,8 @@ export function getLoggedInUser() { export function getSessionLocation() { return new Promise((res, rej) => { const sub = getCurrentUser({ includeAuthStatus: true }).subscribe( - (user) => { - res(user.sessionLocation); + (session) => { + res(session.sessionLocation); sub.unsubscribe(); }, rej @@ -161,5 +166,6 @@ export async function setSessionLocation( }, signal: abortController.signal, }); + refetchCurrentUser(); } diff --git a/packages/framework/esm-api/src/types/fetch.ts b/packages/framework/esm-api/src/types/fetch.ts index 25ac68131..951f132fe 100644 --- a/packages/framework/esm-api/src/types/fetch.ts +++ b/packages/framework/esm-api/src/types/fetch.ts @@ -1,13 +1,9 @@ -import { LoggedInUser, UnauthenticatedUser } from "./user-resource"; +import { Session } from "./user-resource"; export interface FetchResponse extends Response { data: T; } -export type LoggedInUserData = UnauthenticatedUser & { - user?: LoggedInUser; -}; - export interface LoggedInUserFetchResponse extends FetchResponse { - data: LoggedInUserData; + data: Session; } diff --git a/packages/framework/esm-api/src/types/openmrs-resource.ts b/packages/framework/esm-api/src/types/openmrs-resource.ts index 358cd95ae..5a568b5eb 100644 --- a/packages/framework/esm-api/src/types/openmrs-resource.ts +++ b/packages/framework/esm-api/src/types/openmrs-resource.ts @@ -3,25 +3,3 @@ export interface OpenmrsResource { display?: string; [anythingElse: string]: any; } - -export interface SessionUser { - allowedLocales: Array; - authenticated: boolean; - locale: string; - sessionId: string; - user: User; - currentProvider: { uuid: string; identifier: string }; - sessionLocation: any | null; -} - -export interface User { - display: string; - link: Array; - person: any; - priviliges: any; - resourceVersion: any; - roles: Array; - userProperties: any; - username: string; - uuid: string; -} diff --git a/packages/framework/esm-api/src/types/user-resource.ts b/packages/framework/esm-api/src/types/user-resource.ts index b92b5df34..f2dd122f1 100644 --- a/packages/framework/esm-api/src/types/user-resource.ts +++ b/packages/framework/esm-api/src/types/user-resource.ts @@ -10,12 +10,22 @@ export interface CurrentUserWithoutResponseOption extends CurrentUserOptions { includeAuthStatus: false; } +export interface Session { + allowedLocales?: Array; + authenticated: boolean; + locale?: string; + sessionId: string; + user?: LoggedInUser; + currentProvider?: { uuid: string; identifier: string }; + sessionLocation?: SessionLocation; +} + export interface LoggedInUser { uuid: string; display: string; username: string; systemId: string; - userProperties: any; + userProperties: { [key: string]: any } | null; person: Person; privileges: Array; roles: Array; @@ -25,13 +35,6 @@ export interface LoggedInUser { [anythingElse: string]: any; } -export interface UnauthenticatedUser { - sessionId: string; - authenticated: boolean; - user?: LoggedInUser; - sessionLocation?: SessionLocation; -} - export interface SessionLocation { uuid: string; display: string; @@ -47,7 +50,7 @@ export interface Person { export interface Privilege { uuid: string; display: string; - links: Array; + links?: Array; } export interface Role { diff --git a/packages/framework/esm-framework/docs/API.md b/packages/framework/esm-framework/docs/API.md index a59947b65..294c8c6b6 100644 --- a/packages/framework/esm-framework/docs/API.md +++ b/packages/framework/esm-framework/docs/API.md @@ -25,6 +25,7 @@ - [useCurrentPatient](API.md#usecurrentpatient) - [useLocations](API.md#uselocations) - [usePatient](API.md#usepatient) +- [useSession](API.md#usesession) - [useSessionUser](API.md#usesessionuser) - [useVisit](API.md#usevisit) - [useVisitTypes](API.md#usevisittypes) @@ -319,16 +320,6 @@ ___ ___ -### LoggedInUserData - -Ƭ **LoggedInUserData**: [`UnauthenticatedUser`](interfaces/UnauthenticatedUser.md) & { `user?`: [`LoggedInUser`](interfaces/LoggedInUser.md) } - -#### Defined in - -[packages/framework/esm-api/src/types/fetch.ts:7](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/fetch.ts#L7) - -___ - ### ProvidedConfig Ƭ **ProvidedConfig**: `Object` @@ -728,9 +719,9 @@ leak and source of bugs. #### Defined in -[packages/framework/esm-api/src/shared-api-objects/current-user.ts:56](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/shared-api-objects/current-user.ts#L56) +[packages/framework/esm-api/src/shared-api-objects/current-user.ts:57](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/shared-api-objects/current-user.ts#L57) -▸ **getCurrentUser**(`opts`): `Observable`<[`UnauthenticatedUser`](interfaces/UnauthenticatedUser.md)\> +▸ **getCurrentUser**(`opts`): `Observable`<[`Session`](interfaces/Session.md)\> #### Parameters @@ -740,11 +731,11 @@ leak and source of bugs. #### Returns -`Observable`<[`UnauthenticatedUser`](interfaces/UnauthenticatedUser.md)\> +`Observable`<[`Session`](interfaces/Session.md)\> #### Defined in -[packages/framework/esm-api/src/shared-api-objects/current-user.ts:57](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/shared-api-objects/current-user.ts#L57) +[packages/framework/esm-api/src/shared-api-objects/current-user.ts:58](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/shared-api-objects/current-user.ts#L58) ▸ **getCurrentUser**(`opts`): `Observable`<[`LoggedInUser`](interfaces/LoggedInUser.md)\> @@ -760,7 +751,7 @@ leak and source of bugs. #### Defined in -[packages/framework/esm-api/src/shared-api-objects/current-user.ts:60](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/shared-api-objects/current-user.ts#L60) +[packages/framework/esm-api/src/shared-api-objects/current-user.ts:61](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/shared-api-objects/current-user.ts#L61) ___ @@ -788,7 +779,7 @@ ___ #### Defined in -[packages/framework/esm-api/src/shared-api-objects/current-user.ts:131](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/shared-api-objects/current-user.ts#L131) +[packages/framework/esm-api/src/shared-api-objects/current-user.ts:136](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/shared-api-objects/current-user.ts#L136) ___ @@ -802,7 +793,7 @@ ___ #### Defined in -[packages/framework/esm-api/src/shared-api-objects/current-user.ts:140](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/shared-api-objects/current-user.ts#L140) +[packages/framework/esm-api/src/shared-api-objects/current-user.ts:145](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/shared-api-objects/current-user.ts#L145) ___ @@ -1011,7 +1002,7 @@ refetchCurrentUser() #### Defined in -[packages/framework/esm-api/src/shared-api-objects/current-user.ts:113](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/shared-api-objects/current-user.ts#L113) +[packages/framework/esm-api/src/shared-api-objects/current-user.ts:115](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/shared-api-objects/current-user.ts#L115) ___ @@ -1053,7 +1044,7 @@ ___ #### Defined in -[packages/framework/esm-api/src/shared-api-objects/current-user.ts:152](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/shared-api-objects/current-user.ts#L152) +[packages/framework/esm-api/src/shared-api-objects/current-user.ts:157](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/shared-api-objects/current-user.ts#L157) ___ @@ -1190,13 +1181,13 @@ a route listener is set up to update the patient whenever the route changes. ___ -### useSessionUser +### useSession -▸ **useSessionUser**(): ``null`` \| [`SessionUser`](interfaces/SessionUser.md) +▸ **useSession**(): ``null`` \| [`Session`](interfaces/Session.md) #### Returns -``null`` \| [`SessionUser`](interfaces/SessionUser.md) +``null`` \| [`Session`](interfaces/Session.md) #### Defined in @@ -1204,6 +1195,22 @@ ___ ___ +### useSessionUser + +▸ `Const` **useSessionUser**(): ``null`` \| [`Session`](interfaces/Session.md) + +**`deprecated`** + +#### Returns + +``null`` \| [`Session`](interfaces/Session.md) + +#### Defined in + +[packages/framework/esm-react-utils/src/useSessionUser.tsx:21](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-react-utils/src/useSessionUser.tsx#L21) + +___ + ### useVisit ▸ **useVisit**(`patientUuid`): `VisitReturnType` @@ -1253,7 +1260,9 @@ ___ | Name | Type | | :------ | :------ | | `requiredPrivilege` | `string` | -| `user` | [`LoggedInUser`](interfaces/LoggedInUser.md) | +| `user` | `Object` | +| `user.privileges` | [`Privilege`](interfaces/Privilege.md)[] | +| `user.roles` | [`Role`](interfaces/Role.md)[] | #### Returns @@ -1261,7 +1270,7 @@ ___ #### Defined in -[packages/framework/esm-api/src/shared-api-objects/current-user.ts:127](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/shared-api-objects/current-user.ts#L127) +[packages/framework/esm-api/src/shared-api-objects/current-user.ts:129](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/shared-api-objects/current-user.ts#L129) ___ diff --git a/packages/framework/esm-framework/docs/interfaces/LoggedInUser.md b/packages/framework/esm-framework/docs/interfaces/LoggedInUser.md index b4d59803c..39aa5945a 100644 --- a/packages/framework/esm-framework/docs/interfaces/LoggedInUser.md +++ b/packages/framework/esm-framework/docs/interfaces/LoggedInUser.md @@ -30,7 +30,7 @@ #### Defined in -[packages/framework/esm-api/src/types/user-resource.ts:24](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L24) +[packages/framework/esm-api/src/types/user-resource.ts:34](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L34) ___ @@ -40,7 +40,7 @@ ___ #### Defined in -[packages/framework/esm-api/src/types/user-resource.ts:15](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L15) +[packages/framework/esm-api/src/types/user-resource.ts:25](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L25) ___ @@ -50,7 +50,7 @@ ___ #### Defined in -[packages/framework/esm-api/src/types/user-resource.ts:23](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L23) +[packages/framework/esm-api/src/types/user-resource.ts:33](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L33) ___ @@ -60,7 +60,7 @@ ___ #### Defined in -[packages/framework/esm-api/src/types/user-resource.ts:19](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L19) +[packages/framework/esm-api/src/types/user-resource.ts:29](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L29) ___ @@ -70,7 +70,7 @@ ___ #### Defined in -[packages/framework/esm-api/src/types/user-resource.ts:20](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L20) +[packages/framework/esm-api/src/types/user-resource.ts:30](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L30) ___ @@ -80,7 +80,7 @@ ___ #### Defined in -[packages/framework/esm-api/src/types/user-resource.ts:22](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L22) +[packages/framework/esm-api/src/types/user-resource.ts:32](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L32) ___ @@ -90,7 +90,7 @@ ___ #### Defined in -[packages/framework/esm-api/src/types/user-resource.ts:21](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L21) +[packages/framework/esm-api/src/types/user-resource.ts:31](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L31) ___ @@ -100,17 +100,17 @@ ___ #### Defined in -[packages/framework/esm-api/src/types/user-resource.ts:17](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L17) +[packages/framework/esm-api/src/types/user-resource.ts:27](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L27) ___ ### userProperties -• **userProperties**: `any` +• **userProperties**: ``null`` \| { `[key: string]`: `any`; } #### Defined in -[packages/framework/esm-api/src/types/user-resource.ts:18](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L18) +[packages/framework/esm-api/src/types/user-resource.ts:28](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L28) ___ @@ -120,7 +120,7 @@ ___ #### Defined in -[packages/framework/esm-api/src/types/user-resource.ts:16](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L16) +[packages/framework/esm-api/src/types/user-resource.ts:26](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L26) ___ @@ -130,4 +130,4 @@ ___ #### Defined in -[packages/framework/esm-api/src/types/user-resource.ts:14](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L14) +[packages/framework/esm-api/src/types/user-resource.ts:24](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L24) diff --git a/packages/framework/esm-framework/docs/interfaces/LoggedInUserFetchResponse.md b/packages/framework/esm-framework/docs/interfaces/LoggedInUserFetchResponse.md index ad8fb4034..35832a470 100644 --- a/packages/framework/esm-framework/docs/interfaces/LoggedInUserFetchResponse.md +++ b/packages/framework/esm-framework/docs/interfaces/LoggedInUserFetchResponse.md @@ -64,7 +64,7 @@ ___ ### data -• **data**: [`LoggedInUserData`](../API.md#loggedinuserdata) +• **data**: [`Session`](Session.md) #### Overrides @@ -72,7 +72,7 @@ ___ #### Defined in -[packages/framework/esm-api/src/types/fetch.ts:12](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/fetch.ts#L12) +[packages/framework/esm-api/src/types/fetch.ts:8](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/fetch.ts#L8) ___ diff --git a/packages/framework/esm-framework/docs/interfaces/Person.md b/packages/framework/esm-framework/docs/interfaces/Person.md index 15b80fef4..522c6af74 100644 --- a/packages/framework/esm-framework/docs/interfaces/Person.md +++ b/packages/framework/esm-framework/docs/interfaces/Person.md @@ -18,7 +18,7 @@ #### Defined in -[packages/framework/esm-api/src/types/user-resource.ts:43](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L43) +[packages/framework/esm-api/src/types/user-resource.ts:46](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L46) ___ @@ -28,7 +28,7 @@ ___ #### Defined in -[packages/framework/esm-api/src/types/user-resource.ts:44](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L44) +[packages/framework/esm-api/src/types/user-resource.ts:47](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L47) ___ @@ -38,4 +38,4 @@ ___ #### Defined in -[packages/framework/esm-api/src/types/user-resource.ts:42](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L42) +[packages/framework/esm-api/src/types/user-resource.ts:45](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L45) diff --git a/packages/framework/esm-framework/docs/interfaces/Privilege.md b/packages/framework/esm-framework/docs/interfaces/Privilege.md index 33b1dfce7..fcd7050be 100644 --- a/packages/framework/esm-framework/docs/interfaces/Privilege.md +++ b/packages/framework/esm-framework/docs/interfaces/Privilege.md @@ -18,17 +18,17 @@ #### Defined in -[packages/framework/esm-api/src/types/user-resource.ts:49](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L49) +[packages/framework/esm-api/src/types/user-resource.ts:52](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L52) ___ ### links -• **links**: `any`[] +• `Optional` **links**: `any`[] #### Defined in -[packages/framework/esm-api/src/types/user-resource.ts:50](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L50) +[packages/framework/esm-api/src/types/user-resource.ts:53](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L53) ___ @@ -38,4 +38,4 @@ ___ #### Defined in -[packages/framework/esm-api/src/types/user-resource.ts:48](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L48) +[packages/framework/esm-api/src/types/user-resource.ts:51](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L51) diff --git a/packages/framework/esm-framework/docs/interfaces/Role.md b/packages/framework/esm-framework/docs/interfaces/Role.md index 8e275d208..485bb784b 100644 --- a/packages/framework/esm-framework/docs/interfaces/Role.md +++ b/packages/framework/esm-framework/docs/interfaces/Role.md @@ -18,7 +18,7 @@ #### Defined in -[packages/framework/esm-api/src/types/user-resource.ts:55](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L55) +[packages/framework/esm-api/src/types/user-resource.ts:58](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L58) ___ @@ -28,7 +28,7 @@ ___ #### Defined in -[packages/framework/esm-api/src/types/user-resource.ts:56](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L56) +[packages/framework/esm-api/src/types/user-resource.ts:59](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L59) ___ @@ -38,4 +38,4 @@ ___ #### Defined in -[packages/framework/esm-api/src/types/user-resource.ts:54](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L54) +[packages/framework/esm-api/src/types/user-resource.ts:57](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L57) diff --git a/packages/framework/esm-framework/docs/interfaces/Session.md b/packages/framework/esm-framework/docs/interfaces/Session.md new file mode 100644 index 000000000..67c028dc7 --- /dev/null +++ b/packages/framework/esm-framework/docs/interfaces/Session.md @@ -0,0 +1,92 @@ +[@openmrs/esm-framework](../API.md) / Session + +# Interface: Session + +## Table of contents + +### Properties + +- [allowedLocales](Session.md#allowedlocales) +- [authenticated](Session.md#authenticated) +- [currentProvider](Session.md#currentprovider) +- [locale](Session.md#locale) +- [sessionId](Session.md#sessionid) +- [sessionLocation](Session.md#sessionlocation) +- [user](Session.md#user) + +## Properties + +### allowedLocales + +• `Optional` **allowedLocales**: `string`[] + +#### Defined in + +[packages/framework/esm-api/src/types/user-resource.ts:14](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L14) + +___ + +### authenticated + +• **authenticated**: `boolean` + +#### Defined in + +[packages/framework/esm-api/src/types/user-resource.ts:15](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L15) + +___ + +### currentProvider + +• `Optional` **currentProvider**: `Object` + +#### Type declaration + +| Name | Type | +| :------ | :------ | +| `identifier` | `string` | +| `uuid` | `string` | + +#### Defined in + +[packages/framework/esm-api/src/types/user-resource.ts:19](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L19) + +___ + +### locale + +• `Optional` **locale**: `string` + +#### Defined in + +[packages/framework/esm-api/src/types/user-resource.ts:16](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L16) + +___ + +### sessionId + +• **sessionId**: `string` + +#### Defined in + +[packages/framework/esm-api/src/types/user-resource.ts:17](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L17) + +___ + +### sessionLocation + +• `Optional` **sessionLocation**: [`SessionLocation`](SessionLocation.md) + +#### Defined in + +[packages/framework/esm-api/src/types/user-resource.ts:20](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L20) + +___ + +### user + +• `Optional` **user**: [`LoggedInUser`](LoggedInUser.md) + +#### Defined in + +[packages/framework/esm-api/src/types/user-resource.ts:18](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L18) diff --git a/packages/framework/esm-framework/docs/interfaces/SessionLocation.md b/packages/framework/esm-framework/docs/interfaces/SessionLocation.md index 35ac573d7..28411a2be 100644 --- a/packages/framework/esm-framework/docs/interfaces/SessionLocation.md +++ b/packages/framework/esm-framework/docs/interfaces/SessionLocation.md @@ -18,7 +18,7 @@ #### Defined in -[packages/framework/esm-api/src/types/user-resource.ts:37](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L37) +[packages/framework/esm-api/src/types/user-resource.ts:40](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L40) ___ @@ -28,7 +28,7 @@ ___ #### Defined in -[packages/framework/esm-api/src/types/user-resource.ts:38](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L38) +[packages/framework/esm-api/src/types/user-resource.ts:41](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L41) ___ @@ -38,4 +38,4 @@ ___ #### Defined in -[packages/framework/esm-api/src/types/user-resource.ts:36](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L36) +[packages/framework/esm-api/src/types/user-resource.ts:39](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L39) diff --git a/packages/framework/esm-framework/docs/interfaces/SessionUser.md b/packages/framework/esm-framework/docs/interfaces/SessionUser.md deleted file mode 100644 index 57dee608c..000000000 --- a/packages/framework/esm-framework/docs/interfaces/SessionUser.md +++ /dev/null @@ -1,92 +0,0 @@ -[@openmrs/esm-framework](../API.md) / SessionUser - -# Interface: SessionUser - -## Table of contents - -### Properties - -- [allowedLocales](SessionUser.md#allowedlocales) -- [authenticated](SessionUser.md#authenticated) -- [currentProvider](SessionUser.md#currentprovider) -- [locale](SessionUser.md#locale) -- [sessionId](SessionUser.md#sessionid) -- [sessionLocation](SessionUser.md#sessionlocation) -- [user](SessionUser.md#user) - -## Properties - -### allowedLocales - -• **allowedLocales**: `string`[] - -#### Defined in - -[packages/framework/esm-api/src/types/openmrs-resource.ts:8](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/openmrs-resource.ts#L8) - -___ - -### authenticated - -• **authenticated**: `boolean` - -#### Defined in - -[packages/framework/esm-api/src/types/openmrs-resource.ts:9](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/openmrs-resource.ts#L9) - -___ - -### currentProvider - -• **currentProvider**: `Object` - -#### Type declaration - -| Name | Type | -| :------ | :------ | -| `identifier` | `string` | -| `uuid` | `string` | - -#### Defined in - -[packages/framework/esm-api/src/types/openmrs-resource.ts:13](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/openmrs-resource.ts#L13) - -___ - -### locale - -• **locale**: `string` - -#### Defined in - -[packages/framework/esm-api/src/types/openmrs-resource.ts:10](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/openmrs-resource.ts#L10) - -___ - -### sessionId - -• **sessionId**: `string` - -#### Defined in - -[packages/framework/esm-api/src/types/openmrs-resource.ts:11](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/openmrs-resource.ts#L11) - -___ - -### sessionLocation - -• **sessionLocation**: `any` - -#### Defined in - -[packages/framework/esm-api/src/types/openmrs-resource.ts:14](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/openmrs-resource.ts#L14) - -___ - -### user - -• **user**: [`User`](User.md) - -#### Defined in - -[packages/framework/esm-api/src/types/openmrs-resource.ts:12](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/openmrs-resource.ts#L12) diff --git a/packages/framework/esm-framework/docs/interfaces/UnauthenticatedUser.md b/packages/framework/esm-framework/docs/interfaces/UnauthenticatedUser.md deleted file mode 100644 index 4c3801a55..000000000 --- a/packages/framework/esm-framework/docs/interfaces/UnauthenticatedUser.md +++ /dev/null @@ -1,52 +0,0 @@ -[@openmrs/esm-framework](../API.md) / UnauthenticatedUser - -# Interface: UnauthenticatedUser - -## Table of contents - -### Properties - -- [authenticated](UnauthenticatedUser.md#authenticated) -- [sessionId](UnauthenticatedUser.md#sessionid) -- [sessionLocation](UnauthenticatedUser.md#sessionlocation) -- [user](UnauthenticatedUser.md#user) - -## Properties - -### authenticated - -• **authenticated**: `boolean` - -#### Defined in - -[packages/framework/esm-api/src/types/user-resource.ts:30](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L30) - -___ - -### sessionId - -• **sessionId**: `string` - -#### Defined in - -[packages/framework/esm-api/src/types/user-resource.ts:29](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L29) - -___ - -### sessionLocation - -• `Optional` **sessionLocation**: [`SessionLocation`](SessionLocation.md) - -#### Defined in - -[packages/framework/esm-api/src/types/user-resource.ts:32](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L32) - -___ - -### user - -• `Optional` **user**: [`LoggedInUser`](LoggedInUser.md) - -#### Defined in - -[packages/framework/esm-api/src/types/user-resource.ts:31](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/user-resource.ts#L31) diff --git a/packages/framework/esm-framework/docs/interfaces/User.md b/packages/framework/esm-framework/docs/interfaces/User.md deleted file mode 100644 index 0b4f1e696..000000000 --- a/packages/framework/esm-framework/docs/interfaces/User.md +++ /dev/null @@ -1,107 +0,0 @@ -[@openmrs/esm-framework](../API.md) / User - -# Interface: User - -## Table of contents - -### Properties - -- [display](User.md#display) -- [link](User.md#link) -- [person](User.md#person) -- [priviliges](User.md#priviliges) -- [resourceVersion](User.md#resourceversion) -- [roles](User.md#roles) -- [userProperties](User.md#userproperties) -- [username](User.md#username) -- [uuid](User.md#uuid) - -## Properties - -### display - -• **display**: `string` - -#### Defined in - -[packages/framework/esm-api/src/types/openmrs-resource.ts:18](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/openmrs-resource.ts#L18) - -___ - -### link - -• **link**: `string`[] - -#### Defined in - -[packages/framework/esm-api/src/types/openmrs-resource.ts:19](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/openmrs-resource.ts#L19) - -___ - -### person - -• **person**: `any` - -#### Defined in - -[packages/framework/esm-api/src/types/openmrs-resource.ts:20](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/openmrs-resource.ts#L20) - -___ - -### priviliges - -• **priviliges**: `any` - -#### Defined in - -[packages/framework/esm-api/src/types/openmrs-resource.ts:21](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/openmrs-resource.ts#L21) - -___ - -### resourceVersion - -• **resourceVersion**: `any` - -#### Defined in - -[packages/framework/esm-api/src/types/openmrs-resource.ts:22](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/openmrs-resource.ts#L22) - -___ - -### roles - -• **roles**: `any`[] - -#### Defined in - -[packages/framework/esm-api/src/types/openmrs-resource.ts:23](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/openmrs-resource.ts#L23) - -___ - -### userProperties - -• **userProperties**: `any` - -#### Defined in - -[packages/framework/esm-api/src/types/openmrs-resource.ts:24](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/openmrs-resource.ts#L24) - -___ - -### username - -• **username**: `string` - -#### Defined in - -[packages/framework/esm-api/src/types/openmrs-resource.ts:25](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/openmrs-resource.ts#L25) - -___ - -### uuid - -• **uuid**: `string` - -#### Defined in - -[packages/framework/esm-api/src/types/openmrs-resource.ts:26](https://github.com/openmrs/openmrs-esm-core/blob/master/packages/framework/esm-api/src/types/openmrs-resource.ts#L26) diff --git a/packages/framework/esm-react-utils/src/useSessionUser.tsx b/packages/framework/esm-react-utils/src/useSessionUser.tsx index 9fb730e7c..8e257aec8 100644 --- a/packages/framework/esm-react-utils/src/useSessionUser.tsx +++ b/packages/framework/esm-react-utils/src/useSessionUser.tsx @@ -1,21 +1,21 @@ /** @module @category API */ -import { openmrsObservableFetch, SessionUser } from "@openmrs/esm-api"; +import { getCurrentUser, Session } from "@openmrs/esm-api"; import { useState, useEffect } from "react"; -export function useSessionUser() { - const [sessionUser, setSessionUser] = useState(null); +export function useSession() { + const [session, setSession] = useState(null); useEffect(() => { - if (sessionUser === null) { - const sub = openmrsObservableFetch("/ws/rest/v1/session").subscribe( - (user: any) => { - setSessionUser(user.data); - } - ); + const sub = getCurrentUser({ includeAuthStatus: true }).subscribe( + (session) => setSession(session) + ); - return () => sub.unsubscribe(); - } - }, [sessionUser]); + return () => sub.unsubscribe(); + }, [setSession]); - return sessionUser; + return session; } + +/** @deprecated */ +// maintain alias for backwards compatibility +export const useSessionUser = useSession;