From d7f618c7fb735199f7f1b15a498cbdd3b25ac20e Mon Sep 17 00:00:00 2001 From: Ian Date: Mon, 9 Dec 2024 15:39:48 -0500 Subject: [PATCH] (feat) OA-41: openmrsFetch() should handle redirects --- packages/framework/esm-api/src/openmrs-fetch.ts | 8 ++++++++ packages/framework/esm-api/src/setup.ts | 5 +++++ .../esm-framework/docs/classes/OpenmrsFetchError.md | 6 +++--- .../esm-framework/docs/interfaces/FetchConfig.md | 4 ++-- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/packages/framework/esm-api/src/openmrs-fetch.ts b/packages/framework/esm-api/src/openmrs-fetch.ts index e4fc791db..5e3486071 100644 --- a/packages/framework/esm-api/src/openmrs-fetch.ts +++ b/packages/framework/esm-api/src/openmrs-fetch.ts @@ -152,6 +152,14 @@ export function openmrsFetch(path: string, fetchInit: FetchConfig = {}) const response = r as FetchResponse; if (response.ok) { if (response.status === 204) { + const { followRedirects } = await getConfig('@openmrs/esm-api'); + if (followRedirects && response.headers.has('location')) { + const location = response.headers.get('location'); + if (location) { + navigate({ to: location }); + } + } + /* HTTP 204 - No Content * We should not try to download the empty response as json. Instead, * we return null since there is no response body. diff --git a/packages/framework/esm-api/src/setup.ts b/packages/framework/esm-api/src/setup.ts index a5e37d5cb..ba5c264ef 100644 --- a/packages/framework/esm-api/src/setup.ts +++ b/packages/framework/esm-api/src/setup.ts @@ -33,6 +33,11 @@ export function setupApiModule() { "Changes how requests that fail authentication are handled. Try messing with this if redirects to the login page aren't working correctly.", }, }, + followRedirects: { + _type: Type.Boolean, + _default: true, + _description: 'Whether openmrsFetch should support redirects returned from the backend', + }, }); refetchCurrentUser(); diff --git a/packages/framework/esm-framework/docs/classes/OpenmrsFetchError.md b/packages/framework/esm-framework/docs/classes/OpenmrsFetchError.md index 0ea785f73..9dcfd8fc6 100644 --- a/packages/framework/esm-framework/docs/classes/OpenmrsFetchError.md +++ b/packages/framework/esm-framework/docs/classes/OpenmrsFetchError.md @@ -53,7 +53,7 @@ Error.constructor #### Defined in -[packages/framework/esm-api/src/openmrs-fetch.ts:296](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/openmrs-fetch.ts#L296) +[packages/framework/esm-api/src/openmrs-fetch.ts:304](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/openmrs-fetch.ts#L304) ## API Properties @@ -63,7 +63,7 @@ Error.constructor #### Defined in -[packages/framework/esm-api/src/openmrs-fetch.ts:304](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/openmrs-fetch.ts#L304) +[packages/framework/esm-api/src/openmrs-fetch.ts:312](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/openmrs-fetch.ts#L312) ___ @@ -73,7 +73,7 @@ ___ #### Defined in -[packages/framework/esm-api/src/openmrs-fetch.ts:305](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/openmrs-fetch.ts#L305) +[packages/framework/esm-api/src/openmrs-fetch.ts:313](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/openmrs-fetch.ts#L313) ___ diff --git a/packages/framework/esm-framework/docs/interfaces/FetchConfig.md b/packages/framework/esm-framework/docs/interfaces/FetchConfig.md index e6c700fdb..d2083158c 100644 --- a/packages/framework/esm-framework/docs/interfaces/FetchConfig.md +++ b/packages/framework/esm-framework/docs/interfaces/FetchConfig.md @@ -37,7 +37,7 @@ #### Defined in -[packages/framework/esm-api/src/openmrs-fetch.ts:310](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/openmrs-fetch.ts#L310) +[packages/framework/esm-api/src/openmrs-fetch.ts:318](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/openmrs-fetch.ts#L318) ___ @@ -47,7 +47,7 @@ ___ #### Defined in -[packages/framework/esm-api/src/openmrs-fetch.ts:309](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/openmrs-fetch.ts#L309) +[packages/framework/esm-api/src/openmrs-fetch.ts:317](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/openmrs-fetch.ts#L317) ___