RTK Query: Update provisioning/sources endpoint
This commit replaces the `getSources` endpoint with an AWS specific endpoint, `getAWSSources`. This is necessary because the provisioning/sources endpoint was recently updated and now has an optional query parameter, ?provider, that allows requesting sources from a specific provider (aws, azure, etc...). Unfortunately, the response from /provisioning/sources?provider=aws does not include the associated aws account id, which has to be requested from a different endpoint. The new `getAWSSources` endpoint combines these two requests - first it requests the list of aws sources, then it requests the associated aws account id for each source. Combining these two requests into a single RTK Query endpoint is advantageous because all relevant information can be retreived with a single call to the `useGetAWSSourcesQuery` hook.
This commit is contained in:
parent
1ebcfd7af2
commit
7768c44630
1 changed files with 35 additions and 3 deletions
|
|
@ -6,10 +6,42 @@ export const apiSlice = createApi({
|
|||
reducerPath: 'api',
|
||||
baseQuery: fetchBaseQuery({ baseUrl: '' }),
|
||||
endpoints: (builder) => ({
|
||||
getSources: builder.query({
|
||||
query: () => PROVISIONING_SOURCES_ENDPOINT,
|
||||
getAWSSources: builder.query({
|
||||
async queryFn(_arg, _queryApi, _extraOptions, fetchWithBQ) {
|
||||
// The provisioning sources endpoint response does not include the account ids
|
||||
// associated with the sources. For each source, another API request must be
|
||||
// made to get the account id. This custom queryFn combines all of these API
|
||||
// requests so that the React components can call simply call a single hook,
|
||||
// useGetAWSSourcesQuery().
|
||||
|
||||
const awsSources = await fetchWithBQ(
|
||||
`${PROVISIONING_SOURCES_ENDPOINT}?provider=aws`
|
||||
);
|
||||
|
||||
if (awsSources.error) return { error: awsSources.error };
|
||||
|
||||
const awsAccountIds = await Promise.allSettled(
|
||||
awsSources.data.map((source) =>
|
||||
fetchWithBQ(
|
||||
`${PROVISIONING_SOURCES_ENDPOINT}/${source.id}/account_identity`
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
// Merge the account ids into awsSources
|
||||
for (let i = 0; i < awsSources.data.length; i++) {
|
||||
if (awsAccountIds[i].value.error) {
|
||||
awsSources.data[i].account_id = '';
|
||||
} else {
|
||||
awsSources.data[i].account_id =
|
||||
awsAccountIds[i].value.data.aws.account_id;
|
||||
}
|
||||
}
|
||||
|
||||
return awsSources;
|
||||
},
|
||||
}),
|
||||
}),
|
||||
});
|
||||
|
||||
export const { useGetSourcesQuery } = apiSlice;
|
||||
export const { useGetAWSSourcesQuery, usePrefetch } = apiSlice;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue