-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathbackend.ts
59 lines (46 loc) · 1.9 KB
/
backend.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// (C) 2019-2023 GoodData Corporation
import tigerFactory, {
ContextDeferredAuthProvider as TigerContextDeferredAuthProvider,
TigerTokenAuthProvider,
} from "@gooddata/sdk-backend-tiger";
import bearFactory, {
ContextDeferredAuthProvider as BearContextDeferredAuthProvider,
FixedLoginAndPasswordAuthProvider,
} from "@gooddata/sdk-backend-bear";
import { IAnalyticalBackend } from "@gooddata/sdk-backend-spi";
import { DEFAULT_BACKEND_URL } from "./constants.js";
export enum BackendType {
Tiger = "tiger",
Bear = "bear",
}
const isBearBackend = process.env.BACKEND_TYPE === BackendType.Bear;
export function hasCredentialsSetup(): boolean {
return isBearBackend
? !!(process.env.GDC_USERNAME && process.env.GDC_PASSWORD)
: !!process.env.TIGER_API_TOKEN;
}
export function needsAuthentication(): boolean {
return !!process.env.BACKEND_URL && process.env.BACKEND_URL !== DEFAULT_BACKEND_URL;
}
function getTigerBackend(): IAnalyticalBackend {
const newBackend = tigerFactory();
if (hasCredentialsSetup()) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return newBackend.withAuthentication(new TigerTokenAuthProvider(process.env.TIGER_API_TOKEN!));
}
return newBackend.withAuthentication(new TigerContextDeferredAuthProvider());
}
function getBearBackend(): IAnalyticalBackend {
const newBackend = bearFactory();
if (hasCredentialsSetup() && needsAuthentication()) {
return newBackend.withAuthentication(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
new FixedLoginAndPasswordAuthProvider(process.env.GDC_USERNAME!, process.env.GDC_PASSWORD!),
);
}
return newBackend.withAuthentication(new BearContextDeferredAuthProvider());
}
function getBackend() {
return isBearBackend ? getBearBackend() : getTigerBackend();
}
export const backend = getBackend();