-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy patha11y-base-url.ts
More file actions
52 lines (44 loc) · 1.56 KB
/
a11y-base-url.ts
File metadata and controls
52 lines (44 loc) · 1.56 KB
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
import { apiClient } from "./apiClient.js";
import logger from "../logger.js";
import { BrowserStackConfig } from "./types.js";
import { getBrowserStackAuth } from "./get-auth.js";
const A11Y_BASE_URLS = [
"https://api-accessibility.browserstack.com",
"https://api-accessibility-eu.browserstack.com",
"https://api-accessibility-in.browserstack.com",
] as const;
let cachedBaseUrl: string | null = null;
export async function getA11yBaseURL(
config: BrowserStackConfig,
): Promise<string> {
if (cachedBaseUrl) {
logger.debug(`Using cached A11y base URL: ${cachedBaseUrl}`);
return cachedBaseUrl;
}
logger.info(
"No cached A11y base URL found, testing available URLs with authentication",
);
const authString = getBrowserStackAuth(config);
const [username, password] = authString.split(":");
const authHeader =
"Basic " + Buffer.from(`${username}:${password}`).toString("base64");
for (const baseUrl of A11Y_BASE_URLS) {
try {
const res = await apiClient.get({
url: `${baseUrl}/api/automated-tests/v1/projects`,
headers: { Authorization: authHeader },
raise_error: false,
});
if (res.ok) {
cachedBaseUrl = baseUrl;
logger.info(`Selected A11y base URL: ${baseUrl}`);
return baseUrl;
}
} catch (err) {
logger.debug(`Failed A11y base URL: ${baseUrl} (${err})`);
}
}
throw new Error(
"Unable to connect to BrowserStack Accessibility API. Please check your credentials and network connection. Please open an issue on GitHub if the problem persists",
);
}