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.

7.6 KB
Raw Blame
import { normalizeWorkspaceId } from "@/lib/api/workspaces/workspace-id";
import * as z from "zod/v4";
import { uploadedImageSchema } from "./images";
import { booleanQuerySchema, getPaginationQuerySchema } from "./misc";
import { parseUrlSchemaAllowEmpty } from "./utils";

export const RegisteredDomainSchema = z.object({
  id: z.string().describe("The ID of the registered domain record."),
  autoRenewalDisabledAt: z
    .date()
    .nullable()
    .describe("The date the domain auto-renew is disabled."),
  createdAt: z.date().describe("The date the domain was created."),
  expiresAt: z.date().describe("The date the domain expires."),
  renewalFee: z.number().describe("The fee to renew the domain."),
});

export const DomainSchema = z.object({
  id: z.string().describe("The unique identifier of the domain."),
  slug: z.string().describe("The domain name.").meta({ example: "acme.com" }),
  verified: z
    .boolean()
    .default(false)
    .describe("Whether the domain is verified."),
  primary: z
    .boolean()
    .default(false)
    .describe("Whether the domain is the primary domain for the workspace."),
  archived: z
    .boolean()
    .describe("Whether the domain is archived.")
    .default(false),
  placeholder: z
    .string()
    .nullable()
    .describe(
      "Provide context to your teammates in the link creation modal by showing them an example of a link to be shortened.",
    )
    .meta({ example: "https://dub.co/help/article/dub-links" }),
  expiredUrl: z
    .string()
    .nullable()
    .describe(
      "The URL to redirect to when a link under this domain has expired.",
    )
    .meta({ example: "https://acme.com/expired" }),
  notFoundUrl: z
    .string()
    .nullable()
    .describe(
      "The URL to redirect to when a link under this domain doesn't exist.",
    )
    .meta({ example: "https://acme.com/not-found" }),
  logo: z.string().nullable().describe("The logo of the domain."),
  assetLinks: z
    .string()
    .nullable()
    .default(null)
    .describe(
      "assetLinks.json configuration file (for deep link support on Android).",
    ),
  appleAppSiteAssociation: z
    .string()
    .nullable()
    .default(null)
    .describe(
      "apple-app-site-association configuration file (for deep link support on iOS).",
    ),
  createdAt: z.date().describe("The date the domain was created."),
  updatedAt: z.date().describe("The date the domain was last updated."),
  registeredDomain: RegisteredDomainSchema.nullable().describe(
    "The registered domain record.",
  ),
});

export const DOMAINS_MAX_PAGE_SIZE = 50;

export const getDomainsQuerySchema = z
  .object({
    archived: booleanQuerySchema
      .optional()
      .default(false)
      .describe(
        "Whether to include archived domains in the response. Defaults to `false` if not provided.",
      ),
    search: z
      .string()
      .optional()
      .describe("The search term to filter the domains by."),
  })
  .extend(getPaginationQuerySchema({ pageSize: DOMAINS_MAX_PAGE_SIZE }));

export const getDomainsQuerySchemaExtended = getDomainsQuerySchema.extend({
  // only Dub UI uses the following query parameters
  includeLink: booleanQuerySchema.default(false),
});

export const getDomainsCountQuerySchema = getDomainsQuerySchema.omit({
  page: true,
});

export const getDefaultDomainsQuerySchema = getDomainsQuerySchema.pick({
  search: true,
});

export const createDomainBodySchema = z.object({
  slug: z
    .string({ error: "slug is required" })
    .min(1, "slug cannot be empty.")
    .max(190, "slug cannot be longer than 190 characters.")
    .trim()
    .describe("Name of the domain.")
    .meta({ example: "acme.com" }),
  expiredUrl: parseUrlSchemaAllowEmpty({ trim: true })
    .nullish()
    .transform((v) => v || null)
    .describe(
      "Redirect users to a specific URL when any link under this domain has expired.",
    )
    .meta({ example: "https://acme.com/expired" }),
  notFoundUrl: parseUrlSchemaAllowEmpty({ trim: true })
    .nullish()
    .transform((v) => v || null)
    .describe(
      "Redirect users to a specific URL when a link under this domain doesn't exist.",
    )
    .meta({ example: "https://acme.com/not-found" }),
  archived: z
    .boolean()
    .optional()
    .default(false)
    .describe(
      "Whether to archive this domain. `false` will unarchive a previously archived domain.",
    )
    .meta({ example: false }),
  placeholder: parseUrlSchemaAllowEmpty({ maxLength: 100, trim: true })
    .nullish()
    .transform((v) => v || null)
    .describe(
      "Provide context to your teammates in the link creation modal by showing them an example of a link to be shortened.",
    )
    .meta({ example: "https://dub.co/help/article/dub-links" }),
  logo: uploadedImageSchema.nullish().describe("The logo of the domain."),
  assetLinks: z
    .string()
    .nullish()
    .describe(
      "assetLinks.json configuration file (for deep link support on Android).",
    ),
  appleAppSiteAssociation: z
    .string()
    .nullish()
    .describe(
      "apple-app-site-association configuration file (for deep link support on iOS).",
    ),
});

export const createDomainBodySchemaExtended = createDomainBodySchema.extend({
  deepviewData: z.string().nullish(),
});

export const updateDomainBodySchema = createDomainBodySchema.partial();

export const transferDomainBodySchema = z.object({
  newWorkspaceId: z
    .string({ error: "New workspace ID is required." })
    .min(1, "New workspace ID cannot be empty.")
    .transform((v) => normalizeWorkspaceId(v))
    .describe("The ID of the new workspace to transfer the domain to."),
});

export const registerDomainSchema = z.object({
  domain: z
    .string()
    .min(1, "Domain to register is required.")
    .endsWith(".link")
    .transform((domain) => domain.toLowerCase())
    .describe("The domain to claim. We only support .link domains for now.")
    .meta({ example: "acme.link" }),
});

export const searchDomainSchema = z.object({
  domains: z
    .union([z.string(), z.array(z.string())])
    .transform((v) => (Array.isArray(v) ? v : v.split(",")))
    .transform((domains) =>
      domains
        .map((domain) => domain.toLowerCase())
        .filter((domain) => domain.endsWith(".link")),
    )
    .describe("The domains to search. We only support .link domains for now.")
    .meta({
      param: {
        style: "form",
        explode: false,
      },
      anyOf: [
        {
          type: "string",
        },
        {
          type: "array",
          items: {
            type: "string",
          },
        },
      ],
    }),
});

export const DomainStatusSchema = z.object({
  domain: z.string().describe("The domain name."),
  available: z.boolean().describe("Whether the domain is available."),
  premium: z
    .boolean()
    .nullable()
    .describe("Whether the domain is a premium domain."),
  prices: z
    .object({
      registration: z
        .number()
        .nullable()
        .describe("The domain's registration price in USD cents."),
      renewal: z
        .number()
        .nullable()
        .describe("The domain's renewal price in USD cents."),
    })
    .nullable()
    .describe(
      "Price details for the domain. Will be null if the domain is not available.",
    ),
  price: z
    .string()
    .nullable()
    .describe("Deprecated: Use `prices` instead.")
    .meta({ deprecated: true }),
});

export const RegisterDomainSchema = z.object({
  domain: z.string().describe("The domain name."),
  status: z.string().describe("The status of the domain registration."),
  expiration: z
    .number()
    .describe(
      "The expiration timestamp of the domain (Unix timestamp in milliseconds).",
    )
    .nullable(),
});