-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathfetchBanners.mjs
More file actions
38 lines (30 loc) · 1 KB
/
fetchBanners.mjs
File metadata and controls
38 lines (30 loc) · 1 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
/** @import { BannerEntry, RemoteConfig } from './types.d.ts' */
import { isBannerActive } from '../../utils/banner.mjs';
/**
* Fetches and returns active banners for the given version from the remote config.
* Returns an empty array on any fetch or parse failure.
*
* @param {string} remoteConfig
* @param {number | null} versionMajor
* @returns {Promise<BannerEntry[]>}
*/
export const fetchBanners = async (remoteConfig, versionMajor) => {
const res = await fetch(remoteConfig, { signal: AbortSignal.timeout(2500) });
if (!res.ok) {
return [];
}
/** @type {RemoteConfig} */
const config = await res.json();
const active = [];
const globalBanner = config.websiteBanners?.index;
if (globalBanner && isBannerActive(globalBanner)) {
active.push(globalBanner);
}
if (versionMajor != null) {
const versionBanner = config.websiteBanners?.[`v${versionMajor}`];
if (versionBanner && isBannerActive(versionBanner)) {
active.push(versionBanner);
}
}
return active;
};