56 lines
1.5 KiB
TypeScript
56 lines
1.5 KiB
TypeScript
const weatherHost = 'n65khufe5n.re.qweatherapi.com';
|
|
const token = 'fdad5aeb2ba54949a8a1df2a0f3d1efb'
|
|
const xihu = '101210113'; // 西湖
|
|
export const getWeather = async (location: string = xihu) => {
|
|
const url = `https://${weatherHost}/v7/weather/3d?location=${location}`;
|
|
const headers = {
|
|
'Authorization': `Bearer ${token}`
|
|
};
|
|
const res = await fetch(url, { headers });
|
|
if (!res.ok) {
|
|
throw new Error(`HTTP error! status: ${res.status}`);
|
|
}
|
|
const data = await res.json();
|
|
return data;
|
|
}
|
|
// getWeather().then(console.log).catch(console.error);
|
|
|
|
// https://dev.qweather.com/
|
|
class Weather {
|
|
host: string;
|
|
token: string;
|
|
constructor(opts: { host: string; token: string }) {
|
|
this.host = opts.host;
|
|
this.token = opts.token;
|
|
console.log(this.host, this.token);
|
|
}
|
|
getWeather(location: string) {
|
|
return fetch(`https://${this.host}/v7/weather/now?location=${location}`, {
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'X-QW-Api-Key': '<KEY>'.replace('<KEY>', this.token),
|
|
},
|
|
}).then((res) => res.json());
|
|
}
|
|
}
|
|
const newWeather = new Weather({
|
|
host: process.env?.QWEATHER_HOST || weatherHost,
|
|
token: process.env?.QWEATHER_TOKEN || token,
|
|
});
|
|
|
|
|
|
// newWeather.getWeather(xihu).then(console.log).catch(console.error);
|
|
|
|
|
|
import { QueryRouterServer as Mini } from "@kevisual/router";
|
|
|
|
const app = new Mini();
|
|
|
|
app.route({
|
|
path: 'main'
|
|
}).define(async (ctx) => {
|
|
ctx.body = await newWeather.getWeather(xihu);
|
|
}).addTo(app)
|
|
|
|
|
|
app.wait() |