Files
classroomio/apps/dashboard/src/routes/api/unsplash/+server.ts
T

37 lines
943 B
TypeScript
Raw Normal View History

2023-10-05 09:22:39 +00:00
import { json } from '@sveltejs/kit';
2024-09-20 11:16:19 +01:00
import { env } from '$env/dynamic/private';
2023-10-05 09:22:39 +00:00
2024-09-20 11:16:19 +01:00
const UNSPLASH_API_URL = `https://api.unsplash.com/search/photos?page=2&per_page=15&auto=format&fit=crop&w=2970&q=80&client_id=${env.UNSPLASH_API_KEY}`;
2023-10-05 09:22:39 +00:00
// API to fetch photos from Unsplash
export async function POST({ request }) {
const body = await request.json();
const { searchQuery } = body;
const query = searchQuery || 'rocks';
try {
const response = await fetch(`${UNSPLASH_API_URL}&query=${encodeURIComponent(query)}`, {
2023-11-25 21:56:02 +00:00
method: 'GET'
2023-10-05 09:22:39 +00:00
});
if (!response.ok) {
throw new Error('Unable to fetch photos from Unsplash API');
}
const data = await response.json();
return json({
success: true,
2023-11-25 21:56:02 +00:00
photos: data.results
2023-10-05 09:22:39 +00:00
});
} catch (error) {
console.error(error);
2023-11-25 21:56:02 +00:00
return json(
{ success: false, message: 'Error fetching photos from Unsplash' },
{ status: 500 }
);
2023-10-05 09:22:39 +00:00
}
}