Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

835 B
Raw Blame
import { handleAndReturnErrorResponse } from "@/lib/api/errors";
import { ratelimitOrThrow } from "@/lib/api/utils";
import { getRandomKey } from "@/lib/planetscale";
import { domainKeySchema } from "@/lib/zod/schemas/links";
import { getSearchParams } from "@dub/utils";
import { NextRequest, NextResponse } from "next/server";

// GET /api/links/random – get a random available link key for a given domain
export const GET = async (req: NextRequest) => {
  try {
    const searchParams = getSearchParams(req.url);
    const { domain } = domainKeySchema
      .pick({ domain: true })
      .parse(searchParams);

    await ratelimitOrThrow(req, "links-random");

    const response = await getRandomKey({ domain });
    return NextResponse.json(response);
  } catch (error) {
    return handleAndReturnErrorResponse(error);
  }
};