import type { WithAbortSignal } from "#Source/abort/index.ts" import { generalFetch } from "#Source/request/index.ts" import type { Lang } from "../base.ts" export type MinutelyPrecipitationType = "rain" | "snow" /** * @description 调用和风天气分钟级降水接口时使用的参数。 * * @example * ;``` * const options: MinutelyPrecipitationOptions = { * baseUrl: "https://abcxyz.qweatherapi.com", * jwt: "eyJhbGciOiJFZERTQSJ9.payload.signature", * location: "116.38,39.91", * lang: "zh-hans", * timeout: 5_000, * } * ``` */ export interface MinutelyPrecipitationOptions extends WithAbortSignal { /** * @description 和风天气 API Base URL,例如 `https://abcxyz.qweatherapi.com`。 */ baseUrl: string /** * @description 用于 `Authorization: Bearer` 请求头的 JWT。 */ jwt: string /** * @description 需要查询地区的经度、纬度坐标,例如 `116.38,39.91`。 */ location: string /** * @description 返回结果的多语言代码。 */ lang?: Lang | undefined /** * @description 请求超时时间,单位为毫秒。 */ timeout?: number | undefined } /** * @description 分钟级降水响应中的单个 5 分钟预报数据。 * * @example * ;``` * const minutely: MinutelyPrecipitationResultMinutely = { * fxTime: "2021-12-16T18:55+08:00", * precip: "0.15", * type: "rain", * } * ``` */ export interface MinutelyPrecipitationResultMinutely { /** * @description 预报时间。 */ fxTime: string /** * @description 5 分钟累计降水量,单位为毫米。 */ precip: string /** * @description 降水类型,`rain` 表示雨,`snow` 表示雪。 */ type: MinutelyPrecipitationType } /** * @description 分钟级降水响应中的来源与许可信息。 * * @example * ;``` * const refer: MinutelyPrecipitationResultRefer = { * sources: ["QWeather"], * license: ["QWeather Developers License"], * } * ``` */ export interface MinutelyPrecipitationResultRefer { /** * @description 原始数据来源或数据源说明。 */ sources?: string[] | undefined /** * @description 数据许可或版权声明。 */ license?: string[] | undefined } /** * @description 和风天气分钟级降水接口的响应结果。 * * @example * ;``` * const result: MinutelyPrecipitationResult = { * code: "200", * updateTime: "2021-12-16T18:55+08:00", * fxLink: "https://www.qweather.com", * summary: "95分钟后雨就停了", * minutely: [ * { * fxTime: "2021-12-16T18:55+08:00", * precip: "0.15", * type: "rain", * }, * ], * refer: { * sources: ["QWeather"], * license: ["QWeather Developers License"], * }, * } * ``` */ export interface MinutelyPrecipitationResult { /** * @description 接口状态码。 */ code: string /** * @description 当前 API 的最近更新时间。 */ updateTime: string /** * @description 当前数据对应的响应式天气页面链接。 */ fxLink: string /** * @description 分钟级降水文字描述。 */ summary: string /** * @description 未来 2 小时内每 5 分钟的降水预报数据列表。 */ minutely: MinutelyPrecipitationResultMinutely[] /** * @description 响应中的来源与许可信息。 */ refer?: MinutelyPrecipitationResultRefer | undefined } const internalValidateMinutelyPrecipitationOptions = ( options: MinutelyPrecipitationOptions, ): { 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("MinutelyPrecipitation baseUrl must not be empty.") } if (jwt === "") { throw new TypeError("MinutelyPrecipitation jwt must not be empty.") } if (location === "") { throw new TypeError("MinutelyPrecipitation location must not be empty.") } return { baseUrl, jwt, location, } } /** * @description 获取指定坐标的分钟级降水预报。 * * @see {@link https://dev.qweather.com/docs/api/minutely/minutely-precipitation/} */ export const minutelyPrecipitation = async ( options: MinutelyPrecipitationOptions, ): Promise => { const { baseUrl, jwt, location } = internalValidateMinutelyPrecipitationOptions(options) const { lang, timeout, abortSignal } = options return await generalFetch< { query: { location: string lang?: Lang | undefined } }, { type: "json" data: MinutelyPrecipitationResult } >({ baseUrl, path: "/v7/minutely/5m", method: "get", headers: { Authorization: `Bearer ${jwt}`, }, query: { location, lang, }, responseType: "json", timeout, abortSignal, }).getJson() }