forked from KelvinTegelaar/CIPP
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from KelvinTegelaar/dev
[pull] dev from KelvinTegelaar:dev
- Loading branch information
Showing
18 changed files
with
345 additions
and
93 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
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,23 @@ | ||
export default function validateAlphabeticalSort(data, sortKeys) { | ||
if (!sortKeys || sortKeys.length === 0) return data | ||
try { | ||
if (!data) return data | ||
const newList = data.filter((element) => { | ||
return sortKeys.every((key) => { | ||
return (element) => element[key] != null && element[key] != undefined | ||
}) | ||
}) | ||
return newList.sort((a, b) => { | ||
try { | ||
return sortKeys.reduce((acc, key) => { | ||
if (acc !== 0) return acc | ||
return (a[key] ?? '').toString().localeCompare(b[key] ?? '') | ||
}, 0) | ||
} catch (error) { | ||
return 0 | ||
} | ||
}) | ||
} catch (error) { | ||
return data | ||
} | ||
} |
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,30 +1,57 @@ | ||
import axios from 'axios' | ||
let newController = new AbortController() | ||
|
||
let newController = new AbortController() // Controller for managing abortion of requests | ||
|
||
const retryDelays = [100, 200, 300] // Delays in milliseconds for retries | ||
|
||
export const axiosQuery = async ({ path, method = 'get', params, data, hideToast }) => { | ||
try { | ||
const result = await axios({ | ||
signal: path === '/api/ListTenants' ? undefined : newController.signal, | ||
method, | ||
baseURL: window.location.origin, | ||
url: path, | ||
data, | ||
params, | ||
}) | ||
return { data: result?.data } | ||
} catch (error) { | ||
return { | ||
error: { | ||
status: error.response?.status, | ||
data: error.response?.data, | ||
hideToast, | ||
message: error?.message, | ||
}, | ||
let attempt = 0 | ||
|
||
while (attempt <= retryDelays.length) { | ||
try { | ||
const result = await axios({ | ||
signal: newController.signal, | ||
method, | ||
baseURL: window.location.origin, | ||
url: path, | ||
data, | ||
params, | ||
}) | ||
return { data: result.data } // Successful response | ||
} catch (error) { | ||
console.log('error', error) | ||
console.log('path', path) | ||
if (attempt === retryDelays.length || !shouldRetry(error, path)) { | ||
return { | ||
// Max retries reached or error should not trigger a retry | ||
error: { | ||
status: error.response?.status, | ||
data: error.response?.data, | ||
hideToast, | ||
message: error.message, | ||
}, | ||
} | ||
} | ||
await delay(retryDelays[attempt]) // Wait before retrying | ||
attempt++ | ||
} | ||
} | ||
} | ||
|
||
const shouldRetry = (error, path) => { | ||
// Check if the path starts with 'List', error qualifies for a retry, and payload message is 'Backend call failure' | ||
return ( | ||
path.toLowerCase().startsWith('/api/list') && | ||
error.response && | ||
error.response.status >= 500 && | ||
error.response.data === 'Backend call failure' | ||
) | ||
} | ||
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms)) | ||
|
||
export function abortRequestSafe() { | ||
newController.abort() | ||
newController = new AbortController() | ||
newController.abort() // Abort any ongoing request | ||
newController = new AbortController() // Reset the controller for new requests | ||
} | ||
|
||
export const baseQuery = ({ baseUrl } = { baseUrl: '' }) => axiosQuery |
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
Oops, something went wrong.