import type { WithAbortSignal } from "#Source/abort/index.ts" import { generalFetch } from "#Source/request/index.ts" const internalSunriseSunsetDatePattern = /^\d{8}$/ /** * @description 调用和风天气日出日落接口时使用的参数。 * * @example * ;``` * const options: SunriseSunsetOptions = { * baseUrl: "https://abcxyz.qweatherapi.com", * jwt: "eyJhbGciOiJFZERTQSJ9.payload.signature", * location: "101010100", * date: "20210220", * timeout: 5_000, * } * ``` */ export interface SunriseSunsetOptions 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 请求超时时间,单位为毫秒。 */ timeout?: number | undefined } /** * @description 日出日落响应中的来源与许可信息。 * * @example * ;``` * const refer: SunriseSunsetResultRefer = { * sources: ["QWeather"], * license: ["QWeather Developers License"], * } * ``` */ export interface SunriseSunsetResultRefer { /** * @description 原始数据来源或数据源说明。 */ sources?: string[] | undefined /** * @description 数据许可或版权声明。 */ license?: string[] | undefined } /** * @description 和风天气日出日落接口的响应结果。 * * @example * ;``` * const result: SunriseSunsetResult = { * code: "200", * updateTime: "2021-02-17T11:00+08:00", * fxLink: "http://hfx.link/2ax1", * sunrise: "2021-02-20T06:58+08:00", * sunset: "2021-02-20T17:57+08:00", * refer: { * sources: ["QWeather"], * license: ["QWeather Developers License"], * }, * } * ``` */ export interface SunriseSunsetResult { /** * @description 接口状态码。 */ code: string /** * @description 当前 API 的最近更新时间。 */ updateTime: string /** * @description 当前数据对应的响应式天气页面链接。 */ fxLink: string /** * @description 日出时间,在高纬度地区可能为空。 */ sunrise: string /** * @description 日落时间,在高纬度地区可能为空。 */ sunset: string /** * @description 响应中的来源与许可信息。 */ refer?: SunriseSunsetResultRefer | undefined } const internalValidateSunriseSunsetOptions = ( options: SunriseSunsetOptions, ): { 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("SunriseSunset baseUrl must not be empty.") } if (jwt === "") { throw new TypeError("SunriseSunset jwt must not be empty.") } if (location === "") { throw new TypeError("SunriseSunset location must not be empty.") } if (date === "") { throw new TypeError("SunriseSunset date must not be empty.") } if (internalSunriseSunsetDatePattern.test(date) === false) { throw new RangeError("SunriseSunset date must use yyyyMMdd format.") } return { baseUrl, jwt, location, date, } } /** * @description 获取指定日期和地点的日出日落时间。 * * @see {@link https://dev.qweather.com/docs/api/astronomy/sunrise-sunset/} */ export const sunriseSunset = async ( options: SunriseSunsetOptions, ): Promise => { const { baseUrl, jwt, location, date } = internalValidateSunriseSunsetOptions(options) const { timeout, abortSignal } = options return await generalFetch< { query: { location: string date: string } }, { type: "json" data: SunriseSunsetResult } >({ baseUrl, path: "/v7/astronomy/sun", method: "get", headers: { Authorization: `Bearer ${jwt}`, }, query: { location, date, }, responseType: "json", timeout, abortSignal, }).getJson() }