-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d236841
commit 2b027f5
Showing
3 changed files
with
59 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { env } from '$env/dynamic/private'; | ||
import type { Root } from './types/geographies'; | ||
import type { NominatimResponse } from './types/nominatim'; | ||
import type { WeatherResponse } from './types/weather'; | ||
|
||
export const getWeather = async ({ lat, lon }: { lat: string | number; lon: string | number }) => { | ||
const url = `${env.API_URL}/forecast/${env.API_KEY}/${lat},${lon}?exclude=minutely`; | ||
const geoUrl = `${env.GEO_URL}&x=${lon}&y=${lat}`; | ||
|
||
try { | ||
const [weather, location]: [WeatherResponse, Root] = await Promise.all([ | ||
fetch(url).then((res) => res.json()), | ||
fetch(geoUrl).then((res) => res.json()) | ||
]); | ||
|
||
return { | ||
coords: { lat: Number(lat), lon: Number(lon) }, | ||
weather, | ||
location: location.result.geographies['County Subdivisions'][0].BASENAME | ||
}; | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
}; | ||
|
||
export const getZipcodeByCoords = async ({ | ||
lat, | ||
lon, | ||
fetch | ||
}: { | ||
lat: string | number; | ||
lon: string | number; | ||
fetch: typeof window.fetch; | ||
}) => { | ||
try { | ||
const geoUrl = `${env.NOMINATIM_API_URL}&lon=${lon}&lat=${lat}`; | ||
|
||
const locationResponse = await fetch(geoUrl, { | ||
headers: { | ||
'User-Agent': 'weather-app', | ||
age: '31536000', | ||
'cache-control': 'max-age=31536000' | ||
} | ||
}); | ||
|
||
if (!locationResponse.ok) { | ||
throw new Error(locationResponse.statusText); | ||
} | ||
|
||
const location: NominatimResponse = await locationResponse.json(); | ||
|
||
return location.address.postcode; | ||
} catch (error) { | ||
console.error(`Failed to get zipcode for coordinates ${lat}, ${lon}: ${error}`); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
import { sveltekit } from '@sveltejs/kit/vite'; | ||
import { purgeCss } from 'vite-plugin-tailwind-purgecss'; | ||
import { defineConfig } from 'vitest/config'; | ||
|
||
export default defineConfig({ | ||
export default { | ||
plugins: [sveltekit(), purgeCss()] | ||
}); | ||
}; |