2024-12-27 23:00:27 +00:00
|
|
|
import arcjet, { fixedWindow } from '@/lib/arcjet';
|
2024-02-18 00:21:19 +00:00
|
|
|
import { unsplash } from '@/lib/unsplash';
|
2024-12-27 23:00:27 +00:00
|
|
|
|
|
|
|
import { auth } from '@clerk/nextjs/server';
|
2024-02-18 00:21:19 +00:00
|
|
|
import { NextResponse } from 'next/server';
|
|
|
|
|
2024-12-27 23:00:27 +00:00
|
|
|
const aj = arcjet.withRule(
|
|
|
|
fixedWindow({
|
|
|
|
mode: 'LIVE',
|
|
|
|
max: 10,
|
|
|
|
window: '60s',
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
export async function GET(req: Request) {
|
2024-02-18 00:21:19 +00:00
|
|
|
try {
|
2024-12-27 23:00:27 +00:00
|
|
|
const { orgId, userId } = await auth();
|
|
|
|
if (!orgId || !userId)
|
|
|
|
return new NextResponse(JSON.stringify({ error: 'Unauthorized' }), {
|
|
|
|
status: 401,
|
|
|
|
});
|
|
|
|
|
|
|
|
const decision = await aj.protect(req);
|
|
|
|
if (decision.isDenied())
|
|
|
|
return new NextResponse(
|
|
|
|
JSON.stringify({ error: 'Too many requests', reason: decision.reason }),
|
|
|
|
{ status: 429 }
|
|
|
|
);
|
2024-02-18 00:21:19 +00:00
|
|
|
const result = await unsplash.photos.getRandom({
|
|
|
|
collectionIds: ['317099'],
|
|
|
|
count: 9,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (result?.response) {
|
|
|
|
const newImages = result.response as Array<Record<string, any>>;
|
2024-05-01 20:58:27 +00:00
|
|
|
const response = new NextResponse(JSON.stringify(newImages), {
|
|
|
|
status: 200,
|
|
|
|
});
|
|
|
|
return response;
|
2024-02-18 00:21:19 +00:00
|
|
|
} else {
|
|
|
|
return new NextResponse('Failed to get images', { status: 500 });
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
return new NextResponse(JSON.stringify(error), { status: 500 });
|
|
|
|
}
|
|
|
|
}
|