@openmrs/esm-api 6.3.1-pre.3204 → 6.3.1-pre.3211

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.
@@ -1,3 +1,3 @@
1
- [0] Successfully compiled: 13 files with swc (82.78ms)
1
+ [0] Successfully compiled: 14 files with swc (73.83ms)
2
2
  [0] swc --strip-leading-paths src -d dist exited with code 0
3
3
  [1] tsc --project tsconfig.build.json exited with code 0
@@ -0,0 +1,12 @@
1
+ import { type ConfigSchema } from '@openmrs/esm-config';
2
+ export declare const defaultRedirectAuthFailureUrl = "${openmrsSpaBase}/login";
3
+ export declare const configSchema: ConfigSchema;
4
+ export interface EsmApiConfigObject {
5
+ redirectAuthFailure: {
6
+ enabled: boolean;
7
+ url: string;
8
+ errors: number[];
9
+ resolvePromise: boolean;
10
+ };
11
+ followRedirects: boolean;
12
+ }
@@ -0,0 +1,42 @@
1
+ import { Type, validators } from "@openmrs/esm-config";
2
+ export const defaultRedirectAuthFailureUrl = '${openmrsSpaBase}/login';
3
+ export const configSchema = {
4
+ redirectAuthFailure: {
5
+ enabled: {
6
+ _type: Type.Boolean,
7
+ _default: true,
8
+ _description: 'Whether to redirect logged-out users to `redirectAuthFailure.url`'
9
+ },
10
+ url: {
11
+ _type: Type.String,
12
+ _default: defaultRedirectAuthFailureUrl,
13
+ _validators: [
14
+ validators.isUrl
15
+ ],
16
+ _description: 'The url to which users will be redirected when they are logged out. If set to blank, the `location` header from the response will be used.'
17
+ },
18
+ errors: {
19
+ _type: Type.Array,
20
+ _default: [
21
+ 401
22
+ ],
23
+ _elements: {
24
+ _type: Type.Number,
25
+ _validators: [
26
+ validators.inRange(100, 600)
27
+ ]
28
+ },
29
+ _description: 'The HTTP error codes for which users will be redirected'
30
+ },
31
+ resolvePromise: {
32
+ _type: Type.Boolean,
33
+ _default: false,
34
+ _description: "Changes how requests that fail authentication are handled. Try messing with this if redirects to the login page aren't working correctly."
35
+ }
36
+ },
37
+ followRedirects: {
38
+ _type: Type.Boolean,
39
+ _default: true,
40
+ _description: 'Whether openmrsFetch should support redirects returned from the backend'
41
+ }
42
+ };
@@ -15,6 +15,7 @@ import { Observable } from "rxjs";
15
15
  import { isPlainObject } from "lodash-es";
16
16
  import { getConfig } from "@openmrs/esm-config";
17
17
  import { clearHistory, navigate } from "@openmrs/esm-navigation";
18
+ import { defaultRedirectAuthFailureUrl } from "./config-schema.js";
18
19
  export const restBaseUrl = '/ws/rest/v1';
19
20
  export const fhirBaseUrl = '/ws/fhir2/R4';
20
21
  export const sessionEndpoint = `${restBaseUrl}/session`;
@@ -136,9 +137,9 @@ export const sessionEndpoint = `${restBaseUrl}/session`;
136
137
  */ const requestStacktrace = Error();
137
138
  return window.fetch(url, fetchInit).then(async (r)=>{
138
139
  const response = r;
140
+ const { redirectAuthFailure, followRedirects } = await getConfig('@openmrs/esm-api');
139
141
  if (response.ok) {
140
142
  if (response.status === 204) {
141
- const { followRedirects } = await getConfig('@openmrs/esm-api');
142
143
  if (followRedirects && response.headers.has('location')) {
143
144
  const location = response.headers.get('location');
144
145
  if (location) {
@@ -172,11 +173,14 @@ export const sessionEndpoint = `${restBaseUrl}/session`;
172
173
  * to help developers understand the problem and debug
173
174
  */ /*
174
175
  * Redirect to given url when redirect on auth failure is enabled
175
- */ const { redirectAuthFailure } = await getConfig('@openmrs/esm-api');
176
- if (url === makeUrl(sessionEndpoint) && response.status === 403 || redirectAuthFailure.enabled && redirectAuthFailure.errors.includes(response.status)) {
176
+ */ if (url === makeUrl(sessionEndpoint) && response.status === 403 || redirectAuthFailure.enabled && redirectAuthFailure.errors.includes(response.status)) {
177
177
  clearHistory();
178
+ // by default, redirect to the url specified in the config.
179
+ // If blank, use the location header from the response.
180
+ // If that is also blank, use the default redirect url.
181
+ const location = redirectAuthFailure.url || response.headers.get('location') || defaultRedirectAuthFailureUrl;
178
182
  navigate({
179
- to: redirectAuthFailure.url
183
+ to: location
180
184
  });
181
185
  /* We sometimes don't really want this promise to resolve since there's no response data,
182
186
  * nor do we want it to reject because that would trigger error handling. We instead
package/dist/setup.js CHANGED
@@ -1,46 +1,9 @@
1
- import { defineConfigSchema, Type, validators } from "@openmrs/esm-config";
1
+ import { defineConfigSchema } from "@openmrs/esm-config";
2
2
  import { refetchCurrentUser } from "./current-user.js";
3
+ import { configSchema } from "./config-schema.js";
3
4
  /**
4
5
  * @internal
5
6
  */ export function setupApiModule() {
6
- defineConfigSchema('@openmrs/esm-api', {
7
- redirectAuthFailure: {
8
- enabled: {
9
- _type: Type.Boolean,
10
- _default: true,
11
- _description: 'Whether to redirect logged-out users to `redirectAuthFailure.url`'
12
- },
13
- url: {
14
- _type: Type.String,
15
- _default: '${openmrsSpaBase}/login',
16
- _validators: [
17
- validators.isUrl
18
- ]
19
- },
20
- errors: {
21
- _type: Type.Array,
22
- _default: [
23
- 401
24
- ],
25
- _elements: {
26
- _type: Type.Number,
27
- _validators: [
28
- validators.inRange(100, 600)
29
- ]
30
- },
31
- _description: 'The HTTP error codes for which users will be redirected'
32
- },
33
- resolvePromise: {
34
- _type: Type.Boolean,
35
- _default: false,
36
- _description: "Changes how requests that fail authentication are handled. Try messing with this if redirects to the login page aren't working correctly."
37
- }
38
- },
39
- followRedirects: {
40
- _type: Type.Boolean,
41
- _default: true,
42
- _description: 'Whether openmrsFetch should support redirects returned from the backend'
43
- }
44
- });
7
+ defineConfigSchema('@openmrs/esm-api', configSchema);
45
8
  refetchCurrentUser();
46
9
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openmrs/esm-api",
3
- "version": "6.3.1-pre.3204",
3
+ "version": "6.3.1-pre.3211",
4
4
  "license": "MPL-2.0",
5
5
  "description": "The javascript module for interacting with the OpenMRS API",
6
6
  "type": "module",
@@ -61,10 +61,10 @@
61
61
  "@openmrs/esm-navigation": "6.x"
62
62
  },
63
63
  "devDependencies": {
64
- "@openmrs/esm-config": "6.3.1-pre.3204",
65
- "@openmrs/esm-error-handling": "6.3.1-pre.3204",
66
- "@openmrs/esm-globals": "6.3.1-pre.3204",
67
- "@openmrs/esm-navigation": "6.3.1-pre.3204",
64
+ "@openmrs/esm-config": "6.3.1-pre.3211",
65
+ "@openmrs/esm-error-handling": "6.3.1-pre.3211",
66
+ "@openmrs/esm-globals": "6.3.1-pre.3211",
67
+ "@openmrs/esm-navigation": "6.3.1-pre.3211",
68
68
  "@swc/cli": "^0.7.7",
69
69
  "@swc/core": "^1.11.29",
70
70
  "concurrently": "^9.1.2",
@@ -0,0 +1,50 @@
1
+ import { type ConfigSchema, Type, validators } from '@openmrs/esm-config';
2
+
3
+ export const defaultRedirectAuthFailureUrl = '${openmrsSpaBase}/login';
4
+
5
+ export const configSchema: ConfigSchema = {
6
+ redirectAuthFailure: {
7
+ enabled: {
8
+ _type: Type.Boolean,
9
+ _default: true,
10
+ _description: 'Whether to redirect logged-out users to `redirectAuthFailure.url`',
11
+ },
12
+ url: {
13
+ _type: Type.String,
14
+ _default: defaultRedirectAuthFailureUrl,
15
+ _validators: [validators.isUrl],
16
+ _description:
17
+ 'The url to which users will be redirected when they are logged out. If set to blank, the `location` header from the response will be used.',
18
+ },
19
+ errors: {
20
+ _type: Type.Array,
21
+ _default: [401],
22
+ _elements: {
23
+ _type: Type.Number,
24
+ _validators: [validators.inRange(100, 600)],
25
+ },
26
+ _description: 'The HTTP error codes for which users will be redirected',
27
+ },
28
+ resolvePromise: {
29
+ _type: Type.Boolean,
30
+ _default: false,
31
+ _description:
32
+ "Changes how requests that fail authentication are handled. Try messing with this if redirects to the login page aren't working correctly.",
33
+ },
34
+ },
35
+ followRedirects: {
36
+ _type: Type.Boolean,
37
+ _default: true,
38
+ _description: 'Whether openmrsFetch should support redirects returned from the backend',
39
+ },
40
+ };
41
+
42
+ export interface EsmApiConfigObject {
43
+ redirectAuthFailure: {
44
+ enabled: boolean;
45
+ url: string;
46
+ errors: number[];
47
+ resolvePromise: boolean;
48
+ };
49
+ followRedirects: boolean;
50
+ }
@@ -4,6 +4,7 @@ import { isPlainObject } from 'lodash-es';
4
4
  import { getConfig } from '@openmrs/esm-config';
5
5
  import { clearHistory, navigate } from '@openmrs/esm-navigation';
6
6
  import type { FetchResponse } from './types';
7
+ import { defaultRedirectAuthFailureUrl, type EsmApiConfigObject } from './config-schema';
7
8
 
8
9
  export const restBaseUrl = '/ws/rest/v1';
9
10
  export const fhirBaseUrl = '/ws/fhir2/R4';
@@ -148,9 +149,9 @@ export function openmrsFetch<T = any>(path: string, fetchInit: FetchConfig = {})
148
149
 
149
150
  return window.fetch(url, fetchInit as RequestInit).then(async (r) => {
150
151
  const response = r as FetchResponse<T>;
152
+ const { redirectAuthFailure, followRedirects } = await getConfig<EsmApiConfigObject>('@openmrs/esm-api');
151
153
  if (response.ok) {
152
154
  if (response.status === 204) {
153
- const { followRedirects } = await getConfig('@openmrs/esm-api');
154
155
  if (followRedirects && response.headers.has('location')) {
155
156
  const location = response.headers.get('location');
156
157
  if (location) {
@@ -190,14 +191,16 @@ export function openmrsFetch<T = any>(path: string, fetchInit: FetchConfig = {})
190
191
  /*
191
192
  * Redirect to given url when redirect on auth failure is enabled
192
193
  */
193
- const { redirectAuthFailure } = await getConfig('@openmrs/esm-api');
194
-
195
194
  if (
196
195
  (url === makeUrl(sessionEndpoint) && response.status === 403) ||
197
196
  (redirectAuthFailure.enabled && redirectAuthFailure.errors.includes(response.status))
198
197
  ) {
199
198
  clearHistory();
200
- navigate({ to: redirectAuthFailure.url });
199
+ // by default, redirect to the url specified in the config.
200
+ // If blank, use the location header from the response.
201
+ // If that is also blank, use the default redirect url.
202
+ const location = redirectAuthFailure.url || response.headers.get('location') || defaultRedirectAuthFailureUrl;
203
+ navigate({ to: location });
201
204
 
202
205
  /* We sometimes don't really want this promise to resolve since there's no response data,
203
206
  * nor do we want it to reject because that would trigger error handling. We instead
package/src/setup.ts CHANGED
@@ -1,44 +1,12 @@
1
- import { defineConfigSchema, Type, validators } from '@openmrs/esm-config';
1
+ import { defineConfigSchema } from '@openmrs/esm-config';
2
2
  import { refetchCurrentUser } from './current-user';
3
+ import { configSchema } from './config-schema';
3
4
 
4
5
  /**
5
6
  * @internal
6
7
  */
7
8
  export function setupApiModule() {
8
- defineConfigSchema('@openmrs/esm-api', {
9
- redirectAuthFailure: {
10
- enabled: {
11
- _type: Type.Boolean,
12
- _default: true,
13
- _description: 'Whether to redirect logged-out users to `redirectAuthFailure.url`',
14
- },
15
- url: {
16
- _type: Type.String,
17
- _default: '${openmrsSpaBase}/login',
18
- _validators: [validators.isUrl],
19
- },
20
- errors: {
21
- _type: Type.Array,
22
- _default: [401],
23
- _elements: {
24
- _type: Type.Number,
25
- _validators: [validators.inRange(100, 600)],
26
- },
27
- _description: 'The HTTP error codes for which users will be redirected',
28
- },
29
- resolvePromise: {
30
- _type: Type.Boolean,
31
- _default: false,
32
- _description:
33
- "Changes how requests that fail authentication are handled. Try messing with this if redirects to the login page aren't working correctly.",
34
- },
35
- },
36
- followRedirects: {
37
- _type: Type.Boolean,
38
- _default: true,
39
- _description: 'Whether openmrsFetch should support redirects returned from the backend',
40
- },
41
- });
9
+ defineConfigSchema('@openmrs/esm-api', configSchema);
42
10
 
43
11
  refetchCurrentUser();
44
12
  }