@openmrs/esm-api 8.0.1-pre.3783 → 8.0.1-pre.3790
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +1 -1
- package/dist/current-user.d.ts +155 -12
- package/dist/current-user.d.ts.map +1 -1
- package/dist/current-user.js +133 -10
- package/dist/openmrs-backend-dependencies.d.ts +5 -0
- package/dist/openmrs-backend-dependencies.d.ts.map +1 -1
- package/dist/openmrs-backend-dependencies.js +5 -1
- package/dist/openmrs-fetch.d.ts +10 -3
- package/dist/openmrs-fetch.d.ts.map +1 -1
- package/dist/openmrs-fetch.js +10 -6
- package/package.json +5 -5
- package/src/current-user.ts +155 -12
- package/src/openmrs-backend-dependencies.ts +5 -0
- package/src/openmrs-fetch.ts +10 -3
package/.turbo/turbo-build.log
CHANGED
package/dist/current-user.d.ts
CHANGED
|
@@ -19,18 +19,18 @@ export declare const sessionStore: import("zustand").StoreApi<SessionStore>;
|
|
|
19
19
|
* Subsequent values will be produced whenever the user object is
|
|
20
20
|
* updated.
|
|
21
21
|
*
|
|
22
|
-
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
22
|
+
* The function accepts an optional `opts` object with an `includeAuthStatus`
|
|
23
|
+
* boolean property that defaults to `true`. When `includeAuthStatus` is `true`,
|
|
24
|
+
* the entire {@link Session} object from the API will be provided. When
|
|
25
|
+
* `includeAuthStatus` is `false`, only the {@link LoggedInUser} property of the
|
|
26
|
+
* response object will be provided.
|
|
27
27
|
*
|
|
28
|
-
* @returns An Observable that produces zero or more values (as
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
28
|
+
* @returns An Observable that produces zero or more values (as described above).
|
|
29
|
+
* The values produced will be a {@link LoggedInUser} object (if `includeAuthStatus`
|
|
30
|
+
* is set to `false`) or a {@link Session} object with authentication status
|
|
31
|
+
* (if `includeAuthStatus` is set to `true` or not provided).
|
|
32
32
|
*
|
|
33
|
-
*
|
|
33
|
+
* @example
|
|
34
34
|
*
|
|
35
35
|
* ```js
|
|
36
36
|
* import { getCurrentUser } from '@openmrs/esm-api'
|
|
@@ -50,37 +50,180 @@ export declare const sessionStore: import("zustand").StoreApi<SessionStore>;
|
|
|
50
50
|
* leak and source of bugs.
|
|
51
51
|
*/
|
|
52
52
|
declare function getCurrentUser(): Observable<Session>;
|
|
53
|
+
/**
|
|
54
|
+
* @param opts Options for controlling the response format.
|
|
55
|
+
* @param opts.includeAuthStatus When `true`, returns the full {@link Session} object
|
|
56
|
+
* including authentication status.
|
|
57
|
+
* @returns An Observable that produces {@link Session} objects.
|
|
58
|
+
*/
|
|
53
59
|
declare function getCurrentUser(opts: {
|
|
54
60
|
includeAuthStatus: true;
|
|
55
61
|
}): Observable<Session>;
|
|
62
|
+
/**
|
|
63
|
+
* @param opts Options for controlling the response format.
|
|
64
|
+
* @param opts.includeAuthStatus When `false`, returns only the {@link LoggedInUser} object
|
|
65
|
+
* without the surrounding session information.
|
|
66
|
+
* @returns An Observable that produces {@link LoggedInUser} objects.
|
|
67
|
+
*/
|
|
56
68
|
declare function getCurrentUser(opts: {
|
|
57
69
|
includeAuthStatus: false;
|
|
58
70
|
}): Observable<LoggedInUser>;
|
|
59
71
|
export { getCurrentUser };
|
|
72
|
+
/**
|
|
73
|
+
* Returns the global session store containing the current user's session information.
|
|
74
|
+
* If the session data is stale (older than 1 minute) or not yet loaded, this function
|
|
75
|
+
* will trigger a refetch of the current user's session.
|
|
76
|
+
*
|
|
77
|
+
* @returns The global session store that can be subscribed to for session updates.
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```ts
|
|
81
|
+
* import { getSessionStore } from '@openmrs/esm-api';
|
|
82
|
+
* const store = getSessionStore();
|
|
83
|
+
* const unsubscribe = store.subscribe((state) => {
|
|
84
|
+
* if (state.loaded) {
|
|
85
|
+
* console.log('Session:', state.session);
|
|
86
|
+
* }
|
|
87
|
+
* });
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
60
90
|
export declare function getSessionStore(): import("zustand").StoreApi<SessionStore>;
|
|
91
|
+
/**
|
|
92
|
+
* Sets the document's language attribute based on the user's locale preference
|
|
93
|
+
* from the session data. This affects the HTML `lang` attribute which is used
|
|
94
|
+
* for accessibility and internationalization.
|
|
95
|
+
*
|
|
96
|
+
* The locale is determined from either the session's locale or the user's
|
|
97
|
+
* default locale property. Underscores in the locale are converted to hyphens
|
|
98
|
+
* to match BCP 47 language tag format.
|
|
99
|
+
*
|
|
100
|
+
* @param data The session object containing locale information.
|
|
101
|
+
*/
|
|
61
102
|
export declare function setUserLanguage(data: Session): void;
|
|
62
103
|
/**
|
|
63
104
|
* The `refetchCurrentUser` function causes a network request to redownload
|
|
64
105
|
* the user. All subscribers to the current user will be notified of the
|
|
65
106
|
* new users once the new version of the user object is downloaded.
|
|
66
107
|
*
|
|
67
|
-
* @returns The same observable as returned by
|
|
108
|
+
* @returns The same observable as returned by {@link getCurrentUser}.
|
|
68
109
|
*
|
|
69
|
-
*
|
|
110
|
+
* @example
|
|
70
111
|
* ```js
|
|
71
112
|
* import { refetchCurrentUser } from '@openmrs/esm-api'
|
|
72
113
|
* refetchCurrentUser()
|
|
73
114
|
* ```
|
|
74
115
|
*/
|
|
75
116
|
export declare function refetchCurrentUser(username?: string, password?: string): Promise<SessionStore>;
|
|
117
|
+
/**
|
|
118
|
+
* Clears the current user session from the session store, setting the session
|
|
119
|
+
* to an unauthenticated state. This is typically called during logout to reset
|
|
120
|
+
* the application's authentication state.
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* ```ts
|
|
124
|
+
* import { clearCurrentUser } from '@openmrs/esm-api';
|
|
125
|
+
* // During logout
|
|
126
|
+
* clearCurrentUser();
|
|
127
|
+
* ```
|
|
128
|
+
*/
|
|
76
129
|
export declare function clearCurrentUser(): void;
|
|
130
|
+
/**
|
|
131
|
+
* Checks whether the given user has access based on the required privilege(s).
|
|
132
|
+
* A user has access if they have the required privilege(s) or if they are a
|
|
133
|
+
* "System Developer" (super user). If no privilege is required, access is granted.
|
|
134
|
+
*
|
|
135
|
+
* @param requiredPrivilege A single privilege string or an array of privilege strings
|
|
136
|
+
* that the user must have. If an array is provided, the user must have ALL privileges.
|
|
137
|
+
* @param user The user object containing their privileges and roles.
|
|
138
|
+
* @returns `true` if the user has access, `false` otherwise. Returns `true` if no
|
|
139
|
+
* privilege is required, and `false` if the user is undefined but a privilege is required.
|
|
140
|
+
*
|
|
141
|
+
* @example
|
|
142
|
+
* ```ts
|
|
143
|
+
* import { userHasAccess } from '@openmrs/esm-api';
|
|
144
|
+
* const hasAccess = userHasAccess('View Patients', currentUser);
|
|
145
|
+
* const hasMultipleAccess = userHasAccess(['View Patients', 'Edit Patients'], currentUser);
|
|
146
|
+
* ```
|
|
147
|
+
*/
|
|
77
148
|
export declare function userHasAccess(requiredPrivilege: string | Array<string>, user: {
|
|
78
149
|
privileges: Array<Privilege>;
|
|
79
150
|
roles: Array<Role>;
|
|
80
151
|
}): boolean;
|
|
152
|
+
/**
|
|
153
|
+
* Returns a Promise that resolves with the currently logged-in user object.
|
|
154
|
+
* If the user is already loaded in the session store, the Promise resolves immediately.
|
|
155
|
+
* Otherwise, it subscribes to the session store and resolves when a logged-in user
|
|
156
|
+
* becomes available.
|
|
157
|
+
*
|
|
158
|
+
* @returns A Promise that resolves with the LoggedInUser object once available.
|
|
159
|
+
*
|
|
160
|
+
* @example
|
|
161
|
+
* ```ts
|
|
162
|
+
* import { getLoggedInUser } from '@openmrs/esm-api';
|
|
163
|
+
* const user = await getLoggedInUser();
|
|
164
|
+
* console.log('Logged in as:', user.display);
|
|
165
|
+
* ```
|
|
166
|
+
*/
|
|
81
167
|
export declare function getLoggedInUser(): Promise<LoggedInUser>;
|
|
168
|
+
/**
|
|
169
|
+
* Returns a Promise that resolves with the current session location, if one is set.
|
|
170
|
+
* The session location represents the physical location where the user is currently
|
|
171
|
+
* working (e.g., a clinic or ward).
|
|
172
|
+
*
|
|
173
|
+
* @returns A Promise that resolves with the SessionLocation object, or `undefined`
|
|
174
|
+
* if no session location is set.
|
|
175
|
+
*
|
|
176
|
+
* @example
|
|
177
|
+
* ```ts
|
|
178
|
+
* import { getSessionLocation } from '@openmrs/esm-api';
|
|
179
|
+
* const location = await getSessionLocation();
|
|
180
|
+
* if (location) {
|
|
181
|
+
* console.log('Current location:', location.display);
|
|
182
|
+
* }
|
|
183
|
+
* ```
|
|
184
|
+
*/
|
|
82
185
|
export declare function getSessionLocation(): Promise<SessionLocation | undefined>;
|
|
186
|
+
/**
|
|
187
|
+
* Sets the session location for the current user. The session location represents
|
|
188
|
+
* the physical location where the user is working (e.g., a clinic or ward).
|
|
189
|
+
* This triggers a server request to update the session and refreshes the local
|
|
190
|
+
* session store.
|
|
191
|
+
*
|
|
192
|
+
* @param locationUuid The UUID of the location to set as the session location.
|
|
193
|
+
* @param abortController An AbortController to allow cancellation of the request.
|
|
194
|
+
* @returns A Promise that resolves with the updated SessionStore.
|
|
195
|
+
*
|
|
196
|
+
* @example
|
|
197
|
+
* ```ts
|
|
198
|
+
* import { setSessionLocation } from '@openmrs/esm-api';
|
|
199
|
+
* const abortController = new AbortController();
|
|
200
|
+
* await setSessionLocation('location-uuid-here', abortController);
|
|
201
|
+
* ```
|
|
202
|
+
*/
|
|
83
203
|
export declare function setSessionLocation(locationUuid: string, abortController: AbortController): Promise<any>;
|
|
204
|
+
/**
|
|
205
|
+
* Updates the user properties for a specific user. User properties are key-value
|
|
206
|
+
* pairs that store user-specific settings and preferences. After updating the
|
|
207
|
+
* properties on the server, the current user session is refetched to reflect
|
|
208
|
+
* the changes.
|
|
209
|
+
*
|
|
210
|
+
* @param userUuid The UUID of the user whose properties should be updated.
|
|
211
|
+
* @param userProperties An object containing the properties to set or update.
|
|
212
|
+
* @param abortController Optional AbortController to allow cancellation of the request.
|
|
213
|
+
* If not provided, a new AbortController is created.
|
|
214
|
+
* @returns A Promise that resolves with the updated SessionStore after refetching
|
|
215
|
+
* the current user.
|
|
216
|
+
*
|
|
217
|
+
* @example
|
|
218
|
+
* ```ts
|
|
219
|
+
* import { getLoggedInUser, setUserProperties } from '@openmrs/esm-api';
|
|
220
|
+
* const user = await getLoggedInUser();
|
|
221
|
+
* await setUserProperties(user.uuid, {
|
|
222
|
+
* defaultLocale: 'en_GB',
|
|
223
|
+
* customSetting: 'value'
|
|
224
|
+
* });
|
|
225
|
+
* ```
|
|
226
|
+
*/
|
|
84
227
|
export declare function setUserProperties(userUuid: string, userProperties: {
|
|
85
228
|
[x: string]: string;
|
|
86
229
|
}, abortController?: AbortController): Promise<SessionStore>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"current-user.d.ts","sourceRoot":"","sources":["../src/current-user.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;AAElC,OAAO,KAAK,EAAE,YAAY,EAAE,eAAe,EAAE,SAAS,EAAE,IAAI,EAAE,OAAO,EAAiB,MAAM,SAAS,CAAC;AAEtG,MAAM,MAAM,YAAY,GAAG,kBAAkB,GAAG,oBAAoB,CAAC;AAErE,MAAM,MAAM,kBAAkB,GAAG;IAC/B,MAAM,EAAE,IAAI,CAAC;IACb,OAAO,EAAE,OAAO,CAAC;CAClB,CAAC;AAEF,MAAM,MAAM,oBAAoB,GAAG;IACjC,MAAM,EAAE,KAAK,CAAC;IACd,OAAO,EAAE,IAAI,CAAC;CACf,CAAC;AAEF,gBAAgB;AAChB,eAAO,MAAM,YAAY,0CAGvB,CAAC;AAGH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,iBAAS,cAAc,IAAI,UAAU,CAAC,OAAO,CAAC,CAAC;AAC/C,iBAAS,cAAc,CAAC,IAAI,EAAE;IAAE,iBAAiB,EAAE,IAAI,CAAA;CAAE,GAAG,UAAU,CAAC,OAAO,CAAC,CAAC;AAChF,iBAAS,cAAc,CAAC,IAAI,EAAE;I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;;;;;;;;;GAgBG;AACH,wBAAgB,kBAAkB,yCAOjC;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAsB,kBAAkB,CAAC,YAAY,EAAE,MAAM,EAAE,eAAe,EAAE,eAAe,GAAG,OAAO,CAAC,GAAG,CAAC,CAW7G;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAsB,iBAAiB,CACrC,QAAQ,EAAE,MAAM,EAChB,cAAc,EAAE;IACd,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;CACrB,EACD,eAAe,CAAC,EAAE,eAAe,GAChC,OAAO,CAAC,YAAY,CAAC,CAcvB"}
|
package/dist/current-user.js
CHANGED
|
@@ -31,7 +31,24 @@ function getCurrentUser(opts = {
|
|
|
31
31
|
});
|
|
32
32
|
}
|
|
33
33
|
export { getCurrentUser };
|
|
34
|
-
|
|
34
|
+
/**
|
|
35
|
+
* Returns the global session store containing the current user's session information.
|
|
36
|
+
* If the session data is stale (older than 1 minute) or not yet loaded, this function
|
|
37
|
+
* will trigger a refetch of the current user's session.
|
|
38
|
+
*
|
|
39
|
+
* @returns The global session store that can be subscribed to for session updates.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* import { getSessionStore } from '@openmrs/esm-api';
|
|
44
|
+
* const store = getSessionStore();
|
|
45
|
+
* const unsubscribe = store.subscribe((state) => {
|
|
46
|
+
* if (state.loaded) {
|
|
47
|
+
* console.log('Session:', state.session);
|
|
48
|
+
* }
|
|
49
|
+
* });
|
|
50
|
+
* ```
|
|
51
|
+
*/ export function getSessionStore() {
|
|
35
52
|
if (lastFetchTimeMillis < Date.now() - 1000 * 60 || !sessionStore.getState().loaded) {
|
|
36
53
|
refetchCurrentUser();
|
|
37
54
|
}
|
|
@@ -49,7 +66,17 @@ function isValidLocale(locale) {
|
|
|
49
66
|
}
|
|
50
67
|
return true;
|
|
51
68
|
}
|
|
52
|
-
|
|
69
|
+
/**
|
|
70
|
+
* Sets the document's language attribute based on the user's locale preference
|
|
71
|
+
* from the session data. This affects the HTML `lang` attribute which is used
|
|
72
|
+
* for accessibility and internationalization.
|
|
73
|
+
*
|
|
74
|
+
* The locale is determined from either the session's locale or the user's
|
|
75
|
+
* default locale property. Underscores in the locale are converted to hyphens
|
|
76
|
+
* to match BCP 47 language tag format.
|
|
77
|
+
*
|
|
78
|
+
* @param data The session object containing locale information.
|
|
79
|
+
*/ export function setUserLanguage(data) {
|
|
53
80
|
let locale = data.locale ?? data.user?.userProperties?.defaultLocale;
|
|
54
81
|
if (locale && locale.includes('_')) {
|
|
55
82
|
locale = locale.replaceAll('_', '-');
|
|
@@ -81,9 +108,9 @@ function isSuperUser(user) {
|
|
|
81
108
|
* the user. All subscribers to the current user will be notified of the
|
|
82
109
|
* new users once the new version of the user object is downloaded.
|
|
83
110
|
*
|
|
84
|
-
* @returns The same observable as returned by
|
|
111
|
+
* @returns The same observable as returned by {@link getCurrentUser}.
|
|
85
112
|
*
|
|
86
|
-
*
|
|
113
|
+
* @example
|
|
87
114
|
* ```js
|
|
88
115
|
* import { refetchCurrentUser } from '@openmrs/esm-api'
|
|
89
116
|
* refetchCurrentUser()
|
|
@@ -98,7 +125,18 @@ function isSuperUser(user) {
|
|
|
98
125
|
headers
|
|
99
126
|
}));
|
|
100
127
|
}
|
|
101
|
-
|
|
128
|
+
/**
|
|
129
|
+
* Clears the current user session from the session store, setting the session
|
|
130
|
+
* to an unauthenticated state. This is typically called during logout to reset
|
|
131
|
+
* the application's authentication state.
|
|
132
|
+
*
|
|
133
|
+
* @example
|
|
134
|
+
* ```ts
|
|
135
|
+
* import { clearCurrentUser } from '@openmrs/esm-api';
|
|
136
|
+
* // During logout
|
|
137
|
+
* clearCurrentUser();
|
|
138
|
+
* ```
|
|
139
|
+
*/ export function clearCurrentUser() {
|
|
102
140
|
sessionStore.setState({
|
|
103
141
|
loaded: true,
|
|
104
142
|
session: {
|
|
@@ -107,7 +145,24 @@ export function clearCurrentUser() {
|
|
|
107
145
|
}
|
|
108
146
|
});
|
|
109
147
|
}
|
|
110
|
-
|
|
148
|
+
/**
|
|
149
|
+
* Checks whether the given user has access based on the required privilege(s).
|
|
150
|
+
* A user has access if they have the required privilege(s) or if they are a
|
|
151
|
+
* "System Developer" (super user). If no privilege is required, access is granted.
|
|
152
|
+
*
|
|
153
|
+
* @param requiredPrivilege A single privilege string or an array of privilege strings
|
|
154
|
+
* that the user must have. If an array is provided, the user must have ALL privileges.
|
|
155
|
+
* @param user The user object containing their privileges and roles.
|
|
156
|
+
* @returns `true` if the user has access, `false` otherwise. Returns `true` if no
|
|
157
|
+
* privilege is required, and `false` if the user is undefined but a privilege is required.
|
|
158
|
+
*
|
|
159
|
+
* @example
|
|
160
|
+
* ```ts
|
|
161
|
+
* import { userHasAccess } from '@openmrs/esm-api';
|
|
162
|
+
* const hasAccess = userHasAccess('View Patients', currentUser);
|
|
163
|
+
* const hasMultipleAccess = userHasAccess(['View Patients', 'Edit Patients'], currentUser);
|
|
164
|
+
* ```
|
|
165
|
+
*/ export function userHasAccess(requiredPrivilege, user) {
|
|
111
166
|
if (user === undefined) {
|
|
112
167
|
// if the user hasn't been loaded, then return false iff there is a required privilege
|
|
113
168
|
return !Boolean(requiredPrivilege);
|
|
@@ -118,7 +173,21 @@ export function userHasAccess(requiredPrivilege, user) {
|
|
|
118
173
|
}
|
|
119
174
|
return userHasPrivilege(requiredPrivilege, user) || isSuperUser(user);
|
|
120
175
|
}
|
|
121
|
-
|
|
176
|
+
/**
|
|
177
|
+
* Returns a Promise that resolves with the currently logged-in user object.
|
|
178
|
+
* If the user is already loaded in the session store, the Promise resolves immediately.
|
|
179
|
+
* Otherwise, it subscribes to the session store and resolves when a logged-in user
|
|
180
|
+
* becomes available.
|
|
181
|
+
*
|
|
182
|
+
* @returns A Promise that resolves with the LoggedInUser object once available.
|
|
183
|
+
*
|
|
184
|
+
* @example
|
|
185
|
+
* ```ts
|
|
186
|
+
* import { getLoggedInUser } from '@openmrs/esm-api';
|
|
187
|
+
* const user = await getLoggedInUser();
|
|
188
|
+
* console.log('Logged in as:', user.display);
|
|
189
|
+
* ```
|
|
190
|
+
*/ export function getLoggedInUser() {
|
|
122
191
|
let user;
|
|
123
192
|
let unsubscribe;
|
|
124
193
|
return new Promise((res)=>{
|
|
@@ -137,7 +206,23 @@ export function getLoggedInUser() {
|
|
|
137
206
|
}
|
|
138
207
|
});
|
|
139
208
|
}
|
|
140
|
-
|
|
209
|
+
/**
|
|
210
|
+
* Returns a Promise that resolves with the current session location, if one is set.
|
|
211
|
+
* The session location represents the physical location where the user is currently
|
|
212
|
+
* working (e.g., a clinic or ward).
|
|
213
|
+
*
|
|
214
|
+
* @returns A Promise that resolves with the SessionLocation object, or `undefined`
|
|
215
|
+
* if no session location is set.
|
|
216
|
+
*
|
|
217
|
+
* @example
|
|
218
|
+
* ```ts
|
|
219
|
+
* import { getSessionLocation } from '@openmrs/esm-api';
|
|
220
|
+
* const location = await getSessionLocation();
|
|
221
|
+
* if (location) {
|
|
222
|
+
* console.log('Current location:', location.display);
|
|
223
|
+
* }
|
|
224
|
+
* ```
|
|
225
|
+
*/ export function getSessionLocation() {
|
|
141
226
|
return new Promise((res, rej)=>{
|
|
142
227
|
const sub = getCurrentUser().subscribe((session)=>{
|
|
143
228
|
res(session.sessionLocation);
|
|
@@ -145,7 +230,23 @@ export function getSessionLocation() {
|
|
|
145
230
|
sub.unsubscribe();
|
|
146
231
|
});
|
|
147
232
|
}
|
|
148
|
-
|
|
233
|
+
/**
|
|
234
|
+
* Sets the session location for the current user. The session location represents
|
|
235
|
+
* the physical location where the user is working (e.g., a clinic or ward).
|
|
236
|
+
* This triggers a server request to update the session and refreshes the local
|
|
237
|
+
* session store.
|
|
238
|
+
*
|
|
239
|
+
* @param locationUuid The UUID of the location to set as the session location.
|
|
240
|
+
* @param abortController An AbortController to allow cancellation of the request.
|
|
241
|
+
* @returns A Promise that resolves with the updated SessionStore.
|
|
242
|
+
*
|
|
243
|
+
* @example
|
|
244
|
+
* ```ts
|
|
245
|
+
* import { setSessionLocation } from '@openmrs/esm-api';
|
|
246
|
+
* const abortController = new AbortController();
|
|
247
|
+
* await setSessionLocation('location-uuid-here', abortController);
|
|
248
|
+
* ```
|
|
249
|
+
*/ export async function setSessionLocation(locationUuid, abortController) {
|
|
149
250
|
return handleSessionResponse(openmrsFetch(sessionEndpoint, {
|
|
150
251
|
method: 'POST',
|
|
151
252
|
body: {
|
|
@@ -157,7 +258,29 @@ export async function setSessionLocation(locationUuid, abortController) {
|
|
|
157
258
|
signal: abortController.signal
|
|
158
259
|
}));
|
|
159
260
|
}
|
|
160
|
-
|
|
261
|
+
/**
|
|
262
|
+
* Updates the user properties for a specific user. User properties are key-value
|
|
263
|
+
* pairs that store user-specific settings and preferences. After updating the
|
|
264
|
+
* properties on the server, the current user session is refetched to reflect
|
|
265
|
+
* the changes.
|
|
266
|
+
*
|
|
267
|
+
* @param userUuid The UUID of the user whose properties should be updated.
|
|
268
|
+
* @param userProperties An object containing the properties to set or update.
|
|
269
|
+
* @param abortController Optional AbortController to allow cancellation of the request.
|
|
270
|
+
* If not provided, a new AbortController is created.
|
|
271
|
+
* @returns A Promise that resolves with the updated SessionStore after refetching
|
|
272
|
+
* the current user.
|
|
273
|
+
*
|
|
274
|
+
* @example
|
|
275
|
+
* ```ts
|
|
276
|
+
* import { getLoggedInUser, setUserProperties } from '@openmrs/esm-api';
|
|
277
|
+
* const user = await getLoggedInUser();
|
|
278
|
+
* await setUserProperties(user.uuid, {
|
|
279
|
+
* defaultLocale: 'en_GB',
|
|
280
|
+
* customSetting: 'value'
|
|
281
|
+
* });
|
|
282
|
+
* ```
|
|
283
|
+
*/ export async function setUserProperties(userUuid, userProperties, abortController) {
|
|
161
284
|
if (!abortController) {
|
|
162
285
|
abortController = new AbortController();
|
|
163
286
|
}
|
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Defines the minimum required versions of OpenMRS backend modules that the
|
|
3
|
+
* frontend framework depends on. These versions are checked at startup to ensure
|
|
4
|
+
* compatibility between the frontend and backend.
|
|
5
|
+
*/
|
|
1
6
|
export declare const backendDependencies: {
|
|
2
7
|
'webservices.rest': string;
|
|
3
8
|
fhir2: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openmrs-backend-dependencies.d.ts","sourceRoot":"","sources":["../src/openmrs-backend-dependencies.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,mBAAmB;;;CAG/B,CAAC"}
|
|
1
|
+
{"version":3,"file":"openmrs-backend-dependencies.d.ts","sourceRoot":"","sources":["../src/openmrs-backend-dependencies.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,eAAO,MAAM,mBAAmB;;;CAG/B,CAAC"}
|
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Defines the minimum required versions of OpenMRS backend modules that the
|
|
3
|
+
* frontend framework depends on. These versions are checked at startup to ensure
|
|
4
|
+
* compatibility between the frontend and backend.
|
|
5
|
+
*/ export const backendDependencies = {
|
|
2
6
|
'webservices.rest': '2.24.0',
|
|
3
7
|
fhir2: '1.0.0-SNAPSHOT'
|
|
4
8
|
};
|
package/dist/openmrs-fetch.d.ts
CHANGED
|
@@ -1,13 +1,20 @@
|
|
|
1
1
|
/** @module @category API */
|
|
2
2
|
import { Observable } from 'rxjs';
|
|
3
3
|
import type { FetchResponse } from './types';
|
|
4
|
+
/** The base URL for the OpenMRS REST API (e.g., '/ws/rest/v1'). */
|
|
4
5
|
export declare const restBaseUrl = "/ws/rest/v1";
|
|
6
|
+
/** The base URL for the OpenMRS FHIR R4 API (e.g., '/ws/fhir2/R4'). */
|
|
5
7
|
export declare const fhirBaseUrl = "/ws/fhir2/R4";
|
|
8
|
+
/** The endpoint for session management operations. */
|
|
6
9
|
export declare const sessionEndpoint = "/ws/rest/v1/session";
|
|
7
10
|
/**
|
|
8
11
|
* Append `path` to the OpenMRS SPA base.
|
|
9
12
|
*
|
|
10
|
-
*
|
|
13
|
+
* @param path The path to append to the OpenMRS base URL.
|
|
14
|
+
* @returns The full URL with the OpenMRS base prepended. If the path is already
|
|
15
|
+
* an absolute URL (starting with 'http'), it is returned unchanged.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
11
18
|
*
|
|
12
19
|
* ```ts
|
|
13
20
|
* makeUrl('/foo/bar');
|
|
@@ -33,7 +40,7 @@ export declare function makeUrl(path: string): string;
|
|
|
33
40
|
* downloaded the HTTP response body as json, and has an additional
|
|
34
41
|
* `data` property with the HTTP response json as a javascript object.
|
|
35
42
|
*
|
|
36
|
-
*
|
|
43
|
+
* @example
|
|
37
44
|
* ```js
|
|
38
45
|
* import { openmrsFetch } from '@openmrs/esm-api'
|
|
39
46
|
* const abortController = new AbortController();
|
|
@@ -76,7 +83,7 @@ export declare function openmrsFetch<T = any>(path: string, fetchInit?: FetchCon
|
|
|
76
83
|
* @returns An Observable that produces exactly one Response object.
|
|
77
84
|
* The response object is exactly the same as for [[openmrsFetch]].
|
|
78
85
|
*
|
|
79
|
-
*
|
|
86
|
+
* @example
|
|
80
87
|
*
|
|
81
88
|
* ```js
|
|
82
89
|
* import { openmrsObservableFetch } from '@openmrs/esm-api'
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openmrs-fetch.d.ts","sourceRoot":"","sources":["../src/openmrs-fetch.ts"],"names":[],"mappings":"AAAA,4BAA4B;AAC5B,OAAO,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;AAIlC,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAG7C,eAAO,MAAM,WAAW,gBAAgB,CAAC;AACzC,eAAO,MAAM,WAAW,iBAAiB,CAAC;AAC1C,eAAO,MAAM,eAAe,wBAA2B,CAAC;AAExD
|
|
1
|
+
{"version":3,"file":"openmrs-fetch.d.ts","sourceRoot":"","sources":["../src/openmrs-fetch.ts"],"names":[],"mappings":"AAAA,4BAA4B;AAC5B,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,GAAG,IAAI,CAAC;CAC9B;AAED,UAAU,SAAS;IACjB,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;CACpB;AAED,MAAM,WAAW,iBAAiB;IAChC,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;CACpB;AAED,MAAM,WAAW,UAAU;IACzB,QAAQ,EAAE,QAAQ,CAAC;IACnB,YAAY,EAAE,YAAY,GAAG,IAAI,CAAC;CACnC"}
|
package/dist/openmrs-fetch.js
CHANGED
|
@@ -16,13 +16,17 @@ import { isPlainObject } from "lodash-es";
|
|
|
16
16
|
import { getConfig } from "@openmrs/esm-config";
|
|
17
17
|
import { clearHistory, navigate } from "@openmrs/esm-navigation";
|
|
18
18
|
import { defaultRedirectAuthFailureUrl } from "./config-schema.js";
|
|
19
|
-
export const restBaseUrl = '/ws/rest/v1';
|
|
20
|
-
export const fhirBaseUrl = '/ws/fhir2/R4';
|
|
21
|
-
export const sessionEndpoint = `${restBaseUrl}/session`;
|
|
19
|
+
/** The base URL for the OpenMRS REST API (e.g., '/ws/rest/v1'). */ export const restBaseUrl = '/ws/rest/v1';
|
|
20
|
+
/** The base URL for the OpenMRS FHIR R4 API (e.g., '/ws/fhir2/R4'). */ export const fhirBaseUrl = '/ws/fhir2/R4';
|
|
21
|
+
/** The endpoint for session management operations. */ export const sessionEndpoint = `${restBaseUrl}/session`;
|
|
22
22
|
/**
|
|
23
23
|
* Append `path` to the OpenMRS SPA base.
|
|
24
24
|
*
|
|
25
|
-
*
|
|
25
|
+
* @param path The path to append to the OpenMRS base URL.
|
|
26
|
+
* @returns The full URL with the OpenMRS base prepended. If the path is already
|
|
27
|
+
* an absolute URL (starting with 'http'), it is returned unchanged.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
26
30
|
*
|
|
27
31
|
* ```ts
|
|
28
32
|
* makeUrl('/foo/bar');
|
|
@@ -55,7 +59,7 @@ export const sessionEndpoint = `${restBaseUrl}/session`;
|
|
|
55
59
|
* downloaded the HTTP response body as json, and has an additional
|
|
56
60
|
* `data` property with the HTTP response json as a javascript object.
|
|
57
61
|
*
|
|
58
|
-
*
|
|
62
|
+
* @example
|
|
59
63
|
* ```js
|
|
60
64
|
* import { openmrsFetch } from '@openmrs/esm-api'
|
|
61
65
|
* const abortController = new AbortController();
|
|
@@ -218,7 +222,7 @@ export const sessionEndpoint = `${restBaseUrl}/session`;
|
|
|
218
222
|
* @returns An Observable that produces exactly one Response object.
|
|
219
223
|
* The response object is exactly the same as for [[openmrsFetch]].
|
|
220
224
|
*
|
|
221
|
-
*
|
|
225
|
+
* @example
|
|
222
226
|
*
|
|
223
227
|
* ```js
|
|
224
228
|
* import { openmrsObservableFetch } from '@openmrs/esm-api'
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@openmrs/esm-api",
|
|
3
|
-
"version": "8.0.1-pre.
|
|
3
|
+
"version": "8.0.1-pre.3790",
|
|
4
4
|
"license": "MPL-2.0",
|
|
5
5
|
"description": "The javascript module for interacting with the OpenMRS API",
|
|
6
6
|
"type": "module",
|
|
@@ -62,10 +62,10 @@
|
|
|
62
62
|
"@openmrs/esm-navigation": "6.x"
|
|
63
63
|
},
|
|
64
64
|
"devDependencies": {
|
|
65
|
-
"@openmrs/esm-config": "8.0.1-pre.
|
|
66
|
-
"@openmrs/esm-error-handling": "8.0.1-pre.
|
|
67
|
-
"@openmrs/esm-globals": "8.0.1-pre.
|
|
68
|
-
"@openmrs/esm-navigation": "8.0.1-pre.
|
|
65
|
+
"@openmrs/esm-config": "8.0.1-pre.3790",
|
|
66
|
+
"@openmrs/esm-error-handling": "8.0.1-pre.3790",
|
|
67
|
+
"@openmrs/esm-globals": "8.0.1-pre.3790",
|
|
68
|
+
"@openmrs/esm-navigation": "8.0.1-pre.3790",
|
|
69
69
|
"@swc/cli": "^0.7.7",
|
|
70
70
|
"@swc/core": "^1.11.29",
|
|
71
71
|
"@vitest/coverage-v8": "^4.0.7",
|
package/src/current-user.ts
CHANGED
|
@@ -33,18 +33,18 @@ let lastFetchTimeMillis = 0;
|
|
|
33
33
|
* Subsequent values will be produced whenever the user object is
|
|
34
34
|
* updated.
|
|
35
35
|
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
36
|
+
* The function accepts an optional `opts` object with an `includeAuthStatus`
|
|
37
|
+
* boolean property that defaults to `true`. When `includeAuthStatus` is `true`,
|
|
38
|
+
* the entire {@link Session} object from the API will be provided. When
|
|
39
|
+
* `includeAuthStatus` is `false`, only the {@link LoggedInUser} property of the
|
|
40
|
+
* response object will be provided.
|
|
41
41
|
*
|
|
42
|
-
* @returns An Observable that produces zero or more values (as
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
42
|
+
* @returns An Observable that produces zero or more values (as described above).
|
|
43
|
+
* The values produced will be a {@link LoggedInUser} object (if `includeAuthStatus`
|
|
44
|
+
* is set to `false`) or a {@link Session} object with authentication status
|
|
45
|
+
* (if `includeAuthStatus` is set to `true` or not provided).
|
|
46
46
|
*
|
|
47
|
-
*
|
|
47
|
+
* @example
|
|
48
48
|
*
|
|
49
49
|
* ```js
|
|
50
50
|
* import { getCurrentUser } from '@openmrs/esm-api'
|
|
@@ -64,7 +64,19 @@ let lastFetchTimeMillis = 0;
|
|
|
64
64
|
* leak and source of bugs.
|
|
65
65
|
*/
|
|
66
66
|
function getCurrentUser(): Observable<Session>;
|
|
67
|
+
/**
|
|
68
|
+
* @param opts Options for controlling the response format.
|
|
69
|
+
* @param opts.includeAuthStatus When `true`, returns the full {@link Session} object
|
|
70
|
+
* including authentication status.
|
|
71
|
+
* @returns An Observable that produces {@link Session} objects.
|
|
72
|
+
*/
|
|
67
73
|
function getCurrentUser(opts: { includeAuthStatus: true }): Observable<Session>;
|
|
74
|
+
/**
|
|
75
|
+
* @param opts Options for controlling the response format.
|
|
76
|
+
* @param opts.includeAuthStatus When `false`, returns only the {@link LoggedInUser} object
|
|
77
|
+
* without the surrounding session information.
|
|
78
|
+
* @returns An Observable that produces {@link LoggedInUser} objects.
|
|
79
|
+
*/
|
|
68
80
|
function getCurrentUser(opts: { includeAuthStatus: false }): Observable<LoggedInUser>;
|
|
69
81
|
function getCurrentUser(opts = { includeAuthStatus: true }): Observable<Session | LoggedInUser> {
|
|
70
82
|
if (lastFetchTimeMillis < Date.now() - 1000 * 60 || !sessionStore.getState().loaded) {
|
|
@@ -90,6 +102,24 @@ function getCurrentUser(opts = { includeAuthStatus: true }): Observable<Session
|
|
|
90
102
|
|
|
91
103
|
export { getCurrentUser };
|
|
92
104
|
|
|
105
|
+
/**
|
|
106
|
+
* Returns the global session store containing the current user's session information.
|
|
107
|
+
* If the session data is stale (older than 1 minute) or not yet loaded, this function
|
|
108
|
+
* will trigger a refetch of the current user's session.
|
|
109
|
+
*
|
|
110
|
+
* @returns The global session store that can be subscribed to for session updates.
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* ```ts
|
|
114
|
+
* import { getSessionStore } from '@openmrs/esm-api';
|
|
115
|
+
* const store = getSessionStore();
|
|
116
|
+
* const unsubscribe = store.subscribe((state) => {
|
|
117
|
+
* if (state.loaded) {
|
|
118
|
+
* console.log('Session:', state.session);
|
|
119
|
+
* }
|
|
120
|
+
* });
|
|
121
|
+
* ```
|
|
122
|
+
*/
|
|
93
123
|
export function getSessionStore() {
|
|
94
124
|
if (lastFetchTimeMillis < Date.now() - 1000 * 60 || !sessionStore.getState().loaded) {
|
|
95
125
|
refetchCurrentUser();
|
|
@@ -113,6 +143,17 @@ function isValidLocale(locale: unknown): locale is string {
|
|
|
113
143
|
return true;
|
|
114
144
|
}
|
|
115
145
|
|
|
146
|
+
/**
|
|
147
|
+
* Sets the document's language attribute based on the user's locale preference
|
|
148
|
+
* from the session data. This affects the HTML `lang` attribute which is used
|
|
149
|
+
* for accessibility and internationalization.
|
|
150
|
+
*
|
|
151
|
+
* The locale is determined from either the session's locale or the user's
|
|
152
|
+
* default locale property. Underscores in the locale are converted to hyphens
|
|
153
|
+
* to match BCP 47 language tag format.
|
|
154
|
+
*
|
|
155
|
+
* @param data The session object containing locale information.
|
|
156
|
+
*/
|
|
116
157
|
export function setUserLanguage(data: Session) {
|
|
117
158
|
let locale = data.locale ?? data.user?.userProperties?.defaultLocale;
|
|
118
159
|
|
|
@@ -152,9 +193,9 @@ function isSuperUser(user: { roles: Array<Role> }) {
|
|
|
152
193
|
* the user. All subscribers to the current user will be notified of the
|
|
153
194
|
* new users once the new version of the user object is downloaded.
|
|
154
195
|
*
|
|
155
|
-
* @returns The same observable as returned by
|
|
196
|
+
* @returns The same observable as returned by {@link getCurrentUser}.
|
|
156
197
|
*
|
|
157
|
-
*
|
|
198
|
+
* @example
|
|
158
199
|
* ```js
|
|
159
200
|
* import { refetchCurrentUser } from '@openmrs/esm-api'
|
|
160
201
|
* refetchCurrentUser()
|
|
@@ -174,6 +215,18 @@ export function refetchCurrentUser(username?: string, password?: string) {
|
|
|
174
215
|
);
|
|
175
216
|
}
|
|
176
217
|
|
|
218
|
+
/**
|
|
219
|
+
* Clears the current user session from the session store, setting the session
|
|
220
|
+
* to an unauthenticated state. This is typically called during logout to reset
|
|
221
|
+
* the application's authentication state.
|
|
222
|
+
*
|
|
223
|
+
* @example
|
|
224
|
+
* ```ts
|
|
225
|
+
* import { clearCurrentUser } from '@openmrs/esm-api';
|
|
226
|
+
* // During logout
|
|
227
|
+
* clearCurrentUser();
|
|
228
|
+
* ```
|
|
229
|
+
*/
|
|
177
230
|
export function clearCurrentUser() {
|
|
178
231
|
sessionStore.setState({
|
|
179
232
|
loaded: true,
|
|
@@ -181,6 +234,24 @@ export function clearCurrentUser() {
|
|
|
181
234
|
});
|
|
182
235
|
}
|
|
183
236
|
|
|
237
|
+
/**
|
|
238
|
+
* Checks whether the given user has access based on the required privilege(s).
|
|
239
|
+
* A user has access if they have the required privilege(s) or if they are a
|
|
240
|
+
* "System Developer" (super user). If no privilege is required, access is granted.
|
|
241
|
+
*
|
|
242
|
+
* @param requiredPrivilege A single privilege string or an array of privilege strings
|
|
243
|
+
* that the user must have. If an array is provided, the user must have ALL privileges.
|
|
244
|
+
* @param user The user object containing their privileges and roles.
|
|
245
|
+
* @returns `true` if the user has access, `false` otherwise. Returns `true` if no
|
|
246
|
+
* privilege is required, and `false` if the user is undefined but a privilege is required.
|
|
247
|
+
*
|
|
248
|
+
* @example
|
|
249
|
+
* ```ts
|
|
250
|
+
* import { userHasAccess } from '@openmrs/esm-api';
|
|
251
|
+
* const hasAccess = userHasAccess('View Patients', currentUser);
|
|
252
|
+
* const hasMultipleAccess = userHasAccess(['View Patients', 'Edit Patients'], currentUser);
|
|
253
|
+
* ```
|
|
254
|
+
*/
|
|
184
255
|
export function userHasAccess(
|
|
185
256
|
requiredPrivilege: string | Array<string>,
|
|
186
257
|
user: { privileges: Array<Privilege>; roles: Array<Role> },
|
|
@@ -198,6 +269,21 @@ export function userHasAccess(
|
|
|
198
269
|
return userHasPrivilege(requiredPrivilege, user) || isSuperUser(user);
|
|
199
270
|
}
|
|
200
271
|
|
|
272
|
+
/**
|
|
273
|
+
* Returns a Promise that resolves with the currently logged-in user object.
|
|
274
|
+
* If the user is already loaded in the session store, the Promise resolves immediately.
|
|
275
|
+
* Otherwise, it subscribes to the session store and resolves when a logged-in user
|
|
276
|
+
* becomes available.
|
|
277
|
+
*
|
|
278
|
+
* @returns A Promise that resolves with the LoggedInUser object once available.
|
|
279
|
+
*
|
|
280
|
+
* @example
|
|
281
|
+
* ```ts
|
|
282
|
+
* import { getLoggedInUser } from '@openmrs/esm-api';
|
|
283
|
+
* const user = await getLoggedInUser();
|
|
284
|
+
* console.log('Logged in as:', user.display);
|
|
285
|
+
* ```
|
|
286
|
+
*/
|
|
201
287
|
export function getLoggedInUser() {
|
|
202
288
|
let user: LoggedInUser;
|
|
203
289
|
let unsubscribe: () => void;
|
|
@@ -219,6 +305,23 @@ export function getLoggedInUser() {
|
|
|
219
305
|
});
|
|
220
306
|
}
|
|
221
307
|
|
|
308
|
+
/**
|
|
309
|
+
* Returns a Promise that resolves with the current session location, if one is set.
|
|
310
|
+
* The session location represents the physical location where the user is currently
|
|
311
|
+
* working (e.g., a clinic or ward).
|
|
312
|
+
*
|
|
313
|
+
* @returns A Promise that resolves with the SessionLocation object, or `undefined`
|
|
314
|
+
* if no session location is set.
|
|
315
|
+
*
|
|
316
|
+
* @example
|
|
317
|
+
* ```ts
|
|
318
|
+
* import { getSessionLocation } from '@openmrs/esm-api';
|
|
319
|
+
* const location = await getSessionLocation();
|
|
320
|
+
* if (location) {
|
|
321
|
+
* console.log('Current location:', location.display);
|
|
322
|
+
* }
|
|
323
|
+
* ```
|
|
324
|
+
*/
|
|
222
325
|
export function getSessionLocation() {
|
|
223
326
|
return new Promise<SessionLocation | undefined>((res, rej) => {
|
|
224
327
|
const sub = getCurrentUser().subscribe((session) => {
|
|
@@ -228,6 +331,23 @@ export function getSessionLocation() {
|
|
|
228
331
|
});
|
|
229
332
|
}
|
|
230
333
|
|
|
334
|
+
/**
|
|
335
|
+
* Sets the session location for the current user. The session location represents
|
|
336
|
+
* the physical location where the user is working (e.g., a clinic or ward).
|
|
337
|
+
* This triggers a server request to update the session and refreshes the local
|
|
338
|
+
* session store.
|
|
339
|
+
*
|
|
340
|
+
* @param locationUuid The UUID of the location to set as the session location.
|
|
341
|
+
* @param abortController An AbortController to allow cancellation of the request.
|
|
342
|
+
* @returns A Promise that resolves with the updated SessionStore.
|
|
343
|
+
*
|
|
344
|
+
* @example
|
|
345
|
+
* ```ts
|
|
346
|
+
* import { setSessionLocation } from '@openmrs/esm-api';
|
|
347
|
+
* const abortController = new AbortController();
|
|
348
|
+
* await setSessionLocation('location-uuid-here', abortController);
|
|
349
|
+
* ```
|
|
350
|
+
*/
|
|
231
351
|
export async function setSessionLocation(locationUuid: string, abortController: AbortController): Promise<any> {
|
|
232
352
|
return handleSessionResponse(
|
|
233
353
|
openmrsFetch(sessionEndpoint, {
|
|
@@ -241,6 +361,29 @@ export async function setSessionLocation(locationUuid: string, abortController:
|
|
|
241
361
|
);
|
|
242
362
|
}
|
|
243
363
|
|
|
364
|
+
/**
|
|
365
|
+
* Updates the user properties for a specific user. User properties are key-value
|
|
366
|
+
* pairs that store user-specific settings and preferences. After updating the
|
|
367
|
+
* properties on the server, the current user session is refetched to reflect
|
|
368
|
+
* the changes.
|
|
369
|
+
*
|
|
370
|
+
* @param userUuid The UUID of the user whose properties should be updated.
|
|
371
|
+
* @param userProperties An object containing the properties to set or update.
|
|
372
|
+
* @param abortController Optional AbortController to allow cancellation of the request.
|
|
373
|
+
* If not provided, a new AbortController is created.
|
|
374
|
+
* @returns A Promise that resolves with the updated SessionStore after refetching
|
|
375
|
+
* the current user.
|
|
376
|
+
*
|
|
377
|
+
* @example
|
|
378
|
+
* ```ts
|
|
379
|
+
* import { getLoggedInUser, setUserProperties } from '@openmrs/esm-api';
|
|
380
|
+
* const user = await getLoggedInUser();
|
|
381
|
+
* await setUserProperties(user.uuid, {
|
|
382
|
+
* defaultLocale: 'en_GB',
|
|
383
|
+
* customSetting: 'value'
|
|
384
|
+
* });
|
|
385
|
+
* ```
|
|
386
|
+
*/
|
|
244
387
|
export async function setUserProperties(
|
|
245
388
|
userUuid: string,
|
|
246
389
|
userProperties: {
|
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Defines the minimum required versions of OpenMRS backend modules that the
|
|
3
|
+
* frontend framework depends on. These versions are checked at startup to ensure
|
|
4
|
+
* compatibility between the frontend and backend.
|
|
5
|
+
*/
|
|
1
6
|
export const backendDependencies = {
|
|
2
7
|
'webservices.rest': '2.24.0',
|
|
3
8
|
fhir2: '1.0.0-SNAPSHOT',
|
package/src/openmrs-fetch.ts
CHANGED
|
@@ -6,14 +6,21 @@ import { clearHistory, navigate } from '@openmrs/esm-navigation';
|
|
|
6
6
|
import type { FetchResponse } from './types';
|
|
7
7
|
import { defaultRedirectAuthFailureUrl, type EsmApiConfigObject } from './config-schema';
|
|
8
8
|
|
|
9
|
+
/** The base URL for the OpenMRS REST API (e.g., '/ws/rest/v1'). */
|
|
9
10
|
export const restBaseUrl = '/ws/rest/v1';
|
|
11
|
+
/** The base URL for the OpenMRS FHIR R4 API (e.g., '/ws/fhir2/R4'). */
|
|
10
12
|
export const fhirBaseUrl = '/ws/fhir2/R4';
|
|
13
|
+
/** The endpoint for session management operations. */
|
|
11
14
|
export const sessionEndpoint = `${restBaseUrl}/session`;
|
|
12
15
|
|
|
13
16
|
/**
|
|
14
17
|
* Append `path` to the OpenMRS SPA base.
|
|
15
18
|
*
|
|
16
|
-
*
|
|
19
|
+
* @param path The path to append to the OpenMRS base URL.
|
|
20
|
+
* @returns The full URL with the OpenMRS base prepended. If the path is already
|
|
21
|
+
* an absolute URL (starting with 'http'), it is returned unchanged.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
17
24
|
*
|
|
18
25
|
* ```ts
|
|
19
26
|
* makeUrl('/foo/bar');
|
|
@@ -49,7 +56,7 @@ export function makeUrl(path: string) {
|
|
|
49
56
|
* downloaded the HTTP response body as json, and has an additional
|
|
50
57
|
* `data` property with the HTTP response json as a javascript object.
|
|
51
58
|
*
|
|
52
|
-
*
|
|
59
|
+
* @example
|
|
53
60
|
* ```js
|
|
54
61
|
* import { openmrsFetch } from '@openmrs/esm-api'
|
|
55
62
|
* const abortController = new AbortController();
|
|
@@ -251,7 +258,7 @@ export function openmrsFetch<T = any>(path: string, fetchInit: FetchConfig = {})
|
|
|
251
258
|
* @returns An Observable that produces exactly one Response object.
|
|
252
259
|
* The response object is exactly the same as for [[openmrsFetch]].
|
|
253
260
|
*
|
|
254
|
-
*
|
|
261
|
+
* @example
|
|
255
262
|
*
|
|
256
263
|
* ```js
|
|
257
264
|
* import { openmrsObservableFetch } from '@openmrs/esm-api'
|