import type { WithAbortSignal } from "#Source/abort/index.ts" import { generalFetch } from "#Source/request/index.ts" import type { Lang, Unit } from "../base.ts" export type GridWeatherDailyForecastDays = "3d" | "7d" const internalGridWeatherDailyForecastUnitSet = new Set(["m", "i"]) const internalGridWeatherDailyForecastDaysSet = new Set(["3d", "7d"]) /** * @description 调用和风天气格点每日天气预报接口时使用的参数。 * * @example * ;``` * const options: GridWeatherDailyForecastOptions = { * baseUrl: "https://abcxyz.qweatherapi.com", * jwt: "eyJhbGciOiJFZERTQSJ9.payload.signature", * days: "3d", * location: "116.41,39.92", * unit: "m", * } * ``` */ export interface GridWeatherDailyForecastOptions extends WithAbortSignal { /** * @description 和风天气 API Base URL,例如 `https://abcxyz.qweatherapi.com`。 */ baseUrl: string /** * @description 用于 `Authorization: Bearer` 请求头的 JWT。 */ jwt: string days: GridWeatherDailyForecastDays /** * @description 需要查询地区的经度、纬度坐标,例如 `116.41,39.92`。 */ location: string /** * @description 返回结果的多语言代码。 */ lang?: Lang | undefined /** * @description 数据单位设置,`m` 为公制,`i` 为英制。 */ unit?: Unit | undefined /** * @description 请求超时时间,单位为毫秒。 */ timeout?: number | undefined } /** * @description 格点每日天气预报响应中的单日数据。 */ export interface GridWeatherDailyForecastResultDaily { /** * @description 预报日期。 */ fxDate: string /** * @description 预报当天最高温度。 */ tempMax: string /** * @description 预报当天最低温度。 */ tempMin: string /** * @description 白天天气状况的图标代码。 */ iconDay: string /** * @description 夜间天气状况的图标代码。 */ iconNight: string /** * @description 白天天气状况文字描述。 */ textDay: string /** * @description 夜间天气状况文字描述。 */ textNight: string /** * @description 白天风向 360 角度。 */ wind360Day: string /** * @description 白天风向。 */ windDirDay: string /** * @description 白天风力等级。 */ windScaleDay: string /** * @description 白天风速,单位为公里/小时。 */ windSpeedDay: string /** * @description 夜间风向 360 角度。 */ wind360Night: string /** * @description 夜间风向。 */ windDirNight: string /** * @description 夜间风力等级。 */ windScaleNight: string /** * @description 夜间风速,单位为公里/小时。 */ windSpeedNight: string /** * @description 相对湿度,百分比数值。 */ humidity: string /** * @description 预报当天总降水量。 */ precip: string /** * @description 大气压强。 */ pressure: string } /** * @description 和风天气格点每日天气预报接口的响应结果。 */ export interface GridWeatherDailyForecastResultRefer { /** * @description 原始数据来源或数据源说明。 */ sources?: string[] | undefined /** * @description 数据许可或版权声明。 */ license?: string[] | undefined } export interface GridWeatherDailyForecastResult { /** * @description 接口状态码。 */ code: string /** * @description 当前 API 的最近更新时间。 */ updateTime: string /** * @description 当前数据对应的响应式天气页面链接。 */ fxLink: string daily: GridWeatherDailyForecastResultDaily[] /** * @description 响应中的来源与许可信息。 */ refer?: GridWeatherDailyForecastResultRefer | undefined } const internalValidateGridWeatherDailyForecastOptions = ( options: GridWeatherDailyForecastOptions, ): { baseUrl: string jwt: string location: string days: GridWeatherDailyForecastDays } => { const baseUrl = options.baseUrl.trim() const jwt = options.jwt.trim() const location = options.location.trim() if (baseUrl === "") { throw new TypeError("GridWeatherDailyForecast baseUrl must not be empty.") } if (jwt === "") { throw new TypeError("GridWeatherDailyForecast jwt must not be empty.") } if (location === "") { throw new TypeError("GridWeatherDailyForecast location must not be empty.") } if (internalGridWeatherDailyForecastDaysSet.has(options.days) === false) { throw new RangeError("GridWeatherDailyForecast days must be one of: 3d, 7d.") } if ( options.unit !== undefined && internalGridWeatherDailyForecastUnitSet.has(options.unit) === false ) { throw new RangeError("GridWeatherDailyForecast unit must be one of: m, i.") } return { baseUrl, jwt, location, days: options.days, } } /** * @description 获取指定坐标的格点每日天气预报。 * * @see {@link https://dev.qweather.com/docs/api/weather/grid-weather-daily-forecast/} */ export const gridWeatherDailyForecast = async ( options: GridWeatherDailyForecastOptions, ): Promise => { const { baseUrl, jwt, location, days } = internalValidateGridWeatherDailyForecastOptions(options) const { lang, unit, timeout, abortSignal } = options return await generalFetch< { query: { location: string lang?: Lang | undefined unit?: Unit | undefined } }, { type: "json" data: GridWeatherDailyForecastResult } >({ baseUrl, path: `/v7/grid-weather/${days}`, method: "get", headers: { Authorization: `Bearer ${jwt}`, }, query: { location, lang, unit, }, responseType: "json", timeout, abortSignal, }).getJson() }