-
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 (#4758)
* create service * Improve service functionality * add timeout handling * Improve code quality * Fix issue that caused infinite loop * separate interceptor initialization * add interceptor to plugin start * fix double reload and add abort * Add function to disable requests externally * Add auth validation on server to disable requests * Update changelog * Add missing parameters verification * Add missing parameter validation * Add reload to interceptor * Change structure of code * Implement new service * Fix bug * Fix errors * Create unit tests * Fix bad reference to previous PR in changelog * Add missing previous change in changelog (cherry picked from commit 34c3fae)
- Loading branch information
Showing
8 changed files
with
201 additions
and
37 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,89 @@ | ||
import { getCore } from '../kibana-services'; | ||
|
||
let allow = true; | ||
let aborts = []; | ||
let currentid = 0; | ||
|
||
const removeController = (id) => { | ||
const index = aborts.findIndex(object => { | ||
return object.id === id; | ||
}); | ||
if (!id) { | ||
return; | ||
} | ||
aborts.splice(index); | ||
return; | ||
} | ||
|
||
export const disableRequests = () => { | ||
allow = false; | ||
aborts.forEach(item => { | ||
item.controller.abort(); | ||
}) | ||
return; | ||
} | ||
|
||
export const initializeInterceptor = () => { | ||
const core = getCore(); | ||
core.http.intercept({ | ||
responseError: (httpErrorResponse, controller) => { | ||
if ( | ||
httpErrorResponse.response?.status === 401 | ||
) { | ||
disableRequests(); | ||
setTimeout(() => window.location.reload(), 1000); | ||
} | ||
}, | ||
}); | ||
} | ||
|
||
export const request = async (info = '') => { | ||
if (!allow) { | ||
return Promise.reject('Requests are disabled'); | ||
} | ||
|
||
|
||
if (!info.method | !info.path) { | ||
return Promise.reject("Missing parameters") | ||
} | ||
|
||
let { method, path, headers, data, timeout } = info; | ||
const core = getCore(); | ||
const url = path.split('?')[0] | ||
|
||
const query = Object.fromEntries([... new URLSearchParams(path.split('?')[1])]) | ||
const abort = new AbortController(); | ||
let options = { | ||
method: method, | ||
headers: headers, | ||
query: query, | ||
signal: abort.signal, | ||
id: currentid | ||
} | ||
currentid++; | ||
|
||
if (method !== 'GET') { | ||
options = { ...options, body: JSON.stringify(data) } | ||
} | ||
|
||
if (allow) { | ||
try { | ||
aborts.push({ id: options.id, controller: abort }) | ||
if (timeout && timeout !== 0) { | ||
const id = setTimeout(() => abort.abort(), timeout); | ||
const requestData = await core.http.fetch(url, options); | ||
clearTimeout(id); | ||
removeController(options.id); | ||
return Promise.resolve({ data: requestData, timeout: timeout }); | ||
} | ||
else { | ||
const requestData = await core.http.fetch(url, options); | ||
removeController(options.id); | ||
return Promise.resolve({ data: requestData }); | ||
} | ||
} | ||
catch (e) { | ||
return Promise.reject(e); | ||
} | ||
} | ||
} |
Oops, something went wrong.