The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
tweaks
Kiran K52395be07a29
diff --git a/apps/web/app/api/projects/[slug]/import/bitly/route.ts b/apps/web/app/api/projects/[slug]/import/bitly/route.tsindex d6c6750c7c..6889845b71 100644--- a/apps/web/app/api/projects/[slug]/import/bitly/route.ts+++ b/apps/web/app/api/projects/[slug]/import/bitly/route.ts@@ -1,7 +1,6 @@import { addDomainToVercel } from "@/lib/api/domains";import { withAuth } from "@/lib/auth";import { qstash } from "@/lib/cron";-import { DubApiError, handleAndReturnErrorResponse } from "@/lib/errors";import prisma from "@/lib/prisma";import { BitlyGroupProps } from "@/lib/types";import { redis } from "@/lib/upstash";@@ -10,53 +9,43 @@ import { NextResponse } from "next/server";// GET /api/projects/[slug]/import/bitly – get all bitly groups for a projectexport const GET = withAuth(async ({ project }) => {- try {- const accessToken = await redis.get(`import:bitly:${project.id}`);- if (!accessToken) {- throw new DubApiError({- code: "bad_request",- message: "No Bitly access token found",- });- }- const response = await fetch(`https://api-ssl.bitly.com/v4/groups`, {- headers: {- "Content-Type": "application/json",- Authorization: `Bearer ${accessToken}`,- },- });- const data = await response.json();- if (data.message === "FORBIDDEN") {- throw new DubApiError({- code: "forbidden",- message: "Invalid Bitly access token",- });- }+ const accessToken = await redis.get(`import:bitly:${project.id}`);+ if (!accessToken) {+ return new Response("No Bitly access token found", { status: 400 });+ }+ const response = await fetch(`https://api-ssl.bitly.com/v4/groups`, {+ headers: {+ "Content-Type": "application/json",+ Authorization: `Bearer ${accessToken}`,+ },+ });+ const data = await response.json();+ if (data.message === "FORBIDDEN") {+ return new Response("Invalid Bitly access token", { status: 403 });+ }- const groups = data.groups- // filter for active groups only- .filter(({ is_active }) => is_active) as BitlyGroupProps[];+ const groups = data.groups+ // filter for active groups only+ .filter(({ is_active }) => is_active) as BitlyGroupProps[];- const groupsWithTags = await Promise.all(- groups.map(async (group) => ({- ...group,- tags: await fetch(- `https://api-ssl.bitly.com/v4/groups/${group.guid}/tags`,- {- headers: {- "Content-Type": "application/json",- Authorization: `Bearer ${accessToken}`,- },+ const groupsWithTags = await Promise.all(+ groups.map(async (group) => ({+ ...group,+ tags: await fetch(+ `https://api-ssl.bitly.com/v4/groups/${group.guid}/tags`,+ {+ headers: {+ "Content-Type": "application/json",+ Authorization: `Bearer ${accessToken}`,},- )- .then((r) => r.json())- .then((r) => r.tags),- })),- ).then((g) => g.filter(({ bsds }) => bsds.length > 0));+ },+ )+ .then((r) => r.json())+ .then((r) => r.tags),+ })),+ ).then((g) => g.filter(({ bsds }) => bsds.length > 0));- return NextResponse.json(groupsWithTags);- } catch (error) {- return handleAndReturnErrorResponse(error);- }+ return NextResponse.json(groupsWithTags);});// POST /api/projects/[slug]/import/bitly - create job to import links from bitlydiff --git a/apps/web/app/api/projects/[slug]/import/rebrandly/route.ts b/apps/web/app/api/projects/[slug]/import/rebrandly/route.tsindex 8d815eace7..57ed764f8f 100644--- a/apps/web/app/api/projects/[slug]/import/rebrandly/route.ts+++ b/apps/web/app/api/projects/[slug]/import/rebrandly/route.ts@@ -1,7 +1,6 @@import { addDomainToVercel } from "@/lib/api/domains";import { withAuth } from "@/lib/auth";import { qstash } from "@/lib/cron";-import { DubApiError, handleAndReturnErrorResponse } from "@/lib/errors";import prisma from "@/lib/prisma";import { redis } from "@/lib/upstash";import { APP_DOMAIN_WITH_NGROK } from "@dub/utils";@@ -9,63 +8,53 @@ import { NextResponse } from "next/server";// GET /api/projects/[slug]/import/rebrandly – get all Rebrandly domains for a projectexport const GET = withAuth(async ({ project }) => {- try {- const accessToken = await redis.get(`import:rebrandly:${project.id}`);- if (!accessToken) {- throw new DubApiError({- code: "bad_request",- message: "No Rebrandly access token found",- });- }+ const accessToken = await redis.get(`import:rebrandly:${project.id}`);+ if (!accessToken) {+ return new Response("No Rebrandly access token found", { status: 400 });+ }- const response = await fetch(`https://api.rebrandly.com/v1/domains`, {- headers: {- "Content-Type": "application/json",- apikey: accessToken as string,- },- });- const data = await response.json();- if (data.error === "Unauthorized") {- throw new DubApiError({- code: "forbidden",- message: "Invalid Rebrandly access token",- });- }+ const response = await fetch(`https://api.rebrandly.com/v1/domains`, {+ headers: {+ "Content-Type": "application/json",+ apikey: accessToken as string,+ },+ });+ const data = await response.json();+ if (data.error === "Unauthorized") {+ return new Response("Invalid Rebrandly access token", { status: 403 });+ }- const domains = await Promise.all(- data- .filter(({ fullName }) => fullName !== "rebrand.ly")- .map(async ({ id, fullName }: { id: number; fullName: string }) => ({- id,- domain: fullName,- links: await fetch(- `https://api.rebrandly.com/v1/links/count?domain.id=${id}`,- {- headers: {- "Content-Type": "application/json",- apikey: accessToken as string,- },+ const domains = await Promise.all(+ data+ .filter(({ fullName }) => fullName !== "rebrand.ly")+ .map(async ({ id, fullName }: { id: number; fullName: string }) => ({+ id,+ domain: fullName,+ links: await fetch(+ `https://api.rebrandly.com/v1/links/count?domain.id=${id}`,+ {+ headers: {+ "Content-Type": "application/json",+ apikey: accessToken as string,},- )- .then((r) => r.json())- .then((data) => data.count) // subtract 1 to exclude root domain- .catch(() => 0),- })),- );+ },+ )+ .then((r) => r.json())+ .then((data) => data.count) // subtract 1 to exclude root domain+ .catch(() => 0),+ })),+ );- const tagsCount = await fetch("https://api.rebrandly.com/v1/tags/count", {- headers: {- "Content-Type": "application/json",- apikey: accessToken as string,- },- })- .then((r) => r.json())- .then((data) => data.count);+ const tagsCount = await fetch("https://api.rebrandly.com/v1/tags/count", {+ headers: {+ "Content-Type": "application/json",+ apikey: accessToken as string,+ },+ })+ .then((r) => r.json())+ .then((data) => data.count);- return NextResponse.json({ domains, tagsCount });- } catch (error) {- return handleAndReturnErrorResponse(error);- }+ return NextResponse.json({ domains, tagsCount });});// PUT /api/projects/[slug]/import/rebrandly - save Rebrandly API key@@ -115,4 +104,4 @@ export const POST = withAuth(async ({ req, project, session }) => {),);return NextResponse.json(response);-});+});\ No newline at end of filediff --git a/apps/web/app/api/projects/[slug]/import/short/route.ts b/apps/web/app/api/projects/[slug]/import/short/route.tsindex 1ad227fe12..b81c8fba3c 100644--- a/apps/web/app/api/projects/[slug]/import/short/route.ts+++ b/apps/web/app/api/projects/[slug]/import/short/route.ts@@ -1,7 +1,6 @@import { addDomainToVercel } from "@/lib/api/domains";import { withAuth } from "@/lib/auth";import { qstash } from "@/lib/cron";-import { DubApiError, handleAndReturnErrorResponse } from "@/lib/errors";import prisma from "@/lib/prisma";import { redis } from "@/lib/upstash";import { APP_DOMAIN_WITH_NGROK, fetchWithTimeout } from "@dub/utils";@@ -9,52 +8,42 @@ import { NextResponse } from "next/server";// GET /api/projects/[slug]/import/short – get all short.io domains for a projectexport const GET = withAuth(async ({ project }) => {- try {- const accessToken = await redis.get(`import:short:${project.id}`);- if (!accessToken) {- throw new DubApiError({- code: "bad_request",- message: "No Short.io access token found",- });- }+ const accessToken = await redis.get(`import:short:${project.id}`);+ if (!accessToken) {+ return new Response("No Short.io access token found", { status: 400 });+ }- const response = await fetch(`https://api.short.io/api/domains`, {- headers: {- "Content-Type": "application/json",- Authorization: accessToken as string,- },- });- const data = await response.json();- if (data.error === "Unauthorized") {- throw new DubApiError({- code: "forbidden",- message: "Invalid Short.io access token",- });- }+ const response = await fetch(`https://api.short.io/api/domains`, {+ headers: {+ "Content-Type": "application/json",+ Authorization: accessToken as string,+ },+ });+ const data = await response.json();+ if (data.error === "Unauthorized") {+ return new Response("Invalid Short.io access token", { status: 403 });+ }- const domains = await Promise.all(- data.map(async ({ id, hostname }: { id: number; hostname: string }) => ({- id,- domain: hostname,- links: await fetchWithTimeout(- `https://api-v2.short.cm/statistics/domain/${id}?period=total`,- {- headers: {- "Content-Type": "application/json",- Authorization: accessToken as string,- },+ const domains = await Promise.all(+ data.map(async ({ id, hostname }: { id: number; hostname: string }) => ({+ id,+ domain: hostname,+ links: await fetchWithTimeout(+ `https://api-v2.short.cm/statistics/domain/${id}?period=total`,+ {+ headers: {+ "Content-Type": "application/json",+ Authorization: accessToken as string,},- )- .then((r) => r.json())- .then((data) => data.links - 1) // subtract 1 to exclude root domain- .catch(() => 0),- })),- );+ },+ )+ .then((r) => r.json())+ .then((data) => data.links - 1) // subtract 1 to exclude root domain+ .catch(() => 0),+ })),+ );- return NextResponse.json(domains);- } catch (error) {- return handleAndReturnErrorResponse(error);- }+ return NextResponse.json(domains);});// PUT /api/projects/[slug]/import/short - save Short.io API key@@ -103,4 +92,4 @@ export const POST = withAuth(async ({ req, project, session }) => {),);return NextResponse.json(response);-});+});\ No newline at end of file