added api
This commit is contained in:
parent
38a349055e
commit
dea30e8e26
5 changed files with 91 additions and 0 deletions
19
weather_app/app/api/dailyForecast/route.ts
Normal file
19
weather_app/app/api/dailyForecast/route.ts
Normal file
|
@ -0,0 +1,19 @@
|
|||
import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
export async function GET(req: NextRequest){
|
||||
try {
|
||||
const searchParams = req.nextUrl.searchParams
|
||||
const lat = searchParams.get("lat");
|
||||
const lon = searchParams.get("lon");
|
||||
const apiKey = process.env.OPENWEATHERMAP_API_KEY;
|
||||
const url = `http://api.openweathermap.org/data/2.5/forecast?lat=${lat}&lon=${lon}&appid=${apiKey}`
|
||||
const res = await fetch(url, {
|
||||
next: {revalidate: 3600},
|
||||
})
|
||||
const data = await res.json();
|
||||
return NextResponse.json(data)
|
||||
|
||||
} catch (error) {
|
||||
return new Response('Error', {status: 500});
|
||||
}
|
||||
}
|
17
weather_app/app/api/geoCoordinate/route.ts
Normal file
17
weather_app/app/api/geoCoordinate/route.ts
Normal file
|
@ -0,0 +1,17 @@
|
|||
import axios from "axios";
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
export async function GET(req: NextRequest){
|
||||
try {
|
||||
const apiKey = process.env.OPENWEATHERMAP_API_KEY;
|
||||
const searchParams = req.nextUrl.searchParams;
|
||||
const city = searchParams.get('search');
|
||||
const url = `http://api.openweathermap.org/geo/1.0/direct?q=${city}&limit=5&appid=${apiKey}`
|
||||
const res = await axios.get(url);
|
||||
return NextResponse.json(res.data)
|
||||
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
return new Response('Error', {status: 500});
|
||||
}
|
||||
}
|
17
weather_app/app/api/pollution/route.ts
Normal file
17
weather_app/app/api/pollution/route.ts
Normal file
|
@ -0,0 +1,17 @@
|
|||
import axios from "axios";
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
try {
|
||||
const searchParams = req.nextUrl.searchParams
|
||||
const lat = searchParams.get("lat");
|
||||
const lon = searchParams.get("lon");
|
||||
const apiKey = process.env.OPENWEATHERMAP_API_KEY;
|
||||
const url = `http://api.openweathermap.org/data/2.5/air_pollution?lat=${lat}&lon=${lon}&appid=${apiKey}`
|
||||
const res = await axios.get(url)
|
||||
return NextResponse.json(res.data);
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
return new Response('Error', {status: 500});
|
||||
}
|
||||
}
|
19
weather_app/app/api/uvIndex/route.ts
Normal file
19
weather_app/app/api/uvIndex/route.ts
Normal file
|
@ -0,0 +1,19 @@
|
|||
import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
try {
|
||||
const searchParams = req.nextUrl.searchParams
|
||||
const lat = searchParams.get("lat");
|
||||
const lon = searchParams.get("lon");
|
||||
const url = `https://api.open-meteo.com/v1/forecast?latitude=${lat}&longitude=${lon}&daily=uv_index_max,uv_index_clear_sky_max&timezone=auto&forecast_days=1`
|
||||
const res = await fetch(url, {
|
||||
next: {revalidate: 900},
|
||||
});
|
||||
const UVData = await res.json()
|
||||
return NextResponse.json(UVData)
|
||||
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
return new Response('Error', {status: 500});
|
||||
}
|
||||
}
|
19
weather_app/app/api/weather/route.ts
Normal file
19
weather_app/app/api/weather/route.ts
Normal file
|
@ -0,0 +1,19 @@
|
|||
import axios from "axios";
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
export async function GET(req: NextRequest){
|
||||
try {
|
||||
const searchParams = req.nextUrl.searchParams;
|
||||
const lat = searchParams.get("lat");
|
||||
const lon = searchParams.get("lon");
|
||||
const apiKey = process.env.OPENWEATHERMAP_API_KEY;
|
||||
const url = `https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${apiKey}`
|
||||
const res = await axios.get(url);
|
||||
return NextResponse.json(res.data);
|
||||
|
||||
|
||||
} catch (error){
|
||||
console.log(error)
|
||||
return new Response('Error', {status: 500});
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue