-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.js
51 lines (40 loc) · 1.43 KB
/
api.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
50
51
// NOT IN USE
// fetch data from bls api
// change for api version
const API_VERSION = 1; // 1 or 2
// series ID for the Consumer Price Index for All Urban Consumers: All Items (CPI-U)
const seriesId = "CUUR0000SA0";
const v1BaseURL = "https://api.bls.gov/publicAPI/v1/timeseries/data/";
const v2BaseURL = "https://api.bls.gov/publicAPI/v2/timeseries/data/";
const apiKey = "XXXXXXXXXXXXXXXXXXX";
let URL;
switch (API_VERSION) {
case 1:
URL = `${v1BaseURL}${seriesId}`;
break;
case 2:
URL = `${v2BaseURL}${seriesId}?registrationkey=${apiKey}`;
break;
default:
console.log("API Version set incorrectly");
}
// Button to invoke getCPI
// const testBtn = document.getElementById("test")
// testBtn.addEventListener("click", getCPI);
// fetches CPI for current year if needed
function getCPI() {
fetch(URL)
.then((response) => response.json())
.then((data) => {
// parse response for current year's monthly values
const monthlyValues = data.Results.series[0].data
.filter((y) => y.year == currentYear.toString())
.map((v) => Number(v.value));
console.log("monthlyValues: ", monthlyValues);
// calculate the average for the current year
const currentYearAvg = monthlyValues.reduce((a, b) => a + b, 0);
console.log("currentYearAvg: ", currentYearAvg);
// calculate the inflation rate change between the two years
})
.catch((error) => console.error(error));
}