-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathloadBanners.mjs
More file actions
33 lines (28 loc) · 912 Bytes
/
loadBanners.mjs
File metadata and controls
33 lines (28 loc) · 912 Bytes
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
import { isBannerActive } from '../../utils/banner.mjs';
/**
* Fetches and returns active banners for the given version.
* Returns an empty array when remoteConfig is absent, the response is not ok,
* or on any fetch/parse failure.
*
* @param {string | undefined} remoteConfig
* @param {number | null} versionMajor
* @returns {Promise<Array<import('./types.d.ts').BannerEntry>>}
*/
export const loadBanners = async (remoteConfig, versionMajor) => {
if (!remoteConfig) {
return [];
}
try {
const res = await fetch(remoteConfig);
/** @type {import('./types.d.ts').RemoteConfig} */
const { websiteBanners = {} } = await res.json();
const keys = ['index', versionMajor != null && `v${versionMajor}`].filter(
Boolean
);
return keys
.map(key => websiteBanners[key])
.filter(banner => banner && isBannerActive(banner));
} catch {
return [];
}
};