-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add centralized request service (#4831)
* create request handler * Add interceptor initialization to app mount * Implement request handling service * Add request disabling to server * Modify changelog * Use core plugin as parameter * Move services to public * Remove service from backend * Modify imports of the service * Add validation for unauthorized requests * Improve code quality * Remove duplicated logic * Add reload on unauthorized requests * Change when check-wazuh is executed * Remove unnecessary verifications * Improve code quality * fix: condition on request service and replace http status code by constant * fix: remove unused import * fix: default value of parameter in request service Co-authored-by: Federico Rodriguez <[email protected]> Co-authored-by: Antonio David Gutiérrez <[email protected]>
- Loading branch information
1 parent
81a9712
commit 99fbab1
Showing
8 changed files
with
72 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import axios from 'axios'; | ||
import { HTTP_STATUS_CODES } from '../../common/constants'; | ||
|
||
let allow = true; | ||
const source = axios.CancelToken.source(); | ||
|
||
const disableRequests = () => { | ||
allow = false; | ||
source.cancel('Requests cancelled'); | ||
return; | ||
} | ||
|
||
export const initializeInterceptor = (core) => { | ||
core.http.intercept({ | ||
responseError: (httpErrorResponse, controller) => { | ||
if ( | ||
httpErrorResponse.response?.status === HTTP_STATUS_CODES.UNAUTHORIZED | ||
) { | ||
disableRequests(); | ||
} | ||
}, | ||
}); | ||
} | ||
|
||
export const request = async (options = {}) => { | ||
if (!allow) { | ||
return Promise.reject('Requests are disabled'); | ||
}; | ||
if (!options.method || !options.url) { | ||
return Promise.reject("Missing parameters"); | ||
}; | ||
options = { | ||
...options, cancelToken: source.token | ||
}; | ||
|
||
if (allow) { | ||
try { | ||
const requestData = await axios(options); | ||
return Promise.resolve(requestData); | ||
} | ||
catch (e) { | ||
if (e.response?.data?.message === 'Unauthorized' || e.response?.data?.message === 'Authentication required') { | ||
disableRequests(); | ||
window.location.reload(); | ||
} | ||
return Promise.reject(e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
export * as OdfeUtils from './odfe-utils'; | ||
|
||
export { checkPluginVersion } from './check-plugin-version'; | ||
|
||
export { addHelpMenuToAppChrome } from './add_help_menu_to_app'; |
This file was deleted.
Oops, something went wrong.