ObsiViewer/server/integrations/unsplash.routes.mjs
Bruno Charest ee3085ce38 feat: add Nimbus Editor with Unsplash integration
- Integrated Unsplash API for image search functionality with environment configuration
- Added new Nimbus Editor page component with navigation from sidebar and mobile drawer
- Enhanced TOC with highlight animation for editor heading navigation
- Improved CDK overlay z-index hierarchy for proper menu layering
- Removed obsolete logging validation script
2025-11-11 11:38:27 -05:00

46 lines
1.7 KiB
JavaScript

import express from 'express';
const router = express.Router();
// Simple proxy to Unsplash Search API.
// Requires UNSPLASH_ACCESS_KEY in environment; returns 501 if missing.
router.get('/search', async (req, res) => {
try {
const ACCESS_KEY = process.env.UNSPLASH_ACCESS_KEY;
if (!ACCESS_KEY) {
return res.status(501).json({ error: 'unsplash_disabled' });
}
const q = String(req.query.q || '').trim();
const perPage = Math.min(50, Math.max(1, Number(req.query.perPage || 24)));
if (!q) return res.json({ results: [] });
const url = new URL('https://api.unsplash.com/search/photos');
url.searchParams.set('query', q);
url.searchParams.set('per_page', String(perPage));
url.searchParams.set('client_id', ACCESS_KEY);
// Prefer landscape/small for editor usage
url.searchParams.set('orientation', 'landscape');
const upstream = await fetch(url.toString(), { headers: { 'Accept-Version': 'v1' } });
if (!upstream.ok) {
const text = await upstream.text().catch(() => '');
return res.status(502).json({ error: 'unsplash_upstream_error', status: upstream.status, message: text });
}
const json = await upstream.json();
// Map minimal fields used by the client
const results = Array.isArray(json?.results) ? json.results.map((r) => ({
id: r.id,
alt_description: r.alt_description || null,
urls: r.urls,
links: r.links,
user: r.user ? { name: r.user.name } : undefined,
})) : [];
return res.json({ results });
} catch (e) {
console.error('[Unsplash] proxy error', e);
return res.status(500).json({ error: 'internal_error' });
}
});
export default router;