mirror of
https://github.com/Azure/cosmos-explorer.git
synced 2025-12-30 14:22:05 +00:00
Migrated Hosted Explorer to React (#360)
Co-authored-by: Victor Meng <vimeng@microsoft.com> Co-authored-by: Steve Faulkner <stfaul@microsoft.com>
This commit is contained in:
38
src/hooks/useDatabaseAccounts.tsx
Normal file
38
src/hooks/useDatabaseAccounts.tsx
Normal file
@@ -0,0 +1,38 @@
|
||||
import useSWR from "swr";
|
||||
import { DatabaseAccount } from "../Contracts/DataModels";
|
||||
|
||||
interface AccountListResult {
|
||||
nextLink: string;
|
||||
value: DatabaseAccount[];
|
||||
}
|
||||
|
||||
export async function fetchDatabaseAccounts(subscriptionId: string, accessToken: string): Promise<DatabaseAccount[]> {
|
||||
const headers = new Headers();
|
||||
const bearer = `Bearer ${accessToken}`;
|
||||
|
||||
headers.append("Authorization", bearer);
|
||||
|
||||
let accounts: Array<DatabaseAccount> = [];
|
||||
|
||||
let nextLink = `https://management.azure.com/subscriptions/${subscriptionId}/providers/Microsoft.DocumentDB/databaseAccounts?api-version=2020-06-01-preview`;
|
||||
|
||||
while (nextLink) {
|
||||
const response: Response = await fetch(nextLink, { headers });
|
||||
const result: AccountListResult =
|
||||
response.status === 204 || response.status === 304 ? undefined : await response.json();
|
||||
if (!response.ok) {
|
||||
throw result;
|
||||
}
|
||||
nextLink = result.nextLink;
|
||||
accounts = [...accounts, ...result.value];
|
||||
}
|
||||
return accounts;
|
||||
}
|
||||
|
||||
export function useDatabaseAccounts(subscriptionId: string, armToken: string): DatabaseAccount[] | undefined {
|
||||
const { data } = useSWR(
|
||||
() => (armToken && subscriptionId ? ["databaseAccounts", subscriptionId, armToken] : undefined),
|
||||
(_, subscriptionId, armToken) => fetchDatabaseAccounts(subscriptionId, armToken)
|
||||
);
|
||||
return data;
|
||||
}
|
||||
Reference in New Issue
Block a user