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.

1.0 KB
Raw Blame
import { sleep } from "@dub/utils";
import { assertValidDomainFormat } from "./utils";

export const verifyDomain = async (domain: string) => {
  assertValidDomainFormat(domain);
  const res = await fetch(
    `https://api.vercel.com/v9/projects/${process.env.VERCEL_PROJECT_ID}/domains/${domain.toLowerCase()}/verify?teamId=${process.env.TEAM_ID_VERCEL}`,
    {
      method: "POST",
      headers: {
        Authorization: `Bearer ${process.env.VERCEL_API_KEY}`,
        "Content-Type": "application/json",
      },
    },
  );
  return res.json();
};

export const verifyDomainWithRetry = async (
  domain: string,
  {
    attempts = 3,
    delayMs = 2500,
  }: { attempts?: number; delayMs?: number } = {},
) => {
  let last: any = null;
  for (let i = 0; i < attempts; i++) {
    if (i > 0) {
      await sleep(delayMs);
    }
    try {
      last = await verifyDomain(domain);
    } catch (error) {
      if (i === attempts - 1) {
        throw error;
      }
      continue;
    }
    if (last?.verified) {
      return last;
    }
  }
  return last;
};