import type { WithAbortSignal } from "#Source/abort/index.ts" import { generalFetch } from "#Source/request/index.ts" import type { Lang } from "../base.ts" const internalMoonAndMoonPhaseDatePattern = /^\d{8}$/ /** * @description 调用和风天气月升月落和月相接口时使用的参数。 * * @example * ;``` * const options: MoonAndMoonPhaseOptions = { * baseUrl: "https://abcxyz.qweatherapi.com", * jwt: "eyJhbGciOiJFZERTQSJ9.payload.signature", * location: "101010100", * date: "20211120", * lang: "zh-hans", * timeout: 5_000, * } * ``` */ export interface MoonAndMoonPhaseOptions extends WithAbortSignal { /** * @description 和风天气 API Base URL,例如 `https://abcxyz.qweatherapi.com`。 */ baseUrl: string /** * @description 用于 `Authorization: Bearer` 请求头的 JWT。 */ jwt: string /** * @description 需要查询地区的 LocationID,或以英文逗号分隔的经度、纬度坐标。 */ location: string /** * @description 需要查询的日期,格式为 `yyyyMMdd`。 */ date: string /** * @description 返回结果的多语言代码。 */ lang?: Lang | undefined /** * @description 请求超时时间,单位为毫秒。 */ timeout?: number | undefined } /** * @description 月升月落和月相响应中的单条逐小时月相数据。 * * @example * ;``` * const moonPhase: MoonAndMoonPhaseResultMoonPhase = { * fxTime: "2021-11-20T00:00+08:00", * value: "0.51", * name: "亏凸月", * illumination: "100", * icon: "805", * } * ``` */ export interface MoonAndMoonPhaseResultMoonPhase { /** * @description 月相逐小时预报时间。 */ fxTime: string /** * @description 月相数值。 */ value: string /** * @description 月相名称。 */ name: string /** * @description 月亮照明度,百分比数值。 */ illumination: string /** * @description 月相图标代码。 */ icon: string } /** * @description 月升月落和月相响应中的来源与许可信息。 * * @example * ;``` * const refer: MoonAndMoonPhaseResultRefer = { * sources: ["QWeather"], * license: ["QWeather Developers License"], * } * ``` */ export interface MoonAndMoonPhaseResultRefer { /** * @description 原始数据来源或数据源说明。 */ sources?: string[] | undefined /** * @description 数据许可或版权声明。 */ license?: string[] | undefined } /** * @description 和风天气月升月落和月相接口的响应结果。 * * @example * ;``` * const result: MoonAndMoonPhaseResult = { * code: "200", * updateTime: "2021-11-15T17:00+08:00", * fxLink: "http://hfx.link/2ax1", * moonrise: "2021-11-20T17:25+08:00", * moonset: "2021-11-21T07:42+08:00", * moonPhase: [ * { * fxTime: "2021-11-20T00:00+08:00", * value: "0.51", * name: "亏凸月", * illumination: "100", * icon: "805", * }, * ], * refer: { * sources: ["QWeather"], * license: ["QWeather Developers License"], * }, * } * ``` */ export interface MoonAndMoonPhaseResult { /** * @description 接口状态码。 */ code: string /** * @description 当前 API 的最近更新时间。 */ updateTime: string /** * @description 当前数据对应的响应式天气页面链接。 */ fxLink: string /** * @description 当天月升时间,可能为空。 */ moonrise: string /** * @description 当天月落时间,可能为空。 */ moonset: string /** * @description 当天逐小时月相数据。 */ moonPhase: MoonAndMoonPhaseResultMoonPhase[] /** * @description 响应中的来源与许可信息。 */ refer?: MoonAndMoonPhaseResultRefer | undefined } const internalValidateMoonAndMoonPhaseOptions = ( options: MoonAndMoonPhaseOptions, ): { baseUrl: string jwt: string location: string date: string } => { const baseUrl = options.baseUrl.trim() const jwt = options.jwt.trim() const location = options.location.trim() const date = options.date.trim() if (baseUrl === "") { throw new TypeError("MoonAndMoonPhase baseUrl must not be empty.") } if (jwt === "") { throw new TypeError("MoonAndMoonPhase jwt must not be empty.") } if (location === "") { throw new TypeError("MoonAndMoonPhase location must not be empty.") } if (date === "") { throw new TypeError("MoonAndMoonPhase date must not be empty.") } if (internalMoonAndMoonPhaseDatePattern.test(date) === false) { throw new RangeError("MoonAndMoonPhase date must use yyyyMMdd format.") } return { baseUrl, jwt, location, date, } } /** * @description 获取指定日期和地点的月升月落时间及逐小时月相数据。 * * @see {@link https://dev.qweather.com/docs/api/astronomy/moon-and-moon-phase/} */ export const moonAndMoonPhase = async ( options: MoonAndMoonPhaseOptions, ): Promise => { const { baseUrl, jwt, location, date } = internalValidateMoonAndMoonPhaseOptions(options) const { lang, timeout, abortSignal } = options return await generalFetch< { query: { location: string date: string lang?: Lang | undefined } }, { type: "json" data: MoonAndMoonPhaseResult } >({ baseUrl, path: "/v7/astronomy/moon", method: "get", headers: { Authorization: `Bearer ${jwt}`, }, query: { location, date, lang, }, responseType: "json", timeout, abortSignal, }).getJson() }