@openmrs/esm-api 3.2.1-pre.972 → 3.3.1-pre.1173

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/jest.config.js CHANGED
@@ -1,6 +1,6 @@
1
1
  module.exports = {
2
2
  transform: {
3
- "^.+\\.tsx?$": "babel-jest",
3
+ "^.+\\.tsx?$": ["@swc/jest"],
4
4
  },
5
5
  moduleNameMapper: {
6
6
  "lodash-es": "lodash",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openmrs/esm-api",
3
- "version": "3.2.1-pre.972",
3
+ "version": "3.3.1-pre.1173",
4
4
  "license": "MPL-2.0",
5
5
  "description": "The javascript module for interacting with the OpenMRS API",
6
6
  "browser": "dist/openmrs-esm-api.js",
@@ -44,11 +44,11 @@
44
44
  "@openmrs/esm-error-handling": "3.x"
45
45
  },
46
46
  "devDependencies": {
47
- "@openmrs/esm-config": "^3.2.1-pre.972",
48
- "@openmrs/esm-error-handling": "^3.2.1-pre.972",
49
- "@openmrs/esm-state": "^3.2.1-pre.972",
47
+ "@openmrs/esm-config": "^3.3.1-pre.1173",
48
+ "@openmrs/esm-error-handling": "^3.3.1-pre.1173",
49
+ "@openmrs/esm-state": "^3.3.1-pre.1173",
50
50
  "@types/fhir": "0.0.31",
51
51
  "rxjs": "^6.5.3"
52
52
  },
53
- "gitHead": "40fa43724f730fc56bbde679fe128a9315ef931f"
53
+ "gitHead": "957c0c36b8f754808127269f6e50b90992a60559"
54
54
  }
package/src/fhir.ts CHANGED
@@ -1,7 +1,4 @@
1
- /**
2
- * @module
3
- * @category API
4
- */
1
+ /** @module @category API */
5
2
  import { openmrsFetch, FetchHeaders, OpenmrsFetchError } from "./openmrs-fetch";
6
3
  import type { FhirClient } from "./types/fhir";
7
4
 
@@ -1,8 +1,4 @@
1
- /**
2
- * @module
3
- * @category API
4
- */
5
-
1
+ /** @module @category API */
6
2
  import { Observable } from "rxjs";
7
3
  import isPlainObject from "lodash-es/isPlainObject";
8
4
  import { getConfig, navigate } from "@openmrs/esm-config";
@@ -10,6 +6,16 @@ import { FetchResponse } from "./types";
10
6
 
11
7
  export const sessionEndpoint = "/ws/rest/v1/session";
12
8
 
9
+ /**
10
+ * Append `path` to the OpenMRS SPA base.
11
+ *
12
+ * #### Example
13
+ *
14
+ * ```ts
15
+ * makeUrl('/foo/bar');
16
+ * // => '/openmrs/foo/bar'
17
+ * ```
18
+ */
13
19
  export function makeUrl(path: string) {
14
20
  return window.openmrsBase + path;
15
21
  }
package/src/public.ts ADDED
@@ -0,0 +1,11 @@
1
+ export * from "./types";
2
+ export * from "./openmrs-fetch";
3
+ export * from "./fhir";
4
+
5
+ export * from "./shared-api-objects/current-user";
6
+ export * from "./shared-api-objects/current-patient";
7
+ export * from "./shared-api-objects/visit-utils";
8
+ export * from "./shared-api-objects/visit-type";
9
+ export * from "./shared-api-objects/location";
10
+
11
+ export * from "./openmrs-backend-dependencies";
@@ -1,8 +1,4 @@
1
- /**
2
- * @module
3
- * @category API
4
- */
5
-
1
+ /** @module @category API */
6
2
  import { fhir } from "../fhir";
7
3
  import { FetchResponse } from "../types";
8
4
 
@@ -1,22 +1,19 @@
1
- /**
2
- * @module
3
- * @category API
4
- */
5
-
1
+ /** @module @category API */
6
2
  import { Observable, ReplaySubject } from "rxjs";
7
3
  import { filter, map, tap, mergeAll } from "rxjs/operators";
8
4
  import { openmrsFetch, sessionEndpoint } from "../openmrs-fetch";
9
- import {
10
- LoggedInUserData,
5
+ import type {
11
6
  LoggedInUser,
12
7
  CurrentUserWithResponseOption,
13
- UnauthenticatedUser,
14
8
  CurrentUserWithoutResponseOption,
15
9
  CurrentUserOptions,
16
10
  SessionLocation,
11
+ Privilege,
12
+ Role,
13
+ Session,
17
14
  } from "../types";
18
15
 
19
- const userSubject = new ReplaySubject<Promise<LoggedInUserData>>(1);
16
+ const userSubject = new ReplaySubject<Promise<Session>>(1);
20
17
  let lastFetchTimeMillis = 0;
21
18
 
22
19
  /**
@@ -60,13 +57,13 @@ let lastFetchTimeMillis = 0;
60
57
  function getCurrentUser(): Observable<LoggedInUser>;
61
58
  function getCurrentUser(
62
59
  opts: CurrentUserWithResponseOption
63
- ): Observable<UnauthenticatedUser>;
60
+ ): Observable<Session>;
64
61
  function getCurrentUser(
65
62
  opts: CurrentUserWithoutResponseOption
66
63
  ): Observable<LoggedInUser>;
67
64
  function getCurrentUser(
68
65
  opts: CurrentUserOptions = { includeAuthStatus: false }
69
- ): Observable<LoggedInUser | UnauthenticatedUser> {
66
+ ): Observable<LoggedInUser | Session> {
70
67
  if (lastFetchTimeMillis < Date.now() - 1000 * 60) {
71
68
  refetchCurrentUser();
72
69
  }
@@ -76,31 +73,32 @@ function getCurrentUser(
76
73
  tap(setUserLanguage),
77
74
  map((r) => (opts.includeAuthStatus ? r : r.user)),
78
75
  filter(Boolean)
79
- ) as Observable<LoggedInUser | UnauthenticatedUser>;
76
+ ) as Observable<LoggedInUser | Session>;
80
77
  }
81
78
 
82
- function setUserLanguage(data: LoggedInUserData) {
83
- if (data?.user?.userProperties?.defaultLocale) {
84
- const locale = data.user.userProperties.defaultLocale;
85
- const htmlLang = document.documentElement.getAttribute("lang");
79
+ export { getCurrentUser };
80
+
81
+ function setUserLanguage(data: Session) {
82
+ const locale = data?.user?.userProperties?.defaultLocale ?? data.locale;
83
+ const htmlLang = document.documentElement.getAttribute("lang");
86
84
 
87
- if (locale !== htmlLang) {
88
- document.documentElement.setAttribute("lang", locale);
89
- }
85
+ if (locale !== htmlLang) {
86
+ document.documentElement.setAttribute("lang", locale);
90
87
  }
91
88
  }
92
89
 
93
- function userHasPrivilege(requiredPrivilege: string, user: LoggedInUser) {
90
+ function userHasPrivilege(
91
+ requiredPrivilege: string,
92
+ user: { privileges: Array<Privilege> }
93
+ ) {
94
94
  return user.privileges.find((p) => requiredPrivilege === p.display);
95
95
  }
96
96
 
97
- function isSuperUser(user: LoggedInUser) {
97
+ function isSuperUser(user: { roles: Array<Role> }) {
98
98
  const superUserRole = "System Developer";
99
99
  return user.roles.find((role) => role.display === superUserRole);
100
100
  }
101
101
 
102
- export { getCurrentUser };
103
-
104
102
  /**
105
103
  * The `refetchCurrentUser` function causes a network request to redownload
106
104
  * the user. All subscribers to the current user will be notified of the
@@ -128,7 +126,10 @@ export function refetchCurrentUser() {
128
126
  );
129
127
  }
130
128
 
131
- export function userHasAccess(requiredPrivilege: string, user: LoggedInUser) {
129
+ export function userHasAccess(
130
+ requiredPrivilege: string,
131
+ user: { privileges: Array<Privilege>; roles: Array<Role> }
132
+ ) {
132
133
  return userHasPrivilege(requiredPrivilege, user) || isSuperUser(user);
133
134
  }
134
135
 
@@ -144,8 +145,8 @@ export function getLoggedInUser() {
144
145
  export function getSessionLocation() {
145
146
  return new Promise<SessionLocation | undefined>((res, rej) => {
146
147
  const sub = getCurrentUser({ includeAuthStatus: true }).subscribe(
147
- (user) => {
148
- res(user.sessionLocation);
148
+ (session) => {
149
+ res(session.sessionLocation);
149
150
  sub.unsubscribe();
150
151
  },
151
152
  rej
@@ -165,5 +166,6 @@ export async function setSessionLocation(
165
166
  },
166
167
  signal: abortController.signal,
167
168
  });
169
+
168
170
  refetchCurrentUser();
169
171
  }
@@ -1,8 +1,4 @@
1
- /**
2
- * @module
3
- * @category API
4
- */
5
-
1
+ /** @module @category API */
6
2
  import { Observable } from "rxjs";
7
3
  import { map, take } from "rxjs/operators";
8
4
  import { openmrsObservableFetch } from "../openmrs-fetch";
@@ -1,8 +1,4 @@
1
- /**
2
- * @module
3
- * @category API
4
- */
5
-
1
+ /** @module @category API */
6
2
  import { Observable } from "rxjs";
7
3
  import { map, take } from "rxjs/operators";
8
4
  import { openmrsObservableFetch } from "../openmrs-fetch";
@@ -1,8 +1,4 @@
1
- /**
2
- * @module
3
- * @category API
4
- */
5
-
1
+ /** @module @category API */
6
2
  import { Observable, BehaviorSubject } from "rxjs";
7
3
  import { take, map } from "rxjs/operators";
8
4
  import { openmrsObservableFetch } from "../openmrs-fetch";
@@ -13,19 +9,20 @@ import {
13
9
  Visit,
14
10
  } from "../types";
15
11
 
12
+ export const defaultVisitCustomRepresentation =
13
+ "custom:(uuid,encounters:(uuid,encounterDatetime," +
14
+ "form:(uuid,name),location:ref," +
15
+ "encounterType:ref,encounterProviders:(uuid,display," +
16
+ "provider:(uuid,display))),patient:(uuid,uuid)," +
17
+ "visitType:(uuid,name,display),attributes:(uuid,display,value),location:(uuid,name,display),startDatetime," +
18
+ "stopDatetime)";
19
+
16
20
  export function getVisitsForPatient(
17
21
  patientUuid: string,
18
22
  abortController: AbortController,
19
23
  v?: string
20
24
  ): Observable<FetchResponse<{ results: Array<Visit> }>> {
21
- const custom =
22
- v ||
23
- "custom:(uuid,encounters:(uuid,encounterDatetime," +
24
- "form:(uuid,name),location:ref," +
25
- "encounterType:ref,encounterProviders:(uuid,display," +
26
- "provider:(uuid,display))),patient:(uuid,uuid)," +
27
- "visitType:(uuid,name,display),attributes:(uuid,display,value),location:(uuid,name,display),startDatetime," +
28
- "stopDatetime)";
25
+ const custom = v ?? defaultVisitCustomRepresentation;
29
26
 
30
27
  return openmrsObservableFetch(
31
28
  `/ws/rest/v1/visit?patient=${patientUuid}&v=${custom}`,
@@ -1,13 +1,9 @@
1
- import { LoggedInUser, UnauthenticatedUser } from "./user-resource";
1
+ import { Session } from "./user-resource";
2
2
 
3
3
  export interface FetchResponse<T = any> extends Response {
4
4
  data: T;
5
5
  }
6
6
 
7
- export type LoggedInUserData = UnauthenticatedUser & {
8
- user?: LoggedInUser;
9
- };
10
-
11
7
  export interface LoggedInUserFetchResponse extends FetchResponse {
12
- data: LoggedInUserData;
8
+ data: Session;
13
9
  }
@@ -3,25 +3,3 @@ export interface OpenmrsResource {
3
3
  display?: string;
4
4
  [anythingElse: string]: any;
5
5
  }
6
-
7
- export interface SessionUser {
8
- allowedLocales: Array<string>;
9
- authenticated: boolean;
10
- locale: string;
11
- sessionId: string;
12
- user: User;
13
- currentProvider: { uuid: string; identifier: string };
14
- sessionLocation: any | null;
15
- }
16
-
17
- export interface User {
18
- display: string;
19
- link: Array<string>;
20
- person: any;
21
- priviliges: any;
22
- resourceVersion: any;
23
- roles: Array<any>;
24
- userProperties: any;
25
- username: string;
26
- uuid: string;
27
- }
@@ -10,12 +10,22 @@ export interface CurrentUserWithoutResponseOption extends CurrentUserOptions {
10
10
  includeAuthStatus: false;
11
11
  }
12
12
 
13
+ export interface Session {
14
+ allowedLocales?: Array<string>;
15
+ authenticated: boolean;
16
+ locale?: string;
17
+ sessionId: string;
18
+ user?: LoggedInUser;
19
+ currentProvider?: { uuid: string; identifier: string };
20
+ sessionLocation?: SessionLocation;
21
+ }
22
+
13
23
  export interface LoggedInUser {
14
24
  uuid: string;
15
25
  display: string;
16
26
  username: string;
17
27
  systemId: string;
18
- userProperties: any;
28
+ userProperties: { [key: string]: any } | null;
19
29
  person: Person;
20
30
  privileges: Array<Privilege>;
21
31
  roles: Array<Role>;
@@ -25,13 +35,6 @@ export interface LoggedInUser {
25
35
  [anythingElse: string]: any;
26
36
  }
27
37
 
28
- export interface UnauthenticatedUser {
29
- sessionId: string;
30
- authenticated: boolean;
31
- user?: LoggedInUser;
32
- sessionLocation?: SessionLocation;
33
- }
34
-
35
38
  export interface SessionLocation {
36
39
  uuid: string;
37
40
  display: string;
@@ -47,7 +50,7 @@ export interface Person {
47
50
  export interface Privilege {
48
51
  uuid: string;
49
52
  display: string;
50
- links: Array<any>;
53
+ links?: Array<any>;
51
54
  }
52
55
 
53
56
  export interface Role {
package/webpack.config.js CHANGED
@@ -19,7 +19,7 @@ module.exports = (env) => ({
19
19
  {
20
20
  test: /\.m?(js|ts|tsx)$/,
21
21
  exclude: /(node_modules|bower_components)/,
22
- use: "babel-loader",
22
+ use: "swc-loader",
23
23
  },
24
24
  ],
25
25
  },
package/.babelrc DELETED
@@ -1,7 +0,0 @@
1
- {
2
- "presets": [
3
- "@babel/preset-env",
4
- "@babel/preset-typescript",
5
- "@babel/preset-react"
6
- ]
7
- }
@@ -1,14 +0,0 @@
1
- /*! *****************************************************************************
2
- Copyright (c) Microsoft Corporation.
3
-
4
- Permission to use, copy, modify, and/or distribute this software for any
5
- purpose with or without fee is hereby granted.
6
-
7
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
8
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
9
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
10
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
11
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
12
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
13
- PERFORMANCE OF THIS SOFTWARE.
14
- ***************************************************************************** */