Skip to content
← Public packages

@oerd/yr-weather

Ask MET Norway / yr.no for now-and-tomorrow weather at saved cities.

src/get-weather.ts

39 lines · 1.2 KB · TypeScript
import { forecastFor, type ResolvedFrom, type WeatherWhen } from './lib.ts'

/**
 * Look up now-or-tomorrow weather for a city via MET Norway locationforecast.
 * Use when someone asks the temperature or tomorrow's weather. Unknown places are geocoded once and stored in the package lookup map.
 *
 * @param input - Place name and optional when (`now` or `tomorrow`)
 * @returns Short summary, temperatures, and whether the place came from cache or a fresh geocode
 *
 * @example
 * import getWeather from 'kody:@oerd-at-gripable/yr-weather/getWeather'
 *
 * const now = await getWeather({ place: 'Tirana' })
 * const tomorrow = await getWeather({ place: 'Wurzburg', when: 'tomorrow' })
 */
export default async function getWeather(input: {
	place: string
	when?: WeatherWhen
}): Promise<{
	place: string
	when: WeatherWhen
	summary: string
	temperatureC?: number
	minC?: number
	maxC?: number
	symbol?: string
	precipitationMm?: number
	resolvedFrom: ResolvedFrom
	attribution: string
}> {
	if (!input?.place?.trim()) {
		throw new Error('place is required')
	}
	const when = input.when ?? 'now'
	if (when !== 'now' && when !== 'tomorrow') {
		throw new Error('when must be "now" or "tomorrow"')
	}
	return await forecastFor(input.place.trim(), when)
}