cosmos-explorer/src/UserContext.ts

63 lines
2.1 KiB
TypeScript
Raw Normal View History

2021-02-22 14:43:58 -06:00
import { AuthType } from "./AuthType";
import { DatabaseAccount } from "./Contracts/DataModels";
import { SubscriptionType } from "./Contracts/SubscriptionType";
import { DefaultAccountExperienceType } from "./DefaultAccountExperienceType";
interface UserContext {
2021-02-22 14:43:58 -06:00
authType?: AuthType;
masterKey?: string;
subscriptionId?: string;
resourceGroup?: string;
databaseAccount?: DatabaseAccount;
endpoint?: string;
accessToken?: string;
authorizationToken?: string;
resourceToken?: string;
defaultExperience?: DefaultAccountExperienceType;
useSDKOperations?: boolean;
subscriptionType?: SubscriptionType;
quotaId?: string;
2021-03-10 23:02:55 -06:00
// API Type is not yet provided by ARM. You need to manually inspect all the capabilities+kind so we abstract that logic in userContext
// This is coming in a future Cosmos ARM API version as a prperty on databaseAccount
apiType?: ApiType;
2021-03-17 16:02:20 -05:00
isTryCosmosDBSubscription?: boolean;
2021-03-17 15:24:21 -05:00
portalEnv?: PortalEnv;
}
2021-03-10 23:02:55 -06:00
type ApiType = "SQL" | "Mongo" | "Gremlin" | "Tables" | "Cassandra";
2021-03-17 15:24:21 -05:00
export type PortalEnv = "localhost" | "blackforest" | "fairfax" | "mooncake" | "prod" | "dev";
2021-03-10 23:02:55 -06:00
2021-03-17 16:02:20 -05:00
const userContext: UserContext = { isTryCosmosDBSubscription: false, portalEnv: "prod" };
function updateUserContext(newContext: UserContext): void {
Object.assign(userContext, newContext);
2021-03-10 23:02:55 -06:00
Object.assign(userContext, { apiType: apiType(userContext.databaseAccount) });
}
function apiType(account: DatabaseAccount | undefined): ApiType {
if (!account) {
return "SQL";
}
const capabilities = account.properties?.capabilities;
if (capabilities) {
if (capabilities.find((c) => c.name === "EnableCassandra")) {
return "Cassandra";
}
if (capabilities.find((c) => c.name === "EnableGremlin")) {
return "Gremlin";
}
if (capabilities.find((c) => c.name === "EnableMongo")) {
return "Mongo";
}
if (capabilities.find((c) => c.name === "EnableTable")) {
return "Tables";
}
}
if (account.kind === "MongoDB" || account.kind === "Parse") {
return "Mongo";
}
return "SQL";
}
export { userContext, updateUserContext };