-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
45 lines (41 loc) · 1.31 KB
/
index.js
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
async function getPricesForDay (url) {
try {
const response = await fetch(url)
if (!response.ok) {
console.error('Response not OK', response.status)
return []
} else {
const json = await response.json()
return json
}
} catch (error) {
console.error(error)
return []
}
}
function generateUrl (dato = new Date().toISOString(), zone = 'NO2') {
const apiUrl = 'https://www.hvakosterstrommen.no/api/v1/prices'
const datestring = new Date(dato).toISOString().substring(0, 10)
const [year, month, day] = datestring.split('-')
return `${apiUrl}/${year}/${month}-${day}_${zone}.json`
}
function addDays (dateString, numberOfDays) {
const copyOfDate = new Date(dateString)
copyOfDate.setDate(copyOfDate.getDate() + numberOfDays)
return copyOfDate
}
async function getPrices (dato = new Date().toISOString(), zone = 'NO2') {
const today = new Date(dato).toISOString().substring(0, 10)
const tomorrow = addDays(dato, 1).toISOString().substring(0, 10)
const todayUrl = generateUrl(today, zone)
const tomorrowUrl = generateUrl(tomorrow, zone)
const todaysPrices = await getPricesForDay(todayUrl)
const tomorrowsPrices = await getPricesForDay(tomorrowUrl)
return [...todaysPrices, ...tomorrowsPrices]
}
export {
getPrices,
getPricesForDay,
generateUrl,
addDays
}