import type { WithAbortSignal } from "#Source/abort/index.ts" import { generalFetch } from "#Source/request/index.ts" import type { Lang, Unit } from "../base.ts" const internalWeatherNowUnitSet = new Set(["m", "i"]) /** * @description 调用和风天气实时天气接口时使用的参数。 * * @example * ;``` * const options: WeatherNowOptions = { * baseUrl: "https://abcxyz.qweatherapi.com", * jwt: "eyJhbGciOiJFZERTQSJ9.payload.signature", * location: "101010100", * lang: "zh-hans", * unit: "m", * timeout: 5_000, * } * ``` */ export interface WeatherNowOptions extends WithAbortSignal { /** * @description 和风天气 API Base URL,例如 `https://abcxyz.qweatherapi.com`。 */ baseUrl: string /** * @description 用于 `Authorization: Bearer` 请求头的 JWT。 */ jwt: string /** * @description 需要查询地区的 LocationID,或以英文逗号分隔的经度、纬度坐标。 */ location: string /** * @description 返回结果的多语言代码。 */ lang?: Lang | undefined /** * @description 数据单位设置,`m` 为公制,`i` 为英制。 */ unit?: Unit | undefined /** * @description 请求超时时间,单位为毫秒。 */ timeout?: number | undefined } /** * @description 实时天气响应中的实况数据。 */ export interface WeatherNowResultNow { /** * @description 数据观测时间。 */ obsTime: string /** * @description 温度。 */ temp: string /** * @description 体感温度。 */ feelsLike: string /** * @description 天气图标代码。 */ icon: string /** * @description 天气状况文字描述。 */ text: string /** * @description 风向 360 角度。 */ wind360: string /** * @description 风向。 */ windDir: string /** * @description 风力等级。 */ windScale: string /** * @description 风速。 */ windSpeed: string /** * @description 相对湿度。 */ humidity: string /** * @description 过去 1 小时降水量。 */ precip: string /** * @description 大气压强。 */ pressure: string /** * @description 能见度。 */ vis: string /** * @description 云量,可能为空。 */ cloud: string /** * @description 露点温度,可能为空。 */ dew: string } /** * @description 和风天气实时天气接口的响应结果。 * * @example * ;``` * const result: WeatherNowResult = { * code: "200", * updateTime: "2020-06-30T22:00+08:00", * fxLink: "http://hfx.link/2ax1", * now: { * obsTime: "2020-06-30T21:40+08:00", * temp: "24", * feelsLike: "26", * icon: "101", * text: "多云", * wind360: "123", * windDir: "东南风", * windScale: "1", * windSpeed: "3", * humidity: "72", * precip: "0.0", * pressure: "1003", * vis: "16", * cloud: "10", * dew: "21", * }, * refer: { * sources: ["QWeather"], * license: ["QWeather Developers License"], * }, * } * ``` */ export interface WeatherNowResultRefer { /** * @description 原始数据来源或数据源说明。 */ sources?: string[] | undefined /** * @description 数据许可或版权声明。 */ license?: string[] | undefined } export interface WeatherNowResult { /** * @description 接口状态码。 */ code: string /** * @description 当前 API 的最近更新时间。 */ updateTime: string /** * @description 当前数据对应的响应式天气页面链接。 */ fxLink: string /** * @description 实时天气数据。 */ now: WeatherNowResultNow /** * @description 响应中的来源与许可信息。 */ refer?: WeatherNowResultRefer | undefined } const internalValidateWeatherNowOptions = ( options: WeatherNowOptions, ): { baseUrl: string jwt: string location: string } => { const baseUrl = options.baseUrl.trim() const jwt = options.jwt.trim() const location = options.location.trim() if (baseUrl === "") { throw new TypeError("WeatherNow baseUrl must not be empty.") } if (jwt === "") { throw new TypeError("WeatherNow jwt must not be empty.") } if (location === "") { throw new TypeError("WeatherNow location must not be empty.") } if (options.unit !== undefined && internalWeatherNowUnitSet.has(options.unit) === false) { throw new RangeError("WeatherNow unit must be one of: m, i.") } return { baseUrl, jwt, location, } } /** * @description 获取指定地区的实时天气。 * * @see {@link https://dev.qweather.com/docs/api/weather/weather-now/} */ export const weatherNow = async (options: WeatherNowOptions): Promise => { const { baseUrl, jwt, location } = internalValidateWeatherNowOptions(options) const { lang, unit, timeout, abortSignal } = options return await generalFetch< { query: { location: string lang?: Lang | undefined unit?: Unit | undefined } }, { type: "json" data: WeatherNowResult } >({ baseUrl, path: "/v7/weather/now", method: "get", headers: { Authorization: `Bearer ${jwt}`, }, query: { location, lang, unit, }, responseType: "json", timeout, abortSignal, }).getJson() }