The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
make `domain` optional in POST endpoint
Steven Teye3e71516dee8
diff --git a/apps/web/app/api/links/count/route.ts b/apps/web/app/api/links/count/route.tsindex 36667a5434..4c0b2de47a 100644--- a/apps/web/app/api/links/count/route.ts+++ b/apps/web/app/api/links/count/route.ts@@ -1,19 +1,16 @@import { getLinksCount } from "@/lib/api/links";import { withAuth } from "@/lib/auth";-import { DUB_PROJECT_ID } from "@dub/utils";import { NextResponse } from "next/server";// GET /api/links/count – get the number of links for a project-export const GET = withAuth(- async ({ headers, searchParams, project, session }) => {- const { userId } = searchParams;- const count = await getLinksCount({- searchParams,- projectId: project?.id || DUB_PROJECT_ID,- userId: project?.id ? userId : session.user.id,- });- return NextResponse.json(count, {- headers,- });- },-);+export const GET = withAuth(async ({ headers, searchParams, project }) => {+ const { userId } = searchParams;+ const count = await getLinksCount({+ searchParams,+ projectId: project.id,+ userId,+ });+ return NextResponse.json(count, {+ headers,+ });+});diff --git a/apps/web/app/api/route.ts b/apps/web/app/api/route.tsindex f95114d11d..86f738265c 100644--- a/apps/web/app/api/route.ts+++ b/apps/web/app/api/route.ts@@ -178,7 +178,7 @@ export function GET(): NextResponse<OpenAPIV3.Document> {"application/json": {schema: {$ref: "#/components/schemas/LinkBody",- required: ["domain", "url"],+ required: ["url"],},},},@@ -613,7 +613,8 @@ export function GET(): NextResponse<OpenAPIV3.Document> {properties: {domain: {type: "string",- description: "The domain of the short link.",+ description:+ "The domain of the short link. If not provided, the primary domain for the project will be used (or `dub.sh` if the project has no domains).",},key: {type: "string",diff --git a/apps/web/lib/api/links.ts b/apps/web/lib/api/links.tsindex f20d9503e1..bc18bcf9e9 100644--- a/apps/web/lib/api/links.ts+++ b/apps/web/lib/api/links.ts@@ -9,7 +9,6 @@ import { redis } from "@/lib/upstash";import {DEFAULT_REDIRECTS,DUB_DOMAINS,- DUB_PROJECT_ID,SHORT_DOMAIN,getDomainWithoutWWW,getParamsFromURL,@@ -21,7 +20,7 @@ import {} from "@dub/utils";import cloudinary from "cloudinary";import { isIframeable } from "../middleware/utils";-import { LinkProps, ProjectProps, SimpleLinkProps } from "../types";+import { LinkProps, ProjectProps } from "../types";import { Session } from "../auth";export async function getLinksForProject({@@ -216,7 +215,7 @@ export async function processLink({bulk = false,}: {payload: LinkProps;- project: ProjectProps;+ project?: ProjectProps;session?: Session;bulk?: boolean;}) {@@ -251,7 +250,7 @@ export async function processLink({}// free plan restrictions- if (project.plan === "free") {+ if (!project || project.plan === "free") {if (proxy || rewrite || expiresAt || ios || android || geo) {return {link: payload,@@ -273,7 +272,7 @@ export async function processLink({// if domain is not defined, use the project's primary domainif (!domain) {- domain = project.domains?.find((d) => d.primary)?.slug || SHORT_DOMAIN;+ domain = project?.domains?.find((d) => d.primary)?.slug || SHORT_DOMAIN;// if domain is defined, do some checks} else {@@ -311,8 +310,8 @@ export async function processLink({};}- // else, check if it belongs to the project- } else if (!project.domains?.find((d) => d.slug === domain)) {+ // else, check if the domain belongs to the project+ } else if (!project?.domains?.find((d) => d.slug === domain)) {return {link: payload,error: "Domain does not belong to project.",@@ -385,8 +384,8 @@ export async function processLink({...payload,key,url: processedUrl,- // make sure projectId is set to the current project (or Dub's if there's no project)- projectId: project?.id || DUB_PROJECT_ID,+ // make sure projectId is set to the current project+ projectId: project?.id || null,// if session is passed, set userId to the current user's id (we don't change the userId if it's already set, e.g. when editing a link)...(session && {userId: session.user.id,diff --git a/apps/web/lib/auth/index.ts b/apps/web/lib/auth/index.tsindex 5d4d194798..33df93bb13 100644--- a/apps/web/lib/auth/index.ts+++ b/apps/web/lib/auth/index.ts@@ -4,12 +4,7 @@ import { Link as LinkProps } from "@prisma/client";import { PlanProps, ProjectProps } from "../types";import { getServerSession } from "next-auth/next";import { createHash } from "crypto";-import {- API_DOMAIN,- DUB_PROJECT_ID,- getSearchParams,- isDubDomain,-} from "@dub/utils";+import { API_DOMAIN, getSearchParams, isDubDomain } from "@dub/utils";import { ratelimit } from "../upstash";import { exceededLimitError } from "../api/errors";@@ -89,21 +84,32 @@ export const withAuth =const { linkId } = params || {};const slug = params?.slug || searchParams.projectSlug;- if (!slug && !allowAnonymous) {- return new Response(- "Project slug not found. Did you forget to include a `projectSlug` query parameter?",- {- status: 400,- },- );- }-const domain = params?.domain || searchParams.domain;const key = searchParams.key;let session: Session | undefined;let headers = {};+ // if there's no projectSlug defined+ if (!slug) {+ if (allowAnonymous) {+ // @ts-expect-error+ return handler({+ req,+ params: params || {},+ searchParams,+ headers,+ });+ } else {+ return new Response(+ "Project slug not found. Did you forget to include a `projectSlug` query parameter?",+ {+ status: 400,+ },+ );+ }+ }+const authorizationHeader = req.headers.get("Authorization");if (authorizationHeader) {if (!authorizationHeader.includes("Bearer ")) {@@ -181,27 +187,9 @@ export const withAuth =email: user.email || "",},};- // for demo links, we allow anonymous link creation} else {session = await getSession();if (!session?.user?.id) {- if (allowAnonymous) {- const project = await prisma.project.findUnique({- where: {- id: DUB_PROJECT_ID,- },- });-- return handler({- req,- params: params || {},- searchParams,- headers,- // @ts-expect-error- project,- });- }-return new Response("Unauthorized: Login required.", {status: 401,headers,