The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
account for Unsplash rate limits
Steven Tey82e2190411d7
diff --git a/app/api/unsplash/search/route.ts b/app/api/unsplash/search/route.tsindex 104a07fd99..05d59e7867 100644--- a/app/api/unsplash/search/route.ts+++ b/app/api/unsplash/search/route.ts@@ -13,22 +13,27 @@ export async function GET(req: Request) {if (!query) return new Response("Missing query", { status: 400 });const ip = ipAddress(req) || LOCALHOST_IP;- const { success } = await ratelimit(10, "10 s").limit(ip);+ const { success } = await ratelimit(5, "10 s").limit(ip);if (!success) {return new Response("Don't DDoS me pls 🥺", { status: 429 });}- const response = await unsplash.search.getPhotos({- query,- });-- if (response.errors) {- return new Response(JSON.stringify(response.errors), {- status: 500,+ return unsplash.search+ .getPhotos({+ query,+ })+ .then((result) => {+ if (result.errors) {+ // handle error here+ console.log("error occurred: ", result.errors[0]);+ return new Response("Unsplash error", { status: 400 });+ } else {+ const data = result.response;+ return NextResponse.json(data.results);+ }+ })+ .catch((err) => {+ console.log("err", err);+ return new Response("Unsplash rate limit exceeded", { status: 429 });});- }-- const data = response.response?.results;-- return NextResponse.json(data);}diff --git a/components/app/modals/add-edit-link-modal/unsplash-search.tsx b/components/app/modals/add-edit-link-modal/unsplash-search.tsxindex 062829c46f..d5dfa9b63a 100644--- a/components/app/modals/add-edit-link-modal/unsplash-search.tsx+++ b/components/app/modals/add-edit-link-modal/unsplash-search.tsx@@ -43,50 +43,58 @@ export default function UnsplashSearch({/></div>{data ? (- <div className="mt-3 grid grid-cols-2 gap-3">- {data.map((photo) => (- <button- key={photo.id}- type="button"- onClick={() => {- setData((prev) => ({- ...prev,- image: photo.urls.regular,- }));- setOpenPopover(false);- fetch("/api/unsplash/download", {- method: "POST",- headers: {- "Content-Type": "application/json",- },- body: JSON.stringify({- url: photo.links.download_location,- }),- });- }}- className="relative flex h-24 w-full items-center justify-center overflow-hidden rounded-md bg-gray-100 transition-all hover:brightness-75"- >- <img- src={photo.urls.small}- alt={photo.alt_description || "Unsplash image"}- className="absolute h-full w-full object-cover"- />- <p className="absolute bottom-0 left-0 right-0 line-clamp-1 w-full bg-black bg-opacity-10 p-1 text-xs text-white">- by{" "}- <a- className="underline underline-offset-2"- target="_blank"- rel="noopener noreferrer"- href={`${photo.user.links.html}?utm_source=dub.sh&utm_medium=referral`}- >- {photo.user.name}- </a>- </p>- </button>- ))}- </div>+ data.length > 0 ? (+ <div className="mt-3 grid grid-cols-2 gap-3">+ {data.map((photo) => (+ <button+ key={photo.id}+ type="button"+ onClick={() => {+ setData((prev) => ({+ ...prev,+ image: photo.urls.regular,+ }));+ setOpenPopover(false);+ fetch("/api/unsplash/download", {+ method: "POST",+ headers: {+ "Content-Type": "application/json",+ },+ body: JSON.stringify({+ url: photo.links.download_location,+ }),+ });+ }}+ className="relative flex h-24 w-full items-center justify-center overflow-hidden rounded-md bg-gray-100 transition-all hover:brightness-75"+ >+ <img+ src={photo.urls.small}+ alt={photo.alt_description || "Unsplash image"}+ className="absolute h-full w-full object-cover"+ />+ <p className="absolute bottom-0 left-0 right-0 line-clamp-1 w-full bg-black bg-opacity-10 p-1 text-xs text-white">+ by{" "}+ <a+ className="underline underline-offset-2"+ target="_blank"+ rel="noopener noreferrer"+ href={`${photo.user.links.html}?utm_source=dub.sh&utm_medium=referral`}+ >+ {photo.user.name}+ </a>+ </p>+ </button>+ ))}+ </div>+ ) : (+ <div className="flex h-[90%] items-center justify-center">+ <p className="text-center text-sm text-gray-500">+ No results found. <br /> Maybe try tweaking your search query?+ </p>+ </div>+ )) : (- <div className="flex h-full items-center justify-center">+ <div className="flex h-[90%] items-center justify-center"><LoadingSpinner /></div>)}