Skip to content

Commit 3406a2e

Browse files
authored
Upgrade to fetch()
1 parent b4be7e9 commit 3406a2e

File tree

3 files changed

+48
-40
lines changed

3 files changed

+48
-40
lines changed

src/js/utils/http.js

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
1+
import manifest from "../../manifest.json"
2+
13
/**
2-
* Make an HTTP request
3-
* @param {string} method - HTTP method (e.g., "GET", "POST")
4-
* @param {string} url - Request URL
5-
* @param {function} callback - Callback function to handle the response
4+
* Make an HTTP request, returning the response as JSON.
5+
* @param {string} method - HTTP method (GET, POST, etc.)
6+
* @param {string} url - The URL to send the request to
7+
* @returns {Promise<any>} - The response data as JSON
68
*/
7-
export const http = (method, url, callback) => {
8-
if (!window.XMLHttpRequest) return console.log("This browser does not support requests")
9-
const request = new XMLHttpRequest()
10-
request.open(method, url, true)
11-
request.setRequestHeader("Content-Type", "application/json")
12-
request.onload = function () {
13-
if (this.status >= 200 && this.status < 400) {
14-
const json = JSON.parse(this.response)
15-
if (callback && typeof(callback) === "function") {
16-
callback(json)
17-
}
9+
export async function http(method, url) {
10+
if (!window.fetch) return console.error("This browser does not support requests")
11+
12+
const response = await fetch(url, {
13+
method: method.toUpperCase(),
14+
headers: {
15+
"Content-Type": "application/json",
16+
// The only reason I use X-* headers is because some browsers refuse to set custom "unsafe headers"
17+
"X-Referer": "https://alexflipnote.dev/homepage",
18+
"X-User-Agent": `AlexFlipnoteHomepage/${manifest.version}`
1819
}
20+
})
21+
22+
if (!response.ok) {
23+
console.error(`HTTP-Error: ${response.status}`, await response.text())
24+
return
1925
}
20-
request.send(null)
26+
27+
return await response.json()
2128
}

src/js/utils/openstreetmap.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,7 @@ export class WorldMap {
6666
* Has a rate limit of 1 request/second.
6767
* @param {number} lat - Latitude
6868
* @param {number} lon - Longitude
69-
* @param {function} callback - Callback function to handle the response
7069
*/
71-
export function reverseGeocode(lat, lon, callback) {
72-
http("GET", `https://nominatim.openstreetmap.org/reverse?lat=${lat}&lon=${lon}&format=json`, (r) => {
73-
callback(r)
74-
})
70+
export async function reverseGeocode(lat, lon) {
71+
return await http("GET", `https://nominatim.openstreetmap.org/reverse?lat=${lat}&lon=${lon}&format=json`)
7572
}

src/js/utils/weather.js

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,20 @@ export async function getWeather(items, position, lang) {
3232
}
3333
}
3434

35-
http("GET", `https://api.met.no/weatherapi/locationforecast/2.0/compact?lat=${pos.latitude}&lon=${pos.longitude}`, (r) => {
36-
const weatherData = new WeatherData(r.properties.timeseries[0].data, lang || "en")
35+
const weatherResponse = await http(
36+
"GET",
37+
`https://api.met.no/weatherapi/locationforecast/2.0/compact?lat=${pos.latitude}&lon=${pos.longitude}`
38+
)
3739

38-
document.getElementById("wicon").src = `images/weather/${weatherData.symbol_code}.png`
39-
document.getElementById("wdescription").innerText = weatherData.prettyName()
40-
if (items.temp_type === "fahrenheit") {
41-
wtemp.innerText = `${Math.round(weatherData.temprature * (9 / 5) + 32)} °F`
42-
} else {
43-
wtemp.innerText = `${Math.round(weatherData.temprature)} °C`
44-
}
45-
showWeatherContainer()
46-
})
40+
const weatherData = new WeatherData(weatherResponse.properties.timeseries[0].data, lang || "en")
41+
document.getElementById("wicon").src = `images/weather/${weatherData.symbol_code}.png`
42+
document.getElementById("wdescription").innerText = weatherData.prettyName()
43+
if (items.temp_type === "fahrenheit") {
44+
wtemp.innerText = `${Math.round(weatherData.temprature * (9 / 5) + 32)} °F`
45+
} else {
46+
wtemp.innerText = `${Math.round(weatherData.temprature)} °C`
47+
}
48+
showWeatherContainer()
4749

4850
// OpenStreetMap API can be rate limited, so we cache the location name for 1 hour
4951
const weatherLocation = await cache.get(cacheKey)
@@ -55,13 +57,15 @@ export async function getWeather(items, position, lang) {
5557
return
5658
}
5759

58-
reverseGeocode(pos.latitude, pos.longitude, (r) => {
59-
wname.innerText = (
60-
r.address.city || r.address.town ||
61-
r.address.village || r.address.hamlet ||
60+
const geoResponse = await reverseGeocode(pos.latitude, pos.longitude)
61+
62+
wname.innerText = (
63+
geoResponse.address.city || geoResponse.address.town ||
64+
geoResponse.address.village || geoResponse.address.hamlet ||
6265
"Unknown Location"
63-
)
64-
showWeatherContainer()
65-
cache.set(cacheKey, wname.innerText)
66-
})
66+
)
67+
showWeatherContainer()
68+
cache.set(cacheKey, wname.innerText)
69+
6770
}
71+

0 commit comments

Comments
 (0)