mirror of
https://github.com/Azure/cosmos-explorer.git
synced 2025-04-15 14:17:29 +01:00
41 lines
1.5 KiB
TypeScript
41 lines
1.5 KiB
TypeScript
import useSWR from "swr";
|
|
import { configContext } from "../ConfigContext";
|
|
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 = `${configContext.ARM_ENDPOINT}/subscriptions/${subscriptionId}/providers/Microsoft.DocumentDB/databaseAccounts?api-version=2021-06-15`;
|
|
|
|
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.sort((a, b) => a.name.localeCompare(b.name));
|
|
}
|
|
|
|
export function useDatabaseAccounts(subscriptionId: string, armToken: string): DatabaseAccount[] | undefined {
|
|
const { data } = useSWR(
|
|
// eslint-disable-next-line no-null/no-null
|
|
() => (armToken && subscriptionId ? ["databaseAccounts", subscriptionId, armToken] : null),
|
|
(_: string, subscriptionId: string, armToken: string) => fetchDatabaseAccounts(subscriptionId, armToken)
|
|
);
|
|
return data;
|
|
}
|