import type { WithAbortSignal } from "#Source/abort/index.ts" import { generalFetch } from "#Source/request/index.ts" import type { Lang, Unit } from "../base.ts" export type WeatherHourlyForecastHours = "24h" | "72h" | "168h" const internalWeatherHourlyForecastUnitSet = new Set(["m", "i"]) const internalWeatherHourlyForecastHoursSet = new Set([ "24h", "72h", "168h", ]) /** * @description 调用和风天气逐小时天气预报接口时使用的参数。 * * @example * ;``` * const options: WeatherHourlyForecastOptions = { * baseUrl: "https://abcxyz.qweatherapi.com", * jwt: "eyJhbGciOiJFZERTQSJ9.payload.signature", * hours: "24h", * location: "101010100", * lang: "zh-hans", * unit: "m", * } * ``` */ export interface WeatherHourlyForecastOptions extends WithAbortSignal { /** * @description 和风天气 API Base URL,例如 `https://abcxyz.qweatherapi.com`。 */ baseUrl: string /** * @description 用于 `Authorization: Bearer` 请求头的 JWT。 */ jwt: string /** * @description 预报小时数。 */ hours: WeatherHourlyForecastHours /** * @description 需要查询地区的 LocationID,或以英文逗号分隔的经度、纬度坐标。 */ location: string /** * @description 返回结果的多语言代码。 */ lang?: Lang | undefined /** * @description 数据单位设置,`m` 为公制,`i` 为英制。 */ unit?: Unit | undefined /** * @description 请求超时时间,单位为毫秒。 */ timeout?: number | undefined } /** * @description 逐小时天气预报响应中的单小时数据。 */ export interface WeatherHourlyForecastResultHourly { /** * @description 预报时间。 */ fxTime: string /** * @description 温度。 */ temp: string /** * @description 天气状况图标代码。 */ icon: string /** * @description 天气状况文字描述。 */ text: string /** * @description 风向 360 角度。 */ wind360: string /** * @description 风向。 */ windDir: string /** * @description 风力等级。 */ windScale: string /** * @description 风速,单位为公里/小时。 */ windSpeed: string /** * @description 相对湿度,百分比数值。 */ humidity: string /** * @description 降水概率,可能为空。 */ pop: string /** * @description 当前小时累计降水量。 */ precip: string /** * @description 大气压强。 */ pressure: string /** * @description 云量,可能为空。 */ cloud: string /** * @description 露点温度,可能为空。 */ dew: string } /** * @description 和风天气逐小时天气预报接口的响应结果。 * * @example * ;``` * const result: WeatherHourlyForecastResult = { * code: "200", * updateTime: "2021-02-16T13:35+08:00", * fxLink: "http://hfx.link/2ax1", * hourly: [ * { * fxTime: "2021-02-16T15:00+08:00", * temp: "2", * icon: "100", * text: "晴", * wind360: "335", * windDir: "西北风", * windScale: "3-4", * windSpeed: "20", * humidity: "11", * pop: "0", * precip: "0.0", * pressure: "1025", * cloud: "0", * dew: "-25", * }, * ], * refer: { * sources: ["QWeather"], * license: ["QWeather Developers License"], * }, * } * ``` */ export interface WeatherHourlyForecastResultRefer { /** * @description 原始数据来源或数据源说明。 */ sources?: string[] | undefined /** * @description 数据许可或版权声明。 */ license?: string[] | undefined } export interface WeatherHourlyForecastResult { /** * @description 接口状态码。 */ code: string /** * @description 当前 API 的最近更新时间。 */ updateTime: string /** * @description 当前数据对应的响应式天气页面链接。 */ fxLink: string /** * @description 逐小时天气预报数据列表。 */ hourly: WeatherHourlyForecastResultHourly[] /** * @description 响应中的来源与许可信息。 */ refer?: WeatherHourlyForecastResultRefer | undefined } const internalValidateWeatherHourlyForecastOptions = ( options: WeatherHourlyForecastOptions, ): { baseUrl: string jwt: string location: string hours: WeatherHourlyForecastHours } => { const baseUrl = options.baseUrl.trim() const jwt = options.jwt.trim() const location = options.location.trim() if (baseUrl === "") { throw new TypeError("WeatherHourlyForecast baseUrl must not be empty.") } if (jwt === "") { throw new TypeError("WeatherHourlyForecast jwt must not be empty.") } if (location === "") { throw new TypeError("WeatherHourlyForecast location must not be empty.") } if (internalWeatherHourlyForecastHoursSet.has(options.hours) === false) { throw new RangeError("WeatherHourlyForecast hours must be one of: 24h, 72h, 168h.") } if ( options.unit !== undefined && internalWeatherHourlyForecastUnitSet.has(options.unit) === false ) { throw new RangeError("WeatherHourlyForecast unit must be one of: m, i.") } return { baseUrl, jwt, location, hours: options.hours, } } /** * @description 获取指定地区的逐小时天气预报。 * * @see {@link https://dev.qweather.com/docs/api/weather/weather-hourly-forecast/} */ export const weatherHourlyForecast = async ( options: WeatherHourlyForecastOptions, ): Promise => { const { baseUrl, jwt, location, hours } = internalValidateWeatherHourlyForecastOptions(options) const { lang, unit, timeout, abortSignal } = options return await generalFetch< { query: { location: string lang?: Lang | undefined unit?: Unit | undefined } }, { type: "json" data: WeatherHourlyForecastResult } >({ baseUrl, path: `/v7/weather/${hours}`, method: "get", headers: { Authorization: `Bearer ${jwt}`, }, query: { location, lang, unit, }, responseType: "json", timeout, abortSignal, }).getJson() }