-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.js
More file actions
38 lines (30 loc) · 936 Bytes
/
Copy pathsettings.js
File metadata and controls
38 lines (30 loc) · 936 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
34
35
36
37
38
const fetch = require("node-fetch")
const jsonBinUrl = process.env.JSONBIN_ROOT
const jsonBinId = process.env.JSONBIN_BINID
const jsonBinApiKey = process.env.JSONBIN_API_KEY
const settingsCacheExpirationMs = parseInt(process.env.SETTINGS_EXPIRATION_MS, 10)
const settingsCache = {
lastFetched: 0,
settings: null,
}
const getSettings = async () => {
const now = Date.now()
if (!settingsCache.settings || now > settingsCache.lastFetched) {
try {
const response = await fetch(`${jsonBinUrl}/b/${jsonBinId}/latest`, {
headers: {
"Content-Type": "application/json",
"secret-key": jsonBinApiKey,
},
})
const data = await response.json()
settingsCache.lastFetched = now + settingsCacheExpirationMs
settingsCache.settings = data
return data
} catch (error) {
return {}
}
}
return settingsCache.settings
}
module.exports = getSettings