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.1 KB
Raw Blame
import { SCOPES } from "@/lib/api/tokens/scopes";
import * as z from "zod/v4";

// Schema to validate the request body when creating a new token
export const createTokenSchema = z.object({
  name: z
    .string({
      error: "Name is required",
    })
    .min(1)
    .max(50),
  isMachine: z.boolean().optional().default(false),
  scopes: z.array(z.enum(SCOPES)).min(1, {
    error: "Please select at least one scope for this API key.",
  }),
});

// Schema to validate the request body when updating a token
export const updateTokenSchema = createTokenSchema
  .pick({
    name: true,
    scopes: true,
  })
  .required();

// Represent the shape of a token returned from the API
export const tokenSchema = z.object({
  id: z.string(),
  name: z.string(),
  partialKey: z.string(),
  scopes: z
    .string()
    .nullable()
    .transform((val) => val?.split(" ") ?? []),
  expires: z.date().nullable(),
  lastUsed: z.date().nullable(),
  createdAt: z.date(),
  updatedAt: z.date(),
  user: z.object({
    id: z.string(),
    name: z.string().nullable(),
    email: z.string().nullable(),
    image: z.string().nullable(),
    isMachine: z.boolean(),
  }),
});