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.

Merge branch 'main' into feat/improve-api

Kiran K102e22d5038c
diff --cc apps/web/app/api/links/route.ts
index 8c1603abde,b640a8cc31..d07a3fcc8c
--- a/apps/web/app/api/links/route.ts
+++ b/apps/web/app/api/links/route.ts
@@@ -51,56 -37,53 +51,56 @@@ export const GET = withAuth(async ({ he
// POST /api/links – create a new link
export const POST = withAuth(
async ({ req, headers, session, project }) => {
- let body;
try {
- body = await req.json();
- } catch (error) {
- return new Response("Missing or invalid body.", { status: 400, headers });
- }
+ const body = createLinkBodySchema.parse(await req.json());
- if (!session) {
- const ip = req.headers.get("x-forwarded-for") || LOCALHOST_IP;
- const { success } = await ratelimit(10, "1 d").limit(ip);
+ if (!session) {
+ const ip = req.headers.get("x-forwarded-for") || LOCALHOST_IP;
+ const { success } = await ratelimit(10, "1 d").limit(ip);
- if (!success) {
- return new Response(
- "Rate limited – you can only create up to 10 links per day without an account.",
- { status: 429 },
- );
+ if (!success) {
+ throw new DubApiError({
+ code: "rate_limit_exceeded",
+ message:
+ "Rate limited – you can only create up to 10 links per day without an account.",
+ });
+ }
}
- }
- const { link, error, status } = await processLink({
- payload: body,
- project,
- ...(session && { userId: session.user.id }),
- });
+ const { link, error, code } = await processLink({
+ payload: body as LinkWithTagIdsProps,
+ project,
- userId: session.user.id,
++ ...(session && { userId: session.user.id }),
+ });
- if (error) {
- return new Response(error, { status, headers });
- }
+ if (error) {
+ throw new DubApiError({
+ code: code as ErrorCodes,
+ message: error,
+ });
+ }
- const response = await addLink(link);
+ const response = await addLink(link);
- if (response === null) {
- return new Response("Duplicate key: This short link already exists.", {
- status: 409,
- headers,
- });
- }
+ if (response === null) {
+ throw new DubApiError({
+ code: "conflict",
+ message: "Duplicate key: This short link already exists.",
+ });
+ }
- await qstash.publishJSON({
- url: `${APP_DOMAIN_WITH_NGROK}/api/cron/links/event`,
- body: {
- linkId: response.id,
- type: "create",
- },
- });
+ await qstash.publishJSON({
+ url: `${APP_DOMAIN_WITH_NGROK}/api/cron/links/event`,
+ body: {
+ linkId: response.id,
+ type: "create",
+ },
+ });
- return NextResponse.json(response, { headers });
+ return NextResponse.json(response, { headers });
+ } catch (error) {
+ return handleAndReturnErrorResponse(error);
+ }
},
{
needNotExceededLinks: true,
diff --cc apps/web/app/api/projects/route.ts
index dc13b77a71,b97e9fe609..2087591cd8
--- a/apps/web/app/api/projects/route.ts
+++ b/apps/web/app/api/projects/route.ts
@@@ -17,25 -15,6 +17,25 @@@ import
} from "@dub/utils";
import { NextResponse } from "next/server";
+const createProjectSchema = z.object({
+ name: z.string().min(1),
+ slug: z
+ .string()
- .min(1)
++ .min(3, "Slug must be at least 3 characters")
+ .max(48, "Slug must be less than 48 characters")
+ .transform((v) => v.toLowerCase())
+ .refine((v) => validSlugRegex.test(v), { message: "Invalid slug format" })
+ .refine(async (v) => !((await isReservedKey(v)) || DEFAULT_REDIRECTS[v]), {
+ message: "Cannot use reserved slugs",
+ }),
+ domain: z
+ .string()
+ .refine((v) => validDomainRegex.test(v), {
+ message: "Invalid domain format",
+ })
+ .optional(),
+});
+
// GET /api/projects - get all projects for the current user
export const GET = withSession(async ({ session }) => {
const projects = await prisma.project.findMany({