-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
49 lines (43 loc) · 1.03 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
46
47
48
49
import axios from 'axios'
const API_ENDPOINT = 'https://api.tibber.com/v1-beta/gql'
const gql = `{
viewer {
homes {
id,
currentSubscription{
priceInfo{
today {
total
energy
tax
startsAt
}
tomorrow {
total
energy
tax
startsAt
}
}
}
}
}
}`
const query = { query: gql.replace(/(\r\n|\r|\n)/g, '') }
async function getData (token) {
axios.defaults.headers.common.Authorization = `Bearer ${token}`
const { data } = await axios.post(API_ENDPOINT, query)
return data
}
async function getPriceInfo (token, homeId) {
try {
const { data } = await getData(token)
const home = homeId ? data.viewer.homes.find(h => h.id === homeId) : data.viewer.homes[0]
const priceInfo = home.currentSubscription.priceInfo
return [...priceInfo.today, ...priceInfo.tomorrow]
} catch (error) {
console.error(error)
return []
}
}
export default getPriceInfo